KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > connector > ra > TestRAMan


1 /**
2  * ObjectWeb Connector: an implementation of JCA Sun specification along
3  * with some extensions of this specification.
4  * Copyright (C) 2001-2002 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Release: 0.1
21  *
22  * Contact: jorm@objectweb.org
23  *
24  * Author: S. Chassande-Barrioz, P. Dechamboux
25  */

26
27 package org.objectweb.perseus.connector.ra;
28
29 import java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31 import javax.resource.ResourceException JavaDoc;
32 import javax.resource.cci.Connection JavaDoc;
33 import javax.resource.cci.ConnectionFactory JavaDoc;
34 import javax.resource.cci.LocalTransaction JavaDoc;
35 import javax.resource.spi.ConnectionEvent JavaDoc;
36 import javax.resource.spi.ConnectionEventListener JavaDoc;
37 import javax.resource.spi.ConnectionManager JavaDoc;
38 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
39 import javax.resource.spi.ManagedConnection JavaDoc;
40 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
41
42 //import org.objectweb.transaction.api.ConnectionManager;
43
//import org.objectweb.transaction.api.ConnectionFactoryWrapper;
44

45 /**
46  * The class <b>TestRAMan</b> defines a test suite for testing a correct RA
47  * behaviour under managed environment.
48  */

49 public abstract class TestRAMan extends TestRA
50     implements ConnectionManager JavaDoc, ConnectionEventListener JavaDoc {
51
52     private boolean isConnectionClosed = false;
53     private boolean isConnectionError = false;
54     private boolean isLTCommited = false;
55     private boolean isLTRollbacked = false;
56     private boolean isLTStarted = false;
57
58     private boolean isAllocated = false;
59
60     /**
61      * Constructor in according to the JUnit constraints
62      */

63     public TestRAMan(String JavaDoc tn) {
64         super(tn);
65     }
66
67
68     //////////////////////////////////////////////////////////////
69
// IMPLEMENTATION OF ABSTRACT METHODS INHERITED FROM TestRA //
70
//////////////////////////////////////////////////////////////
71
/**
72      * This method can be override to initialize the ManagedConnectionFactory.
73      */

74     protected void initMCF() throws Exception JavaDoc {
75         Object JavaDoc bcf = mcf.createConnectionFactory(
76             (javax.resource.spi.ConnectionManager JavaDoc) this);
77         /*
78         if (bcf instanceof ConnectionFactory)
79             cf = (ConnectionFactory) bcf;
80         else if (bcf instanceof ConnectionFactoryWrapper)
81             cf = ((ConnectionFactoryWrapper) bcf).getCCIConnectionFactory();
82         else
83             throw new Exception(
84                 "Cannot obtain a ConnectionFactory from the RA !");
85         */

86     }
87
88     /////////////////////////////////////////////////////
89
// IMPLEMENTATION OF METHODS OF THE TestCase class //
90
/////////////////////////////////////////////////////
91

92     /**
93      * Initialisation sequence executed before each test case.
94      */

95     protected void setUp() {
96         isConnectionClosed = false;
97         isConnectionError = false;
98         isLTCommited = false;
99         isLTRollbacked = false;
100         isLTStarted = false;
101         isAllocated = false;
102         super.setUp();
103     }
104
105     ///////////////////////////////////////////////////////
106
// IMPLEMENTATION OF THE ConnectionManager INTERFACE //
107
///////////////////////////////////////////////////////
108

109     public Object JavaDoc allocateConnection(ManagedConnectionFactory JavaDoc _mcf,
110                                      ConnectionRequestInfo JavaDoc cri) throws ResourceException JavaDoc {
111         isAllocated = true;
112         if (mcf != _mcf)
113             fail();
114         ManagedConnection JavaDoc mc = _mcf.createManagedConnection(null, cri);
115         mc.addConnectionEventListener(this);
116         Object JavaDoc connection = mc.getConnection(null, cri);
117         mc.associateConnection(connection);
118         return connection;
119     }
120
121     /////////////////////////////////////////////////////////////
122
// IMPLEMENTATION OF THE ConnectionEventListener INTERFACE //
123
/////////////////////////////////////////////////////////////
124

125     public void connectionClosed(ConnectionEvent JavaDoc event) {
126         isConnectionClosed = true;
127     }
128
129     public void connectionErrorOccurred(ConnectionEvent JavaDoc event) {
130         isConnectionError = true;
131     }
132
133     public void localTransactionCommitted(ConnectionEvent JavaDoc event) {
134         isLTCommited = true;
135     }
136
137     public void localTransactionRolledback(ConnectionEvent JavaDoc event) {
138         isLTRollbacked = true;
139     }
140
141     public void localTransactionStarted(ConnectionEvent JavaDoc event) {
142         isLTStarted = true;
143     }
144
145
146     //////////////////////////////////////////////////////
147
// TESTS INTERACTION BETWEEN RA AND ConectionManager//
148
//////////////////////////////////////////////////////
149

150
151     /**
152      * This test checks if during a connection request, the
153      * ConnectionManager is notified
154      */

155     public void testNotifyAllocation() {
156         try {
157             // initialise the test
158
isAllocated = false;
159
160             // ask a connection
161
Connection JavaDoc c = cf.getConnection();
162
163             // check if getContext was done
164
if (!isAllocated)
165                 fail();
166
167             // End the test
168
c.close();
169         } catch (Exception JavaDoc e) {
170             fail();
171         }
172     }
173
174     //////////////////////////////////////////////////////////////
175
// TESTS INTERACTION BETWEEN RA AND ConnectionEventListener //
176
//////////////////////////////////////////////////////////////
177
/**
178      * This test checks if during a request on a connection, the
179      * ConnectionEventListener is notified
180      */

181     public void testNotifyCloseCEL() {
182         try {
183             // initialse test
184
isConnectionClosed = false;
185
186             // ask a connection
187
Connection JavaDoc c = cf.getConnection();
188
189             c.close();
190
191             // check if isConnectionClosed was done
192
if (!isConnectionClosed)
193                 fail();
194         } catch (Exception JavaDoc e) {
195             fail();
196         }
197     }
198
199     /**
200      * This test checks if during a request on a connection, the
201      * ConnectionEventListener is notified
202      */

203     public void testNotifyLTCommitCEL() {
204         try {
205             // initialse test
206
isLTStarted = false;
207             isLTCommited = false;
208
209             // ask a connection
210
Connection JavaDoc c = cf.getConnection();
211             LocalTransaction JavaDoc lt = c.getLocalTransaction();
212             lt.begin();
213             if (!isLTStarted)
214                 fail();
215             Enumeration JavaDoc e = writings.elements();
216             Writing w = null;
217             while (e.hasMoreElements()) {
218                 w = (Writing) e.nextElement();
219                 w.init(c);
220                 w.write(c);
221             }
222             lt.commit();
223             if (!isLTCommited)
224                 fail();
225
226             // End the test
227
c.close();
228         } catch (Exception JavaDoc e) {
229             fail();
230         }
231     }
232
233     /**
234      * This test checks if during a request on a connection, the
235      * ConnectionEventListener is notified
236      */

237     public void testNotifyLTRollbackCEL() {
238         try {
239             // initialse test
240
isLTStarted = false;
241             isLTRollbacked = false;
242
243             // ask a connection
244
Connection JavaDoc c = cf.getConnection();
245             LocalTransaction JavaDoc lt = c.getLocalTransaction();
246             lt.begin();
247             if (!isLTStarted)
248                 fail();
249             Enumeration JavaDoc e = writings.elements();
250             Writing w = null;
251             while (e.hasMoreElements()) {
252                 w = (Writing) e.nextElement();
253                 w.init(c);
254                 w.write(c);
255             }
256             lt.rollback();
257             if (!isLTRollbacked)
258                 fail();
259
260             // End the test
261
c.close();
262         } catch (Exception JavaDoc e) {
263             fail();
264         }
265     }
266 }
Popular Tags