📄 basetestcase.java
字号:
/* * LingPipe v. 3.5 * Copyright (C) 2003-2008 Alias-i * * This program is licensed under the Alias-i Royalty Free License * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Alias-i * Royalty Free License Version 1 for more details. * * You should have received a copy of the Alias-i Royalty Free License * Version 1 along with this program; if not, visit * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211, * +1 (718) 290-9170. */package com.aliasi.test.unit;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.Iterator;import junit.framework.TestCase;/** * An extended test case that provides several extended services for * assertion. May be extended to form a test case, or used through * its static methods. * * <P> * Extended equality testing includes support for symmetric testing * of equality along with hash code quality. Extended inequality * must be symmetric. * </p> * * <P> * Extended serialization testing is performed by serializing * an object into memory, deserializing it, and testing full equality * of the object and the result of serializing and deserializing. * </p> * * @author Bob Carpenter * @version 2.0 * @since LingPipe1.0 */public class BaseTestCase extends TestCase { private static String EMPTY_MSG = ""; /** * Construct a base test case. */ public BaseTestCase() { super(); } /** * Construct a base test case with the specified name. * * @param name Name of the test case. */ public BaseTestCase(String name) { super(name); } /** * Asserts that the two specified objects are not * both <code>null</code> and not both non-<code>null</code> * and equal. * * @param x First object to test. * @param y Second object to test. */ protected void assertNotEquals(Object x, Object y) { assertNotEquals("",x,y); } /** * Asserts that the two specified objects are not * both <code>null</code> and not both non-<code>null</code> * and equal. * * @param msg Message to return for failure of assertion. * @param x First object to test. * @param y Second object to test. */ protected void assertNotEquals(String msg, Object x, Object y) { if (x == null) { assertNotNull(msg,y); // if x is null, y must not be return; } if (y == null) return; // if x is not null, y can be assertFalse(msg,x.equals(y)); } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param xs First array to test. * @param ys Second array to test. */ protected void assertEqualsArray(Object[] xs, Object[] ys) { assertEqualsArray("",xs,ys); } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param msg Message to return if tests fial. * @param xs First array to test. * @param ys Second array to test. */ protected void assertEqualsArray(String msg, Object[] xs, Object[] ys) { assertNotNull("Null first array. " + msg, xs); assertNotNull("Null second array. " + msg, ys); for (int i = 0; i < xs.length && i < ys.length; ++i) { if (xs[i] == null && ys[i] == null) continue; if (xs[i] == null || ys[i] == null) { fail(" Element xs[" + i + "]=" + xs[i] + " != ys[" + i + "]=" + ys[i]); } else if (!xs[i].equals(ys[i])) { fail("xs[" + i + "]==" + xs[i] + " != ys[" + i + "]=" + ys[i]); } } if (xs.length != ys.length) { fail("xs.length=" + xs.length + " != ys.length=" + ys.length); } } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param xs First array to test. * @param ys Second array to test. */ protected void assertEqualsArray(int[] xs, int[] ys) { assertEqualsArray("",xs,ys); } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param msg Message to return if tests fial. * @param xs First array to test. * @param ys Second array to test. */ protected void assertEqualsArray(String msg, int[] xs, int[] ys) { assertNotNull("Null first array. " + msg, xs); assertNotNull("Null second array. " + msg, ys); assertEquals("Arrays have different lengths. " + msg, xs.length,ys.length); for (int i = 0; i < xs.length; ++i) { assertEquals("Element " + i + " differs. " + msg, xs[i],ys[i]); } } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param xs First array to test. * @param ys Second array to test. * @param tolerance Tolerance of double equality test. */ protected void assertEqualsArray(double[] xs, double[] ys, double tolerance) { assertEqualsArray("",xs,ys,tolerance); } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param msg Message to return if tests fial. * @param xs First array to test. * @param ys Second array to test. * @param tolerance Tolerance of double equality test. */ protected void assertEqualsArray(String msg, double[] xs, double[] ys, double tolerance) { assertNotNull("Null first array. " + msg, xs); assertNotNull("Null second array. " + msg, ys); assertEquals("Arrays have different lengths. " + msg, xs.length,ys.length); for (int i = 0; i < xs.length; ++i) assertEquals("row + " + i + ": " + msg, xs[i],ys[i],tolerance); } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param xs First array to test. * @param ys Second array to test. */ protected void assertEqualsArray(char[] xs, char[] ys) { assertEqualsArray("",xs,ys); } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param msg Message to return if tests fial. * @param xs First array to test. * @param ys Second array to test. */ protected void assertEqualsArray(String msg, char[] xs, char[] ys) { assertNotNull("Null first array. " + msg, xs); assertNotNull("null second array. " + msg, ys); assertEquals("Arrays have different lengths. " + msg, xs.length,ys.length); for (int i = 0; i < xs.length; ++i) { assertEquals("Element " + i + " differs. " + msg, xs[i],ys[i]); } } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param xs First array to test. * @param ys Second array to test. */ protected void assertEqualsArray(byte[] xs, byte[] ys) { assertEqualsArray("",xs,ys); } /** * Tests that the two specified arrays are not null, the same * length, and the element at each index is equal. * * @param msg Message to return if tests fial. * @param xs First array to test. * @param ys Second array to test. */ protected void assertEqualsArray(String msg, byte[] xs, byte[] ys) { assertNotNull("Null first array. " + msg, xs); assertNotNull("null second array. " + msg, ys); assertEquals("Arrays have different lengths. " + msg, xs.length,ys.length); for (int i = 0; i < xs.length; ++i) { assertEquals("Element " + i + " differs. " + msg, xs[i],ys[i]); } } /** * Asserts that two objects are equal and they obey the subsequent * requirements of the equality contract. First, the objects must * be symmetrically equal, so that both <code>x.equals(y)</code> * and <code>y.equals(x)</code>. In addition, their hash codes * must be equivalent to pass this test. * * @param x First object to test for equality. * @param y Second object to test for equality. */ protected void assertFullEquals(Object x, Object y) { assertFullEquals(EMPTY_MSG,x,y); } /** * Asserts that two objects are equal and they obey the subsequent * requirements of the equality contract. First, the objects must * be symmetrically equal, so that both <code>x.equals(y)</code> * and <code>y.equals(x)</code>. In addition, their hash codes * must be equivalent to pass this test. * * @param msg Message to print upon failure. * @param x First object to test for equality. * @param y Second object to test for equality. */ protected void assertFullEquals(String msg, Object x, Object y) { BaseTestCase.assertFullEquals(msg,x,y,this); } /** * Asserts that two objects are not equal and they obey the * subsequent reqirements of the equality contract. The objects * must be symmetrically not equal, so that * <code>!x.equals(y)</code> and <code>!y.equals(x)</code>. Note * that their hash codes may be equivalent. * * @param x First object to test for inequality. * @param y Second object to test for inequality. */ protected void assertFullNotEquals(Object x, Object y) { assertFullNotEquals(EMPTY_MSG,x,y); } /** * Asserts that two objects are not equal and they obey the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -