JpaBasedTest with CustomerDaoTest as example

This commit is contained in:
Michael Hoennig 2017-04-13 10:16:08 +02:00
parent 1ccea7bcd4
commit 30211f65ae
12 changed files with 374 additions and 0 deletions

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
<stringAttribute key="org.eclipse.ant.ui.ATTR_ANT_AUTO_TARGETS" value="enhance,"/>
<stringAttribute key="org.eclipse.ant.ui.ATTR_ANT_MANUAL_TARGETS" value="enhance,"/>
<booleanAttribute key="org.eclipse.ant.ui.ATTR_TARGETS_UPDATED" value="true"/>
<booleanAttribute key="org.eclipse.ant.ui.DEFAULT_VM_INSTALL" value="false"/>
<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.ant.ui.AntClasspathProvider"/>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="cust-webapp"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/cust-webapp/builder_project/enhance.xml}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="full,incremental,auto,"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="-Dopenjpa.libs -Dbuild.dir"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${workspace_loc:/cust-webapp/builder_project}"/>
</launchConfiguration>

View File

@ -0,0 +1,24 @@
<project name="jpa_enhance_builder">
<path id="enhance.cp">
<pathelement location="${basedir}${file.separator}${build.dir}"/>
<fileset dir="${basedir}${file.separator}${openjpa.libs}">
<include name="**/*.jar"/>
</fileset>
</path>
<property name="cp" refid="enhance.cp"/>
<target name="openjpa.libs.check" unless="openjpa.libs">
<fail message="Please set -Dopenjpa.libs in your builder configuration!"/>
</target>
<target name="build.dir.check" unless="build.dir">
<fail message="Please set -Dbuild.dir in your builder configuration!"/>
</target>
<target name="enhance" depends="openjpa.libs.check, build.dir.check">
<echo message="${cp}"/>
<taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
<classpath refid="enhance.cp"/>
</taskdef>
<openjpac>
<classpath refid="enhance.cp"/>
</openjpac>
</target>
</project>

13
cust-services/derby.log Normal file
View File

@ -0,0 +1,13 @@
----------------------------------------------------------------
Wed Apr 12 17:45:24 CEST 2017:
Booting Derby version The Apache Software Foundation - Apache Derby - 10.13.1.1 - (1765088): instance a816c00e-015b-62d7-3c87-00000d85f220
on database directory /home/mi/Projekte/Hostsharing/hs.hsadmin.jee-all/cust-services/hsadmin-test-db/derbydb3 with class loader sun.misc.Launcher$AppClassLoader@372f7a8d
Loaded from file:/home/mi/.m2/repository/org/apache/derby/derby/10.13.1.1/derby-10.13.1.1.jar
java.vendor=Oracle Corporation
java.runtime.version=1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13
user.dir=/home/mi/Projekte/Hostsharing/hs.hsadmin.jee-all/cust-services
os.name=Linux
os.arch=amd64
os.version=4.4.0-72-generic
derby.system.home=null
Database Class Loader started - derby.database.classpath=''

View File

@ -40,6 +40,40 @@
<version>0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>2.4.2</version>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-server</artifactId>
@ -55,13 +89,16 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archiveClasses>true</archiveClasses>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.0.2</version>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
@ -100,6 +137,27 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>migratePostgresLiquibaseChangeLogToH2</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${project.basedir}/scripts/migratePostgresLiquibaseChangeLogToH2.${script.extension}</executable>
<arguments>
<argument>${project.parent.basedir}/db-migration/src/main/resources/liquibase/db.changelog.xml</argument>
<argument>${project.build.directory}/classes/liquibase/db.changelog.xml</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.openejb.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
@ -115,6 +173,34 @@
</libs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-maven-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<includes>**/bo/*.class</includes>
<addDefaultConstructor>true</addDefaultConstructor>
<enforcePropertyRestrictions>true</enforcePropertyRestrictions>
</configuration>
<executions>
<execution>
<id>enhancer</id>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<!-- set the version to be the same as the level in your runtime -->
<version>2.4.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,9 @@
#!/bin/bash
mkdir -p $(dirname "$2")
sed \
-e's/ deferrable="[a-z]*" initiallyDeferred="[a-z]*"//' \
-e's/ type="INET"/ type="VARCHAR(19)"/' \
-e'/generated.69/,+5d' \
<"$1" >"$2"
# -e'/changeset.* id="generated-69"/,+5d' \

View File

@ -98,6 +98,9 @@ public class Customer implements Serializable {
@OrderBy("name")
private Set<Pac> pacs;
@Column(name = "shares_signed", columnDefinition = "integer")
private int sharesSigned;
public Customer() {
contacts = new HashSet<>();
pacs = new HashSet<>();
@ -253,4 +256,8 @@ public class Customer implements Serializable {
this.sepaDirectDebits = sepaDirectDebits;
}
public void setSharesSigned(int sharesSigned) {
this.sharesSigned = sharesSigned;
}
}

View File

@ -16,6 +16,13 @@ public class CustomerDao {
@PersistenceContext(name="hsar")
private EntityManager entityManager;
public CustomerDao() {
}
public CustomerDao(EntityManager em) {
this.entityManager = em;
}
public Customer findCustomerByName(final String customerName) throws UserException {
return findCustomerByNameImpl(customerName);
}

View File

@ -0,0 +1,36 @@
package de.hsadmin.dao.customer;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import de.hsadmin.bo.customer.Customer;
import de.hsadmin.common.error.UserException;
import de.hsadmin.test.JpaBasedTest;
public class CustomerDaoTest extends JpaBasedTest {
private CustomerDao customerDao;
@Before
public void init() {
customerDao = new CustomerDao(getEM());
}
@Test
public void findCustomerByName() throws UserException {
Customer newCustomer = store(createNewCustomer(100001, "test"));
Customer foundCustomer = customerDao.findCustomerByName(newCustomer.getName());
assertEquals(newCustomer.getId(), foundCustomer.getId());
}
private Customer createNewCustomer(int memberNo, String memberCode) {
Customer newCustomer = new Customer();
newCustomer.setMemberNo(memberNo);
newCustomer.setName(memberCode);
newCustomer.setSharesSigned(5);
return newCustomer;
}
}

View File

@ -0,0 +1,77 @@
package de.hsadmin.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
public abstract class JpaBasedTest {
private static final String UNIT_NAME = "HSADMIN_H2_TEST_DB";
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory(UNIT_NAME);
private EntityManager em;
@BeforeClass
public static void setupDatabase() throws Exception {
EntityManager em = emf.createEntityManager();
java.sql.Connection connection = em.unwrap(java.sql.Connection.class);
em.getTransaction().begin();
try {
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new Liquibase("liquibase/db.changelog.xml", new ClassLoaderResourceAccessor(), database);
liquibase.update("test");
} finally {
em.getTransaction().commit();
}
}
@Before
public void startTransaction() {
em = emf.createEntityManager();
em.getTransaction().begin();
}
@After
public void rollbacktransaction() {
// make sure constraint violations are found
em.flush();
// but rollback the transaction
try {
EntityTransaction transaction = em.getTransaction();
if ( transaction.isActive() ) {
em.getTransaction().rollback();
}
} finally {
// and close the EntityManager
if (em.isOpen()) {
em.close();
}
}
}
public EntityManager getEM() {
return em;
}
public <T> T store(T newEntity) {
getEM().persist(newEntity);
getEM().flush();
getEM().clear();
return newEntity;
}
}

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="HSADMIN_H2_TEST_DB" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<!-- auto scanning fails if there are methods returning entities in the test :-( -->
<class>de.hsadmin.bo.customer.Customer</class>
<class>de.hsadmin.bo.customer.Contact</class>
<class>de.hsadmin.bo.customer.SEPADirectDebit</class>
<class>de.hsadmin.bo.pac.Pac</class>
<class>de.hsadmin.bo.pac.PacComponent</class>
<class>de.hsadmin.bo.pac.UnixUser</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- HSQLDB
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:.hsadmin-test-db" />
<property name="javax.persistence.jdbc.user" value="sa" />
-->
<!-- H2 -->
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:.hsadmin-test-db;MODE=PostgreSQL;DB_CLOSE_DELAY=-1;MVCC=true" />
<property name="javax.persistence.jdbc.user" value="sa" />
<!-- Derby
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:.hsadmin-test-db/derbydb3;create=true" /> -->
<!-- OpenJPA -->
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
</properties>
</persistence-unit>
</persistence>

View File

@ -66,6 +66,40 @@
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<!--
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>2.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

29
pom.xml
View File

@ -36,6 +36,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>-XX:+AlwaysLockClassLoader</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>
@ -55,4 +58,30 @@
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>Windows</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<properties>
<script.extension>bat</script.extension>
</properties>
</profile>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<script.extension>sh</script.extension>
</properties>
</profile>
</profiles>
</project>