运行环境测试案例
随着 Java 开源,出现了更多的 Java 运行环境,除了 SUN 的参考实现外,BEA、IBM 均有自己的 Java 运行环境,更有如 Apache Harmony 的开源运行环境(尽管现在 Apache Harmony 尚不能称为 Java 运行环境)。运行环境测试案例,为运行环境开发者以及选择运行环境,都能提供一定的帮助。比如说下面的例子就是测试 java.util.ArrayList 和 java.util.Vector 在两个不同运行环境的表现。测试案例写法和普通的测试案例完全一样,我们只需告诉 p-unit 不同的运行环境的 Java 路径以及正确的 classpath,然后调用 runVMs 函数即可:
清单 9. p-unit 运行环境测试案例
public static void main(String[] args) { PUnitSoloRunner runner = new PUnitSoloRunner(); runner.addPUnitEventListener(new OverviewReporter(new ImageRender())); runner.runVMs(ListTestClass.class, new VM[] { VMConfig.HARMONY, VMConfig.SUN });}public class VMConfig { private static String CLASSPATH = " -cp correct_classpath_including_all_jars_and_path"; private static String HARMONY_PATH = "harmony_path\\bin\\java" + CLASSPATH; private static String SUN_PATH = "sun_path\\bin\\java" + CLASSPATH; public static VM HARMONY = new VM(HARMONY_PATH, "HarmONY"); public static VM SUN = new VM(SUN_PATH, "SUN");}public class ListTestClass { private static final int LIST_COUNT = 100000; private static Object element = new Object(); private Random indexGenerator = new Random();; public void testInsertArrayList() { ArrayList arrayList = new ArrayList(LIST_COUNT); insertSequence(arrayList); insertRandom(arrayList); } public void testInsertVector() { Vector vector = new Vector(LIST_COUNT); insertSequence(vector); insertRandom(vector); } public void insertSequence(List list) { for (int i = 0; i < LIST_COUNT; ++i) { list.add(element); } } public void insertRandom(List list) { for (int i = 0; i < LIST_COUNT; ++i) { list.add(indexGenerator .nextInt(LIST_COUNT),element); } } }
文章来源于领测软件测试网 https://www.ltesting.net/