Skip to content

Commit

Permalink
HHH-17772 improve the test
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Mar 1, 2024
1 parent 2beb85e commit 9014182
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public class DataTest extends CompilationTest {
public void test() {
System.out.println( getMetaModelSourceAsString( Author.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 ) );
assertMetamodelClassGeneratedFor( Author.class, true );
assertMetamodelClassGeneratedFor( Book.class, true );
assertMetamodelClassGeneratedFor( Author.class );
assertMetamodelClassGeneratedFor( Book.class );
assertMetamodelClassGeneratedFor( BookAuthorRepository.class );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private TestUtil() {

public static void assertNoSourceFileGeneratedFor(Class<?> 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() );
}

Expand Down Expand Up @@ -184,6 +184,15 @@ public static void assertMetamodelClassGeneratedFor(Class<?> 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) {
try {
getMetamodelClassFor( clazz );
Expand Down Expand Up @@ -246,8 +255,18 @@ public static File getSourceBaseDir(Class<?> testClass) {
* @return the static metamodel class for the specified entity.
*/
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 );
String metaModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX;
String metaModelClassName = getMetaModelClassName( entityClass, prefix );
try {
URL outDirUrl = getOutBaseDir( entityClass ).toURI().toURL();
URL[] urls = new URL[1];
Expand All @@ -262,16 +281,26 @@ public static Class<?> getMetamodelClassFor(Class<?> entityClass) {
return null;
}

public static File getMetaModelSourceFileFor(Class<?> clazz) {
String metaModelClassName = clazz.getName() + META_MODEL_CLASS_POSTFIX;
public static File getMetaModelSourceFileFor(Class<?> clazz, boolean prefix) {
String metaModelClassName = getMetaModelClassName(clazz, prefix);
// generate the file name
String fileName = metaModelClassName.replace( PACKAGE_SEPARATOR, PATH_SEPARATOR );
fileName = fileName.concat( ".java" );
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) {
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();

try {
Expand Down

0 comments on commit 9014182

Please sign in to comment.