HHH-6172 Cleaning up AbstractBasicBindingTests

This commit is contained in:
Hardy Ferentschik 2011-05-19 17:01:10 +02:00
parent 14efea6e21
commit 49cf328bb8
14 changed files with 104 additions and 167 deletions

View File

@ -172,11 +172,6 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
: Collections.singletonList( (SimpleValue) value );
}
@Override
public TableSpecification getTable() {
return getValue().getTable();
}
@Override
public String getPropertyAccessorName() {
return propertyAccessorName;

View File

@ -74,27 +74,14 @@ public interface AttributeBinding {
public Map<String, MetaAttribute> getMetaAttributes();
/**
* In the case that {@link #getValue()} represents a {@link org.hibernate.metamodel.relational.Tuple} this method
* @return In the case that {@link #getValue()} represents a {@link org.hibernate.metamodel.relational.Tuple} this method
* gives access to its compound values. In the case of {@link org.hibernate.metamodel.relational.SimpleValue},
* we return an Iterable over that single simple value.
*
* @return
*/
public Iterable<SimpleValue> getValues();
/**
* @return
*
* @deprecated Use {@link #getValue()}.{@link Value#getTable() getTable()} instead; to be removed on completion of new metamodel code
*/
@Deprecated
public TableSpecification getTable();
public String getPropertyAccessorName();
/**
* @return
*/
public boolean hasFormula();
public boolean isAlternateUniqueKey();

View File

@ -26,11 +26,10 @@ package org.hibernate.metamodel.binding;
import java.util.Iterator;
import org.hibernate.MappingException;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.relational.ForeignKey;
import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState;
import org.hibernate.metamodel.relational.state.ManyToOneRelationalState;
/**
@ -56,13 +55,6 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
isPropertyReference = state.getReferencedAttributeName() != null;
referencedAttributeName = state.getReferencedAttributeName();
referencedEntityName = state.getReferencedEntityName();
if ( referencedEntityName == null ) {
referencedEntityName =
ReflectHelper.reflectedPropertyClass(
getEntityBinding().getEntity().getName(),
state.getAttributeName()
).getName();
}
return this;
}
@ -84,7 +76,7 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
public final String getReferencedAttributeName() {
if ( referencedAttributeName == null ) {
throw new IllegalStateException(
"Referenced attribute name is not available."
"Referenced attribute name is not available."
);
}
return referencedAttributeName;
@ -95,7 +87,7 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
}
public final EntityBinding getReferencedEntityBinding() {
if ( ! isReferenceResolved() ) {
if ( !isReferenceResolved() ) {
throw new IllegalStateException( "EntityBinding reference has not be referenced." );
}
// TODO: throw exception if referencedEntityBinding is null?
@ -103,7 +95,7 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
}
public final void resolveReference(AttributeBinding referencedAttributeBinding) {
if ( ! referencedEntityName.equals( referencedAttributeBinding.getEntityBinding().getEntity().getName() ) ) {
if ( !referencedEntityName.equals( referencedAttributeBinding.getEntityBinding().getEntity().getName() ) ) {
throw new IllegalStateException(
"attempt to set EntityBinding with name: [" +
referencedAttributeBinding.getEntityBinding().getEntity().getName() +
@ -113,10 +105,10 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
if ( referencedAttributeName == null ) {
referencedAttributeName = referencedAttributeBinding.getAttribute().getName();
}
else if ( ! referencedAttributeName.equals( referencedAttributeBinding.getAttribute().getName() ) ) {
else if ( !referencedAttributeName.equals( referencedAttributeBinding.getAttribute().getName() ) ) {
throw new IllegalStateException(
"Inconsistent attribute name; expected: " + referencedAttributeName +
"actual: " + referencedAttributeBinding.getAttribute().getName()
"actual: " + referencedAttributeBinding.getAttribute().getName()
);
}
this.referencedAttributeBinding = referencedAttributeBinding;
@ -125,18 +117,21 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
private void buildForeignKey() {
// TODO: move this stuff to relational model
ForeignKey foreignKey = getTable().createForeignKey( referencedAttributeBinding.getTable(), foreignKeyName );
ForeignKey foreignKey = getValue().getTable()
.createForeignKey( referencedAttributeBinding.getValue().getTable(), foreignKeyName );
Iterator<SimpleValue> referencingValueIterator = getValues().iterator();
Iterator<SimpleValue> targetValueIterator = referencedAttributeBinding.getValues().iterator();
Iterator<SimpleValue> targetValueIterator = referencedAttributeBinding.getValues().iterator();
while ( referencingValueIterator.hasNext() ) {
if ( ! targetValueIterator.hasNext() ) {
if ( !targetValueIterator.hasNext() ) {
// TODO: improve this message
throw new MappingException( "number of values in many-to-one reference is greater than number of values in target" );
throw new MappingException(
"number of values in many-to-one reference is greater than number of values in target"
);
}
SimpleValue referencingValue = referencingValueIterator.next();
SimpleValue targetValue = targetValueIterator.next();
if ( Column.class.isInstance( referencingValue ) ) {
if ( ! Column.class.isInstance( targetValue ) ) {
if ( !Column.class.isInstance( targetValue ) ) {
// TODO improve this message
throw new MappingException( "referencing value is a column, but target is not a column" );
}
@ -159,7 +154,7 @@ public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements
public void validate() {
// can't check this until both the domain and relational states are initialized...
if ( getCascade() != null && getCascade().indexOf( "delete-orphan" ) >= 0 ) {
if ( ! isLogicalOneToOne ) {
if ( !isLogicalOneToOne ) {
throw new MappingException(
"many-to-one attribute [" + getAttribute().getName() + "] does not support orphan delete as it is not unique"
);

View File

@ -37,7 +37,6 @@ import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.PolymorphismType;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.Caching;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.SimpleAttributeBinding;
@ -291,7 +290,7 @@ public class EntityBinder {
);
String name;
if ( jpaEntityAnnotation.value( "name" ) == null ) {
name = StringHelper.unqualify( configuredClass.getName() );
name = configuredClass.getName();
}
else {
name = jpaEntityAnnotation.value( "name" ).asString();
@ -428,7 +427,7 @@ public class EntityBinder {
return null;
}
EntityBinding parentBinding = meta.getEntityBinding( parent.getSimpleName() );
EntityBinding parentBinding = meta.getEntityBinding( parent.getName() );
if ( parentBinding == null ) {
throw new AssertionFailure(
"Parent entity " + parent.getName() + " of entity " + configuredClass.getName() + " not yet created!"

View File

@ -74,6 +74,9 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable
private final SharedCacheMode sharedCacheMode;
private final Database database = new Database();
/**
* Maps the fully qualified class name of an entity to its entity binding
*/
private Map<String, EntityBinding> entityBindingMap = new HashMap<String, EntityBinding>();
private Map<String, PluralAttributeBinding> collectionBindingMap = new HashMap<String, PluralAttributeBinding>();
private Map<String, FetchProfile> fetchProfiles = new HashMap<String, FetchProfile>();

View File

@ -29,8 +29,9 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.BasicServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.internal.BasicServiceRegistryImpl;
@ -50,10 +51,12 @@ import static org.junit.Assert.assertTrue;
public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
private BasicServiceRegistryImpl serviceRegistry;
private MetadataSources sources;
@Before
public void setUp() {
serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry();
sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
}
@After
@ -67,56 +70,31 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
@Test
public void testSimpleEntityMapping() {
checkSimpleEntityMapping( buildSimpleEntityBinding() );
}
MetadataImpl metadata = addSourcesForSimpleEntityBinding( sources );
EntityBinding entityBinding = metadata.getEntityBinding( SimpleEntity.class.getName() );
assertIdAndSimpleProperty( entityBinding );
protected void checkSimpleEntityMapping(EntityBinding entityBinding) {
assertNotNull( entityBinding );
assertNotNull( entityBinding.getEntityIdentifier() );
assertNotNull( entityBinding.getEntityIdentifier().getValueBinding() );
assertNull( entityBinding.getVersioningValueBinding() );
AttributeBinding idAttributeBinding = entityBinding.getAttributeBinding( "id" );
assertNotNull( idAttributeBinding );
assertSame( idAttributeBinding, entityBinding.getEntityIdentifier().getValueBinding() );
assertNotNull( idAttributeBinding.getAttribute() );
assertNotNull( idAttributeBinding.getValue() );
assertTrue( idAttributeBinding.getValue() instanceof Column );
AttributeBinding nameBinding = entityBinding.getAttributeBinding( "name" );
assertNotNull( nameBinding );
assertNotNull( nameBinding.getAttribute() );
assertNotNull( nameBinding.getValue() );
}
@Test
public void testSimpleVersionedEntityMapping() {
EntityBinding entityBinding = buildSimpleVersionedEntityBinding();
assertNotNull( entityBinding );
assertNotNull( entityBinding.getEntityIdentifier() );
assertNotNull( entityBinding.getEntityIdentifier().getValueBinding() );
MetadataImpl metadata = addSourcesForSimpleVersionedEntityBinding( sources );
EntityBinding entityBinding = metadata.getEntityBinding( SimpleVersionedEntity.class.getName() );
assertIdAndSimpleProperty( entityBinding );
assertNotNull( entityBinding.getVersioningValueBinding() );
assertNotNull( entityBinding.getVersioningValueBinding().getAttribute() );
AttributeBinding idAttributeBinding = entityBinding.getAttributeBinding( "id" );
assertNotNull( idAttributeBinding );
assertSame( idAttributeBinding, entityBinding.getEntityIdentifier().getValueBinding() );
assertNotNull( idAttributeBinding.getAttribute() );
assertNotNull( idAttributeBinding.getValue() );
assertTrue( idAttributeBinding.getValue() instanceof Column );
AttributeBinding nameBinding = entityBinding.getAttributeBinding( "name" );
assertNotNull( nameBinding );
assertNotNull( nameBinding.getAttribute() );
assertNotNull( nameBinding.getValue() );
}
@Test
public void testEntityWithManyToOneMapping() {
MetadataImplementor metadata = buildMetadataWithManyToOne();
EntityBinding entityWithManyToOneBinding = metadata.getEntityBinding( EntityWithManyToOne.class.getName() );
MetadataImpl metadata = addSourcesForManyToOne( sources );
EntityBinding simpleEntityBinding = metadata.getEntityBinding( SimpleEntity.class.getName() );
checkSimpleEntityMapping( simpleEntityBinding );
assertIdAndSimpleProperty( simpleEntityBinding );
EntityBinding entityWithManyToOneBinding = metadata.getEntityBinding( ManyToOneEntity.class.getName() );
assertTrue(
1 == simpleEntityBinding.getAttributeBinding( "id" ).getEntityReferencingAttributeBindings().size()
@ -127,15 +105,17 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
assertSame( entityWithManyToOneBinding.getAttributeBinding( "simpleEntity" ), it.next() );
assertFalse( it.hasNext() );
}
/*
@Test
public void testEntityWithElementCollection() {
EntityBinding entityBinding = buildEntityWithElementCollectionBinding();
public abstract MetadataImpl addSourcesForSimpleVersionedEntityBinding(MetadataSources sources);
public abstract MetadataImpl addSourcesForSimpleEntityBinding(MetadataSources sources);
public abstract MetadataImpl addSourcesForManyToOne(MetadataSources sources);
protected void assertIdAndSimpleProperty(EntityBinding entityBinding) {
assertNotNull( entityBinding );
assertNotNull( entityBinding.getEntityIdentifier() );
assertNotNull( entityBinding.getEntityIdentifier().getValueBinding() );
assertNull( entityBinding.getVersioningValueBinding() );
AttributeBinding idAttributeBinding = entityBinding.getAttributeBinding( "id" );
assertNotNull( idAttributeBinding );
@ -149,13 +129,4 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
assertNotNull( nameBinding.getAttribute() );
assertNotNull( nameBinding.getValue() );
}
*/
public abstract EntityBinding buildSimpleVersionedEntityBinding();
public abstract EntityBinding buildSimpleEntityBinding();
public abstract MetadataImplementor buildMetadataWithManyToOne();
//public abstract EntityBinding buildEntityWithElementCollectionBinding();
}

View File

@ -27,8 +27,6 @@ import org.junit.Test;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.FailureExpected;
/**
@ -43,26 +41,20 @@ public class BasicAnnotationBindingTests extends AbstractBasicBindingTests {
super.testEntityWithManyToOneMapping();
}
public EntityBinding buildSimpleEntityBinding() {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
public MetadataImpl addSourcesForSimpleEntityBinding(MetadataSources sources) {
sources.addAnnotatedClass( SimpleEntity.class );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
return (MetadataImpl) sources.buildMetadata();
return metadata.getEntityBinding( SimpleEntity.class.getSimpleName() );
}
public EntityBinding buildSimpleVersionedEntityBinding() {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
public MetadataImpl addSourcesForSimpleVersionedEntityBinding(MetadataSources sources) {
sources.addAnnotatedClass( SimpleVersionedEntity.class );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
return metadata.getEntityBinding( SimpleVersionedEntity.class.getSimpleName() );
return (MetadataImpl) sources.buildMetadata();
}
public MetadataImplementor buildMetadataWithManyToOne() {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
sources.addAnnotatedClass( EntityWithManyToOne.class );
sources.addAnnotatedClass( SimpleVersionedEntity.class );
return (MetadataImplementor) sources.buildMetadata();
public MetadataImpl addSourcesForManyToOne(MetadataSources sources) {
sources.addAnnotatedClass( ManyToOneEntity.class );
sources.addAnnotatedClass( SimpleEntity.class );
return (MetadataImpl) sources.buildMetadata();
}
}

View File

@ -25,9 +25,6 @@ package org.hibernate.metamodel.binding;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import static org.junit.Assert.assertEquals;
/**
* Basic tests of {@code hbm.xml} binding code
@ -35,33 +32,19 @@ import static org.junit.Assert.assertEquals;
* @author Steve Ebersole
*/
public class BasicHbmBindingTests extends AbstractBasicBindingTests {
public EntityBinding buildSimpleEntityBinding() {
return getEntityBinding(
"org/hibernate/metamodel/binding/SimpleEntity.hbm.xml",
SimpleEntity.class.getName()
);
public MetadataImpl addSourcesForSimpleEntityBinding(MetadataSources sources) {
sources.addResource( "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml" );
return (MetadataImpl) sources.buildMetadata();
}
public EntityBinding buildSimpleVersionedEntityBinding() {
return getEntityBinding(
"org/hibernate/metamodel/binding/SimpleVersionedEntity.hbm.xml",
SimpleVersionedEntity.class.getName()
);
public MetadataImpl addSourcesForSimpleVersionedEntityBinding(MetadataSources sources) {
sources.addResource( "org/hibernate/metamodel/binding/SimpleVersionedEntity.hbm.xml" );
return (MetadataImpl) sources.buildMetadata();
}
public MetadataImplementor buildMetadataWithManyToOne() {
MetadataSources metadataSources = new MetadataSources( basicServiceRegistry() );
metadataSources.addResource( "org/hibernate/metamodel/binding/EntityWithManyToOne.hbm.xml" );
metadataSources.addResource( "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml" );
assertEquals( 2, metadataSources.getJaxbRootList().size() );
return ( MetadataImplementor ) metadataSources.buildMetadata();
}
private EntityBinding getEntityBinding(String resourceName, String entityName ) {
MetadataSources metadataSources = new MetadataSources( basicServiceRegistry() );
metadataSources.addResource( resourceName );
assertEquals( 1, metadataSources.getJaxbRootList().size() );
MetadataImpl metadata = ( MetadataImpl ) metadataSources.buildMetadata();
return metadata.getEntityBinding( entityName );
public MetadataImpl addSourcesForManyToOne(MetadataSources sources) {
sources.addResource( "org/hibernate/metamodel/binding/ManyToOneEntity.hbm.xml" );
sources.addResource( "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml" );
return (MetadataImpl) sources.buildMetadata();
}
}

View File

@ -1,15 +0,0 @@
<?xml version="1.0"?>
<hhh:hibernate-mapping package="org.hibernate.metamodel.binding" xmlns:hhh="http://www.hibernate.org/xsd/hibernate-mapping"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<class name="EntityWithManyToOne">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<many-to-one name="simpleEntity"/>
</class>
</hhh:hibernate-mapping>

View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<hhh:hibernate-mapping package="org.hibernate.metamodel.binding"
xmlns:hhh="http://www.hibernate.org/xsd/hibernate-mapping"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<class name="ManyToOneEntity">
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<many-to-one name="simpleEntity"/>
</class>
</hhh:hibernate-mapping>

View File

@ -23,9 +23,6 @@
*/
package org.hibernate.metamodel.binding;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@ -35,17 +32,18 @@ import org.hibernate.annotations.Entity;
* @author Gail Badner
*/
@Entity
public class EntityWithManyToOne {
public class ManyToOneEntity {
@Id
private Long id;
private String theName;
@ManyToOne
SimpleEntity simpleEntity;
public EntityWithManyToOne() {
public ManyToOneEntity() {
}
public EntityWithManyToOne(String name) {
this.theName = theName;
public ManyToOneEntity(String name) {
this.theName = name;
}
public Long getId() {
@ -61,10 +59,9 @@ public class EntityWithManyToOne {
}
public void setName(String name) {
this.theName = theName;
this.theName = name;
}
@ManyToOne
public SimpleEntity getSimpleEntity() {
return simpleEntity;
}
@ -72,4 +69,15 @@ public class EntityWithManyToOne {
public void setSimpleEntity(SimpleEntity simpleEntity) {
this.simpleEntity = simpleEntity;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "EntityWithManyToOne" );
sb.append( "{id=" ).append( id );
sb.append( ", theName='" ).append( theName ).append( '\'' );
sb.append( ", simpleEntity=" ).append( simpleEntity );
sb.append( '}' );
return sb.toString();
}
}

View File

@ -25,7 +25,7 @@ public class MiscAnnotationBindingTest extends BaseUnitTestCase {
sources.addAnnotatedClass( Foo.class );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
EntityBinding binding = metadata.getEntityBinding( MiscAnnotationBindingTest.class.getSimpleName() + "$" + Foo.class.getSimpleName() );
EntityBinding binding = metadata.getEntityBinding( MiscAnnotationBindingTest.class.getName() + "$" + Foo.class.getSimpleName() );
assertEquals( "Wrong where filter", "1=1", binding.getWhereFilter() );
}

View File

@ -65,7 +65,11 @@ public class CacheBindingTests extends BaseUnitTestCase {
EntityBinding binding = getEntityBinding( JpaCacheEntity.class, SharedCacheMode.ALL );
assertNotNull( "There should be a cache binding", binding.getCaching() );
Caching caching = binding.getCaching();
assertEquals( "Wrong region", "CacheBindingTests$JpaCacheEntity", caching.getRegion() );
assertEquals(
"Wrong region",
this.getClass().getName() + "$" + JpaCacheEntity.class.getSimpleName(),
caching.getRegion()
);
assertEquals( "Wrong lazy properties configuration", true, caching.isCacheLazyProperties() );
}
@ -81,7 +85,7 @@ public class CacheBindingTests extends BaseUnitTestCase {
sources.getMetadataBuilder().with( cacheMode );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
return metadata.getEntityBinding( this.getClass().getSimpleName() + "$" + clazz.getSimpleName() );
return metadata.getEntityBinding( this.getClass().getName() + "$" + clazz.getSimpleName() );
}
@Entity

View File

@ -52,7 +52,7 @@ public class InheritanceTypeTest extends BaseUnitTestCase {
@Test
public void testDiscriminatorValue() {
MetadataImpl meta = buildMetadata( RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class );
EntityBinding entityBinding = meta.getEntityBinding( SubclassOfSingleTableInheritance.class.getSimpleName() );
EntityBinding entityBinding = meta.getEntityBinding( SubclassOfSingleTableInheritance.class.getName() );
assertEquals( "Wrong discriminator value", "foo", entityBinding.getDiscriminatorValue() );
}
@ -65,7 +65,7 @@ public class InheritanceTypeTest extends BaseUnitTestCase {
}
private EntityBinding getEntityBindingForInnerClass(MetadataImpl meta, Class<?> clazz) {
return meta.getEntityBinding( this.getClass().getSimpleName() + "$" + clazz.getSimpleName() );
return meta.getEntityBinding( this.getClass().getName() + "$" + clazz.getSimpleName() );
}
@Entity