| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.test; |
| 16 | |
| 17 | import java.lang.reflect.Method; |
| 18 | import java.lang.reflect.Modifier; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import junit.framework.Test; |
| 23 | import junit.framework.TestCase; |
| 24 | import junit.framework.TestSuite; |
| 25 | |
| 26 | /** |
| 27 | * @author Jean-Francois Poilpret |
| 28 | */ |
| 29 | final public class TestSuiteHelper |
| 30 | { |
| 31 | private TestSuiteHelper() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | static public void addAllTests(TestSuite suite, Class clazz, TestFactory factory) |
| 36 | { |
| 37 | Class superClass = clazz; |
| 38 | List<String> names = new ArrayList<String>(); |
| 39 | while (Test.class.isAssignableFrom(superClass)) |
| 40 | { |
| 41 | Method[] methods = superClass.getDeclaredMethods(); |
| 42 | for (int i = 0; i < methods.length; i++) |
| 43 | { |
| 44 | addTestMethod(suite, methods[i], names, factory); |
| 45 | } |
| 46 | superClass = superClass.getSuperclass(); |
| 47 | } |
| 48 | if (suite.testCount() == 0) |
| 49 | { |
| 50 | suite.addTest(warning("No tests found in " + clazz.getName())); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static private void addTestMethod( TestSuite suite, |
| 55 | Method m, |
| 56 | List<String> names, |
| 57 | TestFactory factory) |
| 58 | { |
| 59 | String name = m.getName(); |
| 60 | if (names.contains(name)) |
| 61 | { |
| 62 | return; |
| 63 | } |
| 64 | if (!isPublicTestMethod(m)) |
| 65 | { |
| 66 | if (isTestMethod(m)) |
| 67 | { |
| 68 | suite.addTest(warning("Test method isn't public: " + m.getName())); |
| 69 | } |
| 70 | return; |
| 71 | } |
| 72 | names.add(name); |
| 73 | suite.addTest(factory.createTest(name)); |
| 74 | } |
| 75 | |
| 76 | static private boolean isPublicTestMethod(Method m) |
| 77 | { |
| 78 | return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); |
| 79 | } |
| 80 | |
| 81 | static private boolean isTestMethod(Method m) |
| 82 | { |
| 83 | String name = m.getName(); |
| 84 | Class[] parameters = m.getParameterTypes(); |
| 85 | Class returnType = m.getReturnType(); |
| 86 | return (parameters.length == 0) |
| 87 | && (name.startsWith("test") && returnType.equals(Void.TYPE)); |
| 88 | } |
| 89 | |
| 90 | static private Test warning(final String message) |
| 91 | { |
| 92 | return new TestCase("warning") |
| 93 | { |
| 94 | protected void runTest() |
| 95 | { |
| 96 | fail(message); |
| 97 | } |
| 98 | }; |
| 99 | } |
| 100 | } |