KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > JBossTestSuite


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 /** Override the TestSuite to support the setting of a Properties collection
33  * associated with the TestSuite on each test.
34  *
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 37406 $
37  */

38 public class JBossTestSuite extends TestSuite
39 {
40    protected Properties JavaDoc props;
41
42    public JBossTestSuite(Properties JavaDoc props)
43    {
44       this.props = props;
45    }
46
47    public JBossTestSuite(Class JavaDoc theClass, String JavaDoc name, Properties JavaDoc props)
48    {
49       if (name == null)
50          name = theClass.getName();
51       super.setName(name);
52       this.props = props;
53
54       Class JavaDoc superClass = theClass;
55       while (Test.class.isAssignableFrom(superClass))
56       {
57          Method JavaDoc[] methods = superClass.getDeclaredMethods();
58          Method JavaDoc setProps = null;
59          try
60          {
61             Class JavaDoc[] sig = {Properties JavaDoc.class};
62             setProps = superClass.getMethod("setProps", sig);
63          }
64          catch (Throwable JavaDoc ignore)
65          {
66          }
67          for (int i = 0; i < methods.length; i++)
68          {
69             Method JavaDoc m = methods[i];
70             if( isPublicTestMethod(m) == false )
71                continue;
72             String JavaDoc testName = m.getName();
73             Test test = createTest(theClass, testName);
74             if (setProps != null)
75             {
76                Object JavaDoc[] args = {props};
77                try
78                {
79                   setProps.invoke(test, args);
80                }
81                catch (Throwable JavaDoc t)
82                {
83                   test = failure(t);
84                }
85             }
86             super.addTest(test);
87          }
88          superClass = superClass.getSuperclass();
89       }
90    }
91
92    public JBossTestSuite(Class JavaDoc theClass, Properties JavaDoc props)
93    {
94       this(theClass, null, props);
95    }
96
97    public JBossTestSuite(String JavaDoc name, Properties JavaDoc props)
98    {
99       super(name);
100       this.props = props;
101    }
102
103    public void addTestSuite(Class JavaDoc testClass)
104    {
105       super.addTest(new JBossTestSuite(testClass, props));
106    }
107
108    public static boolean isPublicTestMethod(Method JavaDoc m)
109    {
110       return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
111    }
112
113    public static boolean isTestMethod(Method JavaDoc m)
114    {
115       String JavaDoc name = m.getName();
116       Class JavaDoc[] parameters = m.getParameterTypes();
117       Class JavaDoc returnType = m.getReturnType();
118       return parameters.length == 0 && name.startsWith("test")
119          && returnType.equals(Void.TYPE);
120    }
121
122    /**
123     * Returns a test which will fail and log a warning message.
124     */

125    private static Test failure(final Throwable JavaDoc t)
126    {
127       return new TestCase("failure")
128       {
129          protected void runTest()
130          {
131             fail(t.getMessage());
132          }
133       };
134    }
135 }
136
Popular Tags