restructure model
This commit is contained in:
16
.gitignore
vendored
16
.gitignore
vendored
@@ -1,8 +1,8 @@
|
|||||||
./plantuml
|
/plantuml
|
||||||
./sql
|
/sql
|
||||||
./src
|
/frontend
|
||||||
./angular
|
/backend
|
||||||
./pom_jpa.xml
|
/persistence
|
||||||
./TelosysTools/downloads
|
/TelosysTools/downloads
|
||||||
./TelosysTools/templates
|
/TelosysTools/templates/*
|
||||||
!./TelosysTools/templates/infinimotion
|
!/TelosysTools/templates/infinimotion
|
||||||
4
.idea/model.iml
generated
4
.idea/model.iml
generated
@@ -16,7 +16,9 @@
|
|||||||
<excludeFolder url="file://$MODULE_DIR$/plantuml" />
|
<excludeFolder url="file://$MODULE_DIR$/plantuml" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/sql" />
|
<excludeFolder url="file://$MODULE_DIR$/sql" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/src" />
|
<excludeFolder url="file://$MODULE_DIR$/src" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/TelosysTools/templates/infinimotion/angular" />
|
<excludeFolder url="file://$MODULE_DIR$/frontend" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/backend" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/persistence" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ ROOT_PKG=de.infinimotion
|
|||||||
ENTITY_PKG=de.infinimotion.bean
|
ENTITY_PKG=de.infinimotion.bean
|
||||||
# -------------------------------------------
|
# -------------------------------------------
|
||||||
# --- Project Specific Variables
|
# --- Project Specific Variables
|
||||||
ProjectVariable.MAVEN_ARTIFACT_ID=infinimodel
|
ProjectVariable.MAVEN_ARTIFACT_ID=model
|
||||||
ProjectVariable.MAVEN_GROUP_ID=de.infinimotion
|
ProjectVariable.MAVEN_GROUP_ID=de.infinimotion
|
||||||
ProjectVariable.PROJECT_NAME=infinimodel
|
ProjectVariable.PROJECT_NAME=infinimodel
|
||||||
ProjectVariable.PROJECT_VERSION=0.1
|
ProjectVariable.PROJECT_VERSION=0.0.0
|
||||||
ProjectVariable.REST_SERVER_PORT=3000
|
ProjectVariable.REST_SERVER_PORT=3000
|
||||||
ProjectVariable.REST_API_ROOT=/api/v1
|
ProjectVariable.REST_API_ROOT=/api/v1
|
||||||
ProjectVariable.REST_URL_ROOT=http://localhost:3000
|
ProjectVariable.REST_URL_ROOT=http://localhost:3000
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Java domain class for entity "${entity.name}"
|
* Java backend model class for entity "${entity.name}"
|
||||||
* Created on $now.date ( $now.time )
|
* Created on $now.date ( $now.time )
|
||||||
* Generated by $generator.name ( version $generator.version )
|
* Generated by $generator.name ( version $generator.version )
|
||||||
*/
|
*/
|
||||||
@@ -19,7 +19,7 @@ import $import;
|
|||||||
#set( $linkFields = $entity.getAttributesByCriteria( $const.NOT_KEY, $const.IN_SELECTED_LINKS ) )
|
#set( $linkFields = $entity.getAttributesByCriteria( $const.NOT_KEY, $const.IN_SELECTED_LINKS ) )
|
||||||
##--------------------------------------------------------------------------------------------------------
|
##--------------------------------------------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Domain class for entity "${entity.name}"
|
* Backend model class for entity "${entity.name}"
|
||||||
*
|
*
|
||||||
* @author Telosys Tools Generator
|
* @author Telosys Tools Generator
|
||||||
*
|
*
|
||||||
33
TelosysTools/templates/infinimotion/backend/pom_xml.vm
Normal file
33
TelosysTools/templates/infinimotion/backend/pom_xml.vm
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<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/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<!-- Generated by $generator.name ver $generator.version ( $now.date $now.time ) -->
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>$fn.get("MAVEN_GROUP_ID","to.be.defined")</groupId>
|
||||||
|
<artifactId>$fn.get("MAVEN_ARTIFACT_ID","to.be.defined")-backend</artifactId>
|
||||||
|
<version>$fn.get("PROJECT_VERSION","0.0.0")</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>2.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>21</source>
|
||||||
|
<target>21</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
/*
|
|
||||||
* JUnit test case for bean $entity.name
|
|
||||||
* Created on $now.date ( $now.time )
|
|
||||||
* Generated by $generator.name ( version $generator.version )
|
|
||||||
*/
|
|
||||||
|
|
||||||
package ${target.javaPackageFromFolder(${TEST_SRC})};
|
|
||||||
|
|
||||||
## Same package => no need to import 'Record' class //import $entity.name ;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JUnit test case for bean $entity.name
|
|
||||||
*
|
|
||||||
* @author Telosys Tools Generator
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#set ( $beanVar = ${fn.uncapitalize($entity.name)} )
|
|
||||||
public class ${entity.name}Test
|
|
||||||
{
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSettersAndGetters() {
|
|
||||||
|
|
||||||
System.out.println("Checking class ${entity.name} getters and setters ..." );
|
|
||||||
|
|
||||||
${entity.name} ${beanVar} = new ${entity.name}();
|
|
||||||
|
|
||||||
## #set( $values = $fn.buildValues($entity.attributes, 1) )
|
|
||||||
#set( $attributesToBeTested = $entity.getAttributesByCriteria( $const.NOT_IN_LINKS ) )
|
|
||||||
#set( $values = $fn.buildValues($attributesToBeTested, 1) )
|
|
||||||
|
|
||||||
|
|
||||||
## #foreach( $attribute in $entity.attributes )
|
|
||||||
#foreach( $attribute in $attributesToBeTested )
|
|
||||||
#set( $value = $values.getValue($attribute.name) )
|
|
||||||
//--- Test setter/getter for attribute "$attribute.name" ( type : $attribute.type )
|
|
||||||
${beanVar}.${attribute.setter}( $value ) ;
|
|
||||||
#if ( $value == "null" )
|
|
||||||
Assert.assertNull(${beanVar}.${attribute.getter}());
|
|
||||||
#else
|
|
||||||
Assert.assertEquals( $value, ${beanVar}.${attribute.getter}() ) ;
|
|
||||||
#end
|
|
||||||
|
|
||||||
#end
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
<?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">
|
|
||||||
|
|
||||||
<!-- Generated by Telosys ( $now.date $now.time ) -->
|
|
||||||
|
|
||||||
<persistence-unit name="persistence-unit1" transaction-type="RESOURCE_LOCAL">
|
|
||||||
<provider>org.hibernate.ejb.HibernatePersistence</provider>
|
|
||||||
<properties>
|
|
||||||
<!-- Database configuration (Derby used as an example) -->
|
|
||||||
<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>
|
|
||||||
<property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/mydatabase"/>
|
|
||||||
<property name="hibernate.connection.username" value="root"/>
|
|
||||||
<property name="hibernate.connection.password" value="admin"/>
|
|
||||||
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
|
|
||||||
<!-- Hibernate configuration -->
|
|
||||||
<property name="hibernate.hbm2ddl.auto" value="none"/>
|
|
||||||
<property name="hibernate.show_sql" value="true"/>
|
|
||||||
</properties>
|
|
||||||
</persistence-unit>
|
|
||||||
|
|
||||||
</persistence>
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#checkId($entity)
|
#checkId($entity)
|
||||||
#parse("include/init_var_entity.vm")
|
#parse("persistence/include/init_var_entity.vm")
|
||||||
#parse("include/java_header.vm")
|
#parse("persistence/include/java_header.vm")
|
||||||
##--------------------------------------------------------------------------------------------------------
|
##--------------------------------------------------------------------------------------------------------
|
||||||
package ${target.javaPackageFromFolder($SRC)};
|
package ${target.javaPackageFromFolder($SRC)};
|
||||||
|
|
||||||
@@ -4,8 +4,8 @@
|
|||||||
#end
|
#end
|
||||||
## --------------------------------------------------
|
## --------------------------------------------------
|
||||||
#checkId($entity)
|
#checkId($entity)
|
||||||
#parse("include/init_var_entity.vm")
|
#parse("persistence/include/init_var_entity.vm")
|
||||||
#parse("include/java_header.vm")
|
#parse("persistence/include/java_header.vm")
|
||||||
##---------------------------------------------------------------------------------------
|
##---------------------------------------------------------------------------------------
|
||||||
## JPA CONFIGURATION
|
## JPA CONFIGURATION
|
||||||
##---------------------------------------------------------------------------------------
|
##---------------------------------------------------------------------------------------
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
package ${target.javaPackageFromFolder(${SRC})};
|
package ${target.javaPackageFromFolder(${SRC})};
|
||||||
## IF ENTITY HAS A COMPOSITE PRIMARY KEY => GENERATE AN 'ID CLASS' FOR THIS PRIMARY KEY
|
## IF ENTITY HAS A COMPOSITE PRIMARY KEY => GENERATE AN 'ID CLASS' FOR THIS PRIMARY KEY
|
||||||
#if ( $entity.hasCompositePrimaryKey() )
|
#if ( $entity.hasCompositePrimaryKey() )
|
||||||
$generator.generate($target.entityName , "${jpaEntityIdClass}.java", $target.folder, "main-java/XxxJpaEntityId_java.vm" )
|
$generator.generate($target.entityName , "${jpaEntityIdClass}.java", $target.folder, "persistence/persistence_entity_id_java.vm" )
|
||||||
#end
|
#end
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@@ -6,37 +6,22 @@
|
|||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>$fn.get("MAVEN_GROUP_ID","to.be.defined")</groupId>
|
<groupId>$fn.get("MAVEN_GROUP_ID","to.be.defined")</groupId>
|
||||||
<artifactId>$fn.get("MAVEN_ARTIFACT_ID","to.be.defined")</artifactId>
|
<artifactId>$fn.get("MAVEN_ARTIFACT_ID","to.be.defined")-persistence</artifactId>
|
||||||
<version>$fn.get("PROJECT_VERSION","1.0.0")</version>
|
<version>$fn.get("PROJECT_VERSION","1.0.0")</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<!-- JPA implementation : Hibernate -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hibernate</groupId>
|
<groupId>org.hibernate</groupId>
|
||||||
<artifactId>hibernate-entitymanager</artifactId>
|
<artifactId>hibernate-entitymanager</artifactId>
|
||||||
<version>5.4.18.Final</version>
|
<version>5.4.18.Final</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Dependencies for Unit Testing -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.12</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency> <!-- H2 in-memory database for tests -->
|
|
||||||
<groupId>com.h2database</groupId>
|
|
||||||
<artifactId>h2</artifactId>
|
|
||||||
<version>1.4.200</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -46,8 +31,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>2.5.1</version>
|
<version>2.5.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>21</source>
|
||||||
<target>1.8</target>
|
<target>21</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
@@ -8,35 +8,24 @@
|
|||||||
# . value 5 : number of execution : "1" for "ONCE" for all entities, default is multiple executions ( executed for each entity )
|
# . value 5 : number of execution : "1" for "ONCE" for all entities, default is multiple executions ( executed for each entity )
|
||||||
#---------------------------------------------------------
|
#---------------------------------------------------------
|
||||||
|
|
||||||
Model diagram ; model.plantuml ; plantuml ; class-diag_txt.vm ; 1
|
### PlantUML ###
|
||||||
|
Model diagram ; model.plantuml ; plantuml ; plantuml/class-diag_txt.vm ; 1
|
||||||
|
|
||||||
# PostgreSQL database
|
### PostgreSQL Datenbank ###
|
||||||
PostgreSQL create tables ; postgresql-create-tables.sql ; sql ; postgresql-create-tables_sql.vm ; 1
|
PostgreSQL create tables ; postgresql-create-tables.sql ; sql ; sql/postgresql-create-tables_sql.vm ; 1
|
||||||
|
|
||||||
# Domain class ( keep same package for tests classes )
|
### Frontend ###
|
||||||
Java domain class (bean) ; ${BEANNAME}.java ; ${SRC}/${ROOT_PKG}/data/domain ; domain_entity_java.vm
|
.npmrc ; .npmrc ; frontend ; frontend/.npmrc.vm ; 1
|
||||||
Java domain class JUnit test ; ${BEANNAME}Test.java ; ${TEST_SRC}/${ROOT_PKG}/data/domain ; domain_entity_test_java.vm
|
package.json ; package.json ; frontend ; frontend/package_json.vm ; 1
|
||||||
|
tsconfig.json ; tsconfig.json ; frontend ; frontend/tsconfig_json.vm ; 1
|
||||||
|
tsup.config.ts ; tsup.config.ts ; frontend ; frontend/tsup.config_ts.vm ; 1
|
||||||
|
index.ts ; index.ts ; frontend ; frontend/index_ts.vm ; 1
|
||||||
|
TypeScript Models ; ${BEANNAME}.ts ; frontend ; frontend/Xmodel_ts.vm
|
||||||
|
|
||||||
#--- Project root folder
|
### Backend ###
|
||||||
Maven pom.xml ; pom_jpa.xml ; ; pom_jpa_xml.vm ; 1
|
Java Backend Models ; ${BEANNAME}.java ; backend/${SRC}/${ROOT_PKG}/backend ; backend/backend_entity_java.vm
|
||||||
|
Maven Backend pom.xml ; pom.xml ; backend ; backend/pom_xml.vm ; 1
|
||||||
|
|
||||||
#--- JPA entities
|
### Persistence ###
|
||||||
Java JPA entity class ; ${BEANNAME}.java ; ${SRC}/${ROOT_PKG}/entities ; main-java/XxxJpaEntity_java.vm
|
Java Persistence Models ; ${BEANNAME}.java ; persistence/${SRC}/${ROOT_PKG}/persistence ; persistence/persistence_entity_java.vm
|
||||||
#--- JPA resources
|
Maven Persistence pom.xml ; pom.xml ; persistence ; persistence/pom_xml.vm ; 1
|
||||||
persistence.xml ; persistence.xml ; ${RES}/META-INF ; main-resources/persistence_xml.vm ; 1
|
|
||||||
# persistence.properties ; persistence.properties ; ${RES}/META-INF ; main-resources/persistence_properties.vm ; 1
|
|
||||||
|
|
||||||
#--- src/test/java
|
|
||||||
Tests : generic class ; JpaTest.java ; ${TEST_SRC}/${ROOT_PKG}/entities/tooling ; test-java/JpaTest_java.vm ; 1
|
|
||||||
Tests : database init ; DatabaseInit.java ; ${TEST_SRC}/${ROOT_PKG}/entities/tooling ; test-java/DatabaseInit_java.vm ; 1
|
|
||||||
Tests : entity tests ; ${BEANNAME}JpaTest.java ; ${TEST_SRC}/${ROOT_PKG}/entities ; test-java/XxxJpaTest_java.vm ; *
|
|
||||||
#--- src/test/resources
|
|
||||||
Tests : persistence.xml ; persistence.xml ; ${TEST_RES}/META-INF ; test-resources/persistence_xml.vm ; 1
|
|
||||||
|
|
||||||
# Angular
|
|
||||||
.npmrc ; .npmrc ; angular ; angular/.npmrc.vm ; 1
|
|
||||||
package.json ; package.json ; angular ; angular/package_json.vm ; 1
|
|
||||||
tsconfig.json ; tsconfig.json ; angular ; angular/tsconfig_json.vm ; 1
|
|
||||||
tsup.config.ts ; tsup.config.ts ; angular ; angular/tsup.config_ts.vm ; 1
|
|
||||||
index.ts ; index.ts ; angular ; angular/index_ts.vm ; 1
|
|
||||||
TypeScript Models ; ${BEANNAME}.ts ; angular ; angular/Xmodel_ts.vm
|
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
#parse("/include/java_header.vm")
|
|
||||||
##------------------------------------------------------
|
|
||||||
#set( $entityNames = [] )
|
|
||||||
#foreach( $entity in $model.allEntites )
|
|
||||||
#if ( ! $entity.isJoinEntity() )
|
|
||||||
#set($_ = $entityNames.add( $entity.name ) )
|
|
||||||
#end
|
|
||||||
#end
|
|
||||||
##------------------------------------------------------
|
|
||||||
package ${target.javaPackageFromFolder($TEST_SRC)};
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
|
|
||||||
#foreach( $entityName in $entityNames )
|
|
||||||
## import ${ROOT_PKG}.jpa.data.${entityName}Data;
|
|
||||||
import ${ROOT_PKG}.entities.${entityName}JpaTest;
|
|
||||||
#end
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Database initialization before unit tests
|
|
||||||
*
|
|
||||||
* @author Telosys
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class DatabaseInit {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set database initial state
|
|
||||||
* @param em
|
|
||||||
*/
|
|
||||||
public static void initializeTablesData(EntityManager em) {
|
|
||||||
|
|
||||||
// Disable referential integrity
|
|
||||||
execNativeQuery(em, "SET REFERENTIAL_INTEGRITY FALSE");
|
|
||||||
|
|
||||||
// Initialize each table with one entity
|
|
||||||
#foreach( $entityName in $entityNames )
|
|
||||||
initTableWithEntity(em, ${entityName}JpaTest.getInitEntity());
|
|
||||||
#end
|
|
||||||
|
|
||||||
// Enable referential integrity
|
|
||||||
execNativeQuery(em, "SET REFERENTIAL_INTEGRITY TRUE");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void execNativeQuery(EntityManager em, String sql) {
|
|
||||||
em.getTransaction().begin();
|
|
||||||
em.createNativeQuery(sql).executeUpdate();
|
|
||||||
em.getTransaction().commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void initTableWithEntity(EntityManager em, Object entity) {
|
|
||||||
execQuery(em, "DELETE FROM " + entity.getClass().getSimpleName());
|
|
||||||
initEntity(em, entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void execQuery(EntityManager em, String sql) {
|
|
||||||
em.getTransaction().begin();
|
|
||||||
em.createQuery(sql).executeUpdate();
|
|
||||||
em.getTransaction().commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void initEntity(EntityManager em, Object entity) {
|
|
||||||
em.getTransaction().begin();
|
|
||||||
em.merge(entity);
|
|
||||||
em.getTransaction().commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,247 +0,0 @@
|
|||||||
#parse("/include/java_header.vm")
|
|
||||||
##------------------------------------------------------
|
|
||||||
package ${target.javaPackageFromFolder($TEST_SRC)};
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.EntityManagerFactory;
|
|
||||||
import javax.persistence.Persistence;
|
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic abstract class for JPA JUnit test cases
|
|
||||||
*
|
|
||||||
* @author Telosys
|
|
||||||
*/
|
|
||||||
public abstract class JpaTest {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JPA Persistence Unit name for unit tests
|
|
||||||
* Must be the same as in "src/test/resources/META-INF/persistence.xml"
|
|
||||||
*/
|
|
||||||
private static final String PERSISTENCE_UNIT_NAME = "h2-test";
|
|
||||||
|
|
||||||
private static final boolean LOG_FLAG = true;
|
|
||||||
|
|
||||||
protected static EntityManagerFactory emf;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prints a log message
|
|
||||||
* @param msg
|
|
||||||
*/
|
|
||||||
protected static void log(String msg) {
|
|
||||||
if (LOG_FLAG) {
|
|
||||||
System.out.println("[LOG-TEST] " + msg);
|
|
||||||
System.out.flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private EntityManager createEntityManager() {
|
|
||||||
log(" createEntityManager()...");
|
|
||||||
return emf.createEntityManager();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void closeEntityManager(EntityManager em) {
|
|
||||||
log(" closeEntityManager(em)...");
|
|
||||||
em.clear();
|
|
||||||
em.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
public static void setUpBeforeClass() {
|
|
||||||
log("----- BeforeClass : Initializing JPA env...");
|
|
||||||
// All database tables are created before each test case class
|
|
||||||
// thanks to 'persistence.xml' property "hibernate.hbm2ddl.auto" set to "create"
|
|
||||||
emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void tearDownAfterClass() throws Exception {
|
|
||||||
log("----- AfterClass : Closing JPA env...");
|
|
||||||
if ( emf != null ) {
|
|
||||||
emf.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUpBeforeTest() throws Exception {
|
|
||||||
log("--- Before test : setUpBeforeTest()...");
|
|
||||||
EntityManager em = createEntityManager();
|
|
||||||
log(" DatabaseInit.init(em)...");
|
|
||||||
DatabaseInit.initializeTablesData(em);
|
|
||||||
closeEntityManager(em);
|
|
||||||
log(" Database initialized and ready for test.");
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDownAfterTest() throws Exception {
|
|
||||||
log("--- After test : tearDownAfterTest()...");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call em.find(entityClass, pk)
|
|
||||||
* @param <T>
|
|
||||||
* @param entityClass
|
|
||||||
* @param pk
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
protected <T> T find(Class<T> entityClass, Object pk) {
|
|
||||||
log("find(" + entityClass.getSimpleName() + ", " + pk +")");
|
|
||||||
return findAndPossiblyRefresh(entityClass, pk, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call em.find(entityClass, pk) and call em.refresh(entity) if entity found
|
|
||||||
* @param <T>
|
|
||||||
* @param entityClass
|
|
||||||
* @param pk
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
protected <T> T findAndRefresh(Class<T> entityClass, Object pk) {
|
|
||||||
log("findAndRefresh(" + entityClass.getSimpleName() + ", " + pk +")");
|
|
||||||
return findAndPossiblyRefresh(entityClass, pk, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls find then call refresh if flag is true
|
|
||||||
* @param <T>
|
|
||||||
* @param entityClass
|
|
||||||
* @param pk
|
|
||||||
* @param refreshFlag
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private <T> T findAndPossiblyRefresh(Class<T> entityClass, Object pk, boolean refreshFlag) {
|
|
||||||
EntityManager em = createEntityManager();
|
|
||||||
T entity = em.find(entityClass, pk);
|
|
||||||
if ( entity != null ) {
|
|
||||||
log(" em.find(entity,pk) : found ");
|
|
||||||
if ( refreshFlag ) {
|
|
||||||
log(" em.refresh(entity)");
|
|
||||||
em.refresh(entity); // Useful to refresh links => SQL SELECT
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log(" em.find(entity,pk) : not found ");
|
|
||||||
}
|
|
||||||
closeEntityManager(em);
|
|
||||||
return entity ;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call em.persist(entity) in a transaction
|
|
||||||
* @param entity
|
|
||||||
*/
|
|
||||||
protected void persist(Object entity) {
|
|
||||||
log("persist("+entity+")");
|
|
||||||
persistAndPossiblyRefresh(entity, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call em.persist(entity) in a transaction then call em.refresh(entity)
|
|
||||||
* @param entity
|
|
||||||
*/
|
|
||||||
protected void persistAndRefresh(Object entity) {
|
|
||||||
log("persistAndRefresh("+entity+")");
|
|
||||||
persistAndPossiblyRefresh(entity, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls persist then call refresh if flag is true
|
|
||||||
* @param entity
|
|
||||||
* @param refreshFlag
|
|
||||||
*/
|
|
||||||
private void persistAndPossiblyRefresh(Object entity, boolean refreshFlag) {
|
|
||||||
log("persistAndRefresh("+entity+")");
|
|
||||||
EntityManager em = createEntityManager();
|
|
||||||
log(" em.getTransaction().begin()");
|
|
||||||
em.getTransaction().begin();
|
|
||||||
log(" em.persist(entity)");
|
|
||||||
em.persist(entity);
|
|
||||||
log(" em.getTransaction().commit()");
|
|
||||||
em.getTransaction().commit();
|
|
||||||
// Refresh ?
|
|
||||||
if ( refreshFlag ) {
|
|
||||||
log(" em.refresh("+entity+")");
|
|
||||||
em.refresh(entity); // Useful to refresh links => SQL SELECT
|
|
||||||
}
|
|
||||||
closeEntityManager(em);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls em.merge(entity) in a transaction
|
|
||||||
* @param entity
|
|
||||||
* @return the managed entity (object returned by JPA merge)
|
|
||||||
*/
|
|
||||||
protected Object merge(Object entity) {
|
|
||||||
log("merge("+entity+")");
|
|
||||||
return mergeAndPossiblyRefresh(entity, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls em.merge(entity) in a transaction and refresh the managed entity
|
|
||||||
* @param entity
|
|
||||||
* @return the managed entity (object returned by JPA merge and refreshed)
|
|
||||||
*/
|
|
||||||
protected Object mergeAndRefresh(Object entity) {
|
|
||||||
log("mergeAndRefresh("+entity+")");
|
|
||||||
return mergeAndPossiblyRefresh(entity, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Call merge then call refresh if flag is true
|
|
||||||
* @param entity
|
|
||||||
* @param refreshFlag
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private Object mergeAndPossiblyRefresh(Object entity, boolean refreshFlag) {
|
|
||||||
EntityManager em = createEntityManager();
|
|
||||||
// Merge
|
|
||||||
log(" em.getTransaction().begin()");
|
|
||||||
em.getTransaction().begin();
|
|
||||||
log(" em.merge("+entity+")");
|
|
||||||
Object managedEntity = em.merge(entity);
|
|
||||||
log(" em.getTransaction().commit()");
|
|
||||||
em.getTransaction().commit();
|
|
||||||
// Refresh ?
|
|
||||||
if ( refreshFlag ) {
|
|
||||||
log(" em.refresh("+managedEntity+")");
|
|
||||||
em.refresh(managedEntity); // Useful to refresh links => SQL SELECT
|
|
||||||
}
|
|
||||||
closeEntityManager(em);
|
|
||||||
return managedEntity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Try to find an entity and call em.remove(entity) in a transaction
|
|
||||||
* @param <T>
|
|
||||||
* @param entityClass
|
|
||||||
* @param pk
|
|
||||||
* @return true if found and removed, else false
|
|
||||||
*/
|
|
||||||
protected <T> boolean findAndRemove(Class<T> entityClass, Object pk) {
|
|
||||||
boolean removed = false;
|
|
||||||
log("findAndRemove(" + entityClass.getSimpleName() + ", " + pk +")");
|
|
||||||
EntityManager em = createEntityManager();
|
|
||||||
log(" em.find(entity,pk) ");
|
|
||||||
T entity = em.find(entityClass, pk);
|
|
||||||
if ( entity != null ) {
|
|
||||||
log(" found ");
|
|
||||||
log(" em.getTransaction().begin()");
|
|
||||||
em.getTransaction().begin();
|
|
||||||
log(" em.remove(entity)");
|
|
||||||
em.remove(entity);
|
|
||||||
log(" em.getTransaction().commit()");
|
|
||||||
em.getTransaction().commit();
|
|
||||||
removed = true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
log(" not found ");
|
|
||||||
removed = false;
|
|
||||||
}
|
|
||||||
closeEntityManager(em);
|
|
||||||
return removed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
## --------------------------------------------------
|
|
||||||
#if ( $entity.isJoinEntity() )
|
|
||||||
#cancel("No test class for join entity")
|
|
||||||
#end
|
|
||||||
## --------------------------------------------------
|
|
||||||
#parse("include/init_var_entity.vm")
|
|
||||||
#parse("/include/java_header.vm")
|
|
||||||
#set( $values = $fn.buildValues($entity.attributes, 1) )
|
|
||||||
##------------------------------------------------------
|
|
||||||
package ${target.javaPackageFromFolder($TEST_SRC)};
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
|
|
||||||
import ${ROOT_PKG}.entities.${entity.name};
|
|
||||||
|
|
||||||
public class ${entity.name}Data {
|
|
||||||
|
|
||||||
private static ${entity.name} entity ;
|
|
||||||
|
|
||||||
public static void init(EntityManager em) {
|
|
||||||
// new entity instance
|
|
||||||
${entity.name} newEntity = new ${entity.name}();
|
|
||||||
#foreach( $attribute in $entity.attributes )
|
|
||||||
## #if ( $attribute.isNotNull() )
|
|
||||||
## newEntity.${attribute.setter}( $values.getValue($attribute.name) ) ;
|
|
||||||
## #end
|
|
||||||
newEntity.${attribute.setter}( $values.getValue($attribute.name) ) ;
|
|
||||||
#end
|
|
||||||
|
|
||||||
// save entity in database
|
|
||||||
em.getTransaction().begin();
|
|
||||||
${entity.name} managedEntity = (${entity.name}) em.merge(newEntity);
|
|
||||||
em.getTransaction().commit();
|
|
||||||
entity = managedEntity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ${entity.name} getEntity() {
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
## --------------------------------------------------
|
|
||||||
#if ( $entity.isJoinEntity() )
|
|
||||||
#cancel("No test class for join entity")
|
|
||||||
#end
|
|
||||||
## --------------------------------------------------
|
|
||||||
#parse("include/init_var_entity.vm")
|
|
||||||
#parse("/include/java_header.vm")
|
|
||||||
##------------------------------------------------------
|
|
||||||
package ${target.javaPackageFromFolder($TEST_SRC)};
|
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertNotNull;
|
|
||||||
|
|
||||||
## import ${ROOT_PKG}.jpa.tools.JpaTest;
|
|
||||||
import ${ROOT_PKG}.entities.tooling.JpaTest;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
## macro to define referenced value for the given attribute
|
|
||||||
#macro( setRefValue $attrib )
|
|
||||||
#foreach( $fkPart in $attrib.fkParts )
|
|
||||||
#if ( $foreach.count == 1 )
|
|
||||||
## #set( $refEntity = $model.getEntityByClassName($fkPart.referencedEntityName) )
|
|
||||||
## #set( $refAttribute = $refEntity.getAttributeByName($fkPart.referencedAttributeName) )
|
|
||||||
## #set( $refValue = "${refEntity.name}JpaTest.getInitEntity().${refAttribute.getter}()" )
|
|
||||||
## new (for test)
|
|
||||||
#set( $n = $attrib.fkPartsCount )
|
|
||||||
#set( $e = $fkPart.referencedEntity )
|
|
||||||
#set( $a = $fkPart.referencedAttribute )
|
|
||||||
##
|
|
||||||
#set( $refValue = "${fkPart.referencedEntityName}JpaTest.getInitEntity().${fkPart.referencedAttribute.getter}()" )
|
|
||||||
#end
|
|
||||||
#end
|
|
||||||
#end
|
|
||||||
##
|
|
||||||
#set( $values1 = $fn.buildValues($entity.attributes, 1) )
|
|
||||||
#set( $values2 = $fn.buildValues($entity.attributes, 2) )
|
|
||||||
##
|
|
||||||
public class ${entity.name}JpaTest extends JpaTest {
|
|
||||||
|
|
||||||
public static ${entity.name} getInitEntity() {
|
|
||||||
#buildEntity($values1)
|
|
||||||
}
|
|
||||||
|
|
||||||
private ${entity.name} createEntity() {
|
|
||||||
#buildEntity($values2)
|
|
||||||
}
|
|
||||||
|
|
||||||
#macro( buildEntity $argValues )
|
|
||||||
${entity.name} entity = new ${entity.name}();
|
|
||||||
#foreach( $attribute in $entity.attributes )
|
|
||||||
#if ( $attribute.isFK() )
|
|
||||||
#setRefValue ( $attribute )
|
|
||||||
entity.${attribute.setter}( $refValue ) ;
|
|
||||||
#else
|
|
||||||
#if ( ! $attribute.isAutoIncremented() )
|
|
||||||
entity.${attribute.setter}( $argValues.getValue($attribute.name) ) ;
|
|
||||||
#else
|
|
||||||
// $attribute.name is auto-incremented => do not set
|
|
||||||
#end
|
|
||||||
#end
|
|
||||||
#end
|
|
||||||
return entity;
|
|
||||||
#end
|
|
||||||
|
|
||||||
private Object getEntityKey(${entity.name} entity) {
|
|
||||||
#if ( $entity.hasCompositePrimaryKey() )
|
|
||||||
return new ${jpaEntityIdClass}( $fn.argumentsListWithGetter("entity", $entity.keyAttributes) );
|
|
||||||
#else
|
|
||||||
return entity.${entity.keyAttribute.getter}();
|
|
||||||
#end
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testFind() {
|
|
||||||
#if ( $entity.hasGeneratedKey() )
|
|
||||||
// NB: this entity has an GENERATED PRIMARY KEY
|
|
||||||
// Cannot use a generic test with this kind of PK
|
|
||||||
// Create a specific test for this case
|
|
||||||
#else
|
|
||||||
${entity.name} e = find(${entity.name}.class, getEntityKey(getInitEntity()) );
|
|
||||||
assertNotNull(e);
|
|
||||||
#end
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testPersistFind() {
|
|
||||||
#if ( $entity.hasGeneratedKey() )
|
|
||||||
// NB: this entity has an GENERATED PRIMARY KEY
|
|
||||||
// Cannot use a generic test with this kind of PK
|
|
||||||
// Create a specific test for this case
|
|
||||||
#else
|
|
||||||
${entity.name} entity = createEntity();
|
|
||||||
Object key = getEntityKey(entity);
|
|
||||||
|
|
||||||
// if entity doesn't exist yet
|
|
||||||
if ( find(${entity.name}.class, key) == null ) {
|
|
||||||
|
|
||||||
// Create with 'persist'
|
|
||||||
persistAndRefresh(entity);
|
|
||||||
|
|
||||||
// Find
|
|
||||||
${entity.name} entity2 = find(${entity.name}.class, key);
|
|
||||||
assertNotNull(entity2);
|
|
||||||
|
|
||||||
// Remove (use only if no risk of referential integrity constraint violation)
|
|
||||||
// findAndRemove(${entity.name}.class, key);
|
|
||||||
// assertNull(find(${entity.name}.class, key));
|
|
||||||
}
|
|
||||||
#end
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMergeFind() {
|
|
||||||
#if ( $entity.hasGeneratedKey() )
|
|
||||||
// NB: this entity has an GENERATED PRIMARY KEY
|
|
||||||
// Cannot use a generic test with this kind of PK
|
|
||||||
// Create a specific test for this case
|
|
||||||
#else
|
|
||||||
${entity.name} entity = createEntity();
|
|
||||||
Object key = getEntityKey(entity);
|
|
||||||
|
|
||||||
// Create or update with 'merge'
|
|
||||||
mergeAndRefresh(entity);
|
|
||||||
|
|
||||||
// Find
|
|
||||||
${entity.name} entity2 = find(${entity.name}.class, key);
|
|
||||||
assertNotNull(entity2);
|
|
||||||
|
|
||||||
// Remove (use only if no risk of referential integrity constraint violation)
|
|
||||||
// findAndRemove(${entity.name}.class, key);
|
|
||||||
// assertNull(find(${entity.name}.class, key));
|
|
||||||
#end
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
<?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">
|
|
||||||
|
|
||||||
<!-- Generated by Telosys ( $now.date $now.time ) -->
|
|
||||||
|
|
||||||
<!-- Persistence Unit for JUnit tests with in memory H2 database -->
|
|
||||||
<persistence-unit name="h2-test" transaction-type="RESOURCE_LOCAL">
|
|
||||||
<!-- <provider>org.hibernate.ejb.HibernatePersistence</provider> -->
|
|
||||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
|
||||||
|
|
||||||
|
|
||||||
<!--
|
|
||||||
List of all named managed persistence classes (all entities)
|
|
||||||
This list must be specified to insure portability
|
|
||||||
It is required for JUnit tests
|
|
||||||
-->
|
|
||||||
#set( $DB_SCHEMA_NAME = "TO_BE_DEFINED" )
|
|
||||||
#foreach( $entity in $selectedEntities )
|
|
||||||
<class>${ROOT_PKG}.entities.${entity.name}</class>
|
|
||||||
## Just keep the schema of the last entity
|
|
||||||
#set( $DB_SCHEMA_NAME = $entity.databaseSchema )
|
|
||||||
#end
|
|
||||||
|
|
||||||
## Schema name is determined from entities
|
|
||||||
#if ( ${DB_SCHEMA_NAME} != "" )
|
|
||||||
#set( $INIT_DB = ";INIT=CREATE SCHEMA IF NOT EXISTS ${DB_SCHEMA_NAME}" )
|
|
||||||
#else
|
|
||||||
#set( $INIT_DB = "" )
|
|
||||||
<!-- No SCHEMA NAME in entities, then no DATABASE INIT STRING -->
|
|
||||||
#end
|
|
||||||
<!-- Database schema is "$DB_SCHEMA_NAME" -->
|
|
||||||
<!-- H2 INIT : "${INIT_DB}" -->
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<!-- Database configuration ( model db id = $model.databaseId ) -->
|
|
||||||
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
|
|
||||||
<property name="hibernate.connection.url" value="jdbc:h2:mem:test${INIT_DB}"/>
|
|
||||||
<property name="hibernate.connection.username" value="sa"/>
|
|
||||||
<property name="hibernate.connection.password" value="sa"/>
|
|
||||||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
|
|
||||||
<!-- Hibernate configuration -->
|
|
||||||
<property name="hibernate.hbm2ddl.auto" value="create"/> <!-- Creates all tables for JPA entities -->
|
|
||||||
<property name="hibernate.show_sql" value="true"/>
|
|
||||||
</properties>
|
|
||||||
</persistence-unit>
|
|
||||||
|
|
||||||
</persistence>
|
|
||||||
Reference in New Issue
Block a user