KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > instrument > InstrumentTest


1 //$Id: InstrumentTest.java,v 1.17 2005/07/10 16:51:17 oneovthafew Exp $
2
package org.hibernate.test.instrument;
3
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import junit.framework.Test;
9 import junit.framework.TestSuite;
10
11 import net.sf.cglib.transform.impl.InterceptFieldEnabled;
12
13 import org.hibernate.Hibernate;
14 import org.hibernate.LockMode;
15 import org.hibernate.Session;
16 import org.hibernate.Transaction;
17 import org.hibernate.intercept.FieldInterceptor;
18 import org.hibernate.test.TestCase;
19
20 /**
21  * @author Gavin King
22  */

23 public class InstrumentTest extends TestCase {
24     
25     public InstrumentTest(String JavaDoc str) {
26         super(str);
27     }
28     
29     public void testDirtyCheck() {
30         Session s = openSession();
31         Transaction t = s.beginTransaction();
32         Folder pics = new Folder();
33         pics.setName("pics");
34         Folder docs = new Folder();
35         docs.setName("docs");
36         s.persist(docs);
37         s.persist(pics);
38         t.commit();
39         s.close();
40         
41         s = openSession();
42         t = s.beginTransaction();
43         List JavaDoc list = s.createCriteria(Folder.class).list();
44         for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
45             Folder f = (Folder) iter.next();
46             assertFalse( f.nameWasread );
47         }
48         t.commit();
49         s.close();
50         
51         for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
52             Folder f = (Folder) iter.next();
53             assertFalse( f.nameWasread );
54         }
55
56         s = openSession();
57         t = s.beginTransaction();
58         s.createQuery("delete from Folder").executeUpdate();
59         t.commit();
60         s.close();
61     }
62
63     public void testFetchAll() throws Exception JavaDoc {
64         Session s = openSession();
65         Owner o = new Owner();
66         Document doc = new Document();
67         Folder fol = new Folder();
68         o.setName("gavin");
69         doc.setName("Hibernate in Action");
70         doc.setSummary("blah");
71         doc.updateText("blah blah");
72         fol.setName("books");
73         doc.setOwner(o);
74         doc.setFolder(fol);
75         fol.getDocuments().add(doc);
76         s.persist(o);
77         s.persist(fol);
78         s.flush();
79         s.clear();
80         doc = (Document) s.createQuery("from Document fetch all properties").uniqueResult();
81         assertTrue( Hibernate.isPropertyInitialized( doc, "summary" ) );
82         assertTrue( Hibernate.isPropertyInitialized( doc, "upperCaseName" ) );
83         assertTrue( Hibernate.isPropertyInitialized( doc, "owner" ) );
84         assertEquals( doc.getSummary(), "blah" );
85         s.delete(doc);
86         s.delete( doc.getOwner() );
87         s.delete( doc.getFolder() );
88         s.flush();
89         s.connection().commit();
90         s.close();
91     }
92     
93     public void testLazy() throws Exception JavaDoc {
94         Session s = openSession();
95         Owner o = new Owner();
96         Document doc = new Document();
97         Folder fol = new Folder();
98         o.setName("gavin");
99         doc.setName("Hibernate in Action");
100         doc.setSummary("blah");
101         doc.updateText("blah blah");
102         fol.setName("books");
103         doc.setOwner(o);
104         doc.setFolder(fol);
105         fol.getDocuments().add(doc);
106         s.save(o);
107         s.save(fol);
108         s.flush();
109         s.connection().commit();
110         s.close();
111         
112         getSessions().evict(Document.class);
113         
114         s = openSession();
115         doc = (Document) s.createQuery("from Document").uniqueResult();
116         doc.getName();
117         assertEquals( doc.getText(), "blah blah" );
118         s.connection().commit();
119         s.close();
120
121         s = openSession();
122         doc = (Document) s.createQuery("from Document").uniqueResult();
123         doc.getName();
124         assertFalse(Hibernate.isPropertyInitialized(doc, "text"));
125         assertFalse(Hibernate.isPropertyInitialized(doc, "summary"));
126         assertEquals( doc.getText(), "blah blah" );
127         assertTrue(Hibernate.isPropertyInitialized(doc, "text"));
128         assertTrue(Hibernate.isPropertyInitialized(doc, "summary"));
129         s.connection().commit();
130         s.close();
131
132         s = openSession();
133         doc = (Document) s.createQuery("from Document").uniqueResult();
134         doc.setName("HiA");
135         s.flush();
136         s.connection().commit();
137         s.close();
138
139         s = openSession();
140         doc = (Document) s.createQuery("from Document").uniqueResult();
141         assertEquals( doc.getName(), "HiA" );
142         assertEquals( doc.getText(), "blah blah" );
143         s.connection().commit();
144         s.close();
145
146         s = openSession();
147         doc = (Document) s.createQuery("from Document").uniqueResult();
148         doc.getText();
149         doc.setName("HiA second edition");
150         s.flush();
151         s.connection().commit();
152         s.close();
153
154         s = openSession();
155         doc = (Document) s.createQuery("from Document").uniqueResult();
156         assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty"));
157         assertTrue(Hibernate.isPropertyInitialized(doc, "name"));
158         assertFalse(Hibernate.isPropertyInitialized(doc, "text"));
159         assertFalse(Hibernate.isPropertyInitialized(doc, "upperCaseName"));
160         //assertFalse(Hibernate.isPropertyInitialized(doc, "owner"));
161
assertEquals( doc.getName(), "HiA second edition" );
162         assertEquals( doc.getText(), "blah blah" );
163         assertEquals( doc.getUpperCaseName(), "HIA SECOND EDITION" );
164         assertTrue(Hibernate.isPropertyInitialized(doc, "text"));
165         assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty"));
166         assertTrue(Hibernate.isPropertyInitialized(doc, "upperCaseName"));
167         s.connection().commit();
168         s.close();
169
170         s = openSession();
171         doc = (Document) s.createQuery("from Document").uniqueResult();
172         s.connection().commit();
173         s.close();
174         
175         assertFalse(Hibernate.isPropertyInitialized(doc, "text"));
176
177         s = openSession();
178         s.lock(doc, LockMode.NONE);
179         assertFalse(Hibernate.isPropertyInitialized(doc, "text"));
180         assertEquals( doc.getText(), "blah blah" );
181         assertTrue(Hibernate.isPropertyInitialized(doc, "text"));
182         s.connection().commit();
183         s.close();
184
185         s = openSession();
186         doc = (Document) s.createQuery("from Document").uniqueResult();
187         s.connection().commit();
188         s.close();
189         
190         doc.setName("HiA2");
191         
192         assertFalse(Hibernate.isPropertyInitialized(doc, "text"));
193
194         s = openSession();
195         s.saveOrUpdate(doc);
196         s.flush();
197         assertFalse(Hibernate.isPropertyInitialized(doc, "text"));
198         assertEquals( doc.getText(), "blah blah" );
199         assertTrue(Hibernate.isPropertyInitialized(doc, "text"));
200         doc.updateText("blah blah blah blah");
201         s.flush();
202         s.connection().commit();
203         s.close();
204
205         s = openSession();
206         doc = (Document) s.createQuery("from Document").uniqueResult();
207         assertEquals( doc.getName(), "HiA2" );
208         assertEquals( doc.getText(), "blah blah blah blah" );
209         s.connection().commit();
210         s.close();
211
212         s = openSession();
213         doc = (Document) s.load( Document.class, doc.getId() );
214         doc.getName();
215         assertFalse(Hibernate.isPropertyInitialized(doc, "text"));
216         assertFalse(Hibernate.isPropertyInitialized(doc, "summary"));
217         s.connection().commit();
218         s.close();
219
220         s = openSession();
221         doc = (Document) s.createQuery("from Document").uniqueResult();
222         //s.delete(doc);
223
s.delete( doc.getFolder() );
224         s.delete( doc.getOwner() );
225         s.flush();
226         s.connection().commit();
227         s.close();
228
229     }
230     
231     public void testLazyManyToOne() {
232         Session s = openSession();
233         Transaction t = s.beginTransaction();
234         Owner gavin = new Owner();
235         Document hia = new Document();
236         Folder fol = new Folder();
237         gavin.setName("gavin");
238         hia.setName("Hibernate in Action");
239         hia.setSummary("blah");
240         hia.updateText("blah blah");
241         fol.setName("books");
242         hia.setOwner(gavin);
243         hia.setFolder(fol);
244         fol.getDocuments().add(hia);
245         s.persist(gavin);
246         s.persist(fol);
247         t.commit();
248         s.close();
249         
250         s = openSession();
251         t = s.beginTransaction();
252         hia = (Document) s.createCriteria(Document.class).uniqueResult();
253         assertEquals( hia.getFolder().getClass(), Folder.class);
254         fol = hia.getFolder();
255         assertTrue( Hibernate.isInitialized(fol) );
256         t.commit();
257         s.close();
258
259         s = openSession();
260         t = s.beginTransaction();
261         hia = (Document) s.createCriteria(Document.class).uniqueResult();
262         assertSame( hia.getFolder(), s.load(Folder.class, fol.getId()) );
263         assertTrue( Hibernate.isInitialized( hia.getFolder() ) );
264         t.commit();
265         s.close();
266     
267         s = openSession();
268         t = s.beginTransaction();
269         fol = (Folder) s.get(Folder.class, fol.getId());
270         hia = (Document) s.createCriteria(Document.class).uniqueResult();
271         assertSame( fol, hia.getFolder() );
272         fol = hia.getFolder();
273         assertTrue( Hibernate.isInitialized(fol) );
274         t.commit();
275         s.close();
276         
277         s = openSession();
278         t = s.beginTransaction();
279         fol = (Folder) s.load(Folder.class, fol.getId());
280         hia = (Document) s.createCriteria(Document.class).uniqueResult();
281         assertNotSame( fol, hia.getFolder() );
282         fol = hia.getFolder();
283         assertTrue( Hibernate.isInitialized(fol) );
284         s.delete(hia.getFolder());
285         s.delete(hia.getOwner());
286         t.commit();
287         s.close();
288     }
289     
290     protected String JavaDoc[] getMappings() {
291         return new String JavaDoc[] { "instrument/Documents.hbm.xml" };
292     }
293     
294     public void testSetFieldInterceptor() {
295         Document doc = new Document();
296         FieldInterceptor.initFieldInterceptor(doc, "Document", null, new HashSet JavaDoc());
297         doc.getId();
298     }
299
300     public static Test suite() {
301         return new TestSuite(InstrumentTest.class);
302     }
303     
304     public static boolean isRunnable() {
305         return new Document() instanceof InterceptFieldEnabled;
306     }
307
308 }
309
310
Popular Tags