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 any 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: * --------------------------------------------------------------------------
022: * $Id: RemoteRunnerSL.java 4406 2004-03-19 11:57:20Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: // RemoteRunnerSL.java
027: // Stateless Session bean
028: package org.objectweb.jonas.jtests.beans.remoterunner;
029:
030: import java.io.ByteArrayOutputStream;
031: import java.io.PrintStream;
032: import java.lang.reflect.Constructor;
033: import java.lang.reflect.Method;
034: import java.rmi.RemoteException;
035:
036: import javax.ejb.CreateException;
037: import javax.ejb.SessionBean;
038: import javax.ejb.SessionContext;
039:
040: import junit.framework.Test;
041: import junit.textui.TestRunner;
042:
043: import org.objectweb.jonas.common.Log;
044: import org.objectweb.util.monolog.api.BasicLevel;
045: import org.objectweb.util.monolog.api.Logger;
046:
047: /**
048: *
049: */
050:
051: public class RemoteRunnerSL implements SessionBean {
052:
053: static private Logger logger = null;
054: SessionContext ejbContext;
055:
056: // ------------------------------------------------------------------
057: // SessionBean implementation
058: // ------------------------------------------------------------------
059:
060: /**
061: * Set the associated session context. The container calls this method
062: * after the instance creation.
063: * The enterprise Bean instance should store the reference to the context
064: * object in an instance variable.
065: * This method is called with no transaction context.
066: *
067: * @param sessionContext A SessionContext interface for the instance.
068: * @throws EJBException Thrown by the method to indicate a failure caused by
069: * a system-level error.
070: */
071: public void setSessionContext(SessionContext ctx) {
072: if (logger == null)
073: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
074: logger
075: .log(BasicLevel.DEBUG,
076: "RemoteRunnerSL setSessionContext");
077: ejbContext = ctx;
078: }
079:
080: /**
081: * A container invokes this method before it ends the life of the session object.
082: * This happens as a result of a client's invoking a remove operation, or when a
083: * container decides to terminate the session object after a timeout.
084: * This method is called with no transaction context.
085: *
086: * @throws EJBException Thrown by the method to indicate a failure caused by
087: * a system-level error.
088: */
089: public void ejbRemove() {
090: logger.log(BasicLevel.DEBUG, "RemoteRunnerSL ejbRemove");
091: }
092:
093: /**
094: * The Session bean must define 1 or more ejbCreate methods.
095: *
096: * @throws CreateException Failure to create a session EJB object.
097: */
098: public void ejbCreate() throws CreateException {
099: logger.log(BasicLevel.DEBUG, "RemoteRunnerSL ejbCreate");
100: }
101:
102: /**
103: * A container invokes this method on an instance before the instance
104: * becomes disassociated with a specific EJB object.
105: */
106: public void ejbPassivate() {
107: logger.log(BasicLevel.DEBUG, "RemoteRunnerSL ejbPassivate");
108: }
109:
110: /**
111: * A container invokes this method when the instance is taken out of
112: * the pool of available instances to become associated with a specific
113: * EJB object.
114: */
115: public void ejbActivate() {
116: logger.log(BasicLevel.DEBUG, "RemoteRunnerSL ejbActivate");
117: }
118:
119: // ------------------------------------------------------------------
120: // RemoteRunner implementation
121: // ------------------------------------------------------------------
122:
123: /**
124: * Run a JUnit TestSuite
125: *
126: * @param jtcc the class of the JUnit TestSuite to be run
127: *
128: * The code run here is equivalent to :
129: * TestRunner.run( jtcc.suite())
130: */
131:
132: public String run(Class jtcc) throws RemoteException {
133: logger.log(BasicLevel.DEBUG, "RemoteRunnerSL run");
134: try {
135: Test ts = null;
136: Method method = jtcc.getMethod("suite", null);
137: ts = (Test) method.invoke(null, null);
138: ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
139: PrintStream ps = new java.io.PrintStream(baos);
140: TestRunner tr = new TestRunner(ps);
141: tr.doRun(ts, false);
142: return baos.toString();
143: } catch (Exception e) {
144: throw new RemoteException(e.toString());
145: }
146:
147: }
148:
149: /**
150: * Run a TestCase in the JUnit TestSuite
151: *
152: * @param jtcc the class of the JUnit TestSuite to be run
153: * @param name name of the testcase to run
154: *
155: * This code run here is equivalent to do
156: * TestRunner.run(new jtcc(testtorun))
157: */
158: public String run(Class jtcc, String testtorun)
159: throws RemoteException {
160: logger
161: .log(BasicLevel.DEBUG, "RemoteRunnerSL run: "
162: + testtorun);
163: try {
164: Object suiteInstance = null;
165: int nbParams = 1;
166: Class paramTypes[] = new Class[nbParams]; // constructor argument types
167: Object paramObjects[] = new Object[nbParams]; // constructor argument values
168: paramTypes[0] = java.lang.String.class;
169: paramObjects[0] = (Object) testtorun;
170: Constructor constructor = jtcc.getConstructor(paramTypes);
171: suiteInstance = constructor.newInstance(paramObjects);
172: ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
173: PrintStream ps = new java.io.PrintStream(baos);
174: TestRunner tr = new TestRunner(ps);
175: tr.doRun((Test) suiteInstance, false);
176: return baos.toString();
177: } catch (Exception e) {
178: throw new RemoteException(e.toString());
179: }
180:
181: }
182:
183: }
|