HHH-17772 improve the test

This commit is contained in:
Gavin King 2024-03-01 21:10:41 +01:00
parent 2beb85e695
commit 9014182e3c
2 changed files with 38 additions and 5 deletions

View File

@ -22,7 +22,11 @@ public class DataTest extends CompilationTest {
public void test() { public void test() {
System.out.println( getMetaModelSourceAsString( Author.class ) ); System.out.println( getMetaModelSourceAsString( Author.class ) );
System.out.println( getMetaModelSourceAsString( Book.class ) ); System.out.println( getMetaModelSourceAsString( Book.class ) );
System.out.println( getMetaModelSourceAsString( Author.class, true ) );
System.out.println( getMetaModelSourceAsString( Book.class, true ) );
System.out.println( getMetaModelSourceAsString( BookAuthorRepository.class ) ); System.out.println( getMetaModelSourceAsString( BookAuthorRepository.class ) );
assertMetamodelClassGeneratedFor( Author.class, true );
assertMetamodelClassGeneratedFor( Book.class, true );
assertMetamodelClassGeneratedFor( Author.class ); assertMetamodelClassGeneratedFor( Author.class );
assertMetamodelClassGeneratedFor( Book.class ); assertMetamodelClassGeneratedFor( Book.class );
assertMetamodelClassGeneratedFor( BookAuthorRepository.class ); assertMetamodelClassGeneratedFor( BookAuthorRepository.class );

View File

@ -51,7 +51,7 @@ public class TestUtil {
public static void assertNoSourceFileGeneratedFor(Class<?> clazz) { public static void assertNoSourceFileGeneratedFor(Class<?> clazz) {
assertNotNull( "Class parameter cannot be null", clazz ); assertNotNull( "Class parameter cannot be null", clazz );
File sourceFile = getMetaModelSourceFileFor( clazz ); File sourceFile = getMetaModelSourceFileFor( clazz, false );
assertFalse( "There should be no source file: " + sourceFile.getName(), sourceFile.exists() ); assertFalse( "There should be no source file: " + sourceFile.getName(), sourceFile.exists() );
} }
@ -184,6 +184,15 @@ public class TestUtil {
assertNotNull( getMetamodelClassFor( clazz ) ); assertNotNull( getMetamodelClassFor( clazz ) );
} }
/**
* Asserts that a metamodel class for the specified class got generated.
*
* @param clazz the class for which a metamodel class should have been generated.
*/
public static void assertMetamodelClassGeneratedFor(Class<?> clazz, boolean prefix) {
assertNotNull( getMetamodelClassFor( clazz, prefix ) );
}
public static void assertNoMetamodelClassGeneratedFor(Class<?> clazz) { public static void assertNoMetamodelClassGeneratedFor(Class<?> clazz) {
try { try {
getMetamodelClassFor( clazz ); getMetamodelClassFor( clazz );
@ -246,8 +255,18 @@ public class TestUtil {
* @return the static metamodel class for the specified entity. * @return the static metamodel class for the specified entity.
*/ */
public static Class<?> getMetamodelClassFor(Class<?> entityClass) { public static Class<?> getMetamodelClassFor(Class<?> entityClass) {
return getMetamodelClassFor( entityClass, false );
}
/**
* Returns the static metamodel class for the specified entity.
*
* @param entityClass the entity for which to retrieve the metamodel class. Cannot be {@code null}.
*
* @return the static metamodel class for the specified entity.
*/
public static Class<?> getMetamodelClassFor(Class<?> entityClass, boolean prefix) {
assertNotNull( "Class parameter cannot be null", entityClass ); assertNotNull( "Class parameter cannot be null", entityClass );
String metaModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX; String metaModelClassName = getMetaModelClassName( entityClass, prefix );
try { try {
URL outDirUrl = getOutBaseDir( entityClass ).toURI().toURL(); URL outDirUrl = getOutBaseDir( entityClass ).toURI().toURL();
URL[] urls = new URL[1]; URL[] urls = new URL[1];
@ -262,16 +281,26 @@ public class TestUtil {
return null; return null;
} }
public static File getMetaModelSourceFileFor(Class<?> clazz) { public static File getMetaModelSourceFileFor(Class<?> clazz, boolean prefix) {
String metaModelClassName = clazz.getName() + META_MODEL_CLASS_POSTFIX; String metaModelClassName = getMetaModelClassName(clazz, prefix);
// 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( getOutBaseDir( clazz ), fileName ); return new File( getOutBaseDir( clazz ), fileName );
} }
private static String getMetaModelClassName(Class<?> clazz, boolean prefix) {
return prefix
? clazz.getPackageName() + '.' + META_MODEL_CLASS_POSTFIX + clazz.getSimpleName()
: clazz.getName() + META_MODEL_CLASS_POSTFIX;
}
public static String getMetaModelSourceAsString(Class<?> clazz) { public static String getMetaModelSourceAsString(Class<?> clazz) {
File sourceFile = getMetaModelSourceFileFor( clazz ); return getMetaModelSourceAsString( clazz, false );
}
public static String getMetaModelSourceAsString(Class<?> clazz, boolean prefix) {
File sourceFile = getMetaModelSourceFileFor( clazz, prefix );
StringBuilder contents = new StringBuilder(); StringBuilder contents = new StringBuilder();
try { try {