Merge pull request #892 from tuxji/ant-jpa-task

Add AntJPADomainExporter and its test files
This commit is contained in:
Timo Westkämper 2014-08-14 18:12:27 +03:00
commit 1eff146253
5 changed files with 281 additions and 0 deletions

View File

@ -0,0 +1,141 @@
package com.mysema.query.jpa.ant;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.metamodel.Metamodel;
import com.mysema.query.jpa.codegen.JPADomainExporter;
/**
* AntJPADomainExporter exports JPA 2 metamodels to Querydsl expression types
*
* @author 200003548
*/
public class AntJPADomainExporter {
/**
* Additional property to use when creating the JPA entity manager factory
*/
public static class Property {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
/**
* Set of additional properties to use when creating the JPA entity manager factory
*/
public static class Configuration {
private Map<String, String> properties = new HashMap<String, String>();
public Map<String, String> getProperties() {
return properties;
}
public void addConfiguredProperty(Property property) {
properties.put(property.getName(), property.getValue());
}
}
/**
* Set of additional properties to use when creating the JPA entity manager factory
* (no default - not required)
*/
private Configuration configuration;
/**
* Name prefix for generated query types (default: "Q")
*/
private String namePrefix = "Q";
/**
* Name suffix for generated query types (default: "")
*/
private String nameSuffix = "";
/**
* Target folder in which to generate query types (no default - required)
*/
private String targetFolder;
/**
* Name of persistence unit from which to generate query types (no default - required)
*/
private String persistenceUnitName;
public Configuration getConfiguration() {
return configuration;
}
public void addConfiguration(Configuration configuration) {
this.configuration = configuration;
}
public String getNamePrefix() {
return namePrefix;
}
public void setNamePrefix(String namePrefix) {
this.namePrefix = namePrefix;
}
public String getNameSuffix() {
return nameSuffix;
}
public void setNameSuffix(String nameSuffix) {
this.nameSuffix = nameSuffix;
}
public String getTargetFolder() {
return targetFolder;
}
public void setTargetFolder(String targetFolder) {
this.targetFolder = targetFolder;
}
public String getPersistenceUnitName() {
return persistenceUnitName;
}
public void setPersistenceUnitName(String persistenceUnitName) {
this.persistenceUnitName = persistenceUnitName;
}
/**
* Exports the named persistence unit's metamodel to Querydsl query types. Expects to be
* called by Ant via name convention using a method with signature public void execute().
*/
public void execute() {
// We can assume we have the named persistence unit and its mapping file in our classpath,
// but we may have to allow some properties in that persistence unit to be overridden before
// we can successfully get that persistence unit's metamodel.
Map<String, String> properties = (configuration != null) ? configuration.getProperties() : null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
// Now we can get the persistence unit's metamodel and export it to Querydsl query types.
Metamodel configuration = emf.getMetamodel();
JPADomainExporter exporter = new JPADomainExporter(namePrefix, nameSuffix, new File(targetFolder), configuration);
try {
exporter.execute();
} catch (IOException e) {
throw new RuntimeException("Error in JPADomainExporter", e);
}
}
}

View File

@ -0,0 +1,25 @@
package com.mysema.query.jpa.ant;
import static org.junit.Assert.assertTrue;
import java.io.File;
import org.junit.Test;
public class AntJPADomainExporterTest {
@Test
public void testExecute() {
// Simulate configuring the task in an Ant build.xml file.
AntJPADomainExporter exporter = new AntJPADomainExporter();
exporter.setNamePrefix("Q");
exporter.setNameSuffix("");
exporter.setTargetFolder("target/AntJPADomainExporterTest");
exporter.setPersistenceUnitName("AntJPADomainExporterTest");
exporter.execute();
// Verify that the Querydsl query type was created successfully.
assertTrue(new File("target/AntJPADomainExporterTest/com/mysema/query/jpa/ant/QDepartments.java").exists());
}
}

View File

@ -0,0 +1,65 @@
package com.mysema.query.jpa.ant;
/**
* Auto-generated by:
* org.apache.openjpa.jdbc.meta.ReverseMappingTool$ReverseCodeGenerator
*/
public class Departments {
private Integer commentId;
private String deptDesc;
private int deptId;
private String tag;
private String timeZone;
public Departments() {
}
public Departments(int deptId) {
this.deptId = deptId;
}
public Integer getCommentId() {
return commentId;
}
public void setCommentId(Integer commentId) {
this.commentId = commentId;
}
public String getDeptDesc() {
return deptDesc;
}
public void setDeptDesc(String deptDesc) {
this.deptDesc = deptDesc;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getTimeZone() {
return timeZone;
}
public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}
}

View File

@ -0,0 +1,32 @@
<?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 orm_2_0.xsd" version="2.0">
<package>
com.mysema.query.jpa.ant
</package>
<access>
PROPERTY
</access>
<entity class="Departments">
<table schema="schema" name="departments"/>
<attributes>
<id name="deptId">
<column name="dept_id"/>
</id>
<basic name="commentId">
<column name="comment_id"/>
</basic>
<basic name="deptDesc">
<column name="dept_desc" length="50"/>
</basic>
<basic name="tag">
<column name="tag"/>
</basic>
<basic name="timeZone">
<column name="time_zone" length="100"/>
</basic>
</attributes>
</entity>
</entity-mappings>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="AntJPADomainExporterTest">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<mapping-file>META-INF/orm.xml</mapping-file>
<class>com.mysema.query.jpa.ant.Departments</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
</properties>
</persistence-unit>
</persistence>