001: /* AltTestSuite.java
002: *
003: * Created on Feb 20, 2007
004: *
005: * Copyright (C) 2007 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.archive.crawler.selftest;
024:
025: import java.lang.reflect.Method;
026: import java.lang.reflect.Modifier;
027: import java.util.Vector;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: /**
033: * Variant TestSuite that can build tests including methods with an alternate
034: * prefix (other than 'test'). Copies code from TestSuite because necessary
035: * methods to change are private rather than protected.
036: *
037: * @author gojomo
038: * @version $Id: MaxLinkHopsSelfTest.java 4667 2006-09-26 20:38:48 +0000 (Tue, 26 Sep 2006) paul_jack $
039: */
040: public class AltTestSuite extends TestSuite {
041: /** a method prefix other than 'test' that is also recognized as tests */
042: String altPrefix;
043:
044: /**
045: * Constructs a TestSuite from the given class. Copied from superclass so
046: * that local alternate addTestMethod() will be visible, which in turn uses
047: * an isTestMethod() that accepts methods with the altPrefix in addition
048: * to 'test'.
049: * @param theClass Class from which to build suite
050: * @param prefix alternate method prefix to also find test methods
051: */
052: public AltTestSuite(final Class theClass, String prefix) {
053: this .altPrefix = prefix;
054: setName(theClass.getName());
055: try {
056: getTestConstructor(theClass); // Avoid generating multiple error messages
057: } catch (NoSuchMethodException e) {
058: addTest(warning("Class "
059: + theClass.getName()
060: + " has no public constructor TestCase(String name) or TestCase()"));
061: return;
062: }
063:
064: if (!Modifier.isPublic(theClass.getModifiers())) {
065: addTest(warning("Class " + theClass.getName()
066: + " is not public"));
067: return;
068: }
069:
070: Class super Class = theClass;
071: Vector names = new Vector();
072: while (Test.class.isAssignableFrom(super Class)) {
073: Method[] methods = super Class.getDeclaredMethods();
074: for (int i = 0; i < methods.length; i++) {
075: addTestMethod(methods[i], names, theClass);
076: }
077: super Class = super Class.getSuperclass();
078: }
079: if (testCount() == 0)
080: addTest(warning("No tests found in " + theClass.getName()));
081: }
082:
083: // copied from superclass
084: private void addTestMethod(Method m, Vector names, Class theClass) {
085: String name = m.getName();
086: if (names.contains(name))
087: return;
088: if (!isPublicTestMethod(m)) {
089: if (isTestMethod(m))
090: addTest(warning("Test method isn't public: "
091: + m.getName()));
092: return;
093: }
094: names.addElement(name);
095: addTest(createTest(theClass, name));
096: }
097:
098: // copied from superclass
099: private boolean isPublicTestMethod(Method m) {
100: return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
101: }
102:
103: // copied & extended from superclass
104: private boolean isTestMethod(Method m) {
105: String name = m.getName();
106: Class[] parameters = m.getParameterTypes();
107: Class returnType = m.getReturnType();
108: return parameters.length == 0
109: && (name.startsWith("test") || name
110: .startsWith(altPrefix))
111: && returnType.equals(Void.TYPE);
112: }
113: }
|