From a21b4280754c3a0654d419c44e6fb105972e064f Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Mon, 24 Dec 2018 12:37:33 +0100 Subject: [PATCH] EventBusinessTest, Clubevent equals without parent. --- .../vaadinclubhelper/dao/ClubEventDao.java | 6 + .../dao/ClubEventDaoImpl.java | 6 + .../vaadinclubhelper/data/ClubEvent.java | 18 +-- src/main/resources/schema/ClubEvent.hbm.xml | 2 +- .../business/EventBusinessTest.java | 125 ++++++++++++++++++ .../dao/AbstractDatabaseTest.java | 8 +- vaadin-clubhelper clean package sonar.launch | 17 +++ 7 files changed, 163 insertions(+), 19 deletions(-) create mode 100644 src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java create mode 100644 vaadin-clubhelper clean package sonar.launch diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java index 9ea3faa..6f5a36c 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDao.java @@ -1,7 +1,13 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; +import javax.persistence.EntityManager; + import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; public interface ClubEventDao extends IDao { + void setEntityManager(EntityManager em); + + EntityManager getEntityManager(); + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java index fed0f34..ac122eb 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/ClubEventDaoImpl.java @@ -13,8 +13,14 @@ public class ClubEventDaoImpl extends AbstractDaoImpl implements Club super(ClubEvent.class); } + @Override public void setEntityManager(EntityManager em) { this.em = em; } + @Override + public EntityManager getEntityManager() { + return em; + } + } diff --git a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java index af84bff..f48817d 100644 --- a/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java +++ b/src/main/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/data/ClubEvent.java @@ -145,13 +145,11 @@ public class ClubEvent extends BasicItem { @Override public int hashCode() { final int prime = 31; - int result = super.hashCode(); + int result = 17; result = prime * result + ((iCalUID == null) ? 0 : iCalUID.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((location == null) ? 0 : location.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; } @@ -159,8 +157,6 @@ public class ClubEvent extends BasicItem { public boolean equals(Object obj) { if (this == obj) return true; - if (!super.equals(obj)) - return false; if (getClass() != obj.getClass()) return false; ClubEvent other = (ClubEvent) obj; @@ -184,18 +180,6 @@ public class ClubEvent extends BasicItem { return false; } else if (!organizerDisplayName.equals(other.organizerDisplayName)) 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; } diff --git a/src/main/resources/schema/ClubEvent.hbm.xml b/src/main/resources/schema/ClubEvent.hbm.xml index fd65403..468336a 100644 --- a/src/main/resources/schema/ClubEvent.hbm.xml +++ b/src/main/resources/schema/ClubEvent.hbm.xml @@ -25,7 +25,7 @@ - + diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java new file mode 100644 index 0000000..296c525 --- /dev/null +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/business/EventBusinessTest.java @@ -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 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 stored = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList(); + assertEquals(3, stored.size()); + + List 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 r) { + super.transactional(r); + } + } +} diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDatabaseTest.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDatabaseTest.java index f07be4c..3b0b4ac 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDatabaseTest.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/AbstractDatabaseTest.java @@ -14,6 +14,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; import javax.persistence.EntityTransaction; @@ -105,10 +106,15 @@ public abstract class AbstractDatabaseTest { * @param r */ protected void transactional(Runnable r) { + transactional(session -> r.run()); + } + + protected void transactional(Consumer r) { + EntityTransaction tx = session.getTransaction(); tx.begin(); try { - r.run(); + r.accept(session); tx.commit(); } catch (Exception e) { tx.rollback(); diff --git a/vaadin-clubhelper clean package sonar.launch b/vaadin-clubhelper clean package sonar.launch new file mode 100644 index 0000000..83aee52 --- /dev/null +++ b/vaadin-clubhelper clean package sonar.launch @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + +