KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > proxy > ProxyTest


1 //$Id: ProxyTest.java,v 1.7 2005/04/24 00:18:27 oneovthafew Exp $
2
package org.hibernate.test.proxy;
3
4 import java.math.BigDecimal JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.LockMode;
11 import org.hibernate.Session;
12 import org.hibernate.Transaction;
13 import org.hibernate.proxy.HibernateProxy;
14 import org.hibernate.test.TestCase;
15 import org.hibernate.util.SerializationHelper;
16
17 /**
18  * @author Gavin King
19  */

20 public class ProxyTest extends TestCase {
21     
22     public ProxyTest(String JavaDoc str) {
23         super(str);
24     }
25
26     public void testProxySerialization() {
27         Session s = openSession();
28         Transaction t = s.beginTransaction();
29         DataPoint dp = new DataPoint();
30         dp.setDescription("a data point");
31         dp.setX( new BigDecimal JavaDoc(1.0) );
32         dp.setY( new BigDecimal JavaDoc(2.0) );
33         s.persist(dp);
34         s.flush();
35         s.clear();
36
37         dp = (DataPoint) s.load( DataPoint.class, new Long JavaDoc( dp.getId() ) );
38         assertFalse( Hibernate.isInitialized(dp) );
39         dp.getId();
40         assertFalse( Hibernate.isInitialized(dp) );
41         dp.getDescription();
42         assertTrue( Hibernate.isInitialized(dp) );
43         Object JavaDoc none = s.load( DataPoint.class, new Long JavaDoc(666));
44         assertFalse( Hibernate.isInitialized(none) );
45         
46         t.commit();
47         s.disconnect();
48         
49         Object JavaDoc[] holder = new Object JavaDoc[] { s, dp, none };
50         
51         holder = (Object JavaDoc[]) SerializationHelper.clone(holder);
52         Session sclone = (Session) holder[0];
53         dp = (DataPoint) holder[1];
54         none = holder[2];
55         
56         //close the original:
57
s.close();
58         
59         sclone.reconnect();
60         t = sclone.beginTransaction();
61         
62         DataPoint sdp = (DataPoint) sclone.load( DataPoint.class, new Long JavaDoc( dp.getId() ) );
63         assertSame(dp, sdp);
64         assertFalse(sdp instanceof HibernateProxy);
65         Object JavaDoc snone = sclone.load( DataPoint.class, new Long JavaDoc(666) );
66         assertSame(none, snone);
67         assertTrue(snone instanceof HibernateProxy);
68         
69         sclone.delete(dp);
70         
71         t.commit();
72         sclone.close();
73         
74     }
75     
76     public void testProxy() {
77         Session s = openSession();
78         Transaction t = s.beginTransaction();
79         DataPoint dp = new DataPoint();
80         dp.setDescription("a data point");
81         dp.setX( new BigDecimal JavaDoc(1.0) );
82         dp.setY( new BigDecimal JavaDoc(2.0) );
83         s.persist(dp);
84         s.flush();
85         s.clear();
86
87         dp = (DataPoint) s.load( DataPoint.class, new Long JavaDoc(dp.getId() ));
88         assertFalse( Hibernate.isInitialized(dp) );
89         DataPoint dp2 = (DataPoint) s.get( DataPoint.class, new Long JavaDoc(dp.getId()) );
90         assertSame(dp, dp2);
91         assertTrue( Hibernate.isInitialized(dp) );
92         s.clear();
93
94         dp = (DataPoint) s.load( DataPoint.class, new Long JavaDoc( dp.getId() ) );
95         assertFalse( Hibernate.isInitialized(dp) );
96         dp2 = (DataPoint) s.load( DataPoint.class, new Long JavaDoc( dp.getId() ), LockMode.NONE );
97         assertSame(dp, dp2);
98         assertFalse( Hibernate.isInitialized(dp) );
99         s.clear();
100
101         dp = (DataPoint) s.load( DataPoint.class, new Long JavaDoc( dp.getId() ) );
102         assertFalse( Hibernate.isInitialized(dp) );
103         dp2 = (DataPoint) s.load( DataPoint.class, new Long JavaDoc( dp.getId() ), LockMode.READ );
104         assertSame(dp, dp2);
105         assertTrue( Hibernate.isInitialized(dp) );
106         s.clear();
107
108         dp = (DataPoint) s.load( DataPoint.class, new Long JavaDoc (dp.getId() ));
109         assertFalse( Hibernate.isInitialized(dp) );
110         dp2 = (DataPoint) s.get( DataPoint.class, new Long JavaDoc ( dp.getId() ) , LockMode.READ );
111         assertSame(dp, dp2);
112         assertTrue( Hibernate.isInitialized(dp) );
113         s.clear();
114
115         dp = (DataPoint) s.load( DataPoint.class, new Long JavaDoc ( dp.getId() ) );
116         assertFalse( Hibernate.isInitialized(dp) );
117         dp2 = (DataPoint) s.createQuery("from DataPoint").uniqueResult();
118         assertSame(dp, dp2);
119         assertTrue( Hibernate.isInitialized(dp) );
120         t.commit();
121         s.close();
122     }
123
124     
125     protected String JavaDoc[] getMappings() {
126         return new String JavaDoc[] { "proxy/DataPoint.hbm.xml" };
127     }
128
129     public static Test suite() {
130         return new TestSuite(ProxyTest.class);
131     }
132
133     public String JavaDoc getCacheConcurrencyStrategy() {
134         return null;
135     }
136
137 }
138
139
Popular Tags