~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/org/hibernate/test/extralazy/ExtraLazyTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: ExtraLazyTest.java 10976 2006-12-12 23:22:26Z steve.ebersole@jboss.com $
 
2
package org.hibernate.test.extralazy;
 
3
 
 
4
import java.util.List;
 
5
import java.util.Map;
 
6
 
 
7
import junit.framework.Test;
 
8
 
 
9
import org.hibernate.Hibernate;
 
10
import org.hibernate.Session;
 
11
import org.hibernate.Transaction;
 
12
import org.hibernate.junit.functional.FunctionalTestCase;
 
13
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
 
14
 
 
15
/**
 
16
 * @author Gavin King
 
17
 */
 
18
public class ExtraLazyTest extends FunctionalTestCase {
 
19
 
 
20
        public ExtraLazyTest(String str) {
 
21
                super(str);
 
22
        }
 
23
 
 
24
        public String[] getMappings() {
 
25
                return new String[] { "extralazy/UserGroup.hbm.xml" };
 
26
        }
 
27
 
 
28
        public static Test suite() {
 
29
                return new FunctionalTestClassTestSuite( ExtraLazyTest.class );
 
30
        }
 
31
 
 
32
        public void testOrphanDelete() {
 
33
                Session s = openSession();
 
34
                Transaction t = s.beginTransaction();
 
35
                User gavin = new User("gavin", "secret");
 
36
                Document hia = new Document("HiA", "blah blah blah", gavin);
 
37
                Document hia2 = new Document("HiA2", "blah blah blah blah", gavin);
 
38
                s.persist(gavin);
 
39
                t.commit();
 
40
                s.close();
 
41
                
 
42
                s = openSession();
 
43
                t = s.beginTransaction();
 
44
                gavin = (User) s.get(User.class, "gavin");
 
45
                assertEquals( 2, gavin.getDocuments().size() );
 
46
                gavin.getDocuments().remove(hia2);
 
47
                assertFalse( gavin.getDocuments().contains(hia2) );
 
48
                assertTrue( gavin.getDocuments().contains(hia) );
 
49
                assertEquals( 1, gavin.getDocuments().size() );
 
50
                assertFalse( Hibernate.isInitialized( gavin.getDocuments() ) );
 
51
                t.commit();
 
52
                s.close();
 
53
 
 
54
                s = openSession();
 
55
                t = s.beginTransaction();
 
56
                gavin = (User) s.get(User.class, "gavin");
 
57
                assertEquals( 1, gavin.getDocuments().size() );
 
58
                assertFalse( gavin.getDocuments().contains(hia2) );
 
59
                assertTrue( gavin.getDocuments().contains(hia) );
 
60
                assertFalse( Hibernate.isInitialized( gavin.getDocuments() ) );
 
61
                assertNull( s.get(Document.class, "HiA2") );
 
62
                gavin.getDocuments().clear();
 
63
                assertTrue( Hibernate.isInitialized( gavin.getDocuments() ) );
 
64
                s.delete(gavin);
 
65
                t.commit();
 
66
                s.close();
 
67
        }
 
68
        
 
69
        public void testGet() {
 
70
                Session s = openSession();
 
71
                Transaction t = s.beginTransaction();
 
72
                User gavin = new User("gavin", "secret");
 
73
                User turin = new User("turin", "tiger");
 
74
                Group g = new Group("developers");
 
75
                g.getUsers().put("gavin", gavin);
 
76
                g.getUsers().put("turin", turin);
 
77
                s.persist(g);
 
78
                gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") );
 
79
                gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") );
 
80
                t.commit();
 
81
                s.close();
 
82
 
 
83
                s = openSession();
 
84
                t = s.beginTransaction();
 
85
                g = (Group) s.get(Group.class, "developers");
 
86
                gavin = (User) g.getUsers().get("gavin");
 
87
                turin = (User) g.getUsers().get("turin");
 
88
                assertNotNull(gavin);
 
89
                assertNotNull(turin);
 
90
                assertNull( g.getUsers().get("emmanuel") );
 
91
                assertFalse( Hibernate.isInitialized( g.getUsers() ) );
 
92
                assertNotNull( gavin.getSession().get("foo") );
 
93
                assertNull( turin.getSession().get("foo") );
 
94
                assertFalse( Hibernate.isInitialized( gavin.getSession() ) );
 
95
                assertFalse( Hibernate.isInitialized( turin.getSession() ) );
 
96
                s.delete(gavin);
 
97
                s.delete(turin);
 
98
                s.delete(g);
 
99
                t.commit();
 
100
                s.close();
 
101
        }
 
102
        
 
103
        public void testRemoveClear() {
 
104
                Session s = openSession();
 
105
                Transaction t = s.beginTransaction();
 
106
                User gavin = new User("gavin", "secret");
 
107
                User turin = new User("turin", "tiger");
 
108
                Group g = new Group("developers");
 
109
                g.getUsers().put("gavin", gavin);
 
110
                g.getUsers().put("turin", turin);
 
111
                s.persist(g);
 
112
                gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") );
 
113
                gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") );
 
114
                t.commit();
 
115
                s.close();
 
116
 
 
117
                s = openSession();
 
118
                t = s.beginTransaction();
 
119
                g = (Group) s.get(Group.class, "developers");
 
120
                gavin = (User) g.getUsers().get("gavin");
 
121
                turin = (User) g.getUsers().get("turin");
 
122
                assertFalse( Hibernate.isInitialized( g.getUsers() ) );
 
123
                g.getUsers().clear();
 
124
                gavin.getSession().remove("foo");
 
125
                assertTrue( Hibernate.isInitialized( g.getUsers() ) );
 
126
                assertTrue( Hibernate.isInitialized( gavin.getSession() ) );
 
127
                t.commit();
 
128
                s.close();
 
129
 
 
130
                s = openSession();
 
131
                t = s.beginTransaction();
 
132
                g = (Group) s.get(Group.class, "developers");
 
133
                assertTrue( g.getUsers().isEmpty() );
 
134
                assertFalse( Hibernate.isInitialized( g.getUsers() ) );
 
135
                gavin = (User) s.get(User.class, "gavin");
 
136
                assertFalse( gavin.getSession().containsKey("foo") );
 
137
                assertFalse( Hibernate.isInitialized( gavin.getSession() ) );
 
138
                s.delete(gavin);
 
139
                s.delete(turin);
 
140
                s.delete(g);
 
141
                t.commit();
 
142
                s.close();
 
143
        }
 
144
        
 
145
        public void testIndexFormulaMap() {
 
146
                Session s = openSession();
 
147
                Transaction t = s.beginTransaction();
 
148
                User gavin = new User("gavin", "secret");
 
149
                User turin = new User("turin", "tiger");
 
150
                Group g = new Group("developers");
 
151
                g.getUsers().put("gavin", gavin);
 
152
                g.getUsers().put("turin", turin);
 
153
                s.persist(g);
 
154
                gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") );
 
155
                gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") );
 
156
                t.commit();
 
157
                s.close();
 
158
                
 
159
                s = openSession();
 
160
                t = s.beginTransaction();
 
161
                g = (Group) s.get(Group.class, "developers");
 
162
                assertEquals( g.getUsers().size(), 2 );
 
163
                g.getUsers().remove("turin");
 
164
                Map smap = ( (User) g.getUsers().get("gavin") ).getSession();
 
165
                assertEquals(smap.size(), 2);
 
166
                smap.remove("bar");
 
167
                t.commit();
 
168
                s.close();
 
169
 
 
170
                s = openSession();
 
171
                t = s.beginTransaction();
 
172
                g = (Group) s.get(Group.class, "developers");
 
173
                assertEquals( g.getUsers().size(), 1 );
 
174
                smap = ( (User) g.getUsers().get("gavin") ).getSession();
 
175
                assertEquals(smap.size(), 1);
 
176
                gavin = (User) g.getUsers().put("gavin", turin);
 
177
                s.delete(gavin);
 
178
                assertEquals( s.createQuery("select count(*) from SessionAttribute").uniqueResult(), new Long(0) );
 
179
                t.commit();
 
180
                s.close();
 
181
 
 
182
                s = openSession();
 
183
                t = s.beginTransaction();
 
184
                g = (Group) s.get(Group.class, "developers");
 
185
                assertEquals( g.getUsers().size(), 1 );
 
186
                turin = (User) g.getUsers().get("turin");
 
187
                smap = turin.getSession();
 
188
                assertEquals(smap.size(), 0);
 
189
                assertEquals( s.createQuery("select count(*) from User").uniqueResult(), new Long(1) );
 
190
                s.delete(g);
 
191
                s.delete(turin);
 
192
                assertEquals( s.createQuery("select count(*) from User").uniqueResult(), new Long(0) );
 
193
                t.commit();
 
194
                s.close();
 
195
        }
 
196
        
 
197
        public void testSQLQuery() {
 
198
                Session s = openSession();
 
199
                Transaction t = s.beginTransaction();
 
200
                User gavin = new User("gavin", "secret");
 
201
                User turin = new User("turin", "tiger");
 
202
                gavin.getSession().put( "foo", new SessionAttribute("foo", "foo bar baz") );
 
203
                gavin.getSession().put( "bar", new SessionAttribute("bar", "foo bar baz 2") );
 
204
                s.persist(gavin);
 
205
                s.persist(turin);
 
206
                s.flush();
 
207
                s.clear();
 
208
                List results = s.getNamedQuery("userSessionData").setParameter("uname", "%in").list();
 
209
                assertEquals( results.size(), 2 );
 
210
                gavin = (User) ( (Object[]) results.get(0) )[0];
 
211
                assertEquals( gavin.getName(), "gavin" );
 
212
                assertEquals( gavin.getSession().size(), 2 );
 
213
                s.createQuery("delete SessionAttribute").executeUpdate();
 
214
                s.createQuery("delete User").executeUpdate();
 
215
                t.commit();
 
216
                s.close();
 
217
                
 
218
        }
 
219
 
 
220
}
 
221