diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java index e0c29351b6..270fef0748 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java @@ -39,6 +39,7 @@ import org.hibernate.jpamodelgen.xml.jaxb.Embeddable; import org.hibernate.jpamodelgen.xml.jaxb.EmbeddableAttributes; import org.hibernate.jpamodelgen.xml.jaxb.Entity; import org.hibernate.jpamodelgen.xml.jaxb.Id; +import org.hibernate.jpamodelgen.xml.jaxb.ManyToMany; import org.hibernate.jpamodelgen.xml.jaxb.ManyToOne; import org.hibernate.jpamodelgen.xml.jaxb.MappedSuperclass; import org.hibernate.jpamodelgen.xml.jaxb.OneToMany; @@ -87,7 +88,6 @@ public class XmlMetaEntity implements MetaEntity { this.importContext = new ImportContextImpl( getPackageName() ); this.element = element; Attributes attributes = mappedSuperclass.getAttributes(); - parseAttributes( attributes ); } @@ -98,39 +98,7 @@ public class XmlMetaEntity implements MetaEntity { this.importContext = new ImportContextImpl( getPackageName() ); this.element = element; EmbeddableAttributes attributes = embeddable.getAttributes(); - - XmlMetaSingleAttribute attribute; - for ( Basic basic : attributes.getBasic() ) { - attribute = new XmlMetaSingleAttribute( this, basic.getName(), getType( basic.getName(), null ) ); - members.add( attribute ); - } - - for ( ManyToOne manyToOne : attributes.getManyToOne() ) { - attribute = new XmlMetaSingleAttribute( - this, manyToOne.getName(), getType( manyToOne.getName(), manyToOne.getTargetEntity() ) - ); - members.add( attribute ); - } - - for ( OneToOne oneToOne : attributes.getOneToOne() ) { - attribute = new XmlMetaSingleAttribute( - this, oneToOne.getName(), getType( oneToOne.getName(), oneToOne.getTargetEntity() ) - ); - members.add( attribute ); - } - - XmlMetaCollection metaCollection; - for ( OneToMany oneToMany : attributes.getOneToMany() ) { - String[] types = getCollectionType( oneToMany.getName(), oneToMany.getTargetEntity() ); - metaCollection = new XmlMetaCollection( this, oneToMany.getName(), types[0], types[1] ); - members.add( metaCollection ); - } - - for ( ElementCollection collection : attributes.getElementCollection() ) { - String[] types = getCollectionType( collection.getName(), collection.getTargetClass() ); - metaCollection = new XmlMetaCollection( this, collection.getName(), types[0], types[1] ); - members.add( metaCollection ); - } + parseEmbeddableAttributes( attributes ); } public String getSimpleName() { @@ -272,6 +240,53 @@ public class XmlMetaEntity implements MetaEntity { } XmlMetaCollection metaCollection; + for ( ManyToMany manyToMany : attributes.getManyToMany() ) { + String[] types = getCollectionType( manyToMany.getName(), manyToMany.getTargetEntity() ); + metaCollection = new XmlMetaCollection( this, manyToMany.getName(), types[0], types[1] ); + members.add( metaCollection ); + } + + for ( OneToMany oneToMany : attributes.getOneToMany() ) { + String[] types = getCollectionType( oneToMany.getName(), oneToMany.getTargetEntity() ); + metaCollection = new XmlMetaCollection( this, oneToMany.getName(), types[0], types[1] ); + members.add( metaCollection ); + } + + for ( ElementCollection collection : attributes.getElementCollection() ) { + String[] types = getCollectionType( collection.getName(), collection.getTargetClass() ); + metaCollection = new XmlMetaCollection( this, collection.getName(), types[0], types[1] ); + members.add( metaCollection ); + } + } + + private void parseEmbeddableAttributes(EmbeddableAttributes attributes) { + XmlMetaSingleAttribute attribute; + for ( Basic basic : attributes.getBasic() ) { + attribute = new XmlMetaSingleAttribute( this, basic.getName(), getType( basic.getName(), null ) ); + members.add( attribute ); + } + + for ( ManyToOne manyToOne : attributes.getManyToOne() ) { + attribute = new XmlMetaSingleAttribute( + this, manyToOne.getName(), getType( manyToOne.getName(), manyToOne.getTargetEntity() ) + ); + members.add( attribute ); + } + + for ( OneToOne oneToOne : attributes.getOneToOne() ) { + attribute = new XmlMetaSingleAttribute( + this, oneToOne.getName(), getType( oneToOne.getName(), oneToOne.getTargetEntity() ) + ); + members.add( attribute ); + } + + XmlMetaCollection metaCollection; + for ( ManyToMany manyToMany : attributes.getManyToMany() ) { + String[] types = getCollectionType( manyToMany.getName(), manyToMany.getTargetEntity() ); + metaCollection = new XmlMetaCollection( this, manyToMany.getName(), types[0], types[1] ); + members.add( metaCollection ); + } + for ( OneToMany oneToMany : attributes.getOneToMany() ) { String[] types = getCollectionType( oneToMany.getName(), oneToMany.getTargetEntity() ); metaCollection = new XmlMetaCollection( this, oneToMany.getName(), types[0], types[1] ); diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java index bdd0e54a52..3a6b5a0554 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java @@ -20,9 +20,11 @@ package org.hibernate.jpamodelgen.test.accesstype; import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAbsenceOfField; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertFieldType; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfField; +import org.hibernate.jpamodelgen.test.util.TestUtil; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAbsenceOfFieldInMetamodelFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertFieldTypeInMetaModelFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor; /** * @author Emmanuel Bernard @@ -31,58 +33,58 @@ import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfField public class AccessTypeTest extends CompilationTest { @Test - public void testExcludeTransientFieldAndStatic() throws Exception { - assertAbsenceOfField( Product.class.getName() + "_", "nonPersistent" ); - assertAbsenceOfField( Product.class.getName() + "_", "nonPersistent2" ); + public void testExcludeTransientFieldAndStatic() { + TestUtil.assertAbsenceOfFieldInMetamodelFor( Product.class, "nonPersistent" ); + TestUtil.assertAbsenceOfFieldInMetamodelFor( Product.class, "nonPersistent2" ); } @Test - public void testDefaultAccessTypeOnEntity() throws Exception { - assertAbsenceOfField( User.class.getName() + "_", "nonPersistent" ); + public void testDefaultAccessTypeOnEntity() { + TestUtil.assertAbsenceOfFieldInMetamodelFor( User.class, "nonPersistent" ); } @Test - public void testDefaultAccessTypeForSubclassOfEntity() throws Exception { - assertAbsenceOfField( Customer.class.getName() + "_", "nonPersistent" ); + public void testDefaultAccessTypeForSubclassOfEntity() { + TestUtil.assertAbsenceOfFieldInMetamodelFor( Customer.class, "nonPersistent" ); } @Test - public void testDefaultAccessTypeForEmbeddable() throws Exception { - assertAbsenceOfField( Detail.class.getName() + "_", "nonPersistent" ); + public void testDefaultAccessTypeForEmbeddable() { + TestUtil.assertAbsenceOfFieldInMetamodelFor( Detail.class, "nonPersistent" ); } @Test - public void testInheritedAccessTypeForEmbeddable() throws Exception { - assertAbsenceOfField( Country.class.getName() + "_", "nonPersistent" ); - assertAbsenceOfField( - Pet.class.getName() + "_", "nonPersistent", "Collection of embeddable not taken care of" + public void testInheritedAccessTypeForEmbeddable() { + TestUtil.assertAbsenceOfFieldInMetamodelFor( Country.class, "nonPersistent" ); + assertAbsenceOfFieldInMetamodelFor( + Pet.class, "nonPersistent", "Collection of embeddable not taken care of" ); } @Test - public void testDefaultAccessTypeForMappedSuperclass() throws Exception { - assertAbsenceOfField( Detail.class.getName() + "_", "volume" ); + public void testDefaultAccessTypeForMappedSuperclass() { + TestUtil.assertAbsenceOfFieldInMetamodelFor( Detail.class, "volume" ); } @Test - public void testExplicitAccessTypeAndDefaultFromRootEntity() throws Exception { - assertAbsenceOfField( - LivingBeing.class.getName() + "_", + public void testExplicitAccessTypeAndDefaultFromRootEntity() { + assertAbsenceOfFieldInMetamodelFor( + LivingBeing.class, "nonPersistent", "explicit access type on mapped superclass" ); - assertAbsenceOfField( Hominidae.class.getName() + "_", "nonPersistent", "explicit access type on entity" ); - assertAbsenceOfField( - Human.class.getName() + "_", + assertAbsenceOfFieldInMetamodelFor( Hominidae.class, "nonPersistent", "explicit access type on entity" ); + assertAbsenceOfFieldInMetamodelFor( + Human.class, "nonPersistent", "proper inheritance from root entity access type" ); } @Test - public void testMemberAccessType() throws Exception { - assertPresenceOfField( Customer.class.getName() + "_", "goodPayer", "access type overriding" ); - assertFieldType( Customer.class.getName() + "_", "goodPayer", Boolean.class, "access type overriding" ); + public void testMemberAccessType() { + assertPresenceOfFieldInMetamodelFor( Customer.class, "goodPayer", "access type overriding" ); + assertFieldTypeInMetaModelFor( Customer.class, "goodPayer", Boolean.class, "access type overriding" ); } @Override diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java index 3982122a48..0074de0274 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java @@ -20,27 +20,29 @@ package org.hibernate.jpamodelgen.test.arraytype; import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertFieldType; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertFieldTypeInMetaModelFor; /** * @author Hardy Ferentschik */ public class ArrayTest extends CompilationTest { + /** * METAGEN-2 */ @Test - public void testPrimitiveArray() throws Exception { - assertFieldType( Image.class.getName() + "_", "data", byte[].class, "Wrong type for field." ); + public void testPrimitiveArray() { + assertFieldTypeInMetaModelFor( Image.class, "data", byte[].class, "Wrong type for field." ); } /** * METAGEN-2 */ @Test - public void testIntegerArray() throws Exception { - assertFieldType( - TemperatureSamples.class.getName() + "_", "samples", Integer[].class, "Wrong type for field." + public void testIntegerArray() { + assertFieldTypeInMetaModelFor( + TemperatureSamples.class, "samples", Integer[].class, "Wrong type for field." ); } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java index 8b48a19aae..b2408196f5 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java @@ -20,9 +20,9 @@ package org.hibernate.jpamodelgen.test.elementcollection; import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertClassGenerated; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertClassNotFound; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoGeneratedSourceFile; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoSourceFileGeneratedFor; /** * @author Hardy Ferentschik @@ -33,10 +33,10 @@ public class ElementCollectionTest extends CompilationTest { */ @Test public void testElementCollectionOnMap() { - assertClassGenerated( House.class.getName() + "_" ); - assertClassGenerated( House.class.getName() + "_" ); + assertMetamodelClassGeneratedFor( House.class ); + assertMetamodelClassGeneratedFor( House.class ); // side effect of METAGEN-8 was that a meta class for String was created! - assertNoGeneratedSourceFile( String.class.getName() + "_" ); + assertNoSourceFileGeneratedFor( String.class ); } @Override diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java index 0034c95b88..f178a65d01 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java @@ -19,11 +19,9 @@ package org.hibernate.jpamodelgen.test.generics; import org.testng.annotations.Test; -import org.hibernate.jpamodelgen.test.elementcollection.House; import org.hibernate.jpamodelgen.test.util.CompilationTest; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertClassGenerated; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertNoGeneratedSourceFile; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; /** * @author Emmanuel Bernard @@ -32,8 +30,8 @@ public class GenericsTest extends CompilationTest { @Test public void testGenerics() { - assertClassGenerated( Parent.class.getName() + "_" ); - assertClassGenerated( Child.class.getName() + "_" ); + assertMetamodelClassGeneratedFor( Parent.class ); + assertMetamodelClassGeneratedFor( Child.class ); } @Override diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java index cd5302b7ba..4dc7777211 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java @@ -20,7 +20,8 @@ package org.hibernate.jpamodelgen.test.inheritance; import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSuperClass; + +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSuperClassRelationShipInMetamodel; /** * @author Emmanuel Bernard @@ -29,15 +30,13 @@ import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSuperClass; public class InheritanceTest extends CompilationTest { @Test public void testSuperEntity() throws Exception { - assertSuperClass( - Customer.class.getName() + "_", User.class.getName() + "_" - ); + assertSuperClassRelationShipInMetamodel( Customer.class, User.class ); } @Test public void testMappedSuperclass() throws Exception { - assertSuperClass( House.class.getName() + "_", Building.class.getName() + "_" ); - assertSuperClass( Building.class.getName() + "_", Area.class.getName() + "_" ); + assertSuperClassRelationShipInMetamodel( House.class, Building.class ); + assertSuperClassRelationShipInMetamodel( Building.class, Area.class ); } @Override diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/rawTypes/RawTypesTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/rawTypes/RawTypesTest.java index 0795490e0b..be140b7529 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/rawTypes/RawTypesTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/rawTypes/RawTypesTest.java @@ -21,7 +21,7 @@ import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertClassGenerated; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; /** * @author Emmanuel Bernard @@ -30,8 +30,8 @@ public class RawTypesTest extends CompilationTest { @Test public void testGenerics() { - assertClassGenerated( DeskWithRawType.class.getName() + "_" ); - assertClassGenerated( EmployeeWithRawType.class.getName() + "_" ); + assertMetamodelClassGeneratedFor( DeskWithRawType.class ); + assertMetamodelClassGeneratedFor( EmployeeWithRawType.class ); } @Override diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java index 93bd0ec869..a830836864 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java @@ -55,6 +55,7 @@ public abstract class CompilationTest { public CompilationTest() { try { + TestUtil.clearOutputFolder(); compile(); } catch ( Exception e ) { diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java index eb479542c6..c02e9f4373 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java @@ -18,15 +18,13 @@ package org.hibernate.jpamodelgen.test.util; import java.io.File; -import java.io.FilenameFilter; import java.lang.reflect.Field; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.Collection; -import java.util.Vector; import org.testng.Assert; + import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; @@ -38,6 +36,7 @@ import static org.testng.FileAssert.fail; public class TestUtil { private static final String PATH_SEPARATOR = System.getProperty( "file.separator" ); + private static final String META_MODEL_CLASS_POSTFIX = "_"; private static final String outBaseDir; static { @@ -51,51 +50,54 @@ public class TestUtil { private TestUtil() { } - public static void assertClassGenerated(String className) { - try { - assertNotNull( Class.forName( className ) ); - } - catch ( ClassNotFoundException e ) { - fail( className + " was not generated." ); + public static void clearOutputFolder() { + File outDir = new File( outBaseDir ); + File[] files = outDir.listFiles(); + for ( File file : files ) { + file.delete(); } } - public static void assertClassNotFound(String className) { + /** + * 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) { + assertNotNull( clazz, "Class parameter cannot be null" ); + String metaModelClassName = clazz.getName() + META_MODEL_CLASS_POSTFIX; try { - Class.forName( className ); - fail( "Class " + className + " should not have been found." ); + assertNotNull( Class.forName( metaModelClassName ) ); } catch ( ClassNotFoundException e ) { - // success + fail( metaModelClassName + " was not generated." ); } } - public static void assertNoGeneratedSourceFile(String className) { + public static void assertNoSourceFileGeneratedFor(Class clazz) { + assertNotNull( clazz, "Class parameter cannot be null" ); + String metaModelClassName = clazz.getName() + META_MODEL_CLASS_POSTFIX; // generate the file name - String fileName = className.replace( ".", PATH_SEPARATOR ); + String fileName = metaModelClassName.replace( ".", PATH_SEPARATOR ); fileName = fileName.concat( ".java" ); - File sourceFile = new File(outBaseDir + PATH_SEPARATOR + fileName); - assertFalse(sourceFile.exists(), "There should be no source file: " + fileName); - + File sourceFile = new File( outBaseDir + PATH_SEPARATOR + fileName ); + assertFalse( sourceFile.exists(), "There should be no source file: " + fileName ); } - public static void assertAbsenceOfField(String className, String fieldName) throws ClassNotFoundException { - assertAbsenceOfField( className, fieldName, "field should not be persistent" ); + public static void assertAbsenceOfFieldInMetamodelFor(Class clazz, String fieldName) { + assertAbsenceOfFieldInMetamodelFor( clazz, fieldName, "field should not be persistent" ); } - public static void assertAbsenceOfField(String className, String fieldName, String errorString) - throws ClassNotFoundException { - Assert.assertFalse( isFieldHere( className, fieldName ), errorString ); + public static void assertAbsenceOfFieldInMetamodelFor(Class clazz, String fieldName, String errorString) { + Assert.assertFalse( hasFieldInMetamodelFor( clazz, fieldName ), errorString ); } - public static void assertPresenceOfField(String className, String fieldName, String errorString) - throws ClassNotFoundException { - Assert.assertTrue( isFieldHere( className, fieldName ), errorString ); + public static void assertPresenceOfFieldInMetamodelFor(Class clazz, String fieldName, String errorString) { + Assert.assertTrue( hasFieldInMetamodelFor( clazz, fieldName ), errorString ); } - public static void assertFieldType(String className, String fieldName, Class expectedType, String errorString) - throws ClassNotFoundException { - Field field = getField( className, fieldName ); + public static void assertFieldTypeInMetaModelFor(Class clazz, String fieldName, Class expectedType, String errorString) { + Field field = getFieldFromMetamodelFor( clazz, fieldName ); assertNotNull( field ); ParameterizedType type = ( ParameterizedType ) field.getGenericType(); Type actualType = type.getActualTypeArguments()[1]; @@ -106,15 +108,17 @@ public class TestUtil { assertEquals( actualType, expectedType, errorString ); } - public static void assertSuperClass(String className, String superClassName) { + public static void assertSuperClassRelationShipInMetamodel(Class entityClass, Class superEntityClass) { + String entityModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX; + String superEntityModelClassName = superEntityClass.getName() + META_MODEL_CLASS_POSTFIX; Class clazz; Class superClazz; try { - clazz = Class.forName( className ); - superClazz = Class.forName( superClassName ); + clazz = Class.forName( entityModelClassName ); + superClazz = Class.forName( superEntityModelClassName ); Assert.assertEquals( clazz.getSuperclass(), superClazz, - "Entity " + superClassName + " should be the superclass of " + className + "Entity " + superEntityModelClassName + " should be the superclass of " + entityModelClassName ); } catch ( ClassNotFoundException e ) { @@ -122,18 +126,24 @@ public class TestUtil { } } - private static boolean isFieldHere(String className, String fieldName) throws ClassNotFoundException { - return getField( className, fieldName ) != null; + private static boolean hasFieldInMetamodelFor(Class clazz, String fieldName) { + return getFieldFromMetamodelFor( clazz, fieldName ) != null; } - public static Field getField(String className, String fieldName) throws ClassNotFoundException { - Class clazz = Class.forName( className ); + public static Field getFieldFromMetamodelFor(Class entityClass, String fieldName) { + String entityModelClassName = entityClass.getName() + META_MODEL_CLASS_POSTFIX; + Field field = null; try { - return clazz.getField( fieldName ); + Class clazz = Class.forName( entityModelClassName ); + field = clazz.getField( fieldName ); + } + catch ( ClassNotFoundException e ) { + fail( "Unable to load class " + entityModelClassName ); } catch ( NoSuchFieldException e ) { - return null; + field = null; } + return field; } } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/Boy.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/Boy.java index fe8147e115..7229fd8ec2 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/Boy.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/Boy.java @@ -33,6 +33,36 @@ public class Boy { */ private List nickNames; + private Superhero favoriteSuperhero; + + private List knowsHeros; + + private List savedBy; + + public List getSavedBy() { + return savedBy; + } + + public void setSavedBy(List savedBy) { + this.savedBy = savedBy; + } + + public Superhero getFavoriteSuperhero() { + return favoriteSuperhero; + } + + public void setFavoriteSuperhero(Superhero favoriteSuperhero) { + this.favoriteSuperhero = favoriteSuperhero; + } + + public List getKnowsHeros() { + return knowsHeros; + } + + public void setKnowsHeros(List knowsHeros) { + this.knowsHeros = knowsHeros; + } + public long getId() { return id; } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/FakeHero.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/FakeHero.java new file mode 100644 index 0000000000..d2a037767e --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/FakeHero.java @@ -0,0 +1,24 @@ +// $Id:$ +/* +* JBoss, Home of Professional Open Source +* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package org.hibernate.jpamodelgen.test.xmlmapped; + +/** + * @author Hardy Ferentschik + */ +public class FakeHero { +} \ No newline at end of file diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/Superhero.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/Superhero.java new file mode 100644 index 0000000000..66a65c205a --- /dev/null +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/Superhero.java @@ -0,0 +1,62 @@ +// $Id:$ +/* +* JBoss, Home of Professional Open Source +* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors +* by the @authors tag. See the copyright.txt in the distribution for a +* full listing of individual contributors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package org.hibernate.jpamodelgen.test.xmlmapped; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Hardy Ferentschik + */ +@Entity +public class Superhero { + @Id + @GeneratedValue + private long id; + + private String name; + + private String superPower; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSuperPower() { + return superPower; + } + + public void setSuperPower(String superPower) { + this.superPower = superPower; + } +} + + diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java index 41f5dab06d..9f15c817da 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java @@ -17,52 +17,79 @@ */ package org.hibernate.jpamodelgen.test.xmlmapped; -import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; - import org.testng.annotations.Test; import org.hibernate.jpamodelgen.test.util.CompilationTest; -import static junit.framework.Assert.assertTrue; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertClassGenerated; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfField; -import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSuperClass; -import static org.hibernate.jpamodelgen.test.util.TestUtil.getField; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertFieldTypeInMetaModelFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfFieldInMetamodelFor; +import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSuperClassRelationShipInMetamodel; /** * @author Hardy Ferentschik */ public class XmlMappingTest extends CompilationTest { @Test - public void testXmlConfiguredEmbeddedClassGenerated() throws Exception { - assertClassGenerated( Address.class.getName() + "_" ); + public void testXmlConfiguredEmbeddedClassGenerated() { + assertMetamodelClassGeneratedFor( Address.class ); } @Test - public void testXmlConfiguredMappedSuperclassGenerated() throws Exception { - assertClassGenerated( Building.class.getName() + "_" ); - assertPresenceOfField( - Building.class.getName() + "_", "address", "address field should exist" + public void testXmlConfiguredMappedSuperclassGenerated() { + assertMetamodelClassGeneratedFor( Building.class ); + assertPresenceOfFieldInMetamodelFor( Building.class, "address", "address field should exist" ); + } + + /** + * METAGEN-17 + */ + @Test + public void testTargetEntityOnOneToOne() { + assertMetamodelClassGeneratedFor( Boy.class ); + assertPresenceOfFieldInMetamodelFor( Boy.class, "favoriteSuperhero", "favoriteSuperhero field should exist" ); + assertFieldTypeInMetaModelFor( + Boy.class, "favoriteSuperhero", FakeHero.class, "target entity overridden in xml" + ); + } + + /** + * METAGEN-17 + */ + @Test + public void testTargetEntityOnOneToMany() { + assertMetamodelClassGeneratedFor( Boy.class ); + assertPresenceOfFieldInMetamodelFor( Boy.class, "knowsHeros", "knowsHeros field should exist" ); + assertFieldTypeInMetaModelFor( + Boy.class, "knowsHeros", FakeHero.class, "target entity overridden in xml" + ); + } + + /** + * METAGEN-17 + */ + @Test + public void testTargetEntityOnManyToMany() { + assertMetamodelClassGeneratedFor( Boy.class ); + assertPresenceOfFieldInMetamodelFor( Boy.class, "savedBy", "savedBy field should exist" ); + assertFieldTypeInMetaModelFor( + Boy.class, "savedBy", FakeHero.class, "target entity overridden in xml" ); } @Test - public void testXmlConfiguredElementCollection() throws Exception { - assertClassGenerated( Boy.class.getName() + "_" ); - assertPresenceOfField( - Boy.class.getName() + "_", "nickNames", "nickNames field should exist" - ); - Field field = getField( Boy.class.getName() + "_", "nickNames" ); - ParameterizedType type = ( ParameterizedType ) field.getGenericType(); - assertTrue( "Wrong target type", type.getActualTypeArguments()[1].equals( Integer.class ) ); + public void testXmlConfiguredElementCollection() { + assertMetamodelClassGeneratedFor( Boy.class ); + assertPresenceOfFieldInMetamodelFor( Boy.class, "nickNames", "nickNames field should exist" ); + assertFieldTypeInMetaModelFor( Boy.class, "nickNames", Integer.class, "target class overridden in xml" ); } + @Test - public void testClassHierarchy() throws Exception { - assertClassGenerated( Mammal.class.getName() + "_" ); - assertClassGenerated( LivingBeing.class.getName() + "_" ); - assertSuperClass( Mammal.class.getName() + "_", LivingBeing.class.getName() + "_" ); + public void testClassHierarchy() { + assertMetamodelClassGeneratedFor( Mammal.class ); + assertMetamodelClassGeneratedFor( LivingBeing.class ); + assertSuperClassRelationShipInMetamodel( Mammal.class, LivingBeing.class ); } @Test(expectedExceptions = ClassNotFoundException.class) diff --git a/tooling/metamodel-generator/src/test/resources/org/hibernate/jpamodelgen/test/xmlmapped/boy.xml b/tooling/metamodel-generator/src/test/resources/org/hibernate/jpamodelgen/test/xmlmapped/boy.xml index 43e89a3c43..b244405b77 100644 --- a/tooling/metamodel-generator/src/test/resources/org/hibernate/jpamodelgen/test/xmlmapped/boy.xml +++ b/tooling/metamodel-generator/src/test/resources/org/hibernate/jpamodelgen/test/xmlmapped/boy.xml @@ -10,6 +10,9 @@ + + + diff --git a/tooling/metamodel-generator/src/test/suite/unit-tests.xml b/tooling/metamodel-generator/src/test/suite/unit-tests.xml index ee38630d8c..91528da7bc 100644 --- a/tooling/metamodel-generator/src/test/suite/unit-tests.xml +++ b/tooling/metamodel-generator/src/test/suite/unit-tests.xml @@ -6,7 +6,9 @@ + +