Added a test that reuses classes and mappings from a real project
authorKai Moritz <kai@juplo.de>
Wed, 16 Aug 2017 09:09:25 +0000 (11:09 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 16 Aug 2017 11:36:33 +0000 (13:36 +0200)
pom.xml
src/test/java/de/juplo/jpa/converters/JPAConverterTest.java
src/test/resources/META-INF/date.xml [new file with mode: 0644]
src/test/resources/META-INF/node.xml [new file with mode: 0644]
src/test/resources/META-INF/persistence.xml

diff --git a/pom.xml b/pom.xml
index 0c61b3d..0b86e21 100755 (executable)
--- a/pom.xml
+++ b/pom.xml
@@ -64,7 +64,7 @@
     <hibernate.version>5.0.11.Final</hibernate.version>
     <hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version>
     <junit.version>4.12</junit.version>
-    <slf4j.version>1.7.13</slf4j.version>
+    <slf4j.version>1.7.22</slf4j.version>
 
   </properties>
 
       <version>${h2.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>de.juplo.yourshouter.api</groupId>
+      <artifactId>data-model</artifactId>
+      <version>1.5.1</version>
+      <scope>test</scope>
+    </dependency>
 
   </dependencies>
 
index 130aa7a..0ff1493 100644 (file)
@@ -2,6 +2,8 @@ package de.juplo.jpa.converters;
 
 import de.juplo.jpa.converters.TimeConversions.FixedZoneIdStrategy;
 import de.juplo.jpa.converters.TimeConversions.FixedZoneOffsetStrategy;
+import de.juplo.yourshouter.api.model.Date;
+import java.time.LocalDate;
 import java.time.OffsetDateTime;
 import java.time.ZoneId;
 import java.time.ZoneOffset;
@@ -28,6 +30,7 @@ public class JPAConverterTest
 
   ZonedDateTime zdt = ZonedDateTime.now();
   OffsetDateTime odt = OffsetDateTime.now();
+  LocalDate ld = LocalDate.now();
 
   ZoneId defaultZone = ZoneId.systemDefault();
   ZoneId otherZone = defaultZone.getId().equals("America/New_York") ? ZoneId.of("Europe/Berlin") : ZoneId.of("America/New_York");
@@ -104,6 +107,26 @@ public class JPAConverterTest
   }
 
 
+  @Test
+  public void testDataModel()
+  {
+    em = Persistence.createEntityManagerFactory("data-model").createEntityManager();
+    Date date;
+
+    TimeConversions.setZoneIdStrategy(new FixedZoneIdStrategy(defaultZone));
+    date = new Date();
+    date.setId("TEST");
+    date.setDate(ld);
+    date.setStart(zdt);
+    date.setEnd(zdt);
+    persist(date);
+    date = retrieve(Date.class, date.getId());
+    assertEquals(ld, date.getDate());
+    assertEquals(zdt, date.getStart());
+    assertEquals(zdt, date.getEnd());
+  }
+
+
   private void persist(Object entity)
   {
     EntityTransaction tx = em.getTransaction();
diff --git a/src/test/resources/META-INF/date.xml b/src/test/resources/META-INF/date.xml
new file mode 100644 (file)
index 0000000..e119473
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entity-mappings
+    xmlns="http://java.sun.com/xml/ns/persistence/orm"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_1.xsd"
+    version="2.1">
+
+  <package>de.juplo.yourshouter.api.model</package>
+  <entity class="Date">
+    <discriminator-value>17</discriminator-value>
+    <attributes>
+      <transient name="event"/>
+      <transient name="location"/>
+      <transient name="subunit"/>
+      <transient name="contributors"/>
+      <transient name="emails"/>
+      <transient name="links"/>
+      <transient name="numbers"/>
+      <transient name="media"/>
+      <transient name="prices"/>
+    </attributes>
+  </entity>
+
+</entity-mappings>
diff --git a/src/test/resources/META-INF/node.xml b/src/test/resources/META-INF/node.xml
new file mode 100644 (file)
index 0000000..04f8993
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entity-mappings
+    xmlns="http://java.sun.com/xml/ns/persistence/orm"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_1.xsd"
+    version="2.1">
+
+  <package>de.juplo.yourshouter.api.model</package>
+  <converter class="de.juplo.jpa.converters.ZonedDateTimeConverter" auto-apply="true"/>
+  <converter class="de.juplo.jpa.converters.LocalDateConverter" auto-apply="true"/>
+  <mapped-superclass class="Node" access="FIELD">
+    <inheritance strategy="SINGLE_TABLE"/>
+    <discriminator-column discriminator-type="INTEGER"/>
+    <attributes>
+      <id name="id"/>
+      <transient name="features"/>
+      <transient name="fields"/>
+      <transient name="nodes"/>
+    </attributes>
+  </mapped-superclass>
+
+</entity-mappings>
index 603cd73..a7d213f 100644 (file)
     </properties>
   </persistence-unit>
 
+  <persistence-unit name="data-model">
+    <description>Example, taken from the data-model package</description>
+    <mapping-file>META-INF/node.xml</mapping-file>
+    <mapping-file>META-INF/date.xml</mapping-file>
+    <exclude-unlisted-classes>true</exclude-unlisted-classes>
+    <properties>
+      <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
+      <property name="javax.persistence.jdbc.user" value="sa"/>
+      <property name="javax.persistence.jdbc.password" value=""/>
+      <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:"/>
+      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
+      <property name="hibernate.hbm2ddl.auto" value="create"/>
+      <property name="hibernate.show_sql" value="true"/>
+    </properties>
+  </persistence-unit>
+
 </persistence>