From a53a2ad438038084200a8449c557a41159e409dc Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 29 Apr 2014 17:46:05 +0200 Subject: [PATCH] Added integration-test provided by Guido Wimmel --- src/it/schemaexport-example/pom.xml | 111 ++++++++++++++++++ .../schemaexport-example-domain/pom.xml | 29 +++++ .../test/schemaexport/domain/Department.java | 46 ++++++++ .../de/test/schemaexport/domain/Employee.java | 60 ++++++++++ .../pom.xml | 77 ++++++++++++ .../persistence/dao/IDepartmentDAO.java | 12 ++ .../persistence/dao/IEmployeeDAO.java | 13 ++ .../persistence/dao/impl/DepartmentDAO.java | 17 +++ .../persistence/dao/impl/EmployeeDAO.java | 19 +++ .../main/resources/META-INF/persistence.xml | 18 +++ .../persistence/dao/impl/DAOTest.java | 64 ++++++++++ .../src/test/resources/log4j.properties | 12 ++ 12 files changed, 478 insertions(+) create mode 100644 src/it/schemaexport-example/pom.xml create mode 100644 src/it/schemaexport-example/schemaexport-example-domain/pom.xml create mode 100644 src/it/schemaexport-example/schemaexport-example-domain/src/main/java/de/test/schemaexport/domain/Department.java create mode 100644 src/it/schemaexport-example/schemaexport-example-domain/src/main/java/de/test/schemaexport/domain/Employee.java create mode 100644 src/it/schemaexport-example/schemaexport-example-persistence-impl/pom.xml create mode 100644 src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/IDepartmentDAO.java create mode 100644 src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/IEmployeeDAO.java create mode 100644 src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/impl/DepartmentDAO.java create mode 100644 src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/impl/EmployeeDAO.java create mode 100644 src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/resources/META-INF/persistence.xml create mode 100644 src/it/schemaexport-example/schemaexport-example-persistence-impl/src/test/java/de/test/schemaexport/persistence/dao/impl/DAOTest.java create mode 100644 src/it/schemaexport-example/schemaexport-example-persistence-impl/src/test/resources/log4j.properties diff --git a/src/it/schemaexport-example/pom.xml b/src/it/schemaexport-example/pom.xml new file mode 100644 index 00000000..79812142 --- /dev/null +++ b/src/it/schemaexport-example/pom.xml @@ -0,0 +1,111 @@ + + + + 4.2.6.Final + 4.2.6.Final + 4.2.6.Final + 5.0.1.Final + 1.0.1.Final + 1.4.2 + + 4.0.0 + de.test + schemaexport-example + pom + 1.0-SNAPSHOT + Schemaexport Example + + + + org.hibernate + hibernate-entitymanager + ${hibernate.entitymanager.version} + jar + + + commons-logging + commons-logging + + + + + org.hibernate + hibernate-core + ${hibernate.version} + jar + + + commons-logging + commons-logging + + + + + org.hibernate + hibernate-validator + ${hibernate.validator.version} + jar + + + commons-logging + commons-logging + + + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + ${hibernate.jpa20.version} + jar + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + jar + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + jar + + + commons-logging + commons-logging + 1.0.4 + jar + + + + org.hsqldb + hsqldb + 2.3.0 + + + mysql + mysql-connector-java + 5.1.6 + jar + + + + + + + + maven-compiler-plugin + + 1.6 + 1.6 + UTF-8 + + + + + + + schemaexport-example-domain + schemaexport-example-persistence-impl + + diff --git a/src/it/schemaexport-example/schemaexport-example-domain/pom.xml b/src/it/schemaexport-example/schemaexport-example-domain/pom.xml new file mode 100644 index 00000000..c2bc2d8f --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-domain/pom.xml @@ -0,0 +1,29 @@ + + + + de.test + schemaexport-example + 1.0-SNAPSHOT + + 4.0.0 + schemaexport-example-domain + jar + 1.0-SNAPSHOT + Schemaexport Example Domain + Domain Objects + + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + provided + + + org.hibernate + hibernate-validator + provided + + + + diff --git a/src/it/schemaexport-example/schemaexport-example-domain/src/main/java/de/test/schemaexport/domain/Department.java b/src/it/schemaexport-example/schemaexport-example-domain/src/main/java/de/test/schemaexport/domain/Department.java new file mode 100644 index 00000000..b523659c --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-domain/src/main/java/de/test/schemaexport/domain/Department.java @@ -0,0 +1,46 @@ +package de.test.schemaexport.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * Abteilungsklasse (Generator-Beispielcode). + * + * copyright + * + */ +@Entity +@Table(name = "ABTEILUNG") +public class Department { + + @Id + @Column(name = "OID") + @GeneratedValue(strategy = GenerationType.AUTO) + private long oid; + + @Column(name = "name", nullable = false) + private String name; + + public long getOid() { + return oid; + } + + public void setOid(long oid) { + this.oid = oid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + +} diff --git a/src/it/schemaexport-example/schemaexport-example-domain/src/main/java/de/test/schemaexport/domain/Employee.java b/src/it/schemaexport-example/schemaexport-example-domain/src/main/java/de/test/schemaexport/domain/Employee.java new file mode 100644 index 00000000..6f70af0c --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-domain/src/main/java/de/test/schemaexport/domain/Employee.java @@ -0,0 +1,60 @@ +package de.test.schemaexport.domain; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + + + +import org.hibernate.validator.constraints.Length; + +@Entity +@Table(name = "Employee") +public class Employee { + + @Id + @Column(name = "OID") + @GeneratedValue(strategy = GenerationType.AUTO) + private long oid; + + @Length(min = 4, max = 81) + @Column(name = "name", nullable = false) + private String name; + + @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @JoinColumn(name = "FK_department", nullable = true) + private Department department; + + public long getOid() { + return oid; + } + + public void setOid(long oid) { + this.oid = oid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Department getDepartment() { + return department; + } + + public void setDepartment(Department department) { + this.department = department; + } + + +} diff --git a/src/it/schemaexport-example/schemaexport-example-persistence-impl/pom.xml b/src/it/schemaexport-example/schemaexport-example-persistence-impl/pom.xml new file mode 100644 index 00000000..e7c74191 --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-persistence-impl/pom.xml @@ -0,0 +1,77 @@ + + + + de.test + schemaexport-example + 1.0-SNAPSHOT + + 4.0.0 + schemaexport-example-persistence-impl + jar + 1.0-SNAPSHOT + Schemaexport Example Persistenz + + + de.test + schemaexport-example-domain + 1.0-SNAPSHOT + + + org.hibernate + hibernate-entitymanager + + + org.hibernate + hibernate-core + + + org.hibernate + hibernate-validator + provided + + + org.slf4j + slf4j-log4j12 + + + mysql + mysql-connector-java + + + org.hsqldb + hsqldb + + + junit + junit + 4.6 + test + + + + + + + de.juplo + hibernate4-maven-plugin + @project.version@ + + SCRIPT + org.hsqldb.jdbcDriver + org.hibernate.dialect.HSQLDialect + + + + + + export + + + + + + + \ No newline at end of file diff --git a/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/IDepartmentDAO.java b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/IDepartmentDAO.java new file mode 100644 index 00000000..0479edd1 --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/IDepartmentDAO.java @@ -0,0 +1,12 @@ +package de.test.schemaexport.persistence.dao; + +import javax.persistence.EntityManager; +import de.test.schemaexport.domain.Department; + +public interface IDepartmentDAO { + + Department findByID(EntityManager em, long id); + + Department createOrUpdate(EntityManager em, Department toCreate); + +} diff --git a/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/IEmployeeDAO.java b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/IEmployeeDAO.java new file mode 100644 index 00000000..28d5465c --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/IEmployeeDAO.java @@ -0,0 +1,13 @@ +package de.test.schemaexport.persistence.dao; + +import javax.persistence.EntityManager; + +import de.test.schemaexport.domain.Employee; + +public interface IEmployeeDAO { + + Employee findByID(EntityManager em, long id); + + Employee createOrUpdate(EntityManager em, Employee toCreate); + +} diff --git a/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/impl/DepartmentDAO.java b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/impl/DepartmentDAO.java new file mode 100644 index 00000000..e7f91dd2 --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/impl/DepartmentDAO.java @@ -0,0 +1,17 @@ +package de.test.schemaexport.persistence.dao.impl; + +import javax.persistence.EntityManager; +import de.test.schemaexport.domain.Department; +import de.test.schemaexport.persistence.dao.IDepartmentDAO; + +public class DepartmentDAO implements IDepartmentDAO { + + public Department findByID(EntityManager em, long id) { + return em.find(Department.class, id); + } + + public Department createOrUpdate(EntityManager em, Department toCreateOrUpdate) { + return em.merge(toCreateOrUpdate); + } + +} diff --git a/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/impl/EmployeeDAO.java b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/impl/EmployeeDAO.java new file mode 100644 index 00000000..c402a5e6 --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/java/de/test/schemaexport/persistence/dao/impl/EmployeeDAO.java @@ -0,0 +1,19 @@ +package de.test.schemaexport.persistence.dao.impl; + +import javax.persistence.EntityManager; + +import de.test.schemaexport.domain.Employee; +import de.test.schemaexport.persistence.dao.IEmployeeDAO; + +public class EmployeeDAO implements IEmployeeDAO { + + public Employee findByID(EntityManager em, long id) { + return em.find(Employee.class, id); + } + + public Employee createOrUpdate(EntityManager em, Employee toCreateOrUpdate) { + return em.merge(toCreateOrUpdate); + } + + +} diff --git a/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/resources/META-INF/persistence.xml b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/resources/META-INF/persistence.xml new file mode 100644 index 00000000..75784726 --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,18 @@ + + + + de.test.schemaexport.domain.Department + de.test.schemaexport.domain.Employee + + + + + + + + + + + diff --git a/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/test/java/de/test/schemaexport/persistence/dao/impl/DAOTest.java b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/test/java/de/test/schemaexport/persistence/dao/impl/DAOTest.java new file mode 100644 index 00000000..70a32aa8 --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/test/java/de/test/schemaexport/persistence/dao/impl/DAOTest.java @@ -0,0 +1,64 @@ +package de.test.schemaexport.persistence.dao.impl; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +import de.test.schemaexport.domain.Department; + +public class DAOTest { + + private static EntityManagerFactory emf; + private EntityManager em; + private DepartmentDAO departmentDAO = new DepartmentDAO(); + + @BeforeClass + public static void setUpClass() { + // Use persistence.xml configuration + emf = Persistence.createEntityManagerFactory("swmtestappManagerTest"); + Assert.assertNotNull(emf); + } + + @Before + public void setUp() { + em = emf.createEntityManager(); // Retrieve an application managed entity manager + Assert.assertNotNull(em); + + EntityTransaction tx = em.getTransaction(); + Assert.assertNotNull(tx); + + tx.begin(); + tx.setRollbackOnly(); + } + + @Test + public void testSomething() { + Department department = new Department(); + department.setName("Dep"); + Department result = departmentDAO.createOrUpdate(em, department); + System.out.println(result.getOid()); + } + + @After + public void tearDown() { + em.getTransaction().rollback(); + //em.getTransaction().commit(); + em.close(); + } + + @AfterClass + public static void tearDownClass() { + + emf.close(); + } + + +} diff --git a/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/test/resources/log4j.properties b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/test/resources/log4j.properties new file mode 100644 index 00000000..d5a076d1 --- /dev/null +++ b/src/it/schemaexport-example/schemaexport-example-persistence-impl/src/test/resources/log4j.properties @@ -0,0 +1,12 @@ +# Der Root-Logger hat den Level DEBUG +log4j.rootLogger=WARN, A1 + +# Wir haben einen Appender mit der Destionation Konsole +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# F�r diesen Appender verwenden wir eine Layout +log4j.appender.A1.layout=org.apache.log4j.PatternLayout + +# Datum im ISO-Format ISO-8601 anzeigen +log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n + -- 2.20.1