KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > examples > clients > mdb > F_samplemdb


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
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.1 of the License, or 1any 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
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: F_samplemdb.java,v 1.1 2003/08/18 07:27:59 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.examples.clients.mdb;
28
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import junit.framework.*;
31
32 import org.objectweb.jonas.examples.util.JExampleTestCase;
33 import org.objectweb.jonas.examples.util.JPrintStream;
34 import org.objectweb.jonas.examples.util.NoExitSecurityManager;
35
36 /**
37  * Define a class to test the mdb example
38  * Test the client of mdb/sampleAppli example
39  * (Check if the text is "SampleApplicationClient OK"
40  * @author Florent Benoit
41  */

42 public class F_samplemdb extends JExampleTestCase {
43
44
45     /**
46      * Class of the mdb.client
47      */

48     private static final String JavaDoc CLIENT_CLASS = "samplemdb.MdbClient";
49
50     /**
51      * Text to check
52      */

53     private static final String JavaDoc CLIENTOP_OK_TXT = "MDBsample is Ok";
54
55
56     /**
57      * Main method
58      * @param args the arguments
59      */

60     public static void main(String JavaDoc[] args) {
61
62         String JavaDoc testtorun = null;
63         // Get args
64
for (int argn = 0; argn < args.length; argn++) {
65             String JavaDoc sArg = args[argn];
66             if (sArg.equals("-n")) {
67                 testtorun = args[++argn];
68             }
69         }
70
71         if (testtorun == null) {
72             junit.textui.TestRunner.run(suite());
73         } else {
74             junit.textui.TestRunner.run(new F_samplemdb(testtorun));
75         }
76     }
77
78     /**
79      * Get a new TestSuite for this class
80      * @return a new TestSuite for this class
81      */

82     public static TestSuite suite() {
83         return new TestSuite(F_samplemdb.class);
84     }
85
86     /**
87      * Setup need for these tests
88      * samplemdb is required
89      * @throws Exception if it fails
90      */

91     protected void setUp() throws Exception JavaDoc {
92         super.setUp();
93         useBeans("samplemdb");
94     }
95
96     /**
97      * Step to do at the end
98      * unload samplemdb
99      * @throws Exception if it fails
100      */

101     protected void tearDown() throws Exception JavaDoc {
102         super.tearDown();
103         unUseBeans("samplemdb");
104     }
105
106
107     /**
108      * Constructor with a specified name
109      * @param s name
110      */

111     public F_samplemdb(String JavaDoc s) {
112         super(s);
113     }
114
115
116     /**
117      * Try to launch the client of the example mdb
118      * @throws Exception if an error occurs
119      */

120     public void testClient() throws Exception JavaDoc {
121         JPrintStream jPrintStream = new JPrintStream(System.out);
122         System.setOut(jPrintStream);
123         String JavaDoc txt = null;
124         try {
125             //Define a SecurityManager to handle System.exit() case
126
System.setSecurityManager(new NoExitSecurityManager());
127
128             //Call Method
129
callMainMethod(CLIENT_CLASS);
130
131             txt = jPrintStream.getStringBuffer().toString();
132         } catch (InvocationTargetException JavaDoc ite) {
133             System.out.println("Error = " + ite);
134             ite.printStackTrace();
135             fail("Fail when invoking the client. It can be due to a System.exit()");
136         } catch (Exception JavaDoc e) {
137             fail("Client was not ok" + e);
138         } finally {
139             System.setSecurityManager(new SecurityManager JavaDoc());
140             jPrintStream.remove();
141         }
142
143         System.out.println("Sample mdb txt = " + txt);
144
145         // Check the text
146
if (txt.indexOf(CLIENTOP_OK_TXT) == -1) {
147             fail("The text that the client sent was not " + CLIENTOP_OK_TXT);
148         }
149
150     }
151
152 }
153
Popular Tags