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.lang.reflect.Method;
025: import java.lang.reflect.Modifier;
026: import java.util.Properties;
027:
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031:
032: /** Override the TestSuite to support the setting of a Properties collection
033: * associated with the TestSuite on each test.
034: *
035: * @author Scott.Stark@jboss.org
036: * @version $Revision: 57211 $
037: */
038: public class JBossTestSuite extends TestSuite {
039: protected Properties props;
040:
041: public JBossTestSuite(Properties props) {
042: this .props = props;
043: }
044:
045: public JBossTestSuite(Class theClass, String name, Properties props) {
046: if (name == null)
047: name = theClass.getName();
048: super .setName(name);
049: this .props = props;
050:
051: Class super Class = theClass;
052: while (Test.class.isAssignableFrom(super Class)) {
053: Method[] methods = super Class.getDeclaredMethods();
054: Method setProps = null;
055: try {
056: Class[] sig = { Properties.class };
057: setProps = super Class.getMethod("setProps", sig);
058: } catch (Throwable ignore) {
059: }
060: for (int i = 0; i < methods.length; i++) {
061: Method m = methods[i];
062: if (isPublicTestMethod(m) == false)
063: continue;
064: String testName = m.getName();
065: Test test = createTest(theClass, testName);
066: if (setProps != null) {
067: Object[] args = { props };
068: try {
069: setProps.invoke(test, args);
070: } catch (Throwable t) {
071: test = failure(t);
072: }
073: }
074: super .addTest(test);
075: }
076: super Class = super Class.getSuperclass();
077: }
078: }
079:
080: public JBossTestSuite(Class theClass, Properties props) {
081: this (theClass, null, props);
082: }
083:
084: public JBossTestSuite(String name, Properties props) {
085: super (name);
086: this .props = props;
087: }
088:
089: public void addTestSuite(Class testClass) {
090: super .addTest(new JBossTestSuite(testClass, props));
091: }
092:
093: public static boolean isPublicTestMethod(Method m) {
094: return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
095: }
096:
097: public static boolean isTestMethod(Method m) {
098: String name = m.getName();
099: Class[] parameters = m.getParameterTypes();
100: Class returnType = m.getReturnType();
101: return parameters.length == 0 && name.startsWith("test")
102: && returnType.equals(Void.TYPE);
103: }
104:
105: /**
106: * Returns a test which will fail and log a warning message.
107: */
108: private static Test failure(final Throwable t) {
109: return new TestCase("failure") {
110: protected void runTest() {
111: fail(t.getMessage());
112: }
113: };
114: }
115: }
|