HHH-16261 - More strict test case

This commit is contained in:
Cedomir Igaly 2023-04-02 12:24:55 +02:00 committed by Christian Beikov
parent d9c074de62
commit d8dfa709da
1 changed files with 31 additions and 1 deletions

View File

@ -1,13 +1,23 @@
package org.hibernate.jpamodelgen.test.records; package org.hibernate.jpamodelgen.test.records;
import jakarta.persistence.metamodel.SingularAttribute;
import org.hibernate.jpamodelgen.test.util.CompilationTest; import org.hibernate.jpamodelgen.test.util.CompilationTest;
import org.hibernate.jpamodelgen.test.util.TestForIssue; import org.hibernate.jpamodelgen.test.util.TestForIssue;
import org.hibernate.jpamodelgen.test.util.WithClasses; import org.hibernate.jpamodelgen.test.util.WithClasses;
import org.junit.Test; import org.junit.Test;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import static java.lang.reflect.Modifier.isStatic;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
import static org.hibernate.jpamodelgen.test.util.TestUtil.getFieldFromMetamodelFor; import static org.hibernate.jpamodelgen.test.util.TestUtil.getFieldFromMetamodelFor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class Java14RecordsTest extends CompilationTest { public class Java14RecordsTest extends CompilationTest {
@ -16,7 +26,27 @@ public class Java14RecordsTest extends CompilationTest {
@WithClasses({Address.class, Author.class}) @WithClasses({Address.class, Author.class})
public void testEmbeddableRecordProperty() { public void testEmbeddableRecordProperty() {
assertMetamodelClassGeneratedFor(Address.class); assertMetamodelClassGeneratedFor(Address.class);
for (final String fieldName : List.of("street", "city", "postalCode")) {
assertNotNull("Address must contain '" + fieldName + "' field", getFieldFromMetamodelFor(Address.class, fieldName));
}
assertMetamodelClassGeneratedFor(Author.class); assertMetamodelClassGeneratedFor(Author.class);
assertNotNull("Author must contain 'address' field", getFieldFromMetamodelFor(Author.class, "address"));
final Field addressField = getFieldFromMetamodelFor(Author.class, "address");
assertNotNull("Author must contain 'address' field", addressField);
assertTrue(isStatic(addressField.getModifiers()));
if (addressField.getGenericType() instanceof ParameterizedType parameterizedType) {
assertEquals(SingularAttribute.class, parameterizedType.getRawType());
final Type[] typeArguments = parameterizedType.getActualTypeArguments();
assertEquals(2, typeArguments.length);
assertEquals(Author.class, typeArguments[0]);
assertEquals(Address.class, typeArguments[1]);
} else {
fail("Address field must be instance of ParameterizedType");
}
final Field addressNameField = getFieldFromMetamodelFor(Author.class, "address".toUpperCase());
assertNotNull("Author must contain 'ADDRESS' field", addressNameField);
assertTrue(isStatic(addressNameField.getModifiers()));
assertEquals(String.class, addressNameField.getGenericType());
} }
} }