KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > connections > ConnectionManagementTestCase


1 // $Id: ConnectionManagementTestCase.java,v 1.3 2005/05/31 21:32:56 steveebersole Exp $
2
package org.hibernate.test.connections;
3
4 import org.hibernate.test.TestCase;
5 import org.hibernate.Session;
6 import org.hibernate.util.SerializationHelper;
7
8 /**
9  * Implementation of ConnectionManagementTestCase.
10  *
11  * @author Steve Ebersole
12  */

13 public abstract class ConnectionManagementTestCase extends TestCase {
14
15     public ConnectionManagementTestCase(String JavaDoc name) {
16         super( name );
17     }
18
19     protected final String JavaDoc[] getMappings() {
20         return new String JavaDoc[] { "connections/Silly.hbm.xml" };
21     }
22
23     protected void release(Session session) {
24         if ( session != null ) {
25             try {
26                 session.close();
27             }
28             catch( Throwable JavaDoc ignore ) {
29             }
30         }
31     }
32
33     protected void prepare() throws Throwable JavaDoc {
34     }
35     protected abstract Session getSessionUnderTest() throws Throwable JavaDoc;
36     protected abstract void reconnect(Session session) throws Throwable JavaDoc;
37     protected void done() throws Throwable JavaDoc {
38     }
39
40     public final void testConnectedSerialization() throws Throwable JavaDoc {
41         prepare();
42         Session sessionUnderTest = getSessionUnderTest();
43
44         sessionUnderTest.connection();
45
46         try {
47             SerializationHelper.serialize( sessionUnderTest );
48
49             fail( "Serialization of connected session allowed!" );
50         }
51         catch( IllegalStateException JavaDoc e ) {
52             // expected behaviour
53
}
54         finally {
55             release( sessionUnderTest );
56             done();
57         }
58     }
59
60     public final void testManualDisconnectedSerialization() throws Throwable JavaDoc {
61         prepare();
62         Session sessionUnderTest = getSessionUnderTest();
63
64         sessionUnderTest.disconnect();
65
66         SerializationHelper.serialize( sessionUnderTest );
67
68         release( sessionUnderTest );
69         done();
70     }
71
72     public final void testManualDisconnectChain() throws Throwable JavaDoc {
73         prepare();
74         Session sessionUnderTest = getSessionUnderTest();
75
76         sessionUnderTest.disconnect();
77
78         byte[] bytes = SerializationHelper.serialize( sessionUnderTest );
79         Session s2 = ( Session ) SerializationHelper.deserialize( bytes );
80
81         reconnect( s2 );
82
83         s2.disconnect();
84         reconnect( s2 );
85
86         release( sessionUnderTest );
87         release( s2 );
88         done();
89     }
90
91     public final void testManualDisconnectWithOpenResources() throws Throwable JavaDoc {
92         prepare();
93         Session sessionUnderTest = getSessionUnderTest();
94
95         Silly silly = new Silly( "tester" );
96         sessionUnderTest.save( silly );
97         sessionUnderTest.flush();
98
99         sessionUnderTest.createQuery( "from Silly" ).iterate();
100
101         sessionUnderTest.disconnect();
102         SerializationHelper.serialize( sessionUnderTest );
103
104         reconnect( sessionUnderTest );
105         sessionUnderTest.createQuery( "from Silly" ).scroll();
106
107         sessionUnderTest.disconnect();
108         SerializationHelper.serialize( sessionUnderTest );
109
110         reconnect( sessionUnderTest );
111         sessionUnderTest.delete( silly );
112         sessionUnderTest.flush();
113
114         release( sessionUnderTest );
115         done();
116     }
117 }
118
Popular Tags