abstracttestcase.java

来自「Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规」· Java 代码 · 共 123 行

JAVA
123
字号
/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
package test.org.mandarax.util.resultsetfilters;

import java.util.*;
import junit.framework.TestCase;
import org.mandarax.kernel.*;

import test.org.mandarax.testsupport.TestUtils;


/**
 * Superclass for test cases in this package.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 3.0
 */

public abstract class AbstractTestCase extends TestCase implements ExampleConstants {
	private Object[][] expectedResults = null;	
	private String name = null; 
	protected Collection expectedResultsAsStringList = null;
	/**
	 * Constructor.
	 * @param name the name of this test case
 	 * @param expected the expected results
	 */
	public AbstractTestCase(String name,Object[][] expected) {
		super("test");
		this.name = name;
		this.expectedResults = expected;	
	}
	/**
	 * Set up the test case.
	 */
	protected void setUp() throws Exception {
		super.setUp();
		// convert array to sorted list in order to facilitate comparing
		// and debugging 
		expectedResultsAsStringList = getPrefContainer();
		for (int i=0;i<expectedResults.length;i++) {
			expectedResultsAsStringList.add(TestUtils.record2string(expectedResults[i],TestKB.df));			
		}	
	}

	/**
	 * Convert the results (an entire result set) to a collection
	 * of strings.
	 * @param rs a result set
	 * @param container the collection used to store the strings
	 * @param return the container
	 */
	protected Collection asStrings(ResultSet rs) throws Exception {
		return TestUtils.asStrings(rs,getPrefContainer(),TestKB.df);
	}

	/**
	 * Get the container used to organise results. 
	 * The container selected will detemine whether the order of the resulst is retained etc.
	 */
	protected abstract Collection getPrefContainer() ;
	/**
	 * Get the filtered result set.
	 * @param rs the original result set
	 * @return the filtered result set
	 */
	protected abstract ResultSet filter(ResultSet rs) throws InferenceException ;
	/**
	 * Run the test case.
	 */
	public void test() throws Exception {
		// query result set
		ResultSet rs = TestKB.queryAll();
		// filter
		ResultSet filter = filter(rs);

		
		// compare result by result - first convert each result to a string and sort the strings
		// this is to facilitate debugging
		Collection computedResultsAsStringList = asStrings(filter);
		
		if (expectedResultsAsStringList.size()!=computedResultsAsStringList.size()) assertTrue(false);
		
		Iterator exIter = expectedResultsAsStringList.iterator();
		Iterator compIter = computedResultsAsStringList.iterator();
		
		while(exIter.hasNext() && compIter.hasNext()) {
			String nextExpected = exIter.next().toString();
			String nextComputed = compIter.next().toString();
			if (!nextExpected.equals(nextComputed)) {
				assertTrue(false);
			}
		}
		assertTrue(true);
	}
	/**
	 * Convert the object to a string.
	 * @return the string representation of this object.
	 */
	public String toString() {
		return name;
	}



}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?