Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/ide/TestCaseClassSet.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.max.ide; | |
24 | |
25 import static com.sun.max.lang.Classes.*; | |
26 | |
27 import java.lang.reflect.*; | |
28 import java.util.*; | |
29 | |
30 import junit.framework.*; | |
31 | |
32 import com.sun.max.*; | |
33 import com.sun.max.lang.*; | |
34 import com.sun.max.program.*; | |
35 | |
36 /** | |
37 * A utility for defining and refining a set of {@linkplain #isJUnitTestCaseClass(Class) valid} JUnit test case classes. | |
38 */ | |
39 public class TestCaseClassSet extends LinkedHashSet<Class<? extends TestCase>> { | |
40 | |
41 private final String defaultTestSuiteName; | |
42 | |
43 /** | |
44 * Creates a set of classes whose {@linkplain #toTestSuite() derived} test suite will have a given name. | |
45 * | |
46 * @param defaultTestSuiteName the default name to be used for the test suite derived from this set | |
47 */ | |
48 public TestCaseClassSet(String defaultTestSuiteName) { | |
49 this.defaultTestSuiteName = defaultTestSuiteName; | |
50 } | |
51 | |
52 /** | |
53 * Creates a set of classes by scanning a given package for {@linkplain #isJUnitTestCaseClass(Class) valid} JUnit | |
54 * test case classes. | |
55 * | |
56 * @param maxPackage the package to scan for classes | |
57 * @param scanSubPackages specifies if the sub-packages of {@code maxPackage} should also be scanned | |
58 */ | |
59 // public TestCaseClassSet(MaxPackage maxPackage, boolean scanSubPackages) { | |
60 // this(maxPackage.name(), scanSubPackages); | |
61 // } | |
62 // | |
63 // /** | |
64 // * Creates a set of classes by scanning a given package (but not its sub-packages) for | |
65 // * {@linkplain #isJUnitTestCaseClass(Class) valid} JUnit test case classes. | |
66 // * | |
67 // * @param maxPackage the package to scan for classes | |
68 // */ | |
69 // public TestCaseClassSet(MaxPackage maxPackage) { | |
70 // this(maxPackage, false); | |
71 // } | |
72 | |
73 public TestCaseClassSet(Class packageRepresentative) { | |
74 this(packageRepresentative, false); | |
75 } | |
76 | |
77 public TestCaseClassSet(Class packageRepresentative, boolean scanSubPackages) { | |
78 this(getPackageName(packageRepresentative), scanSubPackages); | |
79 } | |
80 | |
81 public TestCaseClassSet(final String packageName, final boolean scanSubPackages) { | |
82 defaultTestSuiteName = packageName; | |
83 new ClassSearch() { | |
84 @Override | |
85 protected boolean visitClass(boolean isArchiveEntry, String className) { | |
86 if (!className.endsWith("package-info")) { | |
87 if (scanSubPackages || (Classes.getPackageName(className).equals(packageName))) { | |
88 Class javaClass = Classes.forName(className, false, getClass().getClassLoader()); | |
89 if (isJUnitTestCaseClass(javaClass)) { | |
90 final Class<Class<? extends TestCase>> type = null; | |
91 add(Utils.cast(type, javaClass)); | |
92 } | |
93 } | |
94 } | |
95 return true; | |
96 } | |
97 }.run(Classpath.fromSystem(), packageName.replace('.', '/')); | |
98 } | |
99 | |
100 public static boolean isJUnitTestCaseClass(Class javaClass) { | |
101 return javaClass != null && !Modifier.isAbstract(javaClass.getModifiers()) && TestCase.class.isAssignableFrom(javaClass); | |
102 } | |
103 | |
104 /** | |
105 * Adds or moves a given set of classes to this set such that they will be returned after all existing entries | |
106 * when this set is iterated over. | |
107 * | |
108 * @param classes the classes to add | |
109 */ | |
110 public TestCaseClassSet addToEnd(Class... classes) { | |
111 for (int i = 0; i < classes.length; ++i) { | |
112 final Class c = classes[i]; | |
113 if (isJUnitTestCaseClass(c)) { | |
114 remove(c); | |
115 final Class<Class<? extends TestCase>> type = null; | |
116 add(Utils.cast(type, c)); | |
117 } else { | |
118 ProgramWarning.message("Class is not an instantiable subclass of TestCase: " + c); | |
119 } | |
120 } | |
121 return this; | |
122 } | |
123 | |
124 /** | |
125 * Removes a given set of classes from this set. | |
126 * | |
127 * @param classes the classes to remove | |
128 */ | |
129 public TestCaseClassSet removeAll(Class... classes) { | |
130 removeAll(java.util.Arrays.asList(classes)); | |
131 return this; | |
132 } | |
133 | |
134 /** | |
135 * Creates a test suite containing the tests defined by the classes in this set. | |
136 * | |
137 * @param name the name of the suite | |
138 * @return the created suite | |
139 */ | |
140 public TestSuite toTestSuite(String name) { | |
141 final TestSuite suite = new TestSuite(name); | |
142 for (Class<? extends TestCase> testClass : this) { | |
143 suite.addTestSuite(testClass); | |
144 } | |
145 return suite; | |
146 } | |
147 | |
148 public TestSuite toTestSuite() { | |
149 return toTestSuite(defaultTestSuiteName); | |
150 } | |
151 } |