Configure separate source directories for HR and ORM Quarkus Panache tests

This commit is contained in:
Christian Beikov 2024-02-26 15:33:13 +01:00 committed by Gavin King
parent 1fe9c66fb4
commit 9e323963f0
12 changed files with 389 additions and 78 deletions

View File

@ -18,9 +18,27 @@ ext {
xsdDir = file( "${projectDir}/src/main/xsd" ) xsdDir = file( "${projectDir}/src/main/xsd" )
} }
configurations { sourceSets {
quarkusOrmPanache quarkusOrmPanache {
quarkusHrPanache java {
srcDirs = ['src/quarkusOrmPanache/java']
}
resources {
srcDirs tasks.processTestResources
}
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
}
quarkusHrPanache {
java {
srcDirs = ['src/quarkusHrPanache/java']
}
resources {
srcDirs tasks.processTestResources
}
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
}
} }
dependencies { dependencies {
@ -37,16 +55,26 @@ dependencies {
xjc jakartaLibs.jaxb xjc jakartaLibs.jaxb
xjc rootProject.fileTree(dir: 'patched-libs/jaxb2-basics', include: '*.jar') xjc rootProject.fileTree(dir: 'patched-libs/jaxb2-basics', include: '*.jar')
quarkusOrmPanache "io.quarkus:quarkus-hibernate-orm-panache:3.6.2" quarkusOrmPanacheImplementation "io.quarkus:quarkus-hibernate-orm-panache:3.6.2"
quarkusHrPanache "io.quarkus:quarkus-hibernate-reactive-panache:3.6.2" quarkusHrPanacheImplementation "io.quarkus:quarkus-hibernate-reactive-panache:3.6.2"
}
// The source set gets a custom configuration which extends the normal test implementation config
configurations {
quarkusOrmPanacheImplementation.extendsFrom(testImplementation)
quarkusOrmPanacheRuntimeOnly.extendsFrom(testRuntimeOnly)
quarkusOrmPanacheCompileOnly.extendsFrom(testCompileOnly)
quarkusHrPanacheImplementation.extendsFrom(testImplementation)
quarkusHrPanacheRuntimeOnly.extendsFrom(testRuntimeOnly)
quarkusHrPanacheCompileOnly.extendsFrom(testCompileOnly)
} }
def quarkusOrmPanacheTestTask = tasks.register( 'quarkusOrmPanacheTest', Test ) { def quarkusOrmPanacheTestTask = tasks.register( 'quarkusOrmPanacheTest', Test ) {
description = 'Runs the Quarkus ORM Panache tests.' description = 'Runs the Quarkus ORM Panache tests.'
group = 'verification' group = 'verification'
testClassesDirs = sourceSets.test.output.classesDirs testClassesDirs = sourceSets.quarkusOrmPanache.output.classesDirs
classpath = sourceSets.test.runtimeClasspath + configurations.quarkusOrmPanache classpath = sourceSets.quarkusOrmPanache.runtimeClasspath
shouldRunAfter test shouldRunAfter test
dependsOn test dependsOn test
} }
@ -55,8 +83,8 @@ def quarkusHrPanacheTestTask = tasks.register( 'quarkusHrPanacheTest', Test ) {
description = 'Runs the Quarkus HR Panache tests.' description = 'Runs the Quarkus HR Panache tests.'
group = 'verification' group = 'verification'
testClassesDirs = sourceSets.test.output.classesDirs testClassesDirs = sourceSets.quarkusHrPanache.output.classesDirs
classpath = sourceSets.test.runtimeClasspath + configurations.quarkusHrPanache classpath = sourceSets.quarkusHrPanache.runtimeClasspath
shouldRunAfter test shouldRunAfter test
dependsOn test dependsOn test
} }

View File

@ -0,0 +1,19 @@
package org.hibernate.jpamodelgen.test.hrPanache;
import java.util.List;
import org.hibernate.annotations.processing.Find;
import org.hibernate.reactive.mutiny.Mutiny;
import io.quarkus.hibernate.reactive.panache.common.runtime.SessionOperations;
import io.smallrye.mutiny.Uni;
public interface BookRepositoryWithSession {
public default Uni<Mutiny.Session> mySession() {
return SessionOperations.getSession();
}
@Find
public Uni<List<PanacheBook>> findBook(String isbn);
}

View File

@ -0,0 +1,28 @@
package org.hibernate.jpamodelgen.test.hrPanache;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import java.util.List;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL;
import io.quarkus.hibernate.reactive.panache.PanacheEntity;
import io.smallrye.mutiny.Uni;
@Entity
public class PanacheBook extends PanacheEntity {
public @Id String isbn;
public @NaturalId String title;
public @NaturalId String author;
public String text;
public int pages;
@Find
public static native Uni<List<PanacheBook>> findBook(String isbn);
@HQL("WHERE isbn = :isbn")
public static native Uni<List<PanacheBook>> hqlBook(String isbn);
}

View File

@ -0,0 +1,19 @@
package org.hibernate.jpamodelgen.test.hrPanache;
import java.util.List;
import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL;
import io.quarkus.hibernate.reactive.panache.PanacheRepository;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class PanacheBookRepository implements PanacheRepository<PanacheBook> {
@Find
public native Uni<List<PanacheBook>> findBook(String isbn);
@HQL("WHERE isbn = :isbn")
public native Uni<List<PanacheBook>> hqlBook(String isbn);
}

View File

@ -0,0 +1,22 @@
package org.hibernate.jpamodelgen.test.hrPanache;
import java.util.List;
import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL;
import io.smallrye.mutiny.Uni;
public interface QuarkusBookRepository {
@Find
public Uni<List<PanacheBook>> findBook(String isbn);
@HQL("WHERE isbn = :isbn")
public Uni<List<PanacheBook>> hqlBook(String isbn);
@HQL("DELETE FROM PanacheBook")
public Uni<Void> deleteAllBooksVoid();
@HQL("DELETE FROM PanacheBook")
public Uni<Integer> deleteAllBooksInt();
}

View File

@ -0,0 +1,151 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpamodelgen.test.hrPanache;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestUtil;
import org.hibernate.jpamodelgen.test.util.WithClasses;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetamodelClassFor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* @author Gavin King
*/
public class QuarkusHrPanacheTest extends CompilationTest {
@Test
@WithClasses({ PanacheBook.class })
public void testPanacheEntityMetamodel() throws Exception {
// Panache entity
System.out.println( TestUtil.getMetaModelSourceAsString( PanacheBook.class ) );
Class<?> entityClass = getMetamodelClassFor( PanacheBook.class );
Assertions.assertNotNull( entityClass );
// Make sure it has the proper supertype
Class<?> superclass = entityClass.getSuperclass();
if ( superclass != null ) {
Assertions.assertEquals( "io.quarkus.hibernate.reactive.panache.PanacheEntity_", superclass.getName() );
}
// Panache static native method generates a static method
Method method = entityClass.getDeclaredMethod( "hqlBook", Uni.class, String.class );
Assertions.assertNotNull( method );
checkUni(method);
Assertions.assertTrue( Modifier.isStatic( method.getModifiers()) );
// Panache static native method generates a static method
method = entityClass.getDeclaredMethod( "findBook", Uni.class, String.class );
Assertions.assertNotNull( method );
checkUni(method);
Assertions.assertTrue( Modifier.isStatic( method.getModifiers() ) );
}
@Test
@WithClasses({ PanacheBook.class, PanacheBookRepository.class })
public void testPanacheRepositoryMetamodel() throws Exception {
// Panache repository
System.out.println( TestUtil.getMetaModelSourceAsString( PanacheBookRepository.class ) );
Class<?> repositoryClass = getMetamodelClassFor( PanacheBookRepository.class );
Assertions.assertNotNull( repositoryClass );
// Make sure it has the proper supertype
Class<?> superclass = repositoryClass.getSuperclass();
if ( superclass != null ) {
Assertions.assertEquals( "java.lang.Object", superclass.getName() );
}
// Panache native method generates a static method
Method method = repositoryClass.getDeclaredMethod( "hqlBook", Uni.class, String.class );
Assertions.assertNotNull( method );
checkUni(method);
Assertions.assertTrue( Modifier.isStatic(method.getModifiers()) );
// Panache native method generates a static method
method = repositoryClass.getDeclaredMethod( "findBook", Uni.class, String.class );
Assertions.assertNotNull( method );
checkUni(method);
Assertions.assertTrue( Modifier.isStatic( method.getModifiers() ) );
}
private void checkUni(Method method) {
Assertions.assertEquals("io.smallrye.mutiny.Uni<org.hibernate.reactive.mutiny.Mutiny$Session>", method.getGenericParameterTypes()[0].toString());
}
@Test
@WithClasses({ PanacheBook.class, QuarkusBookRepository.class })
public void testQuarkusRepositoryMetamodel() throws Exception {
// Regular repository
System.out.println( TestUtil.getMetaModelSourceAsString( QuarkusBookRepository.class ) );
Class<?> repositoryClass = getMetamodelClassFor( QuarkusBookRepository.class );
Assertions.assertNotNull( repositoryClass );
// Make sure it has the proper supertype
Class<?> superclass = repositoryClass.getSuperclass();
if ( superclass != null ) {
Assertions.assertEquals( "java.lang.Object", superclass.getName() );
}
Class<?>[] interfaces = repositoryClass.getInterfaces();
Assertions.assertEquals( 1, interfaces.length );
Assertions.assertEquals( QuarkusBookRepository.class.getName(), interfaces[0].getName() );
// Annotated method generates an instance method
Method method = repositoryClass.getDeclaredMethod( "hqlBook", String.class );
Assertions.assertNotNull( method );
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
// Annotated method generates an instance method
method = repositoryClass.getDeclaredMethod( "findBook", String.class );
Assertions.assertNotNull( method );
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
// Make sure we have only the default constructor
Constructor<?>[] constructors = repositoryClass.getDeclaredConstructors();
Assertions.assertNotNull( constructors );
Assertions.assertEquals( 1, constructors.length );
Assertions.assertNotNull( repositoryClass.getDeclaredConstructor() );
// Proper return type
method = repositoryClass.getDeclaredMethod( "deleteAllBooksVoid" );
Assertions.assertNotNull( method );
Assertions.assertEquals("io.smallrye.mutiny.Uni<java.lang.Void>", method.getGenericReturnType().toString());
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
// Proper return type
method = repositoryClass.getDeclaredMethod( "deleteAllBooksInt" );
Assertions.assertNotNull( method );
Assertions.assertEquals("io.smallrye.mutiny.Uni<java.lang.Integer>", method.getGenericReturnType().toString());
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
}
@Test
@WithClasses({ PanacheBook.class, BookRepositoryWithSession.class })
public void testBookRepositoryWithSessionMetamodel() throws Exception {
// Regular repository with default session method
System.out.println( TestUtil.getMetaModelSourceAsString( BookRepositoryWithSession.class ) );
Class<?> repositoryClass = getMetamodelClassFor( BookRepositoryWithSession.class );
Assertions.assertNotNull( repositoryClass );
// Make sure we have only the default constructor
Constructor<?>[] constructors = repositoryClass.getDeclaredConstructors();
Assertions.assertNotNull( constructors );
Assertions.assertEquals( 1, constructors.length );
Assertions.assertNotNull( repositoryClass.getDeclaredConstructor() );
// Make sure we do not override the default session method
Assertions.assertThrows( NoSuchMethodException.class, () -> repositoryClass.getDeclaredMethod( "mySession" ) );
}
}

View File

@ -0,0 +1,14 @@
package org.hibernate.jpamodelgen.test.ormPanache;
import java.util.List;
import org.hibernate.annotations.processing.Find;
import org.hibernate.annotations.processing.HQL;
public interface QuarkusBookRepository {
@Find
public List<PanacheBook> findBook(String isbn);
@HQL("WHERE isbn = :isbn")
public List<PanacheBook> hqlBook(String isbn);
}

View File

@ -12,10 +12,12 @@ import org.hibernate.jpamodelgen.test.util.WithClasses;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetamodelClassFor; import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetamodelClassFor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -29,23 +31,23 @@ public class QuarkusOrmPanacheTest extends CompilationTest {
// Panache entity // Panache entity
System.out.println( TestUtil.getMetaModelSourceAsString( PanacheBook.class ) ); System.out.println( TestUtil.getMetaModelSourceAsString( PanacheBook.class ) );
Class<?> entityClass = getMetamodelClassFor( PanacheBook.class ); Class<?> entityClass = getMetamodelClassFor( PanacheBook.class );
Assertions.assertNotNull(entityClass); Assertions.assertNotNull( entityClass );
// Make sure it has the proper supertype // Make sure it has the proper supertype
Class<?> superclass = entityClass.getSuperclass(); Class<?> superclass = entityClass.getSuperclass();
if(superclass != null) { if ( superclass != null ) {
Assertions.assertEquals("io.quarkus.hibernate.orm.panache.PanacheEntity_", superclass.getName()); Assertions.assertEquals( "io.quarkus.hibernate.orm.panache.PanacheEntity_", superclass.getName() );
} }
// Panache static native method generates a static method // Panache static native method generates a static method
Method method = entityClass.getDeclaredMethod("hqlBook", EntityManager.class, String.class); Method method = entityClass.getDeclaredMethod( "hqlBook", EntityManager.class, String.class );
Assertions.assertNotNull(method); Assertions.assertNotNull( method );
Assertions.assertTrue(Modifier.isStatic(method.getModifiers())); Assertions.assertTrue( Modifier.isStatic( method.getModifiers()) );
// Panache static native method generates a static method // Panache static native method generates a static method
method = entityClass.getDeclaredMethod("findBook", EntityManager.class, String.class); method = entityClass.getDeclaredMethod( "findBook", EntityManager.class, String.class );
Assertions.assertNotNull(method); Assertions.assertNotNull( method );
Assertions.assertTrue(Modifier.isStatic(method.getModifiers())); Assertions.assertTrue( Modifier.isStatic( method.getModifiers() ) );
} }
@Test @Test
@ -54,22 +56,55 @@ public class QuarkusOrmPanacheTest extends CompilationTest {
// Panache repository // Panache repository
System.out.println( TestUtil.getMetaModelSourceAsString( PanacheBookRepository.class ) ); System.out.println( TestUtil.getMetaModelSourceAsString( PanacheBookRepository.class ) );
Class<?> repositoryClass = getMetamodelClassFor( PanacheBookRepository.class ); Class<?> repositoryClass = getMetamodelClassFor( PanacheBookRepository.class );
Assertions.assertNotNull(repositoryClass); Assertions.assertNotNull( repositoryClass );
// Make sure it has the proper supertype // Make sure it has the proper supertype
Class<?> superclass = repositoryClass.getSuperclass(); Class<?> superclass = repositoryClass.getSuperclass();
if(superclass != null) { if ( superclass != null ) {
Assertions.assertEquals("java.lang.Object", superclass.getName()); Assertions.assertEquals( "java.lang.Object", superclass.getName() );
} }
// Panache native method generates a static method // Panache native method generates a static method
Method method = repositoryClass.getDeclaredMethod("hqlBook", EntityManager.class, String.class); Method method = repositoryClass.getDeclaredMethod( "hqlBook", EntityManager.class, String.class );
Assertions.assertNotNull(method); Assertions.assertNotNull( method );
Assertions.assertTrue(Modifier.isStatic(method.getModifiers())); Assertions.assertTrue( Modifier.isStatic(method.getModifiers()) );
// Panache native method generates a static method // Panache native method generates a static method
method = repositoryClass.getDeclaredMethod("findBook", EntityManager.class, String.class); method = repositoryClass.getDeclaredMethod( "findBook", EntityManager.class, String.class );
Assertions.assertNotNull(method); Assertions.assertNotNull( method );
Assertions.assertTrue(Modifier.isStatic(method.getModifiers())); Assertions.assertTrue( Modifier.isStatic( method.getModifiers() ) );
}
@Test
@WithClasses({ PanacheBook.class, QuarkusBookRepository.class })
public void testQuarkusRepositoryMetamodel() throws Exception {
// Panache repository
System.out.println( TestUtil.getMetaModelSourceAsString( QuarkusBookRepository.class ) );
Class<?> repositoryClass = getMetamodelClassFor( QuarkusBookRepository.class );
Assertions.assertNotNull( repositoryClass );
// Make sure it has the proper supertype
Class<?> superclass = repositoryClass.getSuperclass();
if ( superclass != null ) {
Assertions.assertEquals( "java.lang.Object", superclass.getName() );
}
Class<?>[] interfaces = repositoryClass.getInterfaces();
Assertions.assertEquals( 1, interfaces.length );
Assertions.assertEquals( QuarkusBookRepository.class.getName(), interfaces[0].getName() );
// Annotated method generates an instance method
Method method = repositoryClass.getDeclaredMethod( "hqlBook", String.class );
Assertions.assertNotNull( method );
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
// Annotated method generates an instance method
method = repositoryClass.getDeclaredMethod( "findBook", String.class );
Assertions.assertNotNull( method );
Assertions.assertFalse( Modifier.isStatic( method.getModifiers() ) );
// Make sure we have the proper constructor
Constructor<?> constructor = repositoryClass.getDeclaredConstructor( EntityManager.class );
Assertions.assertNotNull( constructor );
Assertions.assertTrue( constructor.isAnnotationPresent( Inject.class ) );
} }
} }

View File

@ -61,6 +61,7 @@ public class CompilationRunner extends BlockJUnit4ClassRunner {
return new CompilationStatement( return new CompilationStatement(
statement, statement,
getTestClass().getJavaClass(),
testEntities, testEntities,
preCompileEntities, preCompileEntities,
mappingFiles, mappingFiles,

View File

@ -37,34 +37,9 @@ public class CompilationStatement extends Statement {
private static final Logger log = Logger.getLogger( CompilationStatement.class ); private static final Logger log = Logger.getLogger( CompilationStatement.class );
private static final String PACKAGE_SEPARATOR = "."; private static final String PACKAGE_SEPARATOR = ".";
private static final String ANNOTATION_PROCESSOR_OPTION_PREFIX = "-A"; private static final String ANNOTATION_PROCESSOR_OPTION_PREFIX = "-A";
private static final String SOURCE_BASE_DIR_PROPERTY = "sourceBaseDir";
private static final String SOURCE_BASE_DIR;
static {
// first we try to guess the target directory.
File potentialSourceDirectory = new File(System.getProperty( "user.dir" ), "tooling/metamodel-generator/src/test/java");
// the command line build sets the user.dir to sub project directory
if ( !potentialSourceDirectory.exists() ) {
potentialSourceDirectory = new File(System.getProperty( "user.dir" ), "src/test/java");
}
if ( potentialSourceDirectory.exists() ) {
SOURCE_BASE_DIR = potentialSourceDirectory.getAbsolutePath();
}
else {
String tmp = System.getProperty( SOURCE_BASE_DIR_PROPERTY );
if ( tmp == null ) {
fail(
"Unable to guess determine the source directory. Specify the system property 'sourceBaseDir'" +
" pointing to the base directory of the test java sources."
);
}
SOURCE_BASE_DIR = tmp;
}
}
private final Statement originalStatement; private final Statement originalStatement;
private final Class<?> testClass;
private final List<Class<?>> testEntities; private final List<Class<?>> testEntities;
private final List<Class<?>> preCompileEntities; private final List<Class<?>> preCompileEntities;
private final List<String> xmlMappingFiles; private final List<String> xmlMappingFiles;
@ -73,12 +48,14 @@ public class CompilationStatement extends Statement {
private final List<Diagnostic<?>> compilationDiagnostics; private final List<Diagnostic<?>> compilationDiagnostics;
public CompilationStatement(Statement originalStatement, public CompilationStatement(Statement originalStatement,
Class<?> testClass,
List<Class<?>> testEntities, List<Class<?>> testEntities,
List<Class<?>> proCompileEntities, List<Class<?>> proCompileEntities,
List<String> xmlMappingFiles, List<String> xmlMappingFiles,
Map<String, String> processorOptions, Map<String, String> processorOptions,
boolean ignoreCompilationErrors) { boolean ignoreCompilationErrors) {
this.originalStatement = originalStatement; this.originalStatement = originalStatement;
this.testClass = testClass;
this.testEntities = testEntities; this.testEntities = testEntities;
this.preCompileEntities = proCompileEntities; this.preCompileEntities = proCompileEntities;
this.xmlMappingFiles = xmlMappingFiles; this.xmlMappingFiles = xmlMappingFiles;
@ -114,7 +91,7 @@ public class CompilationStatement extends Statement {
} }
private String getPathToSource(Class<?> testClass) { private String getPathToSource(Class<?> testClass) {
return SOURCE_BASE_DIR + File.separator + testClass.getName() return TestUtil.getSourceBaseDir( testClass ).getAbsolutePath() + File.separator + testClass.getName()
.replace( PACKAGE_SEPARATOR, File.separator ) + ".java"; .replace( PACKAGE_SEPARATOR, File.separator ) + ".java";
} }
@ -136,7 +113,7 @@ public class CompilationStatement extends Statement {
private List<String> createJavaOptions() { private List<String> createJavaOptions() {
List<String> options = new ArrayList<String>(); List<String> options = new ArrayList<String>();
options.add( "-d" ); options.add( "-d" );
options.add( TestUtil.getOutBaseDir().getAbsolutePath() ); options.add( TestUtil.getOutBaseDir( testClass ).getAbsolutePath() );
options.add( "-processor" ); options.add( "-processor" );
options.add( JPAMetaModelEntityProcessor.class.getName() ); options.add( JPAMetaModelEntityProcessor.class.getName() );

View File

@ -22,7 +22,7 @@ public abstract class CompilationTest {
@After @After
public void cleanup() throws Exception { public void cleanup() throws Exception {
TestUtil.deleteProcessorGeneratedFiles(); TestUtil.deleteProcessorGeneratedFiles(getClass());
} }
} }

View File

@ -45,17 +45,6 @@ public class TestUtil {
public static final String RESOURCE_SEPARATOR = "/"; public static final String RESOURCE_SEPARATOR = "/";
private static final String PACKAGE_SEPARATOR = "."; private static final String PACKAGE_SEPARATOR = ".";
private static final String META_MODEL_CLASS_POSTFIX = "_"; private static final String META_MODEL_CLASS_POSTFIX = "_";
private static final File OUT_BASE_DIR;
static {
File targetDir = getTargetDir();
OUT_BASE_DIR = new File( targetDir, "processor-generated-test-classes" );
if ( !OUT_BASE_DIR.exists() ) {
if ( !OUT_BASE_DIR.mkdirs() ) {
fail( "Unable to create test output directory " + OUT_BASE_DIR.toString() );
}
}
}
private TestUtil() { private TestUtil() {
} }
@ -206,9 +195,10 @@ public class TestUtil {
/** /**
* Deletes recursively all files found in the output directory for the annotation processor. * Deletes recursively all files found in the output directory for the annotation processor.
* @return the output directory for the generated source and class files.
*/ */
public static void deleteProcessorGeneratedFiles() { public static void deleteProcessorGeneratedFiles(Class<?> testClass) {
for ( File file : OUT_BASE_DIR.listFiles() ) { for ( File file : getOutBaseDir(testClass).listFiles() ) {
deleteFilesRecursive( file ); deleteFilesRecursive( file );
} }
} }
@ -216,8 +206,36 @@ public class TestUtil {
/** /**
* @return the output directory for the generated source and class files. * @return the output directory for the generated source and class files.
*/ */
public static File getOutBaseDir() { public static File getOutBaseDir(Class<?> testClass) {
return OUT_BASE_DIR; File targetDir = getTargetDir( testClass );
File outBaseDir = new File( targetDir, "processor-generated-test-classes" );
if ( !outBaseDir.exists() ) {
if ( !outBaseDir.mkdirs() ) {
fail( "Unable to create test output directory " + outBaseDir );
}
}
return outBaseDir;
}
public static File getSourceBaseDir(Class<?> testClass) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
String currentTestClassName = testClass.getName();
int hopsToCompileDirectory = currentTestClassName.split( "\\." ).length;
URL classURL = contextClassLoader.getResource( currentTestClassName.replace( '.', '/' ) + ".class" );
File targetDir = new File( classURL.getFile() );
// navigate back to '/target'
for ( int i = 0; i < hopsToCompileDirectory; i++ ) {
targetDir = targetDir.getParentFile();
}
final String configurationDirectory = targetDir.getName();
final File baseDir = targetDir.getParentFile().getParentFile().getParentFile().getParentFile();
final File outBaseDir = new File( baseDir, "src/" + configurationDirectory + "/java" );
if ( !outBaseDir.exists() ) {
if ( !outBaseDir.mkdirs() ) {
fail( "Unable to create test output directory " + outBaseDir );
}
}
return outBaseDir;
} }
/** /**
@ -231,7 +249,7 @@ public class TestUtil {
assertNotNull( "Class parameter cannot be null", entityClass ); assertNotNull( "Class parameter cannot be null", entityClass );
String metaModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX; String metaModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX;
try { try {
URL outDirUrl = OUT_BASE_DIR.toURI().toURL(); URL outDirUrl = getOutBaseDir( entityClass ).toURI().toURL();
URL[] urls = new URL[1]; URL[] urls = new URL[1];
urls[0] = outDirUrl; urls[0] = outDirUrl;
URLClassLoader classLoader = new URLClassLoader( urls, TestUtil.class.getClassLoader() ); URLClassLoader classLoader = new URLClassLoader( urls, TestUtil.class.getClassLoader() );
@ -249,7 +267,7 @@ public class TestUtil {
// generate the file name // generate the file name
String fileName = metaModelClassName.replace( PACKAGE_SEPARATOR, PATH_SEPARATOR ); String fileName = metaModelClassName.replace( PACKAGE_SEPARATOR, PATH_SEPARATOR );
fileName = fileName.concat( ".java" ); fileName = fileName.concat( ".java" );
return new File( OUT_BASE_DIR + PATH_SEPARATOR + fileName ); return new File( getOutBaseDir( clazz ), fileName );
} }
public static String getMetaModelSourceAsString(Class<?> clazz) { public static String getMetaModelSourceAsString(Class<?> clazz) {
@ -390,13 +408,12 @@ public class TestUtil {
* *
* @return the target directory of the build * @return the target directory of the build
*/ */
public static File getTargetDir() { public static File getTargetDir(Class<?> currentTestClass) {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
// get a URL reference to something we now is part of the classpath (our own classes) String currentTestClassName = currentTestClass.getName();
String currentTestClass = TestUtil.class.getName(); int hopsToCompileDirectory = currentTestClassName.split( "\\." ).length;
int hopsToCompileDirectory = currentTestClass.split( "\\." ).length;
int hopsToTargetDirectory = hopsToCompileDirectory + 2; int hopsToTargetDirectory = hopsToCompileDirectory + 2;
URL classURL = contextClassLoader.getResource( currentTestClass.replace( '.', '/' ) + ".class" ); URL classURL = contextClassLoader.getResource( currentTestClassName.replace( '.', '/' ) + ".class" );
// navigate back to '/target' // navigate back to '/target'
File targetDir = new File( classURL.getFile() ); File targetDir = new File( classURL.getFile() );
// navigate back to '/target' // navigate back to '/target'