KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 package org.objectweb.perseus.connector.ra;
29
30 import junit.framework.TestCase;
31
32 import java.util.Enumeration JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Vector JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.resource.NotSupportedException JavaDoc;
37 import javax.resource.ResourceException JavaDoc;
38 import javax.resource.cci.Connection JavaDoc;
39 import javax.resource.cci.ConnectionFactory JavaDoc;
40 import javax.resource.cci.LocalTransaction JavaDoc;
41 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
42
43 /**
44  * The class <b>TestRA</b> defines a test suite for testing a correct RA
45  */

46 public abstract class TestRA extends TestCase {
47     /**
48      * The managed connection factory of the resource adapter
49      */

50     protected ManagedConnectionFactory JavaDoc mcf = null;
51
52     /**
53      * The connection factory of the resource adapter
54      */

55     protected ConnectionFactory JavaDoc cf = null;
56
57     /**
58      * The list of writing tests. This vector contains a list of Writing
59      * elements. A writing element permit to abstract the accesses to the
60      * persitent support.
61      */

62     protected Vector JavaDoc writings = null;
63
64     /**
65      * Constructor in according to the JUnit constraints
66      */

67     protected TestRA(String JavaDoc tn) {
68         super(tn);
69         writings = getWritings();
70     }
71
72
73     //////////////////////
74
// ABSTRACT METHODS //
75
//////////////////////
76

77     /**
78      * The method implementation must return a new instance of the resource
79      * adapter. The ManagedConnectionFactory is the identifer of the
80      * resource adapter.
81      */

82     protected abstract ManagedConnectionFactory JavaDoc getNewMCFInstance()
83         throws Exception JavaDoc;
84
85     /**
86      * The method implementation must initialize the resource adapter
87      */

88     protected abstract void initMCF() throws Exception JavaDoc;
89
90     protected abstract void endMCF() throws Exception JavaDoc;
91
92     /**
93      * This method must return a list of writing test.
94      * The return type must be a Vector of Writing instances
95      */

96     protected abstract Vector JavaDoc getWritings();
97
98
99     /////////////////////////////////////////////////////
100
// IMPLEMENTATION OF METHODS OF THE TestCase class //
101
/////////////////////////////////////////////////////
102

103     /**
104      * Initialisation sequence executed before each test case.
105      */

106     protected void setUp() {
107         try {
108             mcf = getNewMCFInstance();
109             initMCF();
110             cf = (ConnectionFactory JavaDoc) mcf.createConnectionFactory();
111         } catch (Exception JavaDoc e) {
112             System.out.println("Error during setUp");
113             e.printStackTrace();
114 // System.exit(0);
115
}
116     }
117
118     /**
119      * Termination sequence executed after each test case.
120      */

121     protected void tearDown() {
122         try {
123             endMCF();
124         } catch (Exception JavaDoc e) {
125             System.out.println("Error during tearDown");
126             e.printStackTrace();
127 // System.exit(0);
128
}
129         mcf = null;
130         cf = null;
131     }
132
133
134     //////////////////////////////////
135
// TESTS OF THE Client CONTRACT //
136
//////////////////////////////////
137

138     /**
139      * This simple test check if it is possible to fetch a Connection,
140      * then to close it
141      */

142     public void testGetnClose() {
143         try {
144             Connection JavaDoc c = cf.getConnection();
145             c.close();
146         } catch (Exception JavaDoc e) {
147             e.printStackTrace();
148             fail();
149         }
150     }
151
152     /**
153      * This test, fetches a Connection, starts a local transaction, passes all
154      * writings, and commits the transaction
155      */

156     public void testLTCommit() {
157         try {
158             Connection JavaDoc c = cf.getConnection();
159             Enumeration JavaDoc e = writings.elements();
160             Writing w;
161             while (e.hasMoreElements()) {
162                 w = (Writing) e.nextElement();
163                 w.setConnection(c);
164                 c.setAutoCommit(true);
165                 w.init();
166                 if (!w.read(false)) {
167                     System.err.println("Fail-1!!!");
168                     fail();
169                 }
170                 c.setAutoCommit(false);
171                 LocalTransaction JavaDoc lt = c.getLocalTransaction();
172                 lt.begin();
173                 if (w.read(true)) {
174                     System.err.println("Fail-2!!!");
175                     fail();
176                 }
177                 w.write();
178                 if (w.read(false)) {
179                     System.err.println("Fail-3!!!");
180                     fail();
181                 }
182                 lt.commit();
183             }
184             c.close();
185
186             c = cf.getConnection();
187             e = writings.elements();
188             while (e.hasMoreElements()) {
189                 w = (Writing) e.nextElement();
190                 w.setConnection(c);
191                 if (w.read(false)) {
192                     System.err.println("Fail-4!!!");
193                     fail();
194                 }
195             }
196             c.close();
197         } catch (Exception JavaDoc e) {
198             e.printStackTrace();
199             fail();
200         }
201     }
202
203     /**
204      * This test, fetches a Connection, starts a local transaction, passes all
205      * writings, and rollbackes the transaction
206      */

207     public void testLTRollback() {
208         try {
209             Connection JavaDoc c = cf.getConnection();
210             Enumeration JavaDoc e = writings.elements();
211             Writing w = null;
212             while (e.hasMoreElements()) {
213                 w = (Writing) e.nextElement();
214                 w.setConnection(c);
215                 c.setAutoCommit(true);
216                 w.init();
217                 if (!w.read(false)) {
218                     System.err.println("Fail-1!!!");
219                     fail();
220                 }
221                 c.setAutoCommit(false);
222                 LocalTransaction JavaDoc lt = c.getLocalTransaction();
223                 lt.begin();
224                 if (w.read(true)) {
225                     System.err.println("Fail-2!!!");
226                     fail();
227                 }
228                 w.write();
229                 if (w.read(false)) {
230                     System.err.println("Fail-3!!!");
231                     fail();
232                 }
233                 lt.rollback();
234             }
235             c.close();
236
237             c = cf.getConnection();
238             e = writings.elements();
239             while (e.hasMoreElements()) {
240                 w = (Writing) e.nextElement();
241                 w.setConnection(c);
242                 if (!w.read(false)) {
243                     System.err.println("Fail-4!!!");
244                     fail();
245                 }
246             }
247             c.close();
248         } catch (Exception JavaDoc e) {
249             e.printStackTrace();
250             fail();
251         }
252     }
253
254     /**
255      * Check if the meta information of the ConnectionFactory is
256      * reachable and not null.
257      */

258     public void testCFMetaData() {
259         try {
260             try {
261                 cf.getMetaData().getAdapterName();
262             } catch (NotSupportedException JavaDoc nse) {
263                 return;
264             }
265             cf.getMetaData().getAdapterName();
266             cf.getMetaData().getAdapterShortDescription();
267             cf.getMetaData().getAdapterVendorName();
268             cf.getMetaData().getAdapterVersion();
269             cf.getMetaData().getSpecVersion();
270             cf.getMetaData().supportsExecuteWithInputAndOutputRecord();
271             cf.getMetaData().supportsExecuteWithInputRecordOnly();
272             cf.getMetaData().supportsLocalTransactionDemarcation();
273         } catch (Exception JavaDoc e) {
274             fail();
275         }
276     }
277
278     /**
279      * This test checks if the record factory is reachable
280      */

281     public void testCFRecordFactory() {
282         try {
283             cf.getRecordFactory().createIndexedRecord("test1");
284         } catch (NotSupportedException JavaDoc nse) {
285             // OK
286
} catch (Exception JavaDoc e) {
287             fail();
288         }
289
290         try {
291             cf.getRecordFactory().createMappedRecord("test2");
292         } catch (NotSupportedException JavaDoc nse) {
293             // OK
294
} catch (Exception JavaDoc e) {
295             fail();
296         }
297     }
298
299     /**
300      *
301      */

302     public void testAutoCommitFalse() {
303         System.out.println("\n** testAutoCommitFalse doesn't run !");
304         /*
305         System.out.println("testAutoCommitFalse");
306         try {
307             Connection c = cf.getConnection();
308             // Initialise and write data
309             Enumeration e = writings.elements();
310             Writing w = null;
311             while ( e.hasMoreElements() ) {
312                 w = (Writing) e.nextElement();
313                 System.out.println("w : "+w);
314                 w.setConnection(c);
315                 c.setAutoCommit(true);
316                 assert( c.getAutoCommit() );
317                 w.init();
318                 assert( w.read(false) );
319                 System.out.println("\tinit,read Ok");
320                 c.setAutoCommit(false);
321                 // Check the auto commit state
322                 assert( ! c.getAutoCommit() );
323                 w.write();
324                 assert( w.read(true) );
325                 System.out.println("\twrite Ok");
326             }
327         System.out.println("--------Check---------");
328
329             // Check that the writing are not kept
330             Connection c2 = cf.getConnection();
331             e = writings.elements();
332             while ( e.hasMoreElements() ) {
333                 w = (Writing) e.nextElement();
334                 System.out.println("w : "+w);
335                 assert( w.read(c2, false) );
336                 System.out.println("\tread Ok");
337             }
338             c.close();
339             c2.close();
340         } catch (NotSupportedException nse) {
341             assert(true);
342         } catch (Exception re) {
343             assert(false);
344         }
345         */

346     }
347
348     /**
349      *
350      */

351     public void testAutoCommitTrue() {
352         try {
353             Connection JavaDoc c = cf.getConnection();
354             c.setAutoCommit(true);
355             // Check the auto commit state
356
if (!c.getAutoCommit())
357                 fail();
358             // Initialise and write data
359
Enumeration JavaDoc e = writings.elements();
360             Writing w = null;
361             while (e.hasMoreElements()) {
362                 w = (Writing) e.nextElement();
363                 w.init(c);
364                 if (!w.read(c, false))
365                     fail();
366                 w.write(c);
367                 if (!w.read(c, true))
368                     fail();
369             }
370             c.close();
371
372             // Check that the writing are not kept
373
c = cf.getConnection();
374             e = writings.elements();
375             w = null;
376             while (e.hasMoreElements()) {
377                 w = (Writing) e.nextElement();
378                 if (!w.read(c, true))
379                     fail();
380             }
381             c.close();
382         } catch (NotSupportedException JavaDoc nse) {
383             // OK
384
} catch (Exception JavaDoc e) {
385             fail();
386         }
387     }
388 }
Popular Tags