001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: F_sampleappli.java 3038 2003-08-18 07:28:00Z benoitf $
024: * --------------------------------------------------------------------------
025: */
026:
027: package org.objectweb.jonas.examples.clients.mdb;
028:
029: import java.lang.reflect.InvocationTargetException;
030: import junit.framework.*;
031:
032: import org.objectweb.jonas.examples.util.JExampleTestCase;
033: import org.objectweb.jonas.examples.util.JPrintStream;
034: import org.objectweb.jonas.examples.util.NoExitSecurityManager;
035:
036: /**
037: * Define a class to test the mdb example
038: * Test the client of mdb/sampleAppli example
039: * (Check if the text is "SampleApplicationClient OK"
040: * @author Florent Benoit
041: */
042: public class F_sampleappli extends JExampleTestCase {
043:
044: /**
045: * Class of the mdb.client
046: */
047: private static final String CLIENT_CLASS = "sampleappli.SampleAppliClient";
048:
049: /**
050: * Text to check
051: */
052: private static final String CLIENTOP_OK_TXT1 = "Nb messages sent and received OK";
053:
054: /**
055: * Second text to check
056: */
057: private static final String CLIENTOP_OK_TXT2 = "SampleApplicationClient OK";
058:
059: /**
060: * Main method
061: * @param args the arguments
062: */
063: public static void main(String[] args) {
064:
065: String testtorun = null;
066: // Get args
067: for (int argn = 0; argn < args.length; argn++) {
068: String sArg = args[argn];
069: if (sArg.equals("-n")) {
070: testtorun = args[++argn];
071: }
072: }
073:
074: if (testtorun == null) {
075: junit.textui.TestRunner.run(suite());
076: } else {
077: junit.textui.TestRunner.run(new F_sampleappli(testtorun));
078: }
079: }
080:
081: /**
082: * Get a new TestSuite for this class
083: * @return a new TestSuite for this class
084: */
085: public static TestSuite suite() {
086: return new TestSuite(F_sampleappli.class);
087: }
088:
089: /**
090: * Setup need for these tests
091: * sampleappli is required
092: * @throws Exception if it fails
093: */
094: protected void setUp() throws Exception {
095: super .setUp();
096: useBeans("sampleappli");
097: }
098:
099: /**
100: * Step to do at the end
101: * unload sampleappli
102: * @throws Exception if it fails
103: */
104: protected void tearDown() throws Exception {
105: super .tearDown();
106: unUseBeans("sampleappli");
107: }
108:
109: /**
110: * Constructor with a specified name
111: * @param s name
112: */
113: public F_sampleappli(String s) {
114: super (s);
115: }
116:
117: /**
118: * Try to launch the client of the example mdb.sampleappli
119: * @throws Exception if an error occurs
120: */
121: public void testClient() throws Exception {
122: JPrintStream jPrintStream = new JPrintStream(System.out);
123: System.setOut(jPrintStream);
124: String txt = null;
125: try {
126: //Define a SecurityManager to handle System.exit() case
127: System.setSecurityManager(new NoExitSecurityManager());
128:
129: //Call Method
130: callMainMethod(CLIENT_CLASS);
131:
132: txt = jPrintStream.getStringBuffer().toString();
133: } catch (InvocationTargetException ite) {
134: System.out.println("Error = " + ite);
135: ite.printStackTrace();
136: fail("Fail when invoking the client. It can be due to a System.exit()");
137: } catch (Exception e) {
138: fail("Client was not ok" + e);
139: } finally {
140: System.setSecurityManager(new SecurityManager());
141: jPrintStream.remove();
142: }
143:
144: System.out.println("Sample appli txt = " + txt);
145:
146: // Check the text
147: if (txt.indexOf(CLIENTOP_OK_TXT1) == -1) {
148: fail("The text that the client sent was not "
149: + CLIENTOP_OK_TXT1);
150: }
151:
152: // Check the second text
153: if (txt.indexOf(CLIENTOP_OK_TXT2) == -1) {
154: fail("The text that the client sent was not "
155: + CLIENTOP_OK_TXT2);
156: }
157:
158: }
159:
160: }
|