EventBusinessTest, Clubevent equals without parent.

master
Markus Kreth 7 years ago
parent c664335349
commit a21b428075
  1. 6
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java
  2. 6
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java
  3. 18
      src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java
  4. 2
      src/main/resources/schema/ClubEvent.hbm.xml
  5. 125
      src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java
  6. 8
      src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDatabaseTest.java
  7. 17
      vaadin-clubhelper clean package sonar.launch

@ -1,7 +1,13 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao;
import javax.persistence.EntityManager;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent;
public interface ClubEventDao extends IDao<ClubEvent> { public interface ClubEventDao extends IDao<ClubEvent> {
void setEntityManager(EntityManager em);
EntityManager getEntityManager();
} }

@ -13,8 +13,14 @@ public class ClubEventDaoImpl extends AbstractDaoImpl<ClubEvent> implements Club
super(ClubEvent.class); super(ClubEvent.class);
} }
@Override
public void setEntityManager(EntityManager em) { public void setEntityManager(EntityManager em) {
this.em = em; this.em = em;
} }
@Override
public EntityManager getEntityManager() {
return em;
}
} }

@ -145,13 +145,11 @@ public class ClubEvent extends BasicItem {
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = super.hashCode(); int result = 17;
result = prime * result + ((iCalUID == null) ? 0 : iCalUID.hashCode()); result = prime * result + ((iCalUID == null) ? 0 : iCalUID.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((location == null) ? 0 : location.hashCode()); result = prime * result + ((location == null) ? 0 : location.hashCode());
result = prime * result + ((organizerDisplayName == null) ? 0 : organizerDisplayName.hashCode()); result = prime * result + ((organizerDisplayName == null) ? 0 : organizerDisplayName.hashCode());
result = prime * result + ((persons == null) ? 0 : persons.hashCode());
result = prime * result + ((getStart() == null) ? 0 : getStart().hashCode());
return result; return result;
} }
@ -159,8 +157,6 @@ public class ClubEvent extends BasicItem {
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
return true; return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
ClubEvent other = (ClubEvent) obj; ClubEvent other = (ClubEvent) obj;
@ -184,18 +180,6 @@ public class ClubEvent extends BasicItem {
return false; return false;
} else if (!organizerDisplayName.equals(other.organizerDisplayName)) } else if (!organizerDisplayName.equals(other.organizerDisplayName))
return false; return false;
if (persons == null) {
if (other.persons != null)
return false;
} else if (!persons.equals(other.persons))
return false;
ZonedDateTime oStart = other.getStart();
ZonedDateTime start = getStart();
if (start == null) {
if (oStart != null)
return false;
} else if (!start.equals(oStart))
return false;
return true; return true;
} }

@ -25,7 +25,7 @@
<property column="organizerDisplayName" generated="never" <property column="organizerDisplayName" generated="never"
lazy="false" name="organizerDisplayName" type="string" /> lazy="false" name="organizerDisplayName" type="string" />
<set name="persons" table="clubevent_has_person" cascade="save-update" lazy="false"> <set name="persons" table="clubevent_has_person" lazy="false">
<key column="clubevent_id"/> <key column="clubevent_id"/>
<many-to-many column="person_id" class="de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person" /> <many-to-many column="person_id" class="de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person" />
</set> </set>

@ -0,0 +1,125 @@
package de.kreth.vaadin.clubhelper.vaadinclubhelper.business;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.HashSet;
import java.util.List;
import java.util.function.Consumer;
import javax.persistence.EntityManager;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import de.kreth.clubhelperbackend.google.calendar.CalendarAdapter;
import de.kreth.vaadin.clubhelper.HibernateHolder;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AbstractDatabaseTest;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.ClubEventDaoImpl;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent;
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person;
@ExtendWith(SpringExtension.class)
class EventBusinessTest {
EventBusiness eventBusiness;
private List<Person> persons;
private ClubEvent event;
private DatabaseHelper helper;
@Autowired
protected EntityManager entityManager;
private EventBusiness business;
@Configuration
public static class MyConfig {
@Bean
public EntityManager getEntityManager() throws Exception {
// setup the session factory
org.hibernate.cfg.Configuration configuration = HibernateHolder.configuration();
SessionFactory sessionFactory = configuration.buildSessionFactory();
return sessionFactory.openSession();
}
@Bean
public CalendarAdapter getCalendarAdapter() throws GeneralSecurityException, IOException {
return new CalendarAdapter();
}
}
@BeforeEach
void setUp() throws Exception {
helper = new DatabaseHelper(entityManager);
helper.cleanH2Database();
ClubEventDaoImpl dao = new ClubEventDaoImpl();
dao.setEntityManager(entityManager);
business = new EventBusiness();
business.dao = dao;
insertTestData();
business.setSelected(event);
}
private void insertTestData() {
persons = helper.insertPersons(3);
event = helper.creteEvent();
helper.transactional((session) -> session.save(event));
}
@Test
void testDataCorrectlyCreated() {
assertEquals(0, helper.loadEventPersons().size());
List<Person> stored = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList();
assertEquals(3, stored.size());
List<ClubEvent> events = business.loadEvents();
assertEquals(1, events.size());
// assertNotNull(events.get(0).getPersons());
}
@Test
void testAddPersonsToEvent() {
helper.transactional(() -> business.changePersons(new HashSet<>(persons.subList(0, 1))));
helper.transactional(() -> business.changePersons(new HashSet<>(persons.subList(0, 2))));
assertEquals(2, helper.loadEventPersons().size());
}
class DatabaseHelper extends AbstractDatabaseTest {
public DatabaseHelper(EntityManager em) {
this((Session) em);
}
public DatabaseHelper(Session session) {
AbstractDatabaseTest.session = session;
}
@Override
public void transactional(Runnable r) {
super.transactional(r);
}
@Override
public void transactional(Consumer<Session> r) {
super.transactional(r);
}
}
}

@ -14,6 +14,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
@ -105,10 +106,15 @@ public abstract class AbstractDatabaseTest {
* @param r * @param r
*/ */
protected void transactional(Runnable r) { protected void transactional(Runnable r) {
transactional(session -> r.run());
}
protected void transactional(Consumer<Session> r) {
EntityTransaction tx = session.getTransaction(); EntityTransaction tx = session.getTransaction();
tx.begin(); tx.begin();
try { try {
r.run(); r.accept(session);
tx.commit(); tx.commit();
} catch (Exception e) { } catch (Exception e) {
tx.rollback(); tx.rollback();

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType">
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/>
<stringAttribute key="M2_GOALS" value="clean package sonar:sonar"/>
<booleanAttribute key="M2_NON_RECURSIVE" value="false"/>
<booleanAttribute key="M2_OFFLINE" value="false"/>
<stringAttribute key="M2_PROFILES" value=""/>
<listAttribute key="M2_PROPERTIES"/>
<stringAttribute key="M2_RUNTIME" value="apache-maven"/>
<booleanAttribute key="M2_SKIP_TESTS" value="false"/>
<intAttribute key="M2_THREADS" value="1"/>
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/>
<stringAttribute key="M2_USER_SETTINGS" value=""/>
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc:vaadin-clubhelper}"/>
</launchConfiguration>
Loading…
Cancel
Save