source: main/trunk/greenstone3/src/java/org/greenstone/testing/TestCaseLoader.java@ 27825

Last change on this file since 27825 was 25635, checked in by sjm84, 12 years ago

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1package org.greenstone.testing;
2import java.util.*;
3import java.lang.reflect.*;
4import junit.framework.*;
5/**
6 * Responsible for loading classes representing valid test cases.
7 */
8public class TestCaseLoader {
9 final private Vector<Class> classList = new Vector<Class> ();
10 final private String requiredType;
11
12 /**
13 * Adds <code>testCaseClass</code> to the list of classdes
14 * if the class is a test case we wish to load. Calls
15 * <code>shouldLoadTestCase ()</code> to determine that.
16 */
17 private void addClassIfTestCase (final Class testCaseClass) {
18 if (shouldAddTestCase (testCaseClass)) {
19 classList.add (testCaseClass);
20 }
21 }
22
23 /**
24 * Determine if we should load this test case.
25 */
26 private boolean shouldAddTestCase (final Class testCaseClass) {
27 boolean isOfTheCorrectType = false;
28 if (TestCase.class.isAssignableFrom(testCaseClass)) {
29 try {
30 Field testAllIgnoreThisField = testCaseClass.getDeclaredField("TEST_ALL_TEST_TYPE");
31 final int EXPECTED_MODIFIERS = Modifier.STATIC | Modifier.PUBLIC | Modifier.FINAL;
32 if (((testAllIgnoreThisField.getModifiers() & EXPECTED_MODIFIERS) != EXPECTED_MODIFIERS) ||
33 (testAllIgnoreThisField.getType() != String.class)) {
34 throw new IllegalArgumentException ("TEST_ALL_TEST_TYPE should be static public final String");
35 }
36 String testType = (String)testAllIgnoreThisField.get(testCaseClass);
37 isOfTheCorrectType = requiredType.equals (testType);
38 } catch (NoSuchFieldException e) {
39 } catch (IllegalAccessException e) {
40 throw new IllegalArgumentException ("The field " + testCaseClass.getName () + ".TEST_ALL_TEST_TYPE is not accessible.");
41 }
42 }
43 return isOfTheCorrectType;
44 }
45
46
47
48 /**
49 * Load the classes that represent test cases we are interested.
50 * @param classNamesIterator An iterator over a collection of fully qualified class names
51 */
52 public void loadTestCases (final Iterator classNamesIterator) {
53 while (classNamesIterator.hasNext ()) {
54 String className = (String)classNamesIterator.next ();
55 System.out.println("trying class "+className);
56 try {
57 Class candidateClass = Class.forName (className);
58 addClassIfTestCase (candidateClass);
59 } catch (ClassNotFoundException e) {
60 System.err.println ("Cannot load class: " + className + " " + e.getMessage ());
61 } catch (NoClassDefFoundError e) {
62 System.err.println ("Cannot load class that " + className + " is dependant on");
63 }
64 }
65 }
66
67 /**
68 * Construct this instance. Load all the test cases possible that derive
69 * from <code>baseClass</code> and cannot be ignored.
70 * @param classNamesIterator An iterator over a collection of fully qualified class names
71 */
72 public TestCaseLoader(final String requiredType) {
73 if (requiredType == null) throw new IllegalArgumentException ("requiredType is null");
74 this.requiredType = requiredType;
75 }
76
77 /**
78 * Obtain an iterator over the collection of test case classes loaded by <code>loadTestCases</code>
79 */
80 public Iterator<Class> getClasses () {
81 return classList.iterator ();
82 }
83}
Note: See TracBrowser for help on using the repository browser.