HHH-6371 Refactoring entity based test. Introducing @Resources

This commit is contained in:
Hardy Ferentschik 2011-07-18 18:19:39 +02:00
parent 8d3e94ab83
commit 2ea24693c0
21 changed files with 235 additions and 406 deletions

View File

@ -49,7 +49,7 @@ public ProjectionList add(Projection proj) {
}
public ProjectionList add(Projection projection, String alias) {
return add( Projections.alias(projection, alias) );
return add( Projections.alias( projection, alias ) );
}
public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)

View File

@ -199,7 +199,6 @@ private void indexClass(Indexer indexer, String className) {
throw new HibernateException( "Unable to open input stream for class " + className, e );
}
}
}

View File

@ -157,21 +157,19 @@ public static ConfiguredClassHierarchy<EmbeddableClass> createEmbeddableHierarch
*
* @param info the jandex class info
*
* @return {@code true} if the class represented by {@code info} is relevant for the JPA mappings, {@code false} otherwise.
* @return {@code true} if the class represented by {@code info} is annotated with {@code @Entity}, {@code false} otherwise.
*/
private static boolean isEntityClass(ClassInfo info) {
boolean isConfiguredClass = true;
// we are only interested in building the class hierarchies for @Entity
AnnotationInstance jpaEntityAnnotation = JandexHelper.getSingleAnnotation( info, JPADotNames.ENTITY );
AnnotationInstance mappedSuperClassAnnotation = JandexHelper.getSingleAnnotation(
info, JPADotNames.MAPPED_SUPERCLASS
);
// we are only interested in building the class hierarchies for @Entity or @MappedSuperclass
if ( jpaEntityAnnotation == null && mappedSuperClassAnnotation == null ) {
if ( jpaEntityAnnotation == null ) {
return false;
}
// some sanity checks
AnnotationInstance mappedSuperClassAnnotation = JandexHelper.getSingleAnnotation(
info, JPADotNames.MAPPED_SUPERCLASS
);
String className = info.toString();
assertNotEntityAndMappedSuperClass( jpaEntityAnnotation, mappedSuperClassAnnotation, className );
@ -180,7 +178,7 @@ private static boolean isEntityClass(ClassInfo info) {
);
assertNotEntityAndEmbeddable( jpaEntityAnnotation, embeddableAnnotation, className );
return isConfiguredClass;
return true;
}
private static boolean existsHierarchyWithClassInfoAsLeaf(Map<ClassInfo, List<ClassInfo>> processedClassInfos, ClassInfo tmpClassInfo) {

View File

@ -121,13 +121,6 @@ public Iterator<T> iterator() {
return configuredClasses.iterator();
}
/**
* @return Returns the top level configured class
*/
public T getRoot() {
return configuredClasses.get( 0 );
}
/**
* @return Returns the leaf configured class
*/

View File

@ -1,146 +0,0 @@
/*
* 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;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.classmate.MemberResolver;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.ResolvedTypeWithMembers;
import com.fasterxml.classmate.TypeResolver;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.internal.util.Value;
import org.hibernate.metamodel.domain.Type;
import org.hibernate.metamodel.source.MappingDefaults;
import org.hibernate.metamodel.source.MetadataImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService;
/**
* @author Steve Ebersole
*/
public class TestAnnotationsBindingContextImpl implements AnnotationBindingContext {
private Index index;
private ServiceRegistry serviceRegistry;
private NamingStrategy namingStrategy = EJB3NamingStrategy.INSTANCE;
private final TypeResolver typeResolver = new TypeResolver();
private final Map<Class<?>, ResolvedType> resolvedTypeCache = new HashMap<Class<?>, ResolvedType>();
public TestAnnotationsBindingContextImpl(Index index, ServiceRegistry serviceRegistry) {
this.index = index;
this.serviceRegistry = serviceRegistry;
}
@Override
public Index getIndex() {
return index;
}
@Override
public ServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
@Override
public NamingStrategy getNamingStrategy() {
return namingStrategy;
}
@Override
public MappingDefaults getMappingDefaults() {
throw new NotYetImplementedException();
}
@Override
public MetadataImplementor getMetadataImplementor() {
throw new NotYetImplementedException();
}
@Override
public <T> Class<T> locateClassByName(String name) {
return serviceRegistry.getService( ClassLoaderService.class ).classForName( name );
}
@Override
public Type makeJavaType(String className) {
throw new NotYetImplementedException();
}
@Override
public Value<Class<?>> makeClassReference(String className) {
throw new NotYetImplementedException();
}
@Override
public String qualifyClassName(String name) {
return name;
}
@Override
public ClassInfo getClassInfo(String name) {
DotName dotName = DotName.createSimple( name );
return index.getClassByName( dotName );
}
@Override
public void resolveAllTypes(String className) {
// the resolved type for the top level class in the hierarchy
Class<?> clazz = locateClassByName( className );
ResolvedType resolvedType = typeResolver.resolve( clazz );
while ( resolvedType != null ) {
// todo - check whether there is already something in the map
resolvedTypeCache.put( clazz, resolvedType );
resolvedType = resolvedType.getParentClass();
if ( resolvedType != null ) {
clazz = resolvedType.getErasedType();
}
}
}
@Override
public ResolvedType getResolvedType(Class<?> clazz) {
return resolvedTypeCache.get( clazz );
}
@Override
public ResolvedTypeWithMembers resolveMemberTypes(ResolvedType type) {
// todo : is there a reason we create this resolver every time?
MemberResolver memberResolver = new MemberResolver( typeResolver );
return memberResolver.resolve( type, null, null );
}
@Override
public boolean isGloballyQuotedIdentifiers() {
return false;
}
}

View File

@ -24,10 +24,14 @@
package org.hibernate.metamodel.source.annotations.entity;
import org.junit.After;
import org.junit.Rule;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
@ -38,37 +42,38 @@ public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase {
protected MetadataSources sources;
protected MetadataImpl meta;
@Rule
public MethodRule buildMetaData = new MethodRule() {
@Override
public Statement apply(Statement statement, FrameworkMethod frameworkMethod, Object o) {
sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
Resources resourcesAnnotation = frameworkMethod.getAnnotation( Resources.class );
if ( resourcesAnnotation != null ) {
sources.getMetadataBuilder().with( resourcesAnnotation.cacheMode() );
for ( Class<?> annotatedClass : resourcesAnnotation.annotatedClasses() ) {
sources.addAnnotatedClass( annotatedClass );
}
if ( !resourcesAnnotation.ormXmlPath().isEmpty() ) {
sources.addResource( resourcesAnnotation.ormXmlPath() );
}
}
meta = (MetadataImpl) sources.buildMetadata();
return statement;
}
};
@After
public void tearDown() {
sources = null;
meta = null;
}
public void buildMetadataSources(String ormPath, Class<?>... classes) {
sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
if ( ormPath != null ) {
sources.addResource( ormPath );
}
for ( Class clazz : classes ) {
sources.addAnnotatedClass( clazz );
}
}
public void buildMetadataSources(Class<?>... classes) {
buildMetadataSources( null, classes );
}
public EntityBinding getEntityBinding(Class<?> clazz) {
if ( meta == null ) {
meta = (MetadataImpl) sources.buildMetadata();
}
return meta.getEntityBinding( clazz.getName() );
}
public EntityBinding getRootEntityBinding(Class<?> clazz) {
if ( meta == null ) {
meta = (MetadataImpl) sources.buildMetadata();
}
return meta.getRootEntityBinding( clazz.getName() );
}
}

View File

@ -40,15 +40,15 @@
*/
public class BatchSizeBindingTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = NoBatchSizeEntity.class)
public void testNoBatchSize() {
buildMetadataSources( NoBatchSizeEntity.class );
EntityBinding binding = getEntityBinding( NoBatchSizeEntity.class );
assertEquals( "Wrong batch size", -1, binding.getBatchSize() );
}
@Test
@Resources(annotatedClasses = BatchSizeEntity.class)
public void testBatchSize() {
buildMetadataSources( BatchSizeEntity.class );
EntityBinding binding = getEntityBinding( BatchSizeEntity.class );
assertEquals( "Wrong batch size", 100, binding.getBatchSize() );
}

View File

@ -47,9 +47,8 @@
*/
public class CacheBindingTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = HibernateCacheEntity.class, cacheMode = SharedCacheMode.ALL)
public void testHibernateCaching() {
buildMetadataSources( HibernateCacheEntity.class );
sources.getMetadataBuilder().with( SharedCacheMode.ALL );
EntityBinding binding = getEntityBinding( HibernateCacheEntity.class );
assertNotNull( "There should be a cache binding", binding.getCaching() );
Caching caching = binding.getCaching();
@ -59,9 +58,8 @@ public void testHibernateCaching() {
}
@Test
@Resources(annotatedClasses = JpaCacheEntity.class, cacheMode = SharedCacheMode.ALL)
public void testJpaCaching() {
buildMetadataSources( JpaCacheEntity.class );
sources.getMetadataBuilder().with( SharedCacheMode.ALL );
EntityBinding binding = getEntityBinding( JpaCacheEntity.class );
assertNotNull( "There should be a cache binding", binding.getCaching() );
Caching caching = binding.getCaching();
@ -74,9 +72,8 @@ public void testJpaCaching() {
}
@Test
@Resources(annotatedClasses = NoCacheEntity.class, cacheMode = SharedCacheMode.NONE)
public void testNoCaching() {
buildMetadataSources( NoCacheEntity.class );
sources.getMetadataBuilder().with( SharedCacheMode.NONE );
EntityBinding binding = getEntityBinding( NoCacheEntity.class );
assertNull( "There should be no cache binding", binding.getCaching() );
}

View File

@ -48,8 +48,8 @@
*/
public class CustomSQLBindingTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = NoCustomSQLEntity.class)
public void testNoCustomSqlAnnotations() {
buildMetadataSources( NoCustomSQLEntity.class );
EntityBinding binding = getEntityBinding( NoCustomSQLEntity.class );
assertNull( binding.getCustomDelete() );
assertNull( binding.getCustomInsert() );
@ -57,8 +57,8 @@ public void testNoCustomSqlAnnotations() {
}
@Test
@Resources(annotatedClasses = CustomSQLEntity.class)
public void testCustomSqlAnnotations() {
buildMetadataSources( CustomSQLEntity.class );
EntityBinding binding = getEntityBinding( CustomSQLEntity.class );
CustomSQL customSql = binding.getCustomInsert();

View File

@ -31,9 +31,8 @@
import org.junit.Test;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.domain.Component;
import org.hibernate.metamodel.domain.SingularAttribute;;
import org.hibernate.metamodel.domain.SingularAttribute;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
@ -45,8 +44,8 @@
*/
public class EmbeddableBindingTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = { User.class, Address.class })
public void testEmbeddable() {
buildMetadataSources( User.class, Address.class );
EntityBinding binding = getEntityBinding( User.class );
assertNotNull( binding.getAttributeBinding( "street" ) );
assertNotNull( binding.getAttributeBinding( "city" ) );

View File

@ -21,14 +21,11 @@
*/
public class EmbeddedIdTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = { User.class, Address.class })
public void testEmbeddable() {
buildMetadataSources( User.class, Address.class );
EntityBinding binding = getEntityBinding( User.class );
EntityIdentifier identifier = binding.getEntityIdentifier();
assertTrue( identifier.isEmbedded() );
// assertTrue(
// "EmbeddedId generator should be 'assigned'", identifier.getIdentifierGenerator() instanceof Assigned
// );
}
@Entity

View File

@ -43,30 +43,25 @@
*/
public class InheritanceBindingTest extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = SingleEntity.class)
public void testNoInheritance() {
buildMetadataSources( SingleEntity.class );
EntityBinding entityBinding = getEntityBinding( SingleEntity.class );
assertNull( entityBinding.getEntityDiscriminator() );
}
@Test
@Resources(annotatedClasses = { RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class })
public void testDiscriminatorValue() {
buildMetadataSources(
RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class
);
EntityBinding entityBinding = getEntityBinding( SubclassOfSingleTableInheritance.class );
assertEquals( "Wrong discriminator value", "foo", entityBinding.getDiscriminatorValue() );
}
@Test
@Resources(annotatedClasses = { SubclassOfSingleTableInheritance.class, SingleEntity.class, RootOfSingleTableInheritance.class })
@FailureExpected(jiraKey = "HHH-6447", message = "Work in progress")
public void testRootEntityBinding() {
buildMetadataSources(
SubclassOfSingleTableInheritance.class, SingleEntity.class, RootOfSingleTableInheritance.class
);
EntityBinding noInheritanceEntityBinding = getEntityBinding( SingleEntity.class );
assertTrue( noInheritanceEntityBinding.isRoot() );
assertTrue( "SingleEntity should be a root entity", noInheritanceEntityBinding.isRoot() );
assertSame( noInheritanceEntityBinding, getRootEntityBinding( SingleEntity.class ) );
EntityBinding subclassEntityBinding = getEntityBinding( SubclassOfSingleTableInheritance.class );
@ -75,7 +70,7 @@ public void testRootEntityBinding() {
assertSame( rootEntityBinding, getRootEntityBinding( SubclassOfSingleTableInheritance.class ) );
assertTrue( rootEntityBinding.isRoot() );
assertSame( rootEntityBinding, getRootEntityBinding( RootOfSingleTableInheritance.class ));
assertSame( rootEntityBinding, getRootEntityBinding( RootOfSingleTableInheritance.class ) );
}
@Entity

View File

@ -47,12 +47,11 @@
*
* @author Hardy Ferentschik
*/
@FailureExpected(jiraKey = "HHH-6447", message = "Work in progress")
public class MappedSuperclassTests extends BaseAnnotationBindingTestCase {
@Test
@FailureExpected(jiraKey = "HHH-6447", message = "Work in progress")
// @Resources(annotatedClasses = { MyMappedSuperClass.class, MyEntity.class, MyMappedSuperClassBase.class })
public void testSimpleAttributeOverrideInMappedSuperclass() {
buildMetadataSources( MyMappedSuperClass.class, MyEntity.class, MyMappedSuperClassBase.class );
EntityBinding binding = getEntityBinding( MyEntity.class );
AttributeBinding nameBinding = binding.getAttributeBinding( "name" );
assertNotNull( "the name attribute should be bound to MyEntity", nameBinding );
@ -62,10 +61,8 @@ public void testSimpleAttributeOverrideInMappedSuperclass() {
}
@Test
@FailureExpected(jiraKey = "HHH-6447", message = "Work in progress")
// @Resources(annotatedClasses = { MyMappedSuperClass.class, MyEntity.class, MyMappedSuperClassBase.class })
public void testLastAttributeOverrideWins() {
buildMetadataSources( MyMappedSuperClass.class, MyEntity.class, MyMappedSuperClassBase.class );
EntityBinding binding = getEntityBinding( MyEntity.class );
AttributeBinding fooBinding = binding.getAttributeBinding( "foo" );
assertNotNull( "the foo attribute should be bound to MyEntity", fooBinding );
@ -75,9 +72,8 @@ public void testLastAttributeOverrideWins() {
}
@Test
@FailureExpected(jiraKey = "HHH-6447", message = "Work in progress")
// @Resources(annotatedClasses = { SubclassOfNoEntity.class, NoEntity.class })
public void testNonEntityBaseClass() {
buildMetadataSources( SubclassOfNoEntity.class, NoEntity.class );
EntityBinding binding = getEntityBinding( SubclassOfNoEntity.class );
assertEquals( "Wrong entity name", SubclassOfNoEntity.class.getName(), binding.getEntity().getName() );
assertEquals( "Wrong entity name", NoEntity.class.getName(), binding.getEntity().getSuperType().getName() );

View File

@ -42,32 +42,32 @@
*/
public class ProxyBindingTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = ProxiedEntity.class)
public void testProxyNoAttributes() {
buildMetadataSources( ProxiedEntity.class );
EntityBinding binding = getEntityBinding( ProxiedEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", ProxiedEntity.class, binding.getProxyInterfaceType().getValue() );
}
@Test
@Resources(annotatedClasses = NoProxyEntity.class)
public void testNoProxy() {
buildMetadataSources(NoProxyEntity.class);
EntityBinding binding = getEntityBinding( NoProxyEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", NoProxyEntity.class, binding.getProxyInterfaceType().getValue() );
}
@Test
@Resources(annotatedClasses = ProxyDisabledEntity.class)
public void testProxyDisabled() {
buildMetadataSources( ProxyDisabledEntity.class );
EntityBinding binding = getEntityBinding( ProxyDisabledEntity.class );
assertFalse( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", null, binding.getProxyInterfaceType() );
}
@Test
@Resources(annotatedClasses = ProxyInterfaceEntity.class)
public void testProxyInterface() {
buildMetadataSources( ProxyInterfaceEntity.class );
EntityBinding binding = getEntityBinding( ProxyInterfaceEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals(

View File

@ -15,11 +15,11 @@
* @author Strong Liu
*/
public class QuotedIdentifierTests extends BaseAnnotationBindingTestCase {
String ormPath = "org/hibernate/metamodel/source/annotations/xml/orm-quote-identifier.xml";
private final String ormPath = "org/hibernate/metamodel/source/annotations/xml/orm-quote-identifier.xml";
@Test
@Resources(annotatedClasses = { Item.class, Item2.class, Item3.class, Item4.class }, ormXmlPath = ormPath)
public void testDelimitedIdentifiers() {
buildMetadataSources( ormPath, Item.class, Item2.class, Item3.class, Item4.class );
EntityBinding item = getEntityBinding( Item.class );
assertIdentifierEquals( "`Item`", item );

View File

@ -0,0 +1,46 @@
/*
* 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.entity;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.persistence.SharedCacheMode;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Allows to specify the annotated classes and xml configuration files for this test
*
* @author Hardy Ferentschik
*/
@Target(METHOD)
@Retention(RUNTIME)
public @interface Resources {
Class<?>[] annotatedClasses() default { };
String ormXmlPath() default "";
SharedCacheMode cacheMode() default SharedCacheMode.ENABLE_SELECTIVE;
}

View File

@ -40,15 +40,15 @@
*/
public class RowIdBindingTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = NoRowIdEntity.class)
public void testNoRowId() {
buildMetadataSources( NoRowIdEntity.class );
EntityBinding binding = getEntityBinding( NoRowIdEntity.class );
assertEquals( "Wrong row id", null, binding.getRowId() );
}
@Test
@Resources(annotatedClasses = RowIdEntity.class)
public void testRowId() {
buildMetadataSources( RowIdEntity.class );
EntityBinding binding = getEntityBinding( RowIdEntity.class );
assertEquals( "Wrong row id", "rowid", binding.getRowId() );
}

View File

@ -42,8 +42,8 @@
*/
public class SynchronizeBindingTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = TestEntityWithSynchronizeAnnotation.class)
public void testSynchronizeAnnotation() {
buildMetadataSources( TestEntityWithSynchronizeAnnotation.class );
EntityBinding binding = getEntityBinding( TestEntityWithSynchronizeAnnotation.class );
Set<String> synchronizedTableNames = binding.getSynchronizedTableNames();
assertEquals( "Wrong number of synced tables", 2, synchronizedTableNames.size() );
@ -52,8 +52,8 @@ public void testSynchronizeAnnotation() {
}
@Test
@Resources(annotatedClasses = TestEntity.class)
public void testNoSynchronizeAnnotation() {
buildMetadataSources( TestEntity.class );
EntityBinding binding = getEntityBinding( TestEntity.class );
assertTrue( "There should be no cache binding", binding.getSynchronizedTableNames().size() == 0 );
}

View File

@ -23,194 +23,141 @@
*/
package org.hibernate.metamodel.source.annotations.entity;
import java.util.Iterator;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.Table;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchyBuilder;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.InheritanceType;
import org.hibernate.metamodel.source.annotations.TestAnnotationsBindingContextImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.metamodel.relational.Table;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
/**
* @author Hardy Ferentschik
*/
public class TableNameTest extends BaseUnitTestCase {
public class TableNameTest extends BaseAnnotationBindingTestCase {
private BasicServiceRegistryImpl serviceRegistry;
private ClassLoaderService service;
@Before
public void setUp() {
serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry();
service = serviceRegistry.getService( ClassLoaderService.class );
@Entity
class A {
@Id
@GeneratedValue
private int id;
}
@After
public void tearDown() {
serviceRegistry.destroy();
@Entity
class B extends A {
}
@Test
@Resources(annotatedClasses = { A.class, B.class })
public void testSingleInheritanceDefaultTableName() {
@Entity
class A {
@Id
@GeneratedValue
private int id;
}
EntityBinding binding = getEntityBinding( A.class );
// assertEquals( "wrong inheritance type", InheritanceType.SINGLE_TABLE, binding.getInheritanceType() );
assertEquals( "wrong table name", "A", ( (Table) binding.getBaseTable() ).getTableName().getName() );
@Entity
class B extends A {
}
Index index = JandexHelper.indexForClass( service, A.class, B.class );
Set<ConfiguredClassHierarchy<EntityClass>> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
new TestAnnotationsBindingContextImpl( index, serviceRegistry )
);
assertEquals( "There should be only one hierarchy", 1, hierarchies.size() );
Iterator<EntityClass> iter = hierarchies.iterator().next().iterator();
EntityClass entityClass = iter.next();
ClassInfo info = entityClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( A.class.getName() ), info.name() );
assertTrue( entityClass.hasOwnTable() );
Assert.assertEquals(
"wrong inheritance type", InheritanceType.SINGLE_TABLE, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "A", entityClass.getClassNameForTable()
);
assertTrue( iter.hasNext() );
entityClass = iter.next();
info = entityClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( B.class.getName() ), info.name() );
assertFalse( entityClass.hasOwnTable() );
Assert.assertEquals(
"wrong inheritance type", InheritanceType.SINGLE_TABLE, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "A", entityClass.getClassNameForTable()
);
assertFalse( iter.hasNext() );
binding = getEntityBinding( B.class );
assertEquals( "wrong inheritance type", InheritanceType.SINGLE_TABLE, binding.getInheritanceType() );
assertEquals( "wrong table name", "A", ( (Table) binding.getBaseTable() ).getTableName().getName() );
}
@Test
public void testTablePerClassDefaultTableName() {
@Entity
@Inheritance(strategy = javax.persistence.InheritanceType.TABLE_PER_CLASS)
class A {
@Id
@GeneratedValue
private int id;
}
@Entity
class B extends A {
}
Index index = JandexHelper.indexForClass( service, A.class, B.class );
Set<ConfiguredClassHierarchy<EntityClass>> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
new TestAnnotationsBindingContextImpl( index, serviceRegistry )
);
assertEquals( "There should be only one hierarchy", 1, hierarchies.size() );
Iterator<EntityClass> iter = hierarchies.iterator().next().iterator();
EntityClass entityClass = iter.next();
ClassInfo info = entityClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( A.class.getName() ), info.name() );
assertTrue( entityClass.hasOwnTable() );
Assert.assertEquals(
"wrong inheritance type", InheritanceType.TABLE_PER_CLASS, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "A", entityClass.getClassNameForTable()
);
assertTrue( iter.hasNext() );
entityClass = iter.next();
info = entityClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( B.class.getName() ), info.name() );
assertTrue( entityClass.hasOwnTable() );
Assert.assertEquals(
"wrong inheritance type", InheritanceType.TABLE_PER_CLASS, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "B", entityClass.getClassNameForTable()
);
assertFalse( iter.hasNext() );
}
@Test
public void testJoinedSubclassDefaultTableName() {
@Entity
@Inheritance(strategy = javax.persistence.InheritanceType.JOINED)
@Table(name = "FOO")
class A {
@Id
@GeneratedValue
private int id;
}
@Entity
class B extends A {
}
Index index = JandexHelper.indexForClass( service, B.class, A.class );
Set<ConfiguredClassHierarchy<EntityClass>> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
new TestAnnotationsBindingContextImpl( index, serviceRegistry )
);
assertEquals( "There should be only one hierarchy", 1, hierarchies.size() );
Iterator<EntityClass> iter = hierarchies.iterator().next().iterator();
EntityClass entityClass = iter.next();
ClassInfo info = entityClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( A.class.getName() ), info.name() );
assertTrue( entityClass.hasOwnTable() );
Assert.assertEquals(
"wrong inheritance type", InheritanceType.JOINED, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "A", entityClass.getClassNameForTable()
);
assertTrue( iter.hasNext() );
entityClass = iter.next();
info = entityClass.getClassInfo();
assertEquals( "wrong class", DotName.createSimple( B.class.getName() ), info.name() );
assertTrue( entityClass.hasOwnTable() );
Assert.assertEquals(
"wrong inheritance type", InheritanceType.JOINED, entityClass.getInheritanceType()
);
Assert.assertEquals(
"wrong table name", "B", entityClass.getClassNameForTable()
);
assertFalse( iter.hasNext() );
}
// @Test
// public void testTablePerClassDefaultTableName() {
// @Entity
// @Inheritance(strategy = javax.persistence.InheritanceType.TABLE_PER_CLASS)
// class A {
// @Id
// @GeneratedValue
// private int id;
// }
//
// @Entity
// class B extends A {
// }
//
// Index index = JandexHelper.indexForClass( service, A.class, B.class );
// Set<ConfiguredClassHierarchy<EntityClass>> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
// new TestAnnotationsBindingContextImpl( index, serviceRegistry )
// );
// assertEquals( "There should be only one hierarchy", 1, hierarchies.size() );
//
// Iterator<EntityClass> iter = hierarchies.iterator().next().iterator();
// EntityClass entityClass = iter.next();
// ClassInfo info = entityClass.getClassInfo();
// assertEquals( "wrong class", DotName.createSimple( A.class.getName() ), info.name() );
// assertTrue( entityClass.hasOwnTable() );
// Assert.assertEquals(
// "wrong inheritance type", InheritanceType.TABLE_PER_CLASS, entityClass.getInheritanceType()
// );
// Assert.assertEquals(
// "wrong table name", "A", entityClass.getClassNameForTable()
// );
//
// assertTrue( iter.hasNext() );
// entityClass = iter.next();
// info = entityClass.getClassInfo();
// assertEquals( "wrong class", DotName.createSimple( B.class.getName() ), info.name() );
// assertTrue( entityClass.hasOwnTable() );
// Assert.assertEquals(
// "wrong inheritance type", InheritanceType.TABLE_PER_CLASS, entityClass.getInheritanceType()
// );
// Assert.assertEquals(
// "wrong table name", "B", entityClass.getClassNameForTable()
// );
//
// assertFalse( iter.hasNext() );
// }
//
// @Test
// public void testJoinedSubclassDefaultTableName() {
// @Entity
// @Inheritance(strategy = javax.persistence.InheritanceType.JOINED)
// @Table(name = "FOO")
// class A {
// @Id
// @GeneratedValue
// private int id;
// }
//
// @Entity
// class B extends A {
// }
//
// Index index = JandexHelper.indexForClass( service, B.class, A.class );
// Set<ConfiguredClassHierarchy<EntityClass>> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
// new TestAnnotationsBindingContextImpl( index, serviceRegistry )
// );
// assertEquals( "There should be only one hierarchy", 1, hierarchies.size() );
//
// Iterator<EntityClass> iter = hierarchies.iterator().next().iterator();
// EntityClass entityClass = iter.next();
// ClassInfo info = entityClass.getClassInfo();
// assertEquals( "wrong class", DotName.createSimple( A.class.getName() ), info.name() );
// assertTrue( entityClass.hasOwnTable() );
// Assert.assertEquals(
// "wrong inheritance type", InheritanceType.JOINED, entityClass.getInheritanceType()
// );
// Assert.assertEquals(
// "wrong table name", "A", entityClass.getClassNameForTable()
// );
//
// assertTrue( iter.hasNext() );
// entityClass = iter.next();
// info = entityClass.getClassInfo();
// assertEquals( "wrong class", DotName.createSimple( B.class.getName() ), info.name() );
// assertTrue( entityClass.hasOwnTable() );
// Assert.assertEquals(
// "wrong inheritance type", InheritanceType.JOINED, entityClass.getInheritanceType()
// );
// Assert.assertEquals(
// "wrong table name", "B", entityClass.getClassNameForTable()
// );
//
// assertFalse( iter.hasNext() );
// }
}

View File

@ -47,8 +47,8 @@
*/
public class UniqueConstraintBindingTests extends BaseAnnotationBindingTestCase {
@Test
@Resources(annotatedClasses = TableWithUniqueConstraint.class)
public void testTableUniqueConstraints() {
buildMetadataSources( TableWithUniqueConstraint.class );
EntityBinding binding = getEntityBinding( TableWithUniqueConstraint.class );
TableSpecification table = binding.getBaseTable();
Iterable<UniqueKey> uniqueKeyIterable = table.getUniqueKeys();
@ -68,7 +68,6 @@ public void testTableUniqueConstraints() {
assertEquals( "There should only be one unique constraint", 1, i );
}
@Entity
@Table(uniqueConstraints = { @UniqueConstraint(name = "u1", columnNames = { "name", "age" }) })
class TableWithUniqueConstraint {

View File

@ -24,52 +24,56 @@
package org.hibernate.metamodel.source.annotations.util;
import java.util.Set;
import javax.persistence.AccessType;
import org.jboss.jandex.Index;
import org.junit.After;
import org.junit.Before;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.annotations.AnnotationBindingContext;
import org.hibernate.metamodel.source.annotations.AnnotationBindingContextImpl;
import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchyBuilder;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy;
import org.hibernate.metamodel.source.annotations.TestAnnotationsBindingContextImpl;
import org.hibernate.metamodel.source.annotations.entity.EmbeddableClass;
import org.hibernate.metamodel.source.annotations.entity.EntityClass;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/**
* @author Hardy Ferentschik
*/
public abstract class BaseAnnotationIndexTestCase extends BaseUnitTestCase {
private BasicServiceRegistryImpl serviceRegistry;
private MetadataImpl meta;
@Before
public void setUp() {
serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry();
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
meta = (MetadataImpl) sources.buildMetadata();
}
@After
public void tearDown() {
serviceRegistry.destroy();
}
public Set<ConfiguredClassHierarchy<EntityClass>> createEntityHierarchies(Class<?>... clazz) {
Index index = JandexHelper.indexForClass( serviceRegistry.getService( ClassLoaderService.class ), clazz );
TestAnnotationsBindingContextImpl context = new TestAnnotationsBindingContextImpl( index, serviceRegistry );
Index index = JandexHelper.indexForClass(
meta.getServiceRegistry().getService( ClassLoaderService.class ),
clazz
);
AnnotationBindingContext context = new AnnotationBindingContextImpl( meta, index );
return ConfiguredClassHierarchyBuilder.createEntityHierarchies( context );
}
public ConfiguredClassHierarchy<EmbeddableClass> createEmbeddableHierarchy(AccessType accessType, Class<?>... configuredClasses) {
Index index = JandexHelper.indexForClass(
serviceRegistry.getService( ClassLoaderService.class ),
meta.getServiceRegistry().getService( ClassLoaderService.class ),
configuredClasses
);
TestAnnotationsBindingContextImpl context = new TestAnnotationsBindingContextImpl( index, serviceRegistry );
AnnotationBindingContext context = new AnnotationBindingContextImpl( meta, index );
return ConfiguredClassHierarchyBuilder.createEmbeddableHierarchy( configuredClasses[0], accessType, context );
}
}