📄 bulktest.java
字号:
/*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
/**
* A {@link TestCase} that can define both simple and bulk test methods.<P>
*
* A <I>simple test method</I> is the type of test traditionally
* supplied by by {@link TestCase}. To define a simple test, create a public
* no-argument method whose name starts with "test". You can specify the
* the name of simple test in the constructor of <Code>BulkTest</Code>;
* a subsequent call to {@link TestCase#run} will run that simple test.<P>
*
* A <I>bulk test method</I>, on the other hand, returns a new instance
* of <Code>BulkTest</Code>, which can itself define new simple and bulk
* test methods. By using the {@link #makeSuite} method, you can
* automatically create a hierarchal suite of tests and child bulk tests.<P>
*
* For instance, consider the following two classes:
*
* <Pre>
* public class TestSet extends BulkTest {
*
* private Set set;
*
* public TestSet(Set set) {
* this.set = set;
* }
*
* public void testContains() {
* boolean r = set.contains(set.iterator().next()));
* assertTrue("Set should contain first element, r);
* }
*
* public void testClear() {
* set.clear();
* assertTrue("Set should be empty after clear", set.isEmpty());
* }
* }
*
*
* public class TestHashMap extends BulkTest {
*
* private Map makeFullMap() {
* HashMap result = new HashMap();
* result.put("1", "One");
* result.put("2", "Two");
* return result;
* }
*
* public void testClear() {
* Map map = makeFullMap();
* map.clear();
* assertTrue("Map empty after clear", map.isEmpty());
* }
*
* public BulkTest bulkTestKeySet() {
* return new TestSet(makeFullMap().keySet());
* }
*
* public BulkTest bulkTestEntrySet() {
* return new TestSet(makeFullMap().entrySet());
* }
* }
* </Pre>
*
* In the above examples, <Code>TestSet</Code> defines two
* simple test methods and no bulk test methods; <Code>TestHashMap</Code>
* defines one simple test method and two bulk test methods. When
* <Code>makeSuite(TestHashMap.class).run</Code> is executed,
* <I>five</I> simple test methods will be run, in this order:<P>
*
* <Ol>
* <Li>TestHashMap.testClear()
* <Li>TestHashMap.bulkTestKeySet().testContains();
* <Li>TestHashMap.bulkTestKeySet().testClear();
* <Li>TestHashMap.bulkTestEntrySet().testContains();
* <Li>TestHashMap.bulkTestEntrySet().testClear();
* </Ol>
*
* In the graphical junit test runners, the tests would be displayed in
* the following tree:<P>
*
* <UL>
* <LI>TestHashMap</LI>
* <UL>
* <LI>testClear
* <LI>bulkTestKeySet
* <UL>
* <LI>testContains
* <LI>testClear
* </UL>
* <LI>bulkTestEntrySet
* <UL>
* <LI>testContains
* <LI>testClear
* </UL>
* </UL>
* </UL>
*
* A subclass can override a superclass's bulk test by
* returning <Code>null</Code> from the bulk test method. If you only
* want to override specific simple tests within a bulk test, use the
* {@link #ignoredSimpleTests} method.<P>
*
* Note that if you want to use the bulk test methods, you <I>must</I>
* define your <Code>suite()</Code> method to use {@link #makeSuite}.
* The ordinary {@link TestSuite} constructor doesn't know how to
* interpret bulk test methods.
*
* @author Paul Jack
* @version $Id: BulkTest.java,v 1.2.2.1 2004/05/22 12:14:05 scolebourne Exp $
*/
public class BulkTest extends TestCase implements Cloneable {
// Note: BulkTest is Cloneable to make it easier to construct
// BulkTest instances for simple test methods that are defined in
// anonymous inner classes. Basically we don't have to worry about
// finding wierd constructors. (And even if we found them, techinically
// it'd be illegal for anyone but the outer class to invoke them).
// Given one BulkTest instance, we can just clone it and reset the
// method name for every simple test it defines.
/**
* The full name of this bulk test instance. This is the full name
* that is compared to {@link #ignoredSimpleTests} to see if this
* test should be ignored. It's also displayed in the text runner
* to ease debugging.
*/
String verboseName;
/**
* Constructs a new <Code>BulkTest</Code> instance that will run the
* specified simple test.
*
* @param name the name of the simple test method to run
*/
public BulkTest(String name) {
super(name);
this.verboseName = getClass().getName();
}
/**
* Creates a clone of this <Code>BulkTest</Code>.<P>
*
* @return a clone of this <Code>BulkTest</Code>
*/
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new Error(); // should never happen
}
}
/**
* Returns an array of simple test names to ignore.<P>
*
* If a simple test that's defined by this <Code>BulkTest</Code> or
* by one of its bulk test methods has a name that's in the returned
* array, then that simple test will not be executed.<P>
*
* A simple test's name is formed by taking the class name of the
* root <Code>BulkTest</Code>, eliminating the package name, then
* appending the names of any bulk test methods that were invoked
* to get to the simple test, and then appending the simple test
* method name. The method names are delimited by periods:
*
* <Pre>
* TestHashMap.bulkTestEntrySet.testClear
* </Pre>
*
* is the name of one of the simple tests defined in the sample classes
* described above. If the sample <Code>TestHashMap</Code> class
* included this method:
*
* <Pre>
* public String[] ignoredSimpleTests() {
* return new String[] { "TestHashMap.bulkTestEntrySet.testClear" };
* }
* </Pre>
*
* then the entry set's clear method wouldn't be tested, but the key
* set's clear method would.
*
* @return an array of the names of simple tests to ignore, or null if
* no tests should be ignored
*/
public String[] ignoredSimpleTests() {
return null;
}
/**
* Returns the display name of this <Code>BulkTest</Code>.
*
* @return the display name of this <Code>BulkTest</Code>
*/
public String toString() {
return getName() + "(" + verboseName + ") ";
}
/**
* Returns a {@link TestSuite} for testing all of the simple tests
* <I>and</I> all the bulk tests defined by the given class.<P>
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -