KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

21 public class TopLinkInterceptorTests extends TestCase {
22
23     public void testInterceptorWithNoSessionBoundAndNoSynchronizations() throws Throwable JavaDoc {
24         MockControl sessionControl = MockControl.createControl(Session.class);
25         Session session = (Session) sessionControl.getMock();
26         MockControl methodInvocationControl = MockControl.createControl(MethodInvocation.class);
27         MethodInvocation methodInvocation = (MethodInvocation) methodInvocationControl.getMock();
28
29         SessionFactory factory = new SingleSessionFactory(session);
30
31         TopLinkInterceptor interceptor = new TopLinkInterceptor();
32         interceptor.setSessionFactory(factory);
33
34         methodInvocation.proceed();
35         methodInvocationControl.setReturnValue(null, 1);
36         session.release();
37         sessionControl.setVoidCallable(1);
38
39         methodInvocationControl.replay();
40         sessionControl.replay();
41
42         try {
43             interceptor.invoke(methodInvocation);
44         }
45         catch (Throwable JavaDoc t) {
46             System.out.println(t);
47             t.printStackTrace();
48             fail();
49         }
50
51         assertFalse(TransactionSynchronizationManager.hasResource(factory));
52
53         sessionControl.verify();
54         methodInvocationControl.verify();
55         sessionControl.verify();
56     }
57
58     public void testInterceptorWithNoSessionBoundAndSynchronizationsActive() {
59         MockControl sessionControl = MockControl.createControl(Session.class);
60         Session session = (Session) sessionControl.getMock();
61         MockControl methodInvocationControl = MockControl.createControl(MethodInvocation.class);
62         MethodInvocation methodInvocation = (MethodInvocation) methodInvocationControl.getMock();
63
64         SessionFactory factory = new SingleSessionFactory(session);
65
66         TopLinkInterceptor interceptor = new TopLinkInterceptor();
67         interceptor.setSessionFactory(factory);
68
69         try {
70             methodInvocation.proceed();
71         }
72         catch (Throwable JavaDoc e) {
73             fail();
74         }
75         methodInvocationControl.setReturnValue(null, 1);
76
77         methodInvocationControl.replay();
78         sessionControl.replay();
79
80         TransactionSynchronizationManager.initSynchronization();
81         try {
82             interceptor.invoke(methodInvocation);
83         }
84         catch (Throwable JavaDoc t) {
85             fail();
86         }
87
88         assertTrue(TransactionSynchronizationManager.hasResource(factory));
89         assertTrue(TransactionSynchronizationManager.getSynchronizations().size() == 1);
90
91         TransactionSynchronizationManager.clearSynchronization();
92         TransactionSynchronizationManager.unbindResource(factory);
93
94         sessionControl.verify();
95         methodInvocationControl.verify();
96     }
97
98 }
99
Popular Tags