Skip to content

Commit fa8120a

Browse files
committed
Cleanup tests
1 parent d13072c commit fa8120a

15 files changed

+182
-47
lines changed

grace-datastore-gorm-hibernate/src/main/groovy/org/grails/orm/hibernate/proxy/HibernateProxyHandler.java

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,153 @@
1515
*/
1616
package org.grails.orm.hibernate.proxy;
1717

18+
import java.io.Serializable;
19+
20+
import org.hibernate.Hibernate;
1821
import org.hibernate.collection.spi.PersistentCollection;
22+
import org.hibernate.proxy.HibernateProxy;
23+
import org.hibernate.proxy.HibernateProxyHelper;
24+
25+
import org.grails.datastore.mapping.core.Session;
26+
import org.grails.datastore.mapping.engine.AssociationQueryExecutor;
27+
import org.grails.datastore.mapping.proxy.ProxyFactory;
28+
import org.grails.datastore.mapping.proxy.ProxyHandler;
29+
import org.grails.datastore.mapping.reflect.ClassPropertyFetcher;
1930

2031
/**
21-
* Implementation of the ProxyHandler interface for Hibernate.
32+
* Implementation of the ProxyHandler interface for Hibernate using org.hibernate.Hibernate
33+
* and HibernateProxyHelper where possible.
2234
*
2335
* @author Graeme Rocher
2436
* @since 1.2.2
2537
*/
26-
public class HibernateProxyHandler extends SimpleHibernateProxyHandler {
38+
public class HibernateProxyHandler implements ProxyHandler, ProxyFactory {
2739

40+
/**
41+
* Check if the proxy or persistent collection is initialized.
42+
* {@inheritDoc}
43+
*/
44+
@Override
2845
public boolean isInitialized(Object o) {
29-
if (o instanceof PersistentCollection) {
30-
return ((PersistentCollection) o).wasInitialized();
46+
return Hibernate.isInitialized(o);
47+
}
48+
49+
/**
50+
* Check if an association proxy or persistent collection is initialized.
51+
* {@inheritDoc}
52+
*/
53+
@Override
54+
public boolean isInitialized(Object obj, String associationName) {
55+
try {
56+
Object proxy = ClassPropertyFetcher.getInstancePropertyValue(obj, associationName);
57+
return isInitialized(proxy);
58+
}
59+
catch (RuntimeException e) {
60+
return false;
3161
}
62+
}
3263

33-
return super.isInitialized(o);
64+
/**
65+
* Unproxies a HibernateProxy. If the proxy is uninitialized, it automatically triggers an initialization.
66+
* In case the supplied object is null or not a proxy, the object will be returned as-is.
67+
* {@inheritDoc}
68+
* @see Hibernate#unproxy
69+
*/
70+
@Override
71+
public Object unwrap(Object object) {
72+
if (object instanceof PersistentCollection) {
73+
initialize(object);
74+
return object;
75+
}
76+
return Hibernate.unproxy(object);
3477
}
3578

36-
public Object unwrapIfProxy(Object instance) {
37-
if (instance instanceof PersistentCollection) {
38-
initialize(instance);
39-
return instance;
79+
/**
80+
* {@inheritDoc}
81+
* @see org.hibernate.proxy.AbstractLazyInitializer#getIdentifier
82+
*/
83+
@Override
84+
public Serializable getIdentifier(Object o) {
85+
if (o instanceof HibernateProxy) {
86+
return ((HibernateProxy) o).getHibernateLazyInitializer().getIdentifier();
87+
}
88+
else {
89+
//TODO seems we can get the id here if its has normal getId
90+
// PersistentEntity persistentEntity = GormEnhancer.findStaticApi(o.getClass()).getGormPersistentEntity();
91+
// return persistentEntity.getMappingContext().getEntityReflector(persistentEntity).getIdentifier(o);
92+
return null;
4093
}
94+
}
95+
96+
/**
97+
* {@inheritDoc}
98+
* @see HibernateProxyHelper#getClassWithoutInitializingProxy
99+
*/
100+
@Override
101+
public Class<?> getProxiedClass(Object o) {
102+
return HibernateProxyHelper.getClassWithoutInitializingProxy(o);
103+
}
41104

42-
return super.unwrapIfProxy(instance);
105+
/**
106+
* calls unwrap which calls unproxy
107+
* @see #unwrap(Object)
108+
* @deprecated use unwrap
109+
*/
110+
@Deprecated
111+
public Object unwrapIfProxy(Object instance) {
112+
return unwrap(instance);
43113
}
44114

115+
/**
116+
* {@inheritDoc}
117+
*/
118+
@Override
45119
public boolean isProxy(Object o) {
46-
return super.isProxy(o) || (o instanceof PersistentCollection);
120+
return (o instanceof HibernateProxy) || (o instanceof PersistentCollection);
47121
}
48122

123+
/**
124+
* Force initialization of a proxy or persistent collection.
125+
* {@inheritDoc}
126+
*/
127+
@Override
49128
public void initialize(Object o) {
50-
if (o instanceof PersistentCollection) {
51-
final PersistentCollection col = (PersistentCollection) o;
52-
if (!col.wasInitialized()) {
53-
col.forceInitialization();
129+
Hibernate.initialize(o);
130+
}
131+
132+
@Override
133+
public <T> T createProxy(Session session, Class<T> type, Serializable key) {
134+
throw new UnsupportedOperationException("createProxy not supported in HibernateProxyHandler");
135+
}
136+
137+
@Override
138+
public <T, K extends Serializable> T createProxy(Session session, AssociationQueryExecutor<K, T> executor, K associationKey) {
139+
throw new UnsupportedOperationException("createProxy not supported in HibernateProxyHandler");
140+
}
141+
142+
/**
143+
* @deprecated use unwrap
144+
*/
145+
@Deprecated
146+
public Object unwrapProxy(Object proxy) {
147+
return unwrap(proxy);
148+
}
149+
150+
/**
151+
* returns the proxy for an association. returns null if its not a proxy.
152+
* Note: Only used in a test. Deprecate?
153+
*/
154+
public HibernateProxy getAssociationProxy(Object obj, String associationName) {
155+
try {
156+
Object proxy = ClassPropertyFetcher.getInstancePropertyValue(obj, associationName);
157+
if (proxy instanceof HibernateProxy) {
158+
return (HibernateProxy) proxy;
54159
}
160+
return null;
161+
}
162+
catch (RuntimeException e) {
163+
return null;
55164
}
56-
super.initialize(o);
57165
}
58166

59167
}

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/hibernate/mapping/HibernateMappingBuilderTests.groovy

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue
3636
*/
3737
class HibernateMappingBuilderTests {
3838

39-
// void testWildcardApplyToAllProperties() {
40-
// def builder = new HibernateMappingBuilder("Foo")
41-
// def mapping = builder.evaluate {
42-
// '*'(column:"foo")
43-
// '*-1'(column:"foo")
44-
// '1-1'(column:"foo")
45-
// '1-*'(column:"foo")
46-
// '*-*'(column:"foo")
47-
// one cache:true
48-
// two ignoreNoteFound:false
49-
// }
50-
// }
51-
5239
@Test
5340
void testIncludes() {
5441
def callable = {

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/hibernate/mapping/MappingBuilderSpec.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class MappingBuilderSpec extends Specification {
106106
Mapping mapping = define {
107107
sort('foo', 'desc')
108108
}.build()
109+
109110
then:
110111
mapping.sort.name == 'foo'
111112
mapping.sort.direction == 'desc'
@@ -174,6 +175,7 @@ class MappingBuilderSpec extends Specification {
174175
}
175176
}.build()
176177
PropertyConfig pc = mapping.getPropertyConfig(GormProperties.VERSION)
178+
177179
expect:
178180
pc != null
179181
pc.columns.size() == 1

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/CountByWithEmbeddedSpec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CountByWithEmbeddedSpec extends GormSpec {
3131
given:
3232
new CountByPerson(name: 'Fred', bornInCountry: new CountByCountry(name: 'England')).save(flush: true)
3333
new CountByPerson(bornInCountry: new CountByCountry(name: 'Scotland')).save(flush: true)
34+
3435
expect:
3536
CountByPerson.countByNameIsNotNull() == 1
3637
}

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/DetachedCriteriaJoinSpec.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class DetachedCriteriaJoinSpec extends GormSpec {
5656

5757
DynamicFinder.applyDetachedCriteria(query, dc)
5858
def joinType = query.hibernateCriteria.subcriteriaList.first().joinType
59+
5960
expect:
6061
joinType == org.hibernate.sql.JoinType.INNER_JOIN
6162
}
@@ -70,6 +71,7 @@ class DetachedCriteriaJoinSpec extends GormSpec {
7071

7172
DynamicFinder.applyDetachedCriteria(query, dc)
7273
def joinType = query.hibernateCriteria.subcriteriaList.first().joinType
74+
7375
expect:
7476
joinType == org.hibernate.sql.JoinType.LEFT_OUTER_JOIN
7577
}
@@ -84,6 +86,7 @@ class DetachedCriteriaJoinSpec extends GormSpec {
8486

8587
DynamicFinder.applyDetachedCriteria(query, dc)
8688
def joinType = query.hibernateCriteria.subcriteriaList.first().joinType
89+
8790
expect:
8891
joinType == org.hibernate.sql.JoinType.RIGHT_OUTER_JOIN
8992
}

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/DetachedCriteriaProjectionSpec.groovy

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ class DetachedCriteriaProjectionSpec extends Specification {
6868
}
6969
eq 'field', 'abc'
7070
}
71+
7172
when:
7273
// will fail
7374
def results = Entity1.withCriteria {
7475
inList 'id', detachedCriteria
7576
}
77+
7678
then:
7779
results.size() == 1
7880
}
@@ -84,6 +86,7 @@ class DetachedCriteriaProjectionSpec extends Specification {
8486
eq 'field', 'abc'
8587
}
8688
detachedCriteria.projections << new Query.DistinctPropertyProjection('entityId')
89+
8790
expect:
8891
assert Entity1.withCriteria {
8992
inList 'id', detachedCriteria
@@ -100,10 +103,12 @@ class DetachedCriteriaProjectionSpec extends Specification {
100103
}
101104
}
102105
detachedCriteria.projections << new Query.DistinctPropertyProjection('entityId')
106+
103107
when:
104108
def results = Entity1.withCriteria {
105109
inList 'id', detachedCriteria
106110
}
111+
107112
then:
108113
results.size() == 1
109114
}
@@ -122,7 +127,7 @@ class Entity1 {
122127
@Entity
123128
class Entity2 {
124129

125-
static belongsTo = { parent: Entity1 }
130+
static belongsTo = [parent: Entity1]
126131

127132
String field
128133

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/SubclassMultipleListCollectionSpec.groovy

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@ import org.grails.orm.hibernate.HibernateDatastore
3030
/**
3131
* Created by graemerocher on 01/03/2017.
3232
*/
33-
@Ignore('https://issues.apache.org/jira/browse/GROOVY-5106')
33+
@Ignore
3434
class SubclassMultipleListCollectionSpec extends Specification {
3535

36+
@Shared
37+
Map config = [
38+
'dataSource.url' : 'jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000',
39+
'dataSource.dbCreate': 'create-drop',
40+
'dataSource.dialect' : 'org.hibernate.dialect.H2Dialect'
41+
]
42+
3643
@AutoCleanup
3744
@Shared
3845
HibernateDatastore hibernateDatastore
@@ -41,14 +48,13 @@ class SubclassMultipleListCollectionSpec extends Specification {
4148
PlatformTransactionManager transactionManager
4249

4350
void setupSpec() {
44-
hibernateDatastore = new HibernateDatastore(
51+
hibernateDatastore = new HibernateDatastore(config,
4552
SuperProduct, Product, Iteration
4653
)
4754
transactionManager = hibernateDatastore.getTransactionManager()
4855
}
4956

50-
@Ignore
51-
// not yet implemented
57+
@Ignore('not yet implemented')
5258
@Rollback
5359
@Issue('https://github.com/grails/grails-data-mapping/issues/882')
5460
void 'test inheritance with multiple list collections'() {
@@ -82,7 +88,7 @@ class Product extends SuperProduct {
8288

8389
}
8490

85-
//@Entity
91+
@Entity
8692
class SuperProduct {
8793

8894
static constraints = {

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/ToOneProxySpec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ToOneProxySpec extends GormDatastoreSpec {
3333
session.flush()
3434

3535
def proxyHandler = new HibernateProxyHandler()
36+
3637
then: 'The association was not initialized'
3738
proxyHandler.getAssociationProxy(t, 'club') != null
3839
!proxyHandler.isInitialized(t, 'club')

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/UniqueConstraintHibernateSpec.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import org.grails.orm.hibernate.HibernateDatastore
2828

2929
/**
3030
* Tests the unique constraint
31-
*/
32-
/**
3331
*
3432
* NOTE: This test is disabled because in order for the test suite to run quickly we need to run each test in a transaction.
3533
* This makes it not possible to test the scenario outlined here, however tests for this use case exist in the hibernate plugin itself

grace-datastore-gorm-hibernate/src/test/groovy/grails/gorm/tests/compositeid/GlobalConstraintWithCompositeIdSpec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class GlobalConstraintWithCompositeIdSpec extends Specification {
7474
PersistentEntity entity = hibernateDatastore.mappingContext.getPersistentEntity(DomainB.name)
7575
PropertyConfig nameProp = entity.getPropertyByName('name').mapping.mappedForm
7676
PropertyConfig someOtherConfig = entity.getPropertyByName('someOther').mapping.mappedForm
77+
7778
expect:
7879
nameProp.unique
7980
someOtherConfig.unique

0 commit comments

Comments
 (0)