KickJava   Java API By Example, From Geeks To Geeks.

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


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_sampleappli.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_sampleappli extends JExampleTestCase {
43
44
45     /**
46      * Class of the mdb.client
47      */

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

53     private static final String JavaDoc CLIENTOP_OK_TXT1 = "Nb messages sent and received OK";
54     
55     /**
56      * Second text to check
57      */

58     private static final String JavaDoc CLIENTOP_OK_TXT2 = "SampleApplicationClient OK" ;
59
60
61     /**
62      * Main method
63      * @param args the arguments
64      */

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

87     public static TestSuite suite() {
88         return new TestSuite(F_sampleappli.class);
89     }
90
91     /**
92      * Setup need for these tests
93      * sampleappli is required
94      * @throws Exception if it fails
95      */

96     protected void setUp() throws Exception JavaDoc {
97         super.setUp();
98         useBeans("sampleappli");
99     }
100
101     /**
102      * Step to do at the end
103      * unload sampleappli
104      * @throws Exception if it fails
105      */

106     protected void tearDown() throws Exception JavaDoc {
107         super.tearDown();
108         unUseBeans("sampleappli");
109     }
110
111
112     /**
113      * Constructor with a specified name
114      * @param s name
115      */

116     public F_sampleappli(String JavaDoc s) {
117         super(s);
118     }
119
120
121     /**
122      * Try to launch the client of the example mdb.sampleappli
123      * @throws Exception if an error occurs
124      */

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