|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.processor.test.hhh18858; |
| 8 | + |
| 9 | + |
| 10 | +import jakarta.persistence.metamodel.ListAttribute; |
| 11 | +import jakarta.persistence.metamodel.SingularAttribute; |
| 12 | +import org.hibernate.processor.test.util.CompilationTest; |
| 13 | +import org.hibernate.processor.test.util.WithClasses; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.jupiter.api.Assertions; |
| 16 | + |
| 17 | +import java.lang.reflect.Field; |
| 18 | +import java.lang.reflect.ParameterizedType; |
| 19 | +import java.lang.reflect.Type; |
| 20 | +import java.util.Collection; |
| 21 | + |
| 22 | +import static org.hibernate.processor.test.util.TestUtil.getFieldFromMetamodelFor; |
| 23 | +import static org.hibernate.processor.test.util.TestUtil.getMetaModelSourceAsString; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Emmanuel Bernard |
| 28 | + */ |
| 29 | +public class ArrayTest extends CompilationTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + @WithClasses({Competitor.class, Contest.class}) |
| 33 | + public void testOneToMany() throws NoSuchFieldException, IllegalAccessException { |
| 34 | + System.out.println( getMetaModelSourceAsString( Competitor.class ) ); |
| 35 | + assertValidMetamodelField( Competitor.class, "id" ); |
| 36 | + assertValidMetamodelField( Competitor.class, "name" ); |
| 37 | + |
| 38 | + System.out.println( getMetaModelSourceAsString( Contest.class ) ); |
| 39 | + assertValidMetamodelField( Contest.class, "id" ); |
| 40 | + assertValidMetamodelField( Contest.class, "results" ); |
| 41 | + assertValidMetamodelField( Contest.class, "heldIn" ); |
| 42 | + } |
| 43 | + |
| 44 | + private void assertValidMetamodelField(Class<?> entityClass, String fieldName) |
| 45 | + throws NoSuchFieldException, IllegalAccessException { |
| 46 | + final Field entityField = entityClass.getDeclaredField( fieldName ); |
| 47 | + final Class<?> entityFieldType = entityField.getType(); |
| 48 | + |
| 49 | + final Field modelField = getFieldFromMetamodelFor( entityClass, fieldName ); |
| 50 | + final Type modelFieldGenericType = modelField.getGenericType(); |
| 51 | + if (modelFieldGenericType instanceof ParameterizedType) { |
| 52 | + ParameterizedType parametrized = (ParameterizedType) modelFieldGenericType; |
| 53 | + final Type[] typeArguments = parametrized.getActualTypeArguments(); |
| 54 | + assertEquals( 2, typeArguments.length ); |
| 55 | + assertEquals( entityClass, typeArguments[0] ); |
| 56 | + if ( Collection.class.isAssignableFrom( entityFieldType ) || entityFieldType.isArray() ) { |
| 57 | + assertEquals( ListAttribute.class, parametrized.getRawType() ); |
| 58 | + if ( Collection.class.isAssignableFrom( entityFieldType ) ) { |
| 59 | + final ParameterizedType entityFieldGenericType = (ParameterizedType) entityField.getGenericType(); |
| 60 | + assertEquals( entityFieldGenericType.getActualTypeArguments()[0], typeArguments[1] ); |
| 61 | + } |
| 62 | + else if ( entityFieldType.getComponentType().isPrimitive() ) { |
| 63 | + assertEquals( |
| 64 | + entityFieldType.getComponentType(), |
| 65 | + ((Class) typeArguments[1]).getDeclaredField( "TYPE" ).get( null ) |
| 66 | + ); |
| 67 | + } |
| 68 | + else { |
| 69 | + assertEquals( entityFieldType.getComponentType(), typeArguments[1] ); |
| 70 | + } |
| 71 | + } |
| 72 | + else { |
| 73 | + assertEquals( SingularAttribute.class, parametrized.getRawType() ); |
| 74 | + if ( entityFieldType.isPrimitive() ) { |
| 75 | + assertEquals( |
| 76 | + entityFieldType, |
| 77 | + ((Class) typeArguments[1]).getDeclaredField( "TYPE" ).get( null ) |
| 78 | + ); |
| 79 | + } |
| 80 | + else { |
| 81 | + assertEquals( entityFieldType, typeArguments[1] ); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + else { |
| 86 | + Assertions.fail(); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments