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