HHH-6263 Binding @Proxy. Adding some tests

This commit is contained in:
Hardy Ferentschik 2011-05-26 18:21:53 +02:00
parent bd900d9bb1
commit 227141e643
8 changed files with 222 additions and 51 deletions

View File

@ -101,6 +101,7 @@ public class EntityBinding {
// go ahead and set the lazy here, since pojo.proxy can override it.
lazy = MappingHelper.getBooleanValue( entityClazz.isLazy(), defaults.isDefaultLazy() );
proxyInterfaceName = entityClazz.getProxy();
discriminatorValue = MappingHelper.getStringValue( entityClazz.getDiscriminatorValue(), entity.getName() );
dynamicUpdate = entityClazz.isDynamicUpdate();
dynamicInsert = entityClazz.isDynamicInsert();
@ -319,6 +320,14 @@ public class EntityBinding {
this.lazy = lazy;
}
public void setProxyInterfaceName(String proxyInterfaceName) {
this.proxyInterfaceName = proxyInterfaceName;
}
public String getProxyInterfaceName() {
return proxyInterfaceName;
}
public String getWhereFilter() {
return whereFilter;
}

View File

@ -89,6 +89,7 @@ public class EntityBinder {
bindWhereFilter( entityBinding );
bindJpaCaching( entityBinding );
bindHibernateCaching( entityBinding );
bindProxy( entityBinding );
// take care of the id, attributes and relations
if ( configuredClass.isRoot() ) {
@ -228,6 +229,29 @@ public class EntityBinder {
}
}
private void bindProxy(EntityBinding entityBinding) {
AnnotationInstance proxyAnnotation = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), HibernateDotNames.PROXY
);
boolean lazy = true;
String proxyInterfaceClass = null;
if ( proxyAnnotation != null ) {
AnnotationValue lazyValue = proxyAnnotation.value( "lazy" );
if ( lazyValue != null ) {
lazy = lazyValue.asBoolean();
}
AnnotationValue proxyClassValue = proxyAnnotation.value( "proxyClass" );
if ( proxyClassValue != null ) {
proxyInterfaceClass = proxyClassValue.asString();
}
}
entityBinding.setLazy( lazy );
entityBinding.setProxyInterfaceName( proxyInterfaceClass );
}
private Caching createCachingForCacheableAnnotation(EntityBinding entityBinding) {
String region = entityBinding.getEntity().getName();
RegionFactory regionFactory = meta.getServiceRegistry().getService( RegionFactory.class );

View File

@ -151,7 +151,7 @@ abstract class AbstractEntityBinder {
private void bindPojoRepresentation(XMLHibernateMapping.XMLClass entityClazz,
EntityBinding entityBinding) {
String className = hibernateMappingBinder.getClassName( entityClazz.getName() );
String proxyName = hibernateMappingBinder.getClassName( entityClazz.getProxy() );
String proxyName = entityBinding.getProxyInterfaceName();
entityBinding.getEntity().getPojoEntitySpecifics().setClassName( className );

View File

@ -28,8 +28,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dom4j.Attribute;
import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.internal.util.StringHelper;
@ -97,11 +95,6 @@ public class HbmBinder implements MappingDefaults {
return metadata;
}
XMLHibernateMapping getHibernateMapping() {
return hibernateMapping;
}
Origin getOrigin() {
return jaxbRoot.getOrigin();
}
@ -252,12 +245,7 @@ public class HbmBinder implements MappingDefaults {
return HbmHelper.extractEntityName( entityClazz, packageName );
}
String getClassName(Attribute attribute) {
return HbmHelper.getClassName( attribute, packageName );
}
String getClassName(String unqualifiedName) {
return HbmHelper.getClassName( unqualifiedName, packageName );
}
}

View File

@ -0,0 +1,62 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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 org.junit.After;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/**
* @author Hardy Ferentschik
*/
public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase {
protected MetadataSources sources;
protected MetadataImpl meta;
@After
public void tearDown() {
sources = null;
meta = null;
}
public void buildMetadataSources(Class<?>... classes) {
sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
for ( Class clazz : classes ) {
sources.addAnnotatedClass( clazz );
}
}
public EntityBinding getEntityBinding(Class<?> clazz) {
if ( meta == null ) {
meta = (MetadataImpl) sources.buildMetadata();
}
return meta.getEntityBinding( clazz.getName() );
}
}

View File

@ -33,12 +33,8 @@ import org.junit.Test;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.Caching;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
@ -49,10 +45,12 @@ import static junit.framework.Assert.assertNull;
*
* @author Hardy Ferentschik
*/
public class CacheBindingTests extends BaseUnitTestCase {
public class CacheBindingTests extends BaseAnnotationBindingTestCase {
@Test
public void testHibernateCaching() {
EntityBinding binding = getEntityBinding( HibernateCacheEntity.class, SharedCacheMode.ALL );
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();
assertEquals( "Wrong region", "foo", caching.getRegion() );
@ -62,7 +60,9 @@ public class CacheBindingTests extends BaseUnitTestCase {
@Test
public void testJpaCaching() {
EntityBinding binding = getEntityBinding( JpaCacheEntity.class, SharedCacheMode.ALL );
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();
assertEquals(
@ -75,19 +75,12 @@ public class CacheBindingTests extends BaseUnitTestCase {
@Test
public void testNoCaching() {
EntityBinding binding = getEntityBinding( NoCacheEntity.class, SharedCacheMode.NONE );
buildMetadataSources( NoCacheEntity.class );
sources.getMetadataBuilder().with( SharedCacheMode.NONE );
EntityBinding binding = getEntityBinding( NoCacheEntity.class );
assertNull( "There should be no cache binding", binding.getCaching() );
}
private EntityBinding getEntityBinding(Class<?> clazz, SharedCacheMode cacheMode) {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
sources.addAnnotatedClass( clazz );
sources.getMetadataBuilder().with( cacheMode );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
return metadata.getEntityBinding( this.getClass().getName() + "$" + clazz.getSimpleName() );
}
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "foo", include = "non-lazy")
class HibernateCacheEntity {

View File

@ -29,11 +29,7 @@ import javax.persistence.Id;
import org.junit.Test;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
@ -41,33 +37,23 @@ import static junit.framework.Assert.assertNull;
/**
* @author Hardy Ferentschik
*/
public class InheritanceTypeTest extends BaseUnitTestCase {
public class InheritanceTypeTest extends BaseAnnotationBindingTestCase {
@Test
public void testNoInheritance() {
MetadataImpl meta = buildMetadata( SingleEntity.class );
EntityBinding entityBinding = getEntityBindingForInnerClass( meta, SingleEntity.class );
buildMetadataSources( SingleEntity.class );
EntityBinding entityBinding = getEntityBinding( SingleEntity.class );
assertNull( entityBinding.getEntityDiscriminator() );
}
@Test
public void testDiscriminatorValue() {
MetadataImpl meta = buildMetadata( RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class );
EntityBinding entityBinding = meta.getEntityBinding( SubclassOfSingleTableInheritance.class.getName() );
buildMetadataSources(
RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class
);
EntityBinding entityBinding = getEntityBinding( SubclassOfSingleTableInheritance.class );
assertEquals( "Wrong discriminator value", "foo", entityBinding.getDiscriminatorValue() );
}
private MetadataImpl buildMetadata(Class<?>... classes) {
MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
for ( Class clazz : classes ) {
sources.addAnnotatedClass( clazz );
}
return (MetadataImpl) sources.buildMetadata();
}
private EntityBinding getEntityBindingForInnerClass(MetadataImpl meta, Class<?> clazz) {
return meta.getEntityBinding( this.getClass().getName() + "$" + clazz.getSimpleName() );
}
@Entity
class SingleEntity {
@Id

View File

@ -0,0 +1,109 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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 javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.Test;
import org.hibernate.annotations.Proxy;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
/**
* Tests for {@code o.h.a.Cache}.
*
* @author Hardy Ferentschik
*/
public class ProxyBindingTests extends BaseAnnotationBindingTestCase {
@Test
public void testProxyNoAttributes() {
buildMetadataSources( ProxiedEntity.class );
EntityBinding binding = getEntityBinding( ProxiedEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", null, binding.getProxyInterfaceName() );
}
@Test
public void testNoProxy() {
buildMetadataSources(NoProxyEntity.class);
EntityBinding binding = getEntityBinding( NoProxyEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", null, binding.getProxyInterfaceName() );
}
@Test
public void testProxyDisabled() {
buildMetadataSources( ProxyDisabledEntity.class );
EntityBinding binding = getEntityBinding( ProxyDisabledEntity.class );
assertFalse( "Wrong laziness", binding.isLazy() );
assertEquals( "Wrong proxy interface", null, binding.getProxyInterfaceName() );
}
@Test
public void testProxyInterface() {
buildMetadataSources( ProxyInterfaceEntity.class );
EntityBinding binding = getEntityBinding( ProxyInterfaceEntity.class );
assertTrue( "Wrong laziness", binding.isLazy() );
assertEquals(
"Wrong proxy interface",
"org.hibernate.metamodel.source.annotations.entity.ProxyBindingTests$ProxyInterfaceEntity",
binding.getProxyInterfaceName()
);
}
@Entity
class NoProxyEntity {
@Id
private int id;
}
@Entity
@Proxy
class ProxiedEntity {
@Id
private int id;
}
@Entity
@Proxy(lazy = false)
class ProxyDisabledEntity {
@Id
private int id;
}
@Entity
@Proxy(proxyClass = ProxyInterfaceEntity.class)
class ProxyInterfaceEntity {
@Id
private int id;
}
}