KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > toplink > SessionFactoryUtilsTests


1 /*
2  * Created on Mar 18, 2005
3  *
4  */

5
6 package org.springframework.orm.toplink;
7
8 import junit.framework.TestCase;
9 import oracle.toplink.sessions.Session;
10 import org.easymock.MockControl;
11
12 import org.springframework.transaction.support.TransactionSynchronizationManager;
13
14 /**
15  * @author Juergen Hoeller
16  * @author <a HREF="mailto:james.x.clark@oracle.com">James Clark</a>
17  * @since 28.04.2005
18  */

19 public class SessionFactoryUtilsTests extends TestCase {
20
21     /**
22      * When no Session is bound and allowCreate is "false", we should throw an IllegalStateException.
23      * When no Session is bound, and allowCreate is "true", we should get a Session but it should not
24      * be bound to the Thread afterwards.
25      */

26     public void testNoSessionBound() {
27         MockControl sessionControl = MockControl.createControl(Session.class);
28         Session session = (Session) sessionControl.getMock();
29
30         SessionFactory factory = new SingleSessionFactory(session);
31
32         session.hasExternalTransactionController();
33         sessionControl.setReturnValue(false, 1);
34
35         sessionControl.replay();
36         try {
37             Session boundSession = SessionFactoryUtils.getSession(factory, false);
38             fail();
39         }
40         catch (Throwable JavaDoc t) {
41             assertTrue(t.getClass().equals(IllegalStateException JavaDoc.class));
42         }
43
44         Session boundSession = SessionFactoryUtils.getSession(factory, true);
45         assertTrue(session == boundSession);
46         assertFalse(TransactionSynchronizationManager.hasResource(factory));
47         assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
48     }
49
50     /**
51      * When called with no previous Session bound, "allowCreate", and "allowSynchronization",
52      * Session should be returned, it should be bound to the Thread, and a synchronization listener
53      * should be in the list of thread synchronizations.
54      */

55     public void testNoSessionBoundAllowAndInit() {
56         MockControl sessionControl = MockControl.createControl(Session.class);
57         Session session = (Session) sessionControl.getMock();
58
59         SessionFactory factory = new SingleSessionFactory(session);
60
61         session.hasExternalTransactionController();
62         sessionControl.setReturnValue(false, 1);
63
64         sessionControl.replay();
65
66         Session boundSession = SessionFactoryUtils.getSession(factory, true);
67         assertTrue(session == boundSession);
68
69         SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(factory);
70         assertTrue(holder == null);
71
72         TransactionSynchronizationManager.initSynchronization();
73
74         boundSession = SessionFactoryUtils.getSession(factory, true);
75         assertTrue(session == boundSession);
76         assertTrue(TransactionSynchronizationManager.getSynchronizations().size() == 1);
77         assertTrue(TransactionSynchronizationManager.hasResource(factory));
78         assertTrue(session == ((SessionHolder) TransactionSynchronizationManager.getResource(factory)).getSession());
79
80         TransactionSynchronizationManager.clearSynchronization();
81         TransactionSynchronizationManager.unbindResource(factory);
82     }
83
84     public void testNoSessionBoundAllowAndNoInit() {
85         MockControl sessionControl = MockControl.createControl(Session.class);
86         Session session = (Session) sessionControl.getMock();
87
88         SessionFactory factory = new SingleSessionFactory(session);
89
90         session.hasExternalTransactionController();
91         sessionControl.setReturnValue(false, 2);
92
93         sessionControl.replay();
94
95         Session boundSession = SessionFactoryUtils.getSession(factory, true);
96         assertTrue(session == boundSession);
97
98         SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(factory);
99         assertTrue(holder == null);
100
101         boundSession = SessionFactoryUtils.getSession(factory, true);
102         assertTrue(session == boundSession);
103         assertFalse(TransactionSynchronizationManager.hasResource(factory));
104     }
105
106 }
107
Popular Tags