From: Kai Moritz <kai@juplo.de>
Date: Tue, 29 Apr 2014 15:46:05 +0000 (+0200)
Subject: Added integration-test provided by Guido Wimmel <Wimmel.Guido@swm.de>
X-Git-Tag: hibernate4-maven-plugin-1.0.4~7
X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=a53a2ad438038084200a8449c557a41159e409dc;p=hibernate-maven-plugin

Added integration-test provided by Guido Wimmel <Wimmel.Guido@swm.de>
---

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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<properties>
+		<hibernate.version>4.2.6.Final</hibernate.version>
+		<hibernate.entitymanager.version>4.2.6.Final</hibernate.entitymanager.version>
+		<hibernate.c3p0.version>4.2.6.Final</hibernate.c3p0.version>
+		<hibernate.validator.version>5.0.1.Final</hibernate.validator.version>
+		<hibernate.jpa20.version>1.0.1.Final</hibernate.jpa20.version>
+		<slf4j.version>1.4.2</slf4j.version>
+	</properties>
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>de.test</groupId>
+	<artifactId>schemaexport-example</artifactId>
+	<packaging>pom</packaging>
+	<version>1.0-SNAPSHOT</version>
+	<name>Schemaexport Example</name>
+	<dependencyManagement>
+		<dependencies>
+			<dependency>
+				<groupId>org.hibernate</groupId>
+				<artifactId>hibernate-entitymanager</artifactId>
+				<version>${hibernate.entitymanager.version}</version>
+				<type>jar</type>
+				<exclusions>
+					<exclusion>
+						<groupId>commons-logging</groupId>
+						<artifactId>commons-logging</artifactId>
+					</exclusion>
+				</exclusions>
+			</dependency>
+			<dependency>
+				<groupId>org.hibernate</groupId>
+				<artifactId>hibernate-core</artifactId>
+				<version>${hibernate.version}</version>
+				<type>jar</type>
+				<exclusions>
+					<exclusion>
+						<groupId>commons-logging</groupId>
+						<artifactId>commons-logging</artifactId>
+					</exclusion>
+				</exclusions>
+			</dependency>
+			<dependency>
+				<groupId>org.hibernate</groupId>
+				<artifactId>hibernate-validator</artifactId>
+				<version>${hibernate.validator.version}</version>
+				<type>jar</type>
+				<exclusions>
+					<exclusion>
+						<groupId>commons-logging</groupId>
+						<artifactId>commons-logging</artifactId>
+					</exclusion>
+				</exclusions>
+			</dependency>
+			<dependency>
+				<groupId>org.hibernate.javax.persistence</groupId>
+				<artifactId>hibernate-jpa-2.0-api</artifactId>
+				<version>${hibernate.jpa20.version}</version>
+				<type>jar</type>
+			</dependency>
+			<dependency><!-- Logging per SLF4J auf Log4J -->
+				<groupId>org.slf4j</groupId>
+				<artifactId>slf4j-log4j12</artifactId>
+				<version>${slf4j.version}</version>
+				<type>jar</type>
+			</dependency>
+			<dependency><!-- Commons-Logging ueber SLF4J -->
+				<groupId>org.slf4j</groupId>
+				<artifactId>jcl-over-slf4j</artifactId>
+				<version>${slf4j.version}</version>
+				<type>jar</type>
+			</dependency>
+			<dependency><!-- Commons-Logging -->
+				<groupId>commons-logging</groupId>
+				<artifactId>commons-logging</artifactId>
+				<version>1.0.4</version>
+				<type>jar</type>
+			</dependency>
+
+			<dependency>
+				<groupId>org.hsqldb</groupId>
+				<artifactId>hsqldb</artifactId>
+				<version>2.3.0</version>
+			</dependency>
+			<dependency>
+				<groupId>mysql</groupId>
+				<artifactId>mysql-connector-java</artifactId>
+				<version>5.1.6</version>
+				<type>jar</type>
+			</dependency>
+		</dependencies>
+	</dependencyManagement>
+	<build>
+		<pluginManagement>
+			<plugins>
+				<plugin>
+					<artifactId>maven-compiler-plugin</artifactId>
+					<configuration>
+						<source>1.6</source>
+						<target>1.6</target>
+						<encoding>UTF-8</encoding>
+					</configuration>
+				</plugin>
+			</plugins>
+		</pluginManagement>
+	</build>
+  <modules>
+  	<module>schemaexport-example-domain</module>
+    <module>schemaexport-example-persistence-impl</module>
+   </modules>
+</project>
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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<parent>
+		<groupId>de.test</groupId>
+		<artifactId>schemaexport-example</artifactId>
+		<version>1.0-SNAPSHOT</version>
+	</parent>
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>schemaexport-example-domain</artifactId>
+	<packaging>jar</packaging>
+	<version>1.0-SNAPSHOT</version>
+	<name>Schemaexport Example Domain</name>
+	<description>Domain Objects</description>
+	
+	<dependencies>
+		<dependency>
+			<groupId>org.hibernate.javax.persistence</groupId>
+			<artifactId>hibernate-jpa-2.0-api</artifactId>
+			<scope>provided</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-validator</artifactId>
+			<scope>provided</scope>
+		</dependency>
+	</dependencies>
+	
+</project>
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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<parent>
+		<groupId>de.test</groupId>
+		<artifactId>schemaexport-example</artifactId>
+		<version>1.0-SNAPSHOT</version>
+	</parent>
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>schemaexport-example-persistence-impl</artifactId>
+	<packaging>jar</packaging>
+	<version>1.0-SNAPSHOT</version>
+	<name>Schemaexport Example Persistenz</name>
+	<dependencies>
+		<dependency>
+			<groupId>de.test</groupId>
+			<artifactId>schemaexport-example-domain</artifactId>
+			<version>1.0-SNAPSHOT</version>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-entitymanager</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-core</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-validator</artifactId>
+			<scope>provided</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-log4j12</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>mysql</groupId>
+			<artifactId>mysql-connector-java</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.hsqldb</groupId>
+			<artifactId>hsqldb</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.6</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>de.juplo</groupId>
+				<artifactId>hibernate4-maven-plugin</artifactId>
+				<version>@project.version@</version>
+				<configuration>
+					<target>SCRIPT</target>
+					<driverClassName>org.hsqldb.jdbcDriver</driverClassName>
+					<hibernateDialect>org.hibernate.dialect.HSQLDialect</hibernateDialect>
+					<!-- 
+					<driverClassName>com.mysql.jdbc.Driver</driverClassName>
+					<hibernateDialect>org.hibernate.dialect.MySQLDialect</hibernateDialect>
+					-->
+				</configuration>
+				<executions>
+					<execution>
+						<goals>
+							<goal>export</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+</project>
\ 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 @@
+<persistence version="1.0"
+	xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
+	<persistence-unit name="swmtestappManagerTest">
+		<!-- in practice configured via Spring LocalContainerEntityManagerFactoryBean - packagesToScan -->
+		<class>de.test.schemaexport.domain.Department</class>
+		<class>de.test.schemaexport.domain.Employee</class>
+		<properties>
+			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
+			<property name="hibernate.connection.username" value="sa" />
+			<property name="hibernate.connection.password" value="" />
+			<property name="hibernate.show_sql" value="true" />
+			<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
+			<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
+			<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:test"/>
+		</properties>
+	</persistence-unit>
+</persistence>
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
+