001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import java.io.File;
025: import java.net.MalformedURLException;
026: import java.net.URL;
027: import java.util.Arrays;
028: import java.util.Hashtable;
029:
030: import javax.management.MBeanServerConnection;
031: import javax.management.MalformedObjectNameException;
032: import javax.management.ObjectName;
033: import javax.naming.Context;
034: import javax.naming.InitialContext;
035: import javax.security.auth.login.LoginContext;
036:
037: import org.apache.log4j.Category;
038: import org.jboss.test.util.AppCallbackHandler;
039:
040: /**
041: * This is provides services for jboss junit test cases and TestSetups. It supplies
042: * access to log4j logging, the jboss jmx server, jndi, and a method for
043: * deploying ejb packages. You may supply the JNDI name under which the
044: * RMIAdaptor interface is located via the system property jbosstest.server.name
045: * default (jmx/rmi/RMIAdaptor) and the directory for deployable packages with
046: * the system property jbosstest.deploy.dir (default ../lib).
047: *
048: * Should be subclassed to derive junit support for specific services integrated
049: * into jboss.
050: *
051: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
052: * @author <a href="mailto:christoph.jung@jboss.org">Christoph G. Jung</a>
053: * @author <a href="mailto:scott.stark@jboss.org">Scott Stark</a>
054: * @version $Revision: 57221 $
055: */
056: public class JBossTestServices {
057: // Constants -----------------------------------------------------
058: public final static String DEPLOYER_NAME = "jboss.system:service=MainDeployer";
059: public final static String DEFAULT_USERNAME = "jduke";
060: public final static String DEFAULT_PASSWORD = "theduke";
061: public final static String DEFAULT_LOGIN_CONFIG = "other";
062: public final static int DEFAULT_THREADCOUNT = 10;
063: public final static int DEFAULT_ITERATIONCOUNT = 1000;
064: public final static int DEFAULT_BEANCOUNT = 100;
065:
066: // Attributes ----------------------------------------------------
067: protected MBeanServerConnection server;
068: protected Category log;
069: protected InitialContext initialContext;
070: protected Hashtable jndiEnv;
071: protected LoginContext lc;
072:
073: /**
074: * Constructor for the JBossTestCase object
075: *
076: * @param className Test case name
077: */
078: public JBossTestServices(String className) {
079: log = Category.getInstance(className);
080: log.debug("JBossTestServices(), className=" + className);
081: }
082:
083: // Public --------------------------------------------------------
084:
085: /**
086: * The JUnit setup method
087: */
088: public void setUp() throws Exception {
089: log.debug("JBossTestServices.setUp()");
090: init();
091: log.info("jbosstest.beancount: "
092: + System.getProperty("jbosstest.beancount"));
093: log.info("jbosstest.iterationcount: "
094: + System.getProperty("jbosstest.iterationcount"));
095: log.info("jbosstest.threadcount: "
096: + System.getProperty("jbosstest.threadcount"));
097: log.info("jbosstest.nodeploy: "
098: + System.getProperty("jbosstest.nodeploy"));
099: log.info("jbosstest.jndiurl: " + this .getJndiURL());
100: log.info("jbosstest.jndifactory: " + this .getJndiInitFactory());
101: }
102:
103: /**
104: * The teardown method for JUnit
105: */
106: public void tearDown() throws Exception {
107: // server = null;
108: log.debug("JBossTestServices.tearDown()");
109: }
110:
111: /**
112: * Gets the InitialContext attribute of the JBossTestCase object
113: *
114: * @return The InitialContext value
115: */
116: public InitialContext getInitialContext() throws Exception {
117: return initialContext;
118: }
119:
120: /**
121: * Gets the Server attribute of the JBossTestCase object
122: *
123: * @return The Server value
124: */
125: public MBeanServerConnection getServer() throws Exception {
126: if (server == null) {
127: String adaptorName = System.getProperty(
128: "jbosstest.server.name", "jmx/invoker/RMIAdaptor");
129: server = (MBeanServerConnection) initialContext
130: .lookup(adaptorName);
131: }
132: return server;
133: }
134:
135: /**
136: * Gets the Log attribute of the JBossTestCase object
137: *
138: * @return The Log value
139: */
140: Category getLog() {
141: return log;
142: }
143:
144: /**
145: * Gets the Main Deployer Name attribute of the JBossTestCase object
146: *
147: * @return The Main DeployerName value
148: * @exception MalformedObjectNameException Description of Exception
149: */
150: ObjectName getDeployerName() throws MalformedObjectNameException {
151: return new ObjectName(DEPLOYER_NAME);
152: }
153:
154: /**
155: * Returns the deployment directory to use. This does it's best to figure out
156: * where you are looking. If you supply a complete url, it returns it.
157: * Otherwise, it looks for jbosstest.deploy.dir or if missing ../lib. Then it
158: * tries to construct a file url or a url.
159: *
160: * @param filename name of the file/url you want
161: * @return A URL
162: * @exception MalformedURLException Description of Exception
163: */
164: protected URL getDeployURL(final String filename)
165: throws MalformedURLException {
166: // First see if it is already a complete url.
167: try {
168: return new URL(filename);
169: } catch (MalformedURLException e) {
170: log.debug(filename + " is not a valid URL, "
171: + e.getMessage());
172: }
173:
174: // OK, lets see if we can figure out what it might be.
175: String deployDir = System.getProperty("jbosstest.deploy.dir");
176: if (deployDir == null) {
177: deployDir = "output/lib";
178: }
179: String url = deployDir + "/" + filename;
180: log.debug("Testing file: " + url);
181: // try to canonicalize the strings a bit.
182: File file = new File(url);
183: if (file.exists()) {
184: log.debug(file.getAbsolutePath() + " is a valid file");
185: return file.toURL();
186: } else {
187: log.debug("File does not exist, creating url: " + url);
188: return new URL(url);
189: }
190: }
191:
192: /**
193: * invoke wraps an invoke call to the mbean server in a lot of exception
194: * unwrapping.
195: *
196: * @param name ObjectName of the mbean to be called
197: * @param method mbean method to be called
198: * @param args Object[] of arguments for the mbean method.
199: * @param sig String[] of types for the mbean methods parameters.
200: * @return Object returned by mbean method invocation.
201: * @exception Exception Description of Exception
202: */
203: protected Object invoke(ObjectName name, String method,
204: Object[] args, String[] sig) throws Exception {
205: return invoke(getServer(), name, method, args, sig);
206: }
207:
208: protected Object invoke(MBeanServerConnection server,
209: ObjectName name, String method, Object[] args, String[] sig)
210: throws Exception {
211: try {
212: this .getLog().debug(
213: "Invoking " + name.getCanonicalName() + " method="
214: + method);
215: if (args != null)
216: this .getLog().debug("args=" + Arrays.asList(args));
217: return server.invoke(name, method, args, sig);
218: } catch (javax.management.MBeanException e) {
219: log.error("MbeanException", e.getTargetException());
220: throw e.getTargetException();
221: } catch (javax.management.ReflectionException e) {
222: log.error("ReflectionException", e.getTargetException());
223: throw e.getTargetException();
224: } catch (javax.management.RuntimeOperationsException e) {
225: log.error("RuntimeOperationsException", e
226: .getTargetException());
227: throw e.getTargetException();
228: } catch (javax.management.RuntimeMBeanException e) {
229: log.error("RuntimeMbeanException", e.getTargetException());
230: throw e.getTargetException();
231: } catch (javax.management.RuntimeErrorException e) {
232: log.error("RuntimeErrorException", e.getTargetError());
233: throw e.getTargetError();
234: }
235: }
236:
237: /**
238: * Deploy a package with the main deployer. The supplied name is
239: * interpreted as a url, or as a filename in jbosstest.deploy.lib or ../lib.
240: *
241: * @param name filename/url of package to deploy.
242: * @exception Exception Description of Exception
243: */
244: public void deploy(String name) throws Exception {
245: if (Boolean.getBoolean("jbosstest.nodeploy") == true) {
246: log.debug("Skipping deployment of: " + name);
247: return;
248: }
249:
250: URL deployURL = getDeployURL(name);
251: log.debug("Deploying " + name + ", url=" + deployURL);
252: invoke(getDeployerName(), "deploy", new Object[] { deployURL },
253: new String[] { "java.net.URL" });
254: }
255:
256: public void redeploy(String name) throws Exception {
257: if (Boolean.getBoolean("jbosstest.nodeploy") == true) {
258: log.debug("Skipping redeployment of: " + name);
259: return;
260: }
261:
262: URL deployURL = getDeployURL(name);
263: log.debug("Deploying " + name + ", url=" + deployURL);
264: invoke(getDeployerName(), "redeploy",
265: new Object[] { deployURL },
266: new String[] { "java.net.URL" });
267: }
268:
269: /** Do a JAAS login with the current username, password and login config.
270: * @throws Exception
271: */
272: public void login() throws Exception {
273: flushAuthCache("other");
274: String username = getUsername();
275: String pass = getPassword();
276: String config = getLoginConfig();
277: char[] password = null;
278: if (pass != null)
279: password = pass.toCharArray();
280: AppCallbackHandler handler = new AppCallbackHandler(username,
281: password);
282: getLog().debug("Creating LoginContext(" + config + ")");
283: lc = new LoginContext(config, handler);
284: lc.login();
285: getLog().debug(
286: "Created LoginContext, subject=" + lc.getSubject());
287: }
288:
289: public void logout() {
290: try {
291: getLog().debug("logout, LoginContext: " + lc);
292: if (lc != null)
293: lc.logout();
294: } catch (Exception e) {
295: getLog().error("logout error: ", e);
296: }
297: }
298:
299: /**
300: * Undeploy a package with the main deployer. The supplied name is
301: * interpreted as a url, or as a filename in jbosstest.deploy.lib or ../lib.
302: *
303: * @param name filename/url of package to undeploy.
304: * @exception Exception Description of Exception
305: */
306: public void undeploy(String name) throws Exception {
307: if (Boolean.getBoolean("jbosstest.nodeploy") == true)
308: return;
309: URL deployURL = getDeployURL(name);
310: log.debug("Undeploying " + name + ", url=" + deployURL);
311: Object[] args = { deployURL };
312: String[] sig = { "java.net.URL" };
313: invoke(getDeployerName(), "undeploy", args, sig);
314: }
315:
316: /** Flush all authentication credentials for the java:/jaas/other security
317: domain
318: */
319: void flushAuthCache(String domain) throws Exception {
320: ObjectName jaasMgr = new ObjectName(
321: "jboss.security:service=JaasSecurityManager");
322: Object[] params = { domain };
323: String[] signature = { "java.lang.String" };
324: invoke(jaasMgr, "flushAuthenticationCache", params, signature);
325: }
326:
327: void restartDBPool() throws Exception {
328: ObjectName dbPool = new ObjectName(
329: "jboss.jca:service=ManagedConnectionPool,name=DefaultDS");
330: Object[] params = {};
331: String[] signature = {};
332: invoke(dbPool, "stop", params, signature);
333: invoke(dbPool, "start", params, signature);
334: }
335:
336: boolean isSecure() {
337: return Boolean.getBoolean("jbosstest.secure");
338: }
339:
340: String getUsername() {
341: return System.getProperty("jbosstest.username",
342: DEFAULT_USERNAME);
343: }
344:
345: String getPassword() {
346: return System.getProperty("jbosstest.password",
347: DEFAULT_PASSWORD);
348: }
349:
350: String getLoginConfig() {
351: return System.getProperty("jbosstest.loginconfig",
352: DEFAULT_LOGIN_CONFIG);
353: }
354:
355: String getJndiURL() {
356: String url = (String) jndiEnv.get(Context.PROVIDER_URL);
357: return url;
358: }
359:
360: String getJndiInitFactory() {
361: String factory = (String) jndiEnv
362: .get(Context.INITIAL_CONTEXT_FACTORY);
363: return factory;
364: }
365:
366: int getThreadCount() {
367: int result = Integer.getInteger("jbosstest.threadcount",
368: DEFAULT_THREADCOUNT).intValue();
369: log.debug("jbosstest.threadcount=" + result);
370: return result;
371: }
372:
373: int getIterationCount() {
374: int result = Integer.getInteger("jbosstest.iterationcount",
375: DEFAULT_ITERATIONCOUNT).intValue();
376: log.debug("jbosstest.iterationcount=" + result);
377: return result;
378: }
379:
380: int getBeanCount() {
381: int result = Integer.getInteger("jbosstest.beancount",
382: DEFAULT_BEANCOUNT).intValue();
383: log.debug("jbosstest.beancount=" + result);
384: return result;
385: }
386:
387: /**
388: * Initializes the {@link InitialContext} if not set.
389: */
390: public void init() throws Exception {
391: if (initialContext == null) {
392: initialContext = new InitialContext();
393: log.debug("initialContext.getEnvironment()="
394: + initialContext.getEnvironment());
395: jndiEnv = initialContext.getEnvironment();
396: }
397: }
398:
399: /**
400: * Re-initializes the {@link InitialContext}.
401: */
402: public void reinit() throws Exception {
403: initialContext = null;
404: server = null;
405: init();
406: }
407:
408: /**
409: * Returns the JBoss server host from system property "jbosstest.server.host"
410: * This defaults to "localhost"
411: */
412: public String getServerHost() {
413: String hostName = System.getProperty("jbosstest.server.host",
414: "localhost");
415: return hostName;
416: }
417: }
|