Skip to content

Commit 651c563

Browse files
author
graeme
committed
added test for circular relationships
git-svn-id: https://svn.codehaus.org/grails/trunk@830 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent 09f1737 commit 651c563

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.codehaus.groovy.grails.domain
2+
class CircularRelationship {
3+
@Property Long id
4+
@Property Long version
5+
6+
@Property relatesToMany = [children:CircularRelationship]
7+
8+
@Property CircularRelationship parent
9+
@Property Set children
10+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* Copyright 2004-2005 the original author or authors.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.codehaus.groovy.grails.orm.hibernate;
16+
17+
18+
import groovy.lang.GroovyClassLoader;
19+
import groovy.lang.GroovyObject;
20+
21+
import java.util.HashSet;
22+
import java.util.Properties;
23+
import java.util.Set;
24+
25+
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication;
26+
import org.codehaus.groovy.grails.commons.GrailsApplication;
27+
import org.codehaus.groovy.grails.commons.GrailsDomainClass;
28+
import org.codehaus.groovy.grails.orm.hibernate.cfg.DefaultGrailsDomainConfiguration;
29+
import org.hibernate.Session;
30+
import org.hibernate.SessionFactory;
31+
import org.springframework.orm.hibernate3.SessionFactoryUtils;
32+
import org.springframework.orm.hibernate3.SessionHolder;
33+
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
34+
import org.springframework.transaction.support.TransactionSynchronizationManager;
35+
36+
/**
37+
* @author Graeme Rocher
38+
* @since 08-Jul-2005
39+
*/
40+
public class CircularRelationshipTests extends AbstractDependencyInjectionSpringContextTests {
41+
GroovyClassLoader cl = new GroovyClassLoader();
42+
43+
44+
protected GrailsApplication grailsApplication = null;
45+
protected SessionFactory sessionFactory = null;
46+
47+
48+
protected String[] getConfigLocations() {
49+
return new String[] { "org/codehaus/groovy/grails/orm/hibernate/grails-hibernate-configuration-tests.xml" };
50+
}
51+
52+
53+
protected void onSetUp() throws Exception {
54+
Thread.currentThread().setContextClassLoader(cl);
55+
cl.loadClass("org.codehaus.groovy.grails.domain.CircularRelationship");
56+
Class[] loadedClasses = cl.getLoadedClasses();
57+
grailsApplication = new DefaultGrailsApplication(loadedClasses,cl);
58+
59+
60+
DefaultGrailsDomainConfiguration config = new DefaultGrailsDomainConfiguration();
61+
config.setGrailsApplication(this.grailsApplication);
62+
Properties props = new Properties();
63+
props.put("hibernate.connection.username","sa");
64+
props.put("hibernate.connection.password","");
65+
props.put("hibernate.connection.url","jdbc:hsqldb:mem:grailsDB");
66+
props.put("hibernate.connection.driver_class","org.hsqldb.jdbcDriver");
67+
props.put("hibernate.dialect","org.hibernate.dialect.HSQLDialect");
68+
props.put("hibernate.hbm2ddl.auto","create-drop");
69+
70+
//props.put("hibernate.hbm2ddl.auto","update");
71+
config.setProperties(props);
72+
//originalClassLoader = Thread.currentThread().getContextClassLoader();
73+
Thread.currentThread().setContextClassLoader(this.cl);
74+
this.sessionFactory = config.buildSessionFactory();
75+
76+
77+
78+
if(!TransactionSynchronizationManager.hasResource(this.sessionFactory)) {
79+
Session hibSession = this.sessionFactory.openSession();
80+
TransactionSynchronizationManager.bindResource(this.sessionFactory, new SessionHolder(hibSession));
81+
}
82+
83+
}
84+
85+
86+
/* (non-Javadoc)
87+
* @see org.springframework.test.AbstractDependencyInjectionSpringContextTests#onTearDown()
88+
*/
89+
protected void onTearDown() throws Exception {
90+
if(TransactionSynchronizationManager.hasResource(this.sessionFactory)) {
91+
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(this.sessionFactory);
92+
Session s = holder.getSession();
93+
s.flush();
94+
TransactionSynchronizationManager.unbindResource(this.sessionFactory);
95+
SessionFactoryUtils.releaseSession(s, this.sessionFactory);
96+
}
97+
}
98+
99+
100+
public void testHibernateConfiguration() throws Exception {
101+
assertNotNull(this.sessionFactory);
102+
103+
GrailsDomainClass[] domainClasses = grailsApplication.getGrailsDomainClasses();
104+
assertEquals(1,domainClasses.length);
105+
106+
107+
//HibernateTemplate template = new HibernateTemplate(this.sessionFactory);
108+
GroovyObject obj = (GroovyObject)grailsApplication.getGrailsDomainClass("org.codehaus.groovy.grails.domain.CircularRelationship").newInstance();
109+
GroovyObject child = (GroovyObject)grailsApplication.getGrailsDomainClass("org.codehaus.groovy.grails.domain.CircularRelationship").newInstance();
110+
assertNotNull(obj);
111+
112+
child.setProperty("parent",obj);
113+
Set children = new HashSet();
114+
children.add(child);
115+
obj.setProperty("children",children);
116+
obj.invokeMethod("save",new Object[0]);
117+
118+
119+
120+
obj = (GroovyObject)obj.getMetaClass().invokeStaticMethod(obj,"get",new Object[] { new Long(1) });
121+
assertNotNull(obj.getProperty("children"));
122+
assertEquals(1,((Set)obj.getProperty("children")).size());
123+
}
124+
125+
}

0 commit comments

Comments
 (0)