HHH-5800 This test now also derives from HibernateTestCase. This way the test is aligned with the tests added for this issue.

This commit is contained in:
Hardy Ferentschik 2011-01-03 16:58:46 +01:00
parent 95c23b6f50
commit dc91727742
1 changed files with 489 additions and 439 deletions

View File

@ -1,439 +1,489 @@
//$Id$ /*
package org.hibernate.test.annotations.reflection; * Hibernate, Relational Persistence for Idiomatic Java
*
import java.io.BufferedInputStream; * Copyright (c) 2011 by Red Hat Inc and/or its affiliates or by
import java.io.IOException; * third-party contributors as indicated by either @author tags or express
import java.io.InputStream; * copyright attribution statements applied by the authors. All
import java.lang.reflect.Field; * third-party contributions are distributed under license by Red Hat Inc.
import java.lang.reflect.Method; *
import java.util.ArrayList; * This copyrighted material is made available to anyone wishing to use, modify,
import java.util.List; * copy, or redistribute it subject to the terms and conditions of the GNU
import javax.persistence.AssociationOverrides; * Lesser General Public License, as published by the Free Software Foundation.
import javax.persistence.AttributeOverrides; *
import javax.persistence.Basic; * This program is distributed in the hope that it will be useful,
import javax.persistence.Column; * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
import javax.persistence.DiscriminatorColumn; * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
import javax.persistence.DiscriminatorValue; * for more details.
import javax.persistence.Embedded; *
import javax.persistence.EmbeddedId; * You should have received a copy of the GNU Lesser General Public License
import javax.persistence.Entity; * along with this distribution; if not, write to:
import javax.persistence.EnumType; * Free Software Foundation, Inc.
import javax.persistence.Enumerated; * 51 Franklin Street, Fifth Floor
import javax.persistence.ExcludeDefaultListeners; * Boston, MA 02110-1301 USA
import javax.persistence.ExcludeSuperclassListeners; */
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; package org.hibernate.test.annotations.reflection;
import javax.persistence.GenerationType;
import javax.persistence.Id; import java.io.BufferedInputStream;
import javax.persistence.IdClass; import java.io.IOException;
import javax.persistence.Inheritance; import java.io.InputStream;
import javax.persistence.InheritanceType; import java.lang.reflect.Field;
import javax.persistence.JoinColumns; import java.lang.reflect.Method;
import javax.persistence.JoinTable; import java.util.ArrayList;
import javax.persistence.Lob; import java.util.List;
import javax.persistence.ManyToMany; import javax.persistence.AssociationOverrides;
import javax.persistence.MapKey; import javax.persistence.AttributeOverrides;
import javax.persistence.MappedSuperclass; import javax.persistence.Basic;
import javax.persistence.NamedNativeQueries; import javax.persistence.Column;
import javax.persistence.NamedQueries; import javax.persistence.DiscriminatorColumn;
import javax.persistence.OneToMany; import javax.persistence.DiscriminatorValue;
import javax.persistence.OneToOne; import javax.persistence.Embedded;
import javax.persistence.OrderBy; import javax.persistence.EmbeddedId;
import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumns; import javax.persistence.EntityListeners;
import javax.persistence.SecondaryTable; import javax.persistence.EnumType;
import javax.persistence.SecondaryTables; import javax.persistence.Enumerated;
import javax.persistence.SequenceGenerator; import javax.persistence.ExcludeDefaultListeners;
import javax.persistence.SqlResultSetMappings; import javax.persistence.ExcludeSuperclassListeners;
import javax.persistence.Table; import javax.persistence.FetchType;
import javax.persistence.TableGenerator; import javax.persistence.GeneratedValue;
import javax.persistence.Temporal; import javax.persistence.GenerationType;
import javax.persistence.TemporalType; import javax.persistence.Id;
import javax.persistence.Transient; import javax.persistence.IdClass;
import javax.persistence.Version; import javax.persistence.Inheritance;
import javax.persistence.PrePersist; import javax.persistence.InheritanceType;
import javax.persistence.EntityListeners; import javax.persistence.JoinColumns;
import javax.persistence.PostLoad; import javax.persistence.JoinTable;
import javax.persistence.PostPersist; import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import junit.framework.TestCase; import javax.persistence.MapKey;
import org.dom4j.DocumentException; import javax.persistence.MappedSuperclass;
import org.dom4j.io.SAXReader; import javax.persistence.NamedNativeQueries;
import org.hibernate.annotations.Columns; import javax.persistence.NamedQueries;
import org.hibernate.cfg.EJB3DTDEntityResolver; import javax.persistence.OneToMany;
import org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader; import javax.persistence.OneToOne;
import org.hibernate.cfg.annotations.reflection.XMLContext; import javax.persistence.OrderBy;
import org.hibernate.util.XMLHelper; import javax.persistence.PostLoad;
import org.xml.sax.InputSource; import javax.persistence.PostPersist;
import org.xml.sax.SAXException; import javax.persistence.PrePersist;
import org.xml.sax.SAXNotSupportedException; import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.PrimaryKeyJoinColumns;
import javax.persistence.SecondaryTable;
/** import javax.persistence.SecondaryTables;
* @author Emmanuel Bernard import javax.persistence.SequenceGenerator;
*/ import javax.persistence.SqlResultSetMappings;
public class JPAOverridenAnnotationReaderTest extends TestCase { import javax.persistence.Table;
public void testMappedSuperclassAnnotations() throws Exception { import javax.persistence.TableGenerator;
XMLContext context = buildContext( import javax.persistence.Temporal;
"org/hibernate/test/annotations/reflection/metadata-complete.xml" import javax.persistence.TemporalType;
); import javax.persistence.Transient;
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Organization.class, context ); import javax.persistence.Version;
assertTrue( reader.isAnnotationPresent( MappedSuperclass.class ) );
} import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public void testEntityRelatedAnnotations() throws Exception { import org.xml.sax.InputSource;
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" ); import org.xml.sax.SAXException;
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Administration.class, context ); import org.xml.sax.SAXNotSupportedException;
assertNotNull( reader.getAnnotation( Entity.class ) );
assertEquals( import org.hibernate.annotations.Columns;
"Default value in xml entity should not override @Entity.name", "JavaAdministration", import org.hibernate.cfg.EJB3DTDEntityResolver;
reader.getAnnotation( Entity.class ).name() import org.hibernate.cfg.annotations.reflection.JPAOverridenAnnotationReader;
); import org.hibernate.cfg.annotations.reflection.XMLContext;
assertNotNull( reader.getAnnotation( Table.class ) ); import org.hibernate.testing.junit.functional.annotations.HibernateTestCase;
assertEquals( "@Table not overriden", "tbl_admin", reader.getAnnotation( Table.class ).name() ); import org.hibernate.util.XMLHelper;
assertEquals( "Default schema not overriden", "myschema", reader.getAnnotation( Table.class ).schema() );
assertEquals(
"Proper @Table.uniqueConstraints", 2, /**
reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames().length * @author Emmanuel Bernard
); */
String columnName = reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames()[0]; public class JPAOverridenAnnotationReaderTest extends HibernateTestCase {
assertTrue(
"Proper @Table.uniqueConstraints", "firstname".equals( columnName ) || "lastname".equals( columnName ) @Override
); protected void buildConfiguration() throws Exception {
assertNull( "Both Java and XML used", reader.getAnnotation( SecondaryTable.class ) ); //Do nothing
assertNotNull( "XML does not work", reader.getAnnotation( SecondaryTables.class ) ); }
SecondaryTable[] tables = reader.getAnnotation( SecondaryTables.class ).value();
assertEquals( 1, tables.length ); @Override
assertEquals( "admin2", tables[0].name() ); protected void runSchemaGeneration() {
assertEquals( "unique constraints ignored", 1, tables[0].uniqueConstraints().length ); //Do nothing
assertEquals( "pk join column ignored", 1, tables[0].pkJoinColumns().length ); }
assertEquals( "pk join column ignored", "admin_id", tables[0].pkJoinColumns()[0].name() );
assertNotNull( "Sequence Overriding not working", reader.getAnnotation( SequenceGenerator.class ) ); @Override
assertEquals( protected void runSchemaDrop() {
"wrong sequence name", "seqhilo", reader.getAnnotation( SequenceGenerator.class ).sequenceName() //Do nothing
); }
assertEquals( "default fails", 50, reader.getAnnotation( SequenceGenerator.class ).allocationSize() );
assertNotNull( "TableOverriding not working", reader.getAnnotation( TableGenerator.class ) ); @Override
assertEquals( "wrong tble name", "tablehilo", reader.getAnnotation( TableGenerator.class ).table() ); protected void handleUnclosedResources() {
assertEquals( "no schema overriding", "myschema", reader.getAnnotation( TableGenerator.class ).schema() ); //Do nothing
}
reader = new JPAOverridenAnnotationReader( Match.class, context );
assertNotNull( reader.getAnnotation( Table.class ) ); @Override
assertEquals( protected Class<?>[] getAnnotatedClasses() {
"Java annotation not taken into account", "matchtable", reader.getAnnotation( Table.class ).name() return new Class<?>[0];
); }
assertEquals(
"Java annotation not taken into account", "matchschema", reader.getAnnotation( Table.class ).schema() public void testMappedSuperclassAnnotations() throws Exception {
); XMLContext context = buildContext(
assertEquals( "Overriding not taken into account", "mycatalog", reader.getAnnotation( Table.class ).catalog() ); "org/hibernate/test/annotations/reflection/metadata-complete.xml"
assertNotNull( "SecondaryTable swallowed", reader.getAnnotation( SecondaryTables.class ) ); );
assertEquals( JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Organization.class, context );
"Default schema not taken into account", "myschema", assertTrue( reader.isAnnotationPresent( MappedSuperclass.class ) );
reader.getAnnotation( SecondaryTables.class ).value()[0].schema() }
);
assertNotNull( reader.getAnnotation( Inheritance.class ) ); public void testEntityRelatedAnnotations() throws Exception {
assertEquals( XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
"inheritance strategy not overriden", InheritanceType.JOINED, JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Administration.class, context );
reader.getAnnotation( Inheritance.class ).strategy() assertNotNull( reader.getAnnotation( Entity.class ) );
); assertEquals(
assertNotNull( "NamedQuery not overriden", reader.getAnnotation( NamedQueries.class ) ); "Default value in xml entity should not override @Entity.name", "JavaAdministration",
assertEquals( "No deduplication", 3, reader.getAnnotation( NamedQueries.class ).value().length ); reader.getAnnotation( Entity.class ).name()
assertEquals( );
"deduplication kept the Java version", 1, assertNotNull( reader.getAnnotation( Table.class ) );
reader.getAnnotation( NamedQueries.class ).value()[1].hints().length assertEquals( "@Table not overriden", "tbl_admin", reader.getAnnotation( Table.class ).name() );
); assertEquals( "Default schema not overriden", "myschema", reader.getAnnotation( Table.class ).schema() );
assertEquals( assertEquals(
"org.hibernate.timeout", reader.getAnnotation( NamedQueries.class ).value()[1].hints()[0].name() "Proper @Table.uniqueConstraints", 2,
); reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames().length
assertNotNull( "NamedNativeQuery not overriden", reader.getAnnotation( NamedNativeQueries.class ) ); );
assertEquals( "No deduplication", 3, reader.getAnnotation( NamedNativeQueries.class ).value().length ); String columnName = reader.getAnnotation( Table.class ).uniqueConstraints()[0].columnNames()[0];
assertEquals( assertTrue(
"deduplication kept the Java version", 1, "Proper @Table.uniqueConstraints", "firstname".equals( columnName ) || "lastname".equals( columnName )
reader.getAnnotation( NamedNativeQueries.class ).value()[1].hints().length );
); assertNull( "Both Java and XML used", reader.getAnnotation( SecondaryTable.class ) );
assertEquals( assertNotNull( "XML does not work", reader.getAnnotation( SecondaryTables.class ) );
"org.hibernate.timeout", reader.getAnnotation( NamedNativeQueries.class ).value()[1].hints()[0].name() SecondaryTable[] tables = reader.getAnnotation( SecondaryTables.class ).value();
); assertEquals( 1, tables.length );
assertNotNull( reader.getAnnotation( SqlResultSetMappings.class ) ); assertEquals( "admin2", tables[0].name() );
assertEquals( assertEquals( "unique constraints ignored", 1, tables[0].uniqueConstraints().length );
"competitor1Point", reader.getAnnotation( SqlResultSetMappings.class ).value()[0].columns()[0].name() assertEquals( "pk join column ignored", 1, tables[0].pkJoinColumns().length );
); assertEquals( "pk join column ignored", "admin_id", tables[0].pkJoinColumns()[0].name() );
assertEquals( assertNotNull( "Sequence Overriding not working", reader.getAnnotation( SequenceGenerator.class ) );
"competitor1Point", assertEquals(
reader.getAnnotation( SqlResultSetMappings.class ).value()[0].entities()[0].fields()[0].column() "wrong sequence name", "seqhilo", reader.getAnnotation( SequenceGenerator.class ).sequenceName()
); );
assertNotNull( reader.getAnnotation( ExcludeSuperclassListeners.class ) ); assertEquals( "default fails", 50, reader.getAnnotation( SequenceGenerator.class ).allocationSize() );
assertNotNull( reader.getAnnotation( ExcludeDefaultListeners.class ) ); assertNotNull( "TableOverriding not working", reader.getAnnotation( TableGenerator.class ) );
assertEquals( "wrong tble name", "tablehilo", reader.getAnnotation( TableGenerator.class ).table() );
reader = new JPAOverridenAnnotationReader( Competition.class, context ); assertEquals( "no schema overriding", "myschema", reader.getAnnotation( TableGenerator.class ).schema() );
assertNotNull( reader.getAnnotation( MappedSuperclass.class ) );
reader = new JPAOverridenAnnotationReader( Match.class, context );
reader = new JPAOverridenAnnotationReader( TennisMatch.class, context ); assertNotNull( reader.getAnnotation( Table.class ) );
assertNull( "Mutualize PKJC into PKJCs", reader.getAnnotation( PrimaryKeyJoinColumn.class ) ); assertEquals(
assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) ); "Java annotation not taken into account", "matchtable", reader.getAnnotation( Table.class ).name()
assertEquals( );
"PrimaryKeyJoinColumn overrden", "id", assertEquals(
reader.getAnnotation( PrimaryKeyJoinColumns.class ).value()[0].name() "Java annotation not taken into account", "matchschema", reader.getAnnotation( Table.class ).schema()
); );
assertNotNull( reader.getAnnotation( AttributeOverrides.class ) ); assertEquals( "Overriding not taken into account", "mycatalog", reader.getAnnotation( Table.class ).catalog() );
assertEquals( "Wrong deduplication", 3, reader.getAnnotation( AttributeOverrides.class ).value().length ); assertNotNull( "SecondaryTable swallowed", reader.getAnnotation( SecondaryTables.class ) );
assertEquals( assertEquals(
"Wrong priority (XML vs java annotations)", "fld_net", "Default schema not taken into account", "myschema",
reader.getAnnotation( AttributeOverrides.class ).value()[0].column().name() reader.getAnnotation( SecondaryTables.class ).value()[0].schema()
); );
assertEquals( assertNotNull( reader.getAnnotation( Inheritance.class ) );
"Column mapping", 2, reader.getAnnotation( AttributeOverrides.class ).value()[1].column().scale() assertEquals(
); "inheritance strategy not overriden", InheritanceType.JOINED,
assertEquals( reader.getAnnotation( Inheritance.class ).strategy()
"Column mapping", true, reader.getAnnotation( AttributeOverrides.class ).value()[1].column().unique() );
); assertNotNull( "NamedQuery not overriden", reader.getAnnotation( NamedQueries.class ) );
assertNotNull( reader.getAnnotation( AssociationOverrides.class ) ); assertEquals( "No deduplication", 3, reader.getAnnotation( NamedQueries.class ).value().length );
assertEquals( "no XML processing", 1, reader.getAnnotation( AssociationOverrides.class ).value().length ); assertEquals(
assertEquals( "deduplication kept the Java version", 1,
"wrong xml processing", "id", reader.getAnnotation( NamedQueries.class ).value()[1].hints().length
reader.getAnnotation( AssociationOverrides.class ).value()[0].joinColumns()[0].referencedColumnName() );
); assertEquals(
"org.hibernate.timeout", reader.getAnnotation( NamedQueries.class ).value()[1].hints()[0].name()
);
reader = new JPAOverridenAnnotationReader( SocialSecurityPhysicalAccount.class, context ); assertNotNull( "NamedNativeQuery not overriden", reader.getAnnotation( NamedNativeQueries.class ) );
assertNotNull( reader.getAnnotation( IdClass.class ) ); assertEquals( "No deduplication", 3, reader.getAnnotation( NamedNativeQueries.class ).value().length );
assertEquals( "id-class not used", SocialSecurityNumber.class, reader.getAnnotation( IdClass.class ).value() ); assertEquals(
assertEquals( "deduplication kept the Java version", 1,
"discriminator-value not used", "Physical", reader.getAnnotation( DiscriminatorValue.class ).value() reader.getAnnotation( NamedNativeQueries.class ).value()[1].hints().length
); );
assertNotNull( "discriminator-column not used", reader.getAnnotation( DiscriminatorColumn.class ) ); assertEquals(
assertEquals( "org.hibernate.timeout", reader.getAnnotation( NamedNativeQueries.class ).value()[1].hints()[0].name()
"discriminator-column.name default value broken", "DTYPE", );
reader.getAnnotation( DiscriminatorColumn.class ).name() assertNotNull( reader.getAnnotation( SqlResultSetMappings.class ) );
); assertEquals(
assertEquals( "competitor1Point", reader.getAnnotation( SqlResultSetMappings.class ).value()[0].columns()[0].name()
"discriminator-column.length broken", 34, reader.getAnnotation( DiscriminatorColumn.class ).length() );
); assertEquals(
} "competitor1Point",
reader.getAnnotation( SqlResultSetMappings.class ).value()[0].entities()[0].fields()[0].column()
public void testEntityRelatedAnnotationsMetadataComplete() throws Exception { );
XMLContext context = buildContext( assertNotNull( reader.getAnnotation( ExcludeSuperclassListeners.class ) );
"org/hibernate/test/annotations/reflection/metadata-complete.xml" assertNotNull( reader.getAnnotation( ExcludeDefaultListeners.class ) );
);
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Administration.class, context ); reader = new JPAOverridenAnnotationReader( Competition.class, context );
assertNotNull( reader.getAnnotation( Entity.class ) ); assertNotNull( reader.getAnnotation( MappedSuperclass.class ) );
assertEquals(
"Metadata complete should ignore java annotations", "", reader.getAnnotation( Entity.class ).name() reader = new JPAOverridenAnnotationReader( TennisMatch.class, context );
); assertNull( "Mutualize PKJC into PKJCs", reader.getAnnotation( PrimaryKeyJoinColumn.class ) );
assertNotNull( reader.getAnnotation( Table.class ) ); assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() ); assertEquals(
assertEquals( "Default schema not overriden", "myschema", reader.getAnnotation( Table.class ).schema() ); "PrimaryKeyJoinColumn overrden", "id",
reader.getAnnotation( PrimaryKeyJoinColumns.class ).value()[0].name()
reader = new JPAOverridenAnnotationReader( Match.class, context ); );
assertNotNull( reader.getAnnotation( Table.class ) ); assertNotNull( reader.getAnnotation( AttributeOverrides.class ) );
assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() ); assertEquals( "Wrong deduplication", 3, reader.getAnnotation( AttributeOverrides.class ).value().length );
assertEquals( "Overriding not taken into account", "myschema", reader.getAnnotation( Table.class ).schema() ); assertEquals(
assertEquals( "Overriding not taken into account", "mycatalog", reader.getAnnotation( Table.class ).catalog() ); "Wrong priority (XML vs java annotations)", "fld_net",
assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTable.class ) ); reader.getAnnotation( AttributeOverrides.class ).value()[0].column().name()
assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTables.class ) ); );
assertNull( "Ignore Java annotation", reader.getAnnotation( Inheritance.class ) ); assertEquals(
assertNull( reader.getAnnotation( NamedQueries.class ) ); "Column mapping", 2, reader.getAnnotation( AttributeOverrides.class ).value()[1].column().scale()
assertNull( reader.getAnnotation( NamedNativeQueries.class ) ); );
assertEquals(
reader = new JPAOverridenAnnotationReader( TennisMatch.class, context ); "Column mapping", true, reader.getAnnotation( AttributeOverrides.class ).value()[1].column().unique()
assertNull( reader.getAnnotation( PrimaryKeyJoinColumn.class ) ); );
assertNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) ); assertNotNull( reader.getAnnotation( AssociationOverrides.class ) );
assertEquals( "no XML processing", 1, reader.getAnnotation( AssociationOverrides.class ).value().length );
reader = new JPAOverridenAnnotationReader( Competition.class, context ); assertEquals(
assertNull( reader.getAnnotation( MappedSuperclass.class ) ); "wrong xml processing", "id",
reader.getAnnotation( AssociationOverrides.class ).value()[0].joinColumns()[0].referencedColumnName()
reader = new JPAOverridenAnnotationReader( SocialSecurityMoralAccount.class, context ); );
assertNull( reader.getAnnotation( IdClass.class ) );
assertNull( reader.getAnnotation( DiscriminatorValue.class ) );
assertNull( reader.getAnnotation( DiscriminatorColumn.class ) ); reader = new JPAOverridenAnnotationReader( SocialSecurityPhysicalAccount.class, context );
assertNull( reader.getAnnotation( SequenceGenerator.class ) ); assertNotNull( reader.getAnnotation( IdClass.class ) );
assertNull( reader.getAnnotation( TableGenerator.class ) ); assertEquals( "id-class not used", SocialSecurityNumber.class, reader.getAnnotation( IdClass.class ).value() );
} assertEquals(
"discriminator-value not used", "Physical", reader.getAnnotation( DiscriminatorValue.class ).value()
public void testIdRelatedAnnotations() throws Exception { );
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" ); assertNotNull( "discriminator-column not used", reader.getAnnotation( DiscriminatorColumn.class ) );
Method method = Administration.class.getDeclaredMethod( "getId" ); assertEquals(
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context ); "discriminator-column.name default value broken", "DTYPE",
assertNull( reader.getAnnotation( Id.class ) ); reader.getAnnotation( DiscriminatorColumn.class ).name()
assertNull( reader.getAnnotation( Column.class ) ); );
Field field = Administration.class.getDeclaredField( "id" ); assertEquals(
reader = new JPAOverridenAnnotationReader( field, context ); "discriminator-column.length broken", 34, reader.getAnnotation( DiscriminatorColumn.class ).length()
assertNotNull( reader.getAnnotation( Id.class ) ); );
assertNotNull( reader.getAnnotation( GeneratedValue.class ) ); }
assertEquals( GenerationType.SEQUENCE, reader.getAnnotation( GeneratedValue.class ).strategy() );
assertEquals( "generator", reader.getAnnotation( GeneratedValue.class ).generator() ); public void testEntityRelatedAnnotationsMetadataComplete() throws Exception {
assertNotNull( reader.getAnnotation( SequenceGenerator.class ) ); XMLContext context = buildContext(
assertEquals( "seq", reader.getAnnotation( SequenceGenerator.class ).sequenceName() ); "org/hibernate/test/annotations/reflection/metadata-complete.xml"
assertNotNull( reader.getAnnotation( Columns.class ) ); );
assertEquals( 1, reader.getAnnotation( Columns.class ).columns().length ); JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( Administration.class, context );
assertEquals( "fld_id", reader.getAnnotation( Columns.class ).columns()[0].name() ); assertNotNull( reader.getAnnotation( Entity.class ) );
assertNotNull( reader.getAnnotation( Temporal.class ) ); assertEquals(
assertEquals( TemporalType.DATE, reader.getAnnotation( Temporal.class ).value() ); "Metadata complete should ignore java annotations", "", reader.getAnnotation( Entity.class ).name()
);
context = buildContext( assertNotNull( reader.getAnnotation( Table.class ) );
"org/hibernate/test/annotations/reflection/metadata-complete.xml" assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() );
); assertEquals( "Default schema not overriden", "myschema", reader.getAnnotation( Table.class ).schema() );
method = Administration.class.getDeclaredMethod( "getId" );
reader = new JPAOverridenAnnotationReader( method, context ); reader = new JPAOverridenAnnotationReader( Match.class, context );
assertNotNull( assertNotNull( reader.getAnnotation( Table.class ) );
"Default access type when not defined in metadata complete should be property", assertEquals( "@Table should not be used", "", reader.getAnnotation( Table.class ).name() );
reader.getAnnotation( Id.class ) assertEquals( "Overriding not taken into account", "myschema", reader.getAnnotation( Table.class ).schema() );
); assertEquals( "Overriding not taken into account", "mycatalog", reader.getAnnotation( Table.class ).catalog() );
field = Administration.class.getDeclaredField( "id" ); assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTable.class ) );
reader = new JPAOverridenAnnotationReader( field, context ); assertNull( "Ignore Java annotation", reader.getAnnotation( SecondaryTables.class ) );
assertNull( assertNull( "Ignore Java annotation", reader.getAnnotation( Inheritance.class ) );
"Default access type when not defined in metadata complete should be property", assertNull( reader.getAnnotation( NamedQueries.class ) );
reader.getAnnotation( Id.class ) assertNull( reader.getAnnotation( NamedNativeQueries.class ) );
);
reader = new JPAOverridenAnnotationReader( TennisMatch.class, context );
method = BusTrip.class.getDeclaredMethod( "getId" ); assertNull( reader.getAnnotation( PrimaryKeyJoinColumn.class ) );
reader = new JPAOverridenAnnotationReader( method, context ); assertNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
assertNull( reader.getAnnotation( EmbeddedId.class ) );
field = BusTrip.class.getDeclaredField( "id" ); reader = new JPAOverridenAnnotationReader( Competition.class, context );
reader = new JPAOverridenAnnotationReader( field, context ); assertNull( reader.getAnnotation( MappedSuperclass.class ) );
assertNotNull( reader.getAnnotation( EmbeddedId.class ) );
assertNotNull( reader.getAnnotation( AttributeOverrides.class ) ); reader = new JPAOverridenAnnotationReader( SocialSecurityMoralAccount.class, context );
assertEquals( 1, reader.getAnnotation( AttributeOverrides.class ).value().length ); assertNull( reader.getAnnotation( IdClass.class ) );
} assertNull( reader.getAnnotation( DiscriminatorValue.class ) );
assertNull( reader.getAnnotation( DiscriminatorColumn.class ) );
public void testBasicRelatedAnnotations() throws Exception { assertNull( reader.getAnnotation( SequenceGenerator.class ) );
XMLContext context = buildContext( assertNull( reader.getAnnotation( TableGenerator.class ) );
"org/hibernate/test/annotations/reflection/metadata-complete.xml" }
);
Field field = BusTrip.class.getDeclaredField( "status" ); public void testIdRelatedAnnotations() throws Exception {
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context ); XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
assertNotNull( reader.getAnnotation( Enumerated.class ) ); Method method = Administration.class.getDeclaredMethod( "getId" );
assertEquals( EnumType.STRING, reader.getAnnotation( Enumerated.class ).value() ); JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context );
assertEquals( false, reader.getAnnotation( Basic.class ).optional() ); assertNull( reader.getAnnotation( Id.class ) );
field = BusTrip.class.getDeclaredField( "serial" ); assertNull( reader.getAnnotation( Column.class ) );
reader = new JPAOverridenAnnotationReader( field, context ); Field field = Administration.class.getDeclaredField( "id" );
assertNotNull( reader.getAnnotation( Lob.class ) ); reader = new JPAOverridenAnnotationReader( field, context );
assertEquals( "serialbytes", reader.getAnnotation( Columns.class ).columns()[0].name() ); assertNotNull( reader.getAnnotation( Id.class ) );
field = BusTrip.class.getDeclaredField( "terminusTime" ); assertNotNull( reader.getAnnotation( GeneratedValue.class ) );
reader = new JPAOverridenAnnotationReader( field, context ); assertEquals( GenerationType.SEQUENCE, reader.getAnnotation( GeneratedValue.class ).strategy() );
assertNotNull( reader.getAnnotation( Temporal.class ) ); assertEquals( "generator", reader.getAnnotation( GeneratedValue.class ).generator() );
assertEquals( TemporalType.TIMESTAMP, reader.getAnnotation( Temporal.class ).value() ); assertNotNull( reader.getAnnotation( SequenceGenerator.class ) );
assertEquals( FetchType.LAZY, reader.getAnnotation( Basic.class ).fetch() ); assertEquals( "seq", reader.getAnnotation( SequenceGenerator.class ).sequenceName() );
assertNotNull( reader.getAnnotation( Columns.class ) );
field = BusTripPk.class.getDeclaredField( "busDriver" ); assertEquals( 1, reader.getAnnotation( Columns.class ).columns().length );
reader = new JPAOverridenAnnotationReader( field, context ); assertEquals( "fld_id", reader.getAnnotation( Columns.class ).columns()[0].name() );
assertNotNull( reader.isAnnotationPresent( Basic.class ) ); assertNotNull( reader.getAnnotation( Temporal.class ) );
} assertEquals( TemporalType.DATE, reader.getAnnotation( Temporal.class ).value() );
public void testVersionRelatedAnnotations() throws Exception { context = buildContext(
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" ); "org/hibernate/test/annotations/reflection/metadata-complete.xml"
Method method = Administration.class.getDeclaredMethod( "getVersion" ); );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context ); method = Administration.class.getDeclaredMethod( "getId" );
assertNotNull( reader.getAnnotation( Version.class ) ); reader = new JPAOverridenAnnotationReader( method, context );
assertNotNull(
Field field = Match.class.getDeclaredField( "version" ); "Default access type when not defined in metadata complete should be property",
reader = new JPAOverridenAnnotationReader( field, context ); reader.getAnnotation( Id.class )
assertNotNull( reader.getAnnotation( Version.class ) ); );
} field = Administration.class.getDeclaredField( "id" );
reader = new JPAOverridenAnnotationReader( field, context );
public void testTransientAndEmbeddedRelatedAnnotations() throws Exception { assertNull(
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" ); "Default access type when not defined in metadata complete should be property",
reader.getAnnotation( Id.class )
Field field = Administration.class.getDeclaredField( "transientField" ); );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Transient.class ) ); method = BusTrip.class.getDeclaredMethod( "getId" );
assertNull( reader.getAnnotation( Basic.class ) ); reader = new JPAOverridenAnnotationReader( method, context );
assertNull( reader.getAnnotation( EmbeddedId.class ) );
field = Match.class.getDeclaredField( "playerASSN" ); field = BusTrip.class.getDeclaredField( "id" );
reader = new JPAOverridenAnnotationReader( field, context ); reader = new JPAOverridenAnnotationReader( field, context );
assertNotNull( reader.getAnnotation( Embedded.class ) ); assertNotNull( reader.getAnnotation( EmbeddedId.class ) );
} assertNotNull( reader.getAnnotation( AttributeOverrides.class ) );
assertEquals( 1, reader.getAnnotation( AttributeOverrides.class ).value().length );
public void testAssociationRelatedAnnotations() throws Exception { }
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
public void testBasicRelatedAnnotations() throws Exception {
Field field = Administration.class.getDeclaredField( "defaultBusTrip" ); XMLContext context = buildContext(
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context ); "org/hibernate/test/annotations/reflection/metadata-complete.xml"
assertNotNull( reader.getAnnotation( OneToOne.class ) ); );
assertNull( reader.getAnnotation( JoinColumns.class ) ); Field field = BusTrip.class.getDeclaredField( "status" );
assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) ); JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context );
assertEquals( "pk", reader.getAnnotation( PrimaryKeyJoinColumns.class ).value()[0].name() ); assertNotNull( reader.getAnnotation( Enumerated.class ) );
assertEquals( 5, reader.getAnnotation( OneToOne.class ).cascade().length ); assertEquals( EnumType.STRING, reader.getAnnotation( Enumerated.class ).value() );
assertEquals( FetchType.LAZY, reader.getAnnotation( OneToOne.class ).fetch() ); assertEquals( false, reader.getAnnotation( Basic.class ).optional() );
assertEquals( "test", reader.getAnnotation( OneToOne.class ).mappedBy() ); field = BusTrip.class.getDeclaredField( "serial" );
reader = new JPAOverridenAnnotationReader( field, context );
context = buildContext( assertNotNull( reader.getAnnotation( Lob.class ) );
"org/hibernate/test/annotations/reflection/metadata-complete.xml" assertEquals( "serialbytes", reader.getAnnotation( Columns.class ).columns()[0].name() );
); field = BusTrip.class.getDeclaredField( "terminusTime" );
field = BusTrip.class.getDeclaredField( "players" ); reader = new JPAOverridenAnnotationReader( field, context );
reader = new JPAOverridenAnnotationReader( field, context ); assertNotNull( reader.getAnnotation( Temporal.class ) );
assertNotNull( reader.getAnnotation( OneToMany.class ) ); assertEquals( TemporalType.TIMESTAMP, reader.getAnnotation( Temporal.class ).value() );
assertNotNull( reader.getAnnotation( JoinColumns.class ) ); assertEquals( FetchType.LAZY, reader.getAnnotation( Basic.class ).fetch() );
assertEquals( 2, reader.getAnnotation( JoinColumns.class ).value().length );
assertEquals( "driver", reader.getAnnotation( JoinColumns.class ).value()[0].name() ); field = BusTripPk.class.getDeclaredField( "busDriver" );
assertNotNull( reader.getAnnotation( MapKey.class ) ); reader = new JPAOverridenAnnotationReader( field, context );
assertEquals( "name", reader.getAnnotation( MapKey.class ).name() ); assertNotNull( reader.isAnnotationPresent( Basic.class ) );
}
field = BusTrip.class.getDeclaredField( "roads" );
reader = new JPAOverridenAnnotationReader( field, context ); public void testVersionRelatedAnnotations() throws Exception {
assertNotNull( reader.getAnnotation( ManyToMany.class ) ); XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
assertNotNull( reader.getAnnotation( JoinTable.class ) ); Method method = Administration.class.getDeclaredMethod( "getVersion" );
assertEquals( "bus_road", reader.getAnnotation( JoinTable.class ).name() ); JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context );
assertEquals( 2, reader.getAnnotation( JoinTable.class ).joinColumns().length ); assertNotNull( reader.getAnnotation( Version.class ) );
assertEquals( 1, reader.getAnnotation( JoinTable.class ).inverseJoinColumns().length );
assertEquals( 2, reader.getAnnotation( JoinTable.class ).uniqueConstraints()[0].columnNames().length ); Field field = Match.class.getDeclaredField( "version" );
assertNotNull( reader.getAnnotation( OrderBy.class ) ); reader = new JPAOverridenAnnotationReader( field, context );
assertEquals( "maxSpeed", reader.getAnnotation( OrderBy.class ).value() ); assertNotNull( reader.getAnnotation( Version.class ) );
} }
public void testEntityListeners() throws Exception { public void testTransientAndEmbeddedRelatedAnnotations() throws Exception {
XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" ); XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
Method method = Administration.class.getDeclaredMethod( "calculate" ); Field field = Administration.class.getDeclaredField( "transientField" );
JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context ); JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context );
assertTrue( reader.isAnnotationPresent( PrePersist.class ) ); assertNotNull( reader.getAnnotation( Transient.class ) );
assertNull( reader.getAnnotation( Basic.class ) );
reader = new JPAOverridenAnnotationReader( Administration.class, context );
assertTrue( reader.isAnnotationPresent( EntityListeners.class ) ); field = Match.class.getDeclaredField( "playerASSN" );
assertEquals( 1, reader.getAnnotation( EntityListeners.class ).value().length ); reader = new JPAOverridenAnnotationReader( field, context );
assertEquals( LogListener.class, reader.getAnnotation( EntityListeners.class ).value()[0] ); assertNotNull( reader.getAnnotation( Embedded.class ) );
}
method = LogListener.class.getDeclaredMethod( "noLog", Object.class );
reader = new JPAOverridenAnnotationReader( method, context ); public void testAssociationRelatedAnnotations() throws Exception {
assertTrue( reader.isAnnotationPresent( PostLoad.class ) ); XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
method = LogListener.class.getDeclaredMethod( "log", Object.class ); Field field = Administration.class.getDeclaredField( "defaultBusTrip" );
reader = new JPAOverridenAnnotationReader( method, context ); JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( field, context );
assertTrue( reader.isAnnotationPresent( PrePersist.class ) ); assertNotNull( reader.getAnnotation( OneToOne.class ) );
assertFalse( reader.isAnnotationPresent( PostPersist.class ) ); assertNull( reader.getAnnotation( JoinColumns.class ) );
assertNotNull( reader.getAnnotation( PrimaryKeyJoinColumns.class ) );
assertEquals( 1, context.getDefaultEntityListeners().size() ); assertEquals( "pk", reader.getAnnotation( PrimaryKeyJoinColumns.class ).value()[0].name() );
assertEquals( OtherLogListener.class.getName(), context.getDefaultEntityListeners().get(0) ); assertEquals( 5, reader.getAnnotation( OneToOne.class ).cascade().length );
} assertEquals( FetchType.LAZY, reader.getAnnotation( OneToOne.class ).fetch() );
assertEquals( "test", reader.getAnnotation( OneToOne.class ).mappedBy() );
private XMLContext buildContext(String ormfile) throws SAXException, DocumentException, IOException {
XMLHelper xmlHelper = new XMLHelper(); context = buildContext(
ClassLoader cl = Thread.currentThread().getContextClassLoader(); "org/hibernate/test/annotations/reflection/metadata-complete.xml"
InputStream is = cl.getResourceAsStream( ormfile ); );
assertNotNull( "ORM.xml not found: " + ormfile, is ); field = BusTrip.class.getDeclaredField( "players" );
XMLContext context = new XMLContext(); reader = new JPAOverridenAnnotationReader( field, context );
List errors = new ArrayList(); assertNotNull( reader.getAnnotation( OneToMany.class ) );
SAXReader saxReader = xmlHelper.createSAXReader( "XML InputStream", errors, EJB3DTDEntityResolver.INSTANCE ); assertNotNull( reader.getAnnotation( JoinColumns.class ) );
//saxReader.setValidation( false ); assertEquals( 2, reader.getAnnotation( JoinColumns.class ).value().length );
try { assertEquals( "driver", reader.getAnnotation( JoinColumns.class ).value()[0].name() );
saxReader.setFeature( "http://apache.org/xml/features/validation/schema", true ); assertNotNull( reader.getAnnotation( MapKey.class ) );
} assertEquals( "name", reader.getAnnotation( MapKey.class ).name() );
catch (SAXNotSupportedException e) {
saxReader.setValidation( false ); field = BusTrip.class.getDeclaredField( "roads" );
} reader = new JPAOverridenAnnotationReader( field, context );
org.dom4j.Document doc; assertNotNull( reader.getAnnotation( ManyToMany.class ) );
try { assertNotNull( reader.getAnnotation( JoinTable.class ) );
doc = saxReader assertEquals( "bus_road", reader.getAnnotation( JoinTable.class ).name() );
.read( new InputSource( new BufferedInputStream( is ) ) ); assertEquals( 2, reader.getAnnotation( JoinTable.class ).joinColumns().length );
} assertEquals( 1, reader.getAnnotation( JoinTable.class ).inverseJoinColumns().length );
finally { assertEquals( 2, reader.getAnnotation( JoinTable.class ).uniqueConstraints()[0].columnNames().length );
is.close(); assertNotNull( reader.getAnnotation( OrderBy.class ) );
} assertEquals( "maxSpeed", reader.getAnnotation( OrderBy.class ).value() );
if (errors.size() > 0) { }
System.out.println( errors.get( 0 ) );
} public void testEntityListeners() throws Exception {
assertEquals( 0, errors.size() ); XMLContext context = buildContext( "org/hibernate/test/annotations/reflection/orm.xml" );
context.addDocument( doc );
return context; Method method = Administration.class.getDeclaredMethod( "calculate" );
} JPAOverridenAnnotationReader reader = new JPAOverridenAnnotationReader( method, context );
} assertTrue( reader.isAnnotationPresent( PrePersist.class ) );
reader = new JPAOverridenAnnotationReader( Administration.class, context );
assertTrue( reader.isAnnotationPresent( EntityListeners.class ) );
assertEquals( 1, reader.getAnnotation( EntityListeners.class ).value().length );
assertEquals( LogListener.class, reader.getAnnotation( EntityListeners.class ).value()[0] );
method = LogListener.class.getDeclaredMethod( "noLog", Object.class );
reader = new JPAOverridenAnnotationReader( method, context );
assertTrue( reader.isAnnotationPresent( PostLoad.class ) );
method = LogListener.class.getDeclaredMethod( "log", Object.class );
reader = new JPAOverridenAnnotationReader( method, context );
assertTrue( reader.isAnnotationPresent( PrePersist.class ) );
assertFalse( reader.isAnnotationPresent( PostPersist.class ) );
assertEquals( 1, context.getDefaultEntityListeners().size() );
assertEquals( OtherLogListener.class.getName(), context.getDefaultEntityListeners().get( 0 ) );
}
private XMLContext buildContext(String ormfile) throws SAXException, DocumentException, IOException {
XMLHelper xmlHelper = new XMLHelper();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = cl.getResourceAsStream( ormfile );
assertNotNull( "ORM.xml not found: " + ormfile, is );
XMLContext context = new XMLContext();
List errors = new ArrayList();
SAXReader saxReader = xmlHelper.createSAXReader( "XML InputStream", errors, EJB3DTDEntityResolver.INSTANCE );
//saxReader.setValidation( false );
try {
saxReader.setFeature( "http://apache.org/xml/features/validation/schema", true );
}
catch ( SAXNotSupportedException e ) {
saxReader.setValidation( false );
}
org.dom4j.Document doc;
try {
doc = saxReader
.read( new InputSource( new BufferedInputStream( is ) ) );
}
finally {
is.close();
}
if ( errors.size() > 0 ) {
System.out.println( errors.get( 0 ) );
}
assertEquals( 0, errors.size() );
context.addDocument( doc );
return context;
}
}