KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

20 public class TopLinkTemplateTests extends TestCase {
21
22     public void testTemplateNotAllowingCreate() {
23         MockControl sessionControl = MockControl.createControl(Session.class);
24         Session session = (Session) sessionControl.getMock();
25
26         SessionFactory factory = new SingleSessionFactory(session);
27
28         TopLinkTemplate template = new TopLinkTemplate();
29         template.setAllowCreate(false);
30         template.setSessionFactory(factory);
31         try {
32             template.execute(new TopLinkCallback() {
33                 public Object JavaDoc doInTopLink(Session session) throws TopLinkException {
34                     return null;
35                 }
36             });
37             fail();
38         }
39         catch (Exception JavaDoc e) {
40         }
41     }
42
43     public void testTemplateWithCreate() {
44         MockControl sessionControl = MockControl.createControl(Session.class);
45         Session session = (Session) sessionControl.getMock();
46
47         SessionFactory factory = new SingleSessionFactory(session);
48
49         session.release();
50         sessionControl.setVoidCallable(1);
51
52         sessionControl.replay();
53
54         TopLinkTemplate template = new TopLinkTemplate();
55         template.setAllowCreate(true);
56         template.setSessionFactory(factory);
57         template.execute(new TopLinkCallback() {
58             public Object JavaDoc doInTopLink(Session session) throws TopLinkException {
59                 assertTrue(session != null);
60                 return null;
61             }
62         });
63         assertFalse(TransactionSynchronizationManager.hasResource(factory));
64
65         sessionControl.verify();
66     }
67
68     public void testTemplateWithExistingSessionAndNoCreate() {
69         MockControl sessionControl = MockControl.createControl(Session.class);
70         Session session = (Session) sessionControl.getMock();
71
72         SessionFactory factory = new SingleSessionFactory(session);
73
74         sessionControl.replay();
75
76         SessionHolder sessionHolder = new SessionHolder(factory.createSession());
77         TransactionSynchronizationManager.bindResource(factory, sessionHolder);
78
79         TopLinkTemplate template = new TopLinkTemplate();
80         template.setAllowCreate(false);
81         template.setSessionFactory(factory);
82         template.execute(new TopLinkCallback() {
83             public Object JavaDoc doInTopLink(Session session) throws TopLinkException {
84                 assertTrue(session != null);
85                 return null;
86             }
87         });
88         assertTrue(TransactionSynchronizationManager.hasResource(factory));
89         sessionControl.verify();
90         TransactionSynchronizationManager.unbindResource(factory);
91     }
92
93     public void testTemplateWithExistingSessionAndCreateAllowed() {
94         MockControl sessionControl = MockControl.createControl(Session.class);
95         Session session = (Session) sessionControl.getMock();
96
97         SessionFactory factory = new SingleSessionFactory(session);
98
99         sessionControl.replay();
100
101         SessionHolder sessionHolder = new SessionHolder(factory.createSession());
102         TransactionSynchronizationManager.bindResource(factory, sessionHolder);
103
104         TopLinkTemplate template = new TopLinkTemplate();
105         template.setAllowCreate(true);
106         template.setSessionFactory(factory);
107         template.execute(new TopLinkCallback() {
108             public Object JavaDoc doInTopLink(Session session) throws TopLinkException {
109                 assertTrue(session != null);
110                 return null;
111             }
112         });
113         assertTrue(TransactionSynchronizationManager.hasResource(factory));
114         sessionControl.verify();
115         TransactionSynchronizationManager.unbindResource(factory);
116     }
117 }
118
Popular Tags