KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > common > TestUtils


1 package org.jacorb.test.common;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2003 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.lang.reflect.Method JavaDoc;
25 import java.net.*;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Utility class used to setup JUnit-TestSuite
31  *
32  * @author Alphonse Bendt
33  * @version $Id: TestUtils.java,v 1.3 2005/05/10 13:32:38 andre.spiegel Exp $
34  */

35
36 public class TestUtils
37 {
38     private static final String JavaDoc[] STRING_ARRAY_TEMPLATE = new String JavaDoc[0];
39     private static String JavaDoc testHome = null;
40     
41     /**
42      * this method returns a List of all public Methods which Names start with the Prefix "test" and
43      * accept no Parameters e.g:
44      *
45      * <ul>
46      * <li>testOperation
47      * <li>testSomething
48      * </ul>
49      *
50      */

51     public static String JavaDoc[] getTestMethods(Class JavaDoc clazz)
52     {
53         return getTestMethods(clazz, "test");
54     }
55
56     public static String JavaDoc[] getTestMethods(Class JavaDoc clazz, String JavaDoc prefix)
57     {
58         Method JavaDoc[] methods = clazz.getMethods();
59
60         List JavaDoc result = new ArrayList JavaDoc();
61
62         for (int x = 0; x < methods.length; ++x)
63         {
64             if (methods[x].getName().startsWith(prefix))
65             {
66                 if (methods[x].getParameterTypes().length == 0)
67                 {
68                     result.add(methods[x].getName());
69                 }
70             }
71         }
72
73         return (String JavaDoc[]) result.toArray(STRING_ARRAY_TEMPLATE);
74     }
75     
76     /**
77      * Returns the name of the home directory of this regression suite.
78      */

79     public static String JavaDoc testHome()
80     {
81         if (testHome == null)
82         {
83             URL url = TestUtils.class.getResource("/.");
84             String JavaDoc result = url.toString();
85             if (result.matches("file:/.*?/classes/"))
86                 // strip the leading "file:" and the trailing
87
// "/classes/" from the result
88
result = result.substring (5, result.length() - 9);
89             else
90                 throw new RuntimeException JavaDoc ("cannot find test home");
91             testHome = result;
92         }
93         return testHome;
94     }
95 }
Popular Tags