diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/MockedNames.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/MockedNames.java index f8104e4f1d..f7f017b068 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/MockedNames.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/MockedNames.java @@ -1,3 +1,26 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc.. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ package org.hibernate.metamodel.source.annotations.xml; import org.jboss.jandex.DotName; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java index 01c9d433dd..b3342d5ddb 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java @@ -1,3 +1,26 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc.. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ package org.hibernate.metamodel.source.annotations.xml; import java.util.ArrayList; @@ -9,10 +32,8 @@ import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings; import org.hibernate.metamodel.source.annotations.xml.mocker.EntityMappingsMocker; import org.hibernate.metamodel.source.internal.JaxbRoot; import org.hibernate.metamodel.source.internal.MetadataImpl; - /** * @author Hardy Ferentschik - * @todo Need some create some XMLContext as well which can be populated w/ information which can not be expressed via annotations */ public class OrmXmlParser { private final MetadataImpl meta; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/AbstractMockerTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/AbstractMockerTest.java index c01ffaaa9a..323f3ee355 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/AbstractMockerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/AbstractMockerTest.java @@ -107,7 +107,8 @@ public abstract class AbstractMockerTest { return index; } - protected Index getMockedIndex(String ormFileName){ + + protected Index getMockedIndex(String ormFileName) { EntityMappingsMocker mocker = getEntityMappingsMocker( ormFileName ); return mocker.mockNewIndex(); } @@ -138,36 +139,23 @@ public abstract class AbstractMockerTest { return serviceRegistry; } - protected void assertHasAnnotation(Index index, DotName className, DotName annName) { - assertHasAnnotation( index, className, annName, 1 ); - } - protected void assertHasNoAnnotation(Index index, DotName className, DotName annName) { - ClassInfo classInfo = index.getClassByName( className ); - if ( classInfo == null ) { - fail( "Can't find " + className + " from Index" ); - } - if ( classInfo.annotations() != null ) { - List annotationInstanceList = classInfo.annotations().get( annName ); - if ( annotationInstanceList != null ) { - if(!annotationInstanceList.isEmpty()){ - fail( className+" has Annotation "+annName ); - } + List annotationInstanceList = getAnnotationInstances( index, className, annName ); + if ( annotationInstanceList != null ) { + if ( !annotationInstanceList.isEmpty() ) { + fail( className + " has Annotation " + annName ); } } } + protected void assertHasAnnotation(Index index, DotName className, DotName annName) { + assertHasAnnotation( index, className, annName, 1 ); + } + protected void assertHasAnnotation(Index index, DotName className, DotName annName, int size) { - ClassInfo classInfo = index.getClassByName( className ); - if ( classInfo == null ) { - fail( "Can't find " + className + " from Index" ); - } - if ( classInfo.annotations() == null ) { - fail( classInfo + " doesn't have any annotations defined" ); - } - List annotationInstanceList = classInfo.annotations().get( annName ); + List annotationInstanceList = getAnnotationInstances( index, className, annName ); if ( annotationInstanceList == null || annotationInstanceList.isEmpty() ) { - fail( classInfo + " doesn't have annotation " + annName ); + fail( "Expected annotation " + annName + " size is " + size + ", but no one can be found in Index" ); } assertEquals( "Expected annotation " + annName + " size is " + size + ", but it actually is " + annotationInstanceList @@ -188,13 +176,28 @@ public abstract class AbstractMockerTest { protected void assertAnnotationValue(Index index, DotName className, DotName annName, int size, AnnotationValueChecker checker) { assertHasAnnotation( index, className, annName, size ); - ClassInfo classInfo = index.getClassByName( className ); - List annotationInstanceList = classInfo.annotations().get( annName ); + List annotationInstanceList = getAnnotationInstances( index,className,annName ); for ( AnnotationInstance annotationInstance : annotationInstanceList ) { checker.check( annotationInstance ); } } + private List getAnnotationInstances(Index index, DotName className, DotName annName) { + if ( className != null ) { + ClassInfo classInfo = index.getClassByName( className ); + if ( classInfo == null ) { + fail( "Can't find " + className + " from Index" ); + } + if ( classInfo.annotations() == null ) { + fail( classInfo + " doesn't have any annotations defined" ); + } + return classInfo.annotations().get( annName ); + } + else { + return index.getAnnotations( annName ); + } + } + static interface AnnotationValueChecker { void check(AnnotationInstance annotationInstance); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMockerTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMockerTest.java index 81dacff01e..929d4d4c6c 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMockerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/mocker/PersistenceMetadataMockerTest.java @@ -1,7 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc.. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ package org.hibernate.metamodel.source.annotations.xml.mocker; +import javax.persistence.AccessType; + +import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.Index; import org.junit.Test; +import static org.junit.Assert.*; + +import org.hibernate.metamodel.source.annotations.xml.MockedNames; /** * @author Strong Liu @@ -10,6 +39,12 @@ public class PersistenceMetadataMockerTest extends AbstractMockerTest { @Test public void testPersistenceMetadata() { Index index = getMockedIndex( "persistence-metadata.xml" ); - index.printAnnotations(); + assertHasAnnotation( index, null, MockedNames.DEFAULT_ACCESS, 1 ); + assertAnnotationValue(index,null,MockedNames.DEFAULT_ACCESS,new AnnotationValueChecker(){ + @Override + public void check(AnnotationInstance annotationInstance) { + assertEquals( AccessType.FIELD.toString(), annotationInstance.value().asEnum()); + } + }); } }