serializationtestcase.java
来自「Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规」· Java 代码 · 共 263 行
JAVA
263 行
/*
* 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.ser;
import java.io.*;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Properties;
import org.mandarax.kernel.*;
import org.mandarax.util.xmlser.LogExceptionListener;
import test.org.mandarax.testsupport.TestUtils;
import java.beans.*;
/**
* Superclass for testing serialization of objects.
* From version 3.1, tests are supported for binary serialization as well as for (jdk 1.4 and later)
* xml serialization!
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 1.9.1
*/
public abstract class SerializationTestCase extends junit.framework.TestCase {
public static final String FILENAME4BIN_MODE = "sertest.ser";
public static final String FILENAME4XML_MODE = "sertest.xml";
public static final int BIN_SERIALIZE = 0;
public static final int XML_SERIALIZE = 1;
public static final int XML_SERIALIZE_WITH_DELEGATES = 2;
protected LogicFactory lfactory = LogicFactory.getDefaultFactory ();
// the serialization mode - one of the constants BIN_SERIALIZE, XML_SERIALIZE, XML_SERIALIZE_WITH_DELEGATES
private int serializationMode = BIN_SERIALIZE;
private Predicate predicate = new SimplePredicate("a predicate",new Class[]{SerializableTestObject.class,SerializableTestObject.class});
/**
* Constructor.
* @param lfactory the logic factory used
*/
public SerializationTestCase(LogicFactory lfactory) {
super ("test");
this.lfactory = lfactory;
}
/**
* Constructor.
*/
public SerializationTestCase() {
super ("test");
}
/**
* Set the serialization mode.
* @param serializationMode an int constant defined in the class
*/
public void setSerializationMode(int serMode) {
serializationMode = serMode;
}
/**
* Compare the original object and the object that has been recovered from
* the serialized file.
* @param obj original
* @param deserialized the deserialized object
* @return a boolean
*/
protected boolean compare(Object original,Object deserialized) {
boolean result = true;
if (original instanceof PropertiesSupport || deserialized instanceof PropertiesSupport) {
Properties p1 = ((PropertiesSupport)original).getProperties();
Properties p2 = ((PropertiesSupport)deserialized).getProperties();
result = result && p1==null?p2==null:p1.equals(p2);
}
return result && original.equals(deserialized);
}
/**
* Get the object to be tested.
* @return an object
*/
protected abstract Object getObject();
/**
* Perform the test.
*/
public void test() throws Exception {
String FILENAME = serializationMode==BIN_SERIALIZE?FILENAME4BIN_MODE:FILENAME4XML_MODE;
FILENAME = TestUtils.getFileName(FILENAME);
Object obj = getObject();
if (obj instanceof PropertiesSupport) {
PropertiesSupport ps = (PropertiesSupport)obj;
ps.setProperty("key1","value1");
ps.setProperty("key2","value2");
}
Object deserialized = null;
if (serializationMode==BIN_SERIALIZE) {
// BINARY
// serialize
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILENAME));
out.writeObject(obj);
out.close();
// deserialize and compare
ObjectInputStream in = new ObjectInputStream(new FileInputStream(FILENAME));
deserialized = in.readObject();
}
else if (serializationMode==XML_SERIALIZE){
// XML
// serialize
XMLEncoder encoder = new XMLEncoder(new FileOutputStream(FILENAME));
encoder.setExceptionListener(new LogExceptionListener());
// org.mandarax.util.xmlser.XMLEncoderConfigurator.configure(encoder);
encoder.writeObject(obj);
encoder.close();
// deserialize and compare
XMLDecoder decoder = new XMLDecoder(new FileInputStream(FILENAME));
decoder.setExceptionListener(new LogExceptionListener());
// org.mandarax.util.xmlser.XMLEncoderConfigurator.configure(decoder);
deserialized = decoder.readObject();
}
else if (serializationMode==XML_SERIALIZE_WITH_DELEGATES){
// XML
// serialize (use delegates)
XMLEncoder encoder = new XMLEncoder(new FileOutputStream(FILENAME));
encoder.setExceptionListener(new LogExceptionListener());
org.mandarax.util.xmlser.XMLEncoderConfigurator.configure(encoder);
encoder.writeObject(obj);
encoder.close();
// deserialize and compare
XMLDecoder decoder = new XMLDecoder(new FileInputStream(FILENAME));
decoder.setExceptionListener(new LogExceptionListener());
org.mandarax.util.xmlser.XMLEncoderConfigurator.configure(decoder);
deserialized = decoder.readObject();
}
assertTrue(compare(obj,deserialized));
}
/**
* Create a fact,
* @return a fact.
*/
protected Fact createFact() {
Term t1 = lfactory.createConstantTerm(new SerializableTestObject());
Term t2 = lfactory.createVariableTerm("y",SerializableTestObject.class);
Term[] terms = {t1,t2};
return lfactory.createFact(predicate,terms);
}
/**
* Create a prerequisite
* @return a prerequisite.
*/
protected Prerequisite createPrereq() {
Term t1 = lfactory.createConstantTerm(new SerializableTestObject());
Term t2 = lfactory.createVariableTerm("y",SerializableTestObject.class);
Term[] terms = {t1,t2};
return lfactory.createPrerequisite(predicate,terms,false);
}
/**
* Create a rule.
* @return a rule
*/
protected Rule createRule() {
Prerequisite f1 = createPrereq();
Prerequisite f2 = createPrereq();
Fact f3 = createFact();
java.util.List body = new java.util.ArrayList();
body.add(f1);
body.add(f2);
return lfactory.createRule(body,f3);
}
/**
* Create a query.
* @return a query
*/
protected Query createQuery() {
Fact[] queryFacts = {createFact(),createFact()};
return lfactory.createQuery(queryFacts,"a query");
}
/**
* Compare knowledge bases.
* @param obj original
* @param deserialized the deserialized knowledge base
* @return a boolean
*/
protected boolean compareKBs(KnowledgeBase kb1,Object deserialized) {
if (deserialized==null || !(deserialized instanceof KnowledgeBase)) return false;
KnowledgeBase kb2 = (KnowledgeBase)deserialized;
boolean result = true;
try {
// compare implementation classes
result = result && (kb1.getClass()==kb2.getClass());
// compare clauses
Iterator iter1 = kb1.clauses();
Iterator iter2 = kb2.clauses();
while (result && iter1.hasNext() && iter2.hasNext()) {
result = result && (iter1.next().equals(iter2.next()));
}
// compare queries
iter1 = kb1.queries();
iter2 = kb2.queries();
while (result && iter1.hasNext() && iter2.hasNext()) {
result = result && (iter1.next().equals(iter2.next()));
}
// if classes implement ExtendedKnowledgeBase, compare comparators
if (kb1 instanceof ExtendedKnowledgeBase && kb2 instanceof ExtendedKnowledgeBase) {
ExtendedKnowledgeBase xkb1 = (ExtendedKnowledgeBase)kb1;
ExtendedKnowledgeBase xkb2 = (ExtendedKnowledgeBase)kb2;
Comparator comp1 = xkb1.getComparator();
Comparator comp2 = xkb2.getComparator();
result = result && (comp1==null?(comp2==null):(comp1.equals(comp2)));
}
return result;
}
catch (ClauseSetException x) {
x.printStackTrace();
}
return false;
}
/**
* Convert the object to a string.
* @return a string
*/
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("Test ");
if (serializationMode==BIN_SERIALIZE) buf.append("binary");
else if (serializationMode==XML_SERIALIZE) buf.append("xml");
else if (serializationMode==XML_SERIALIZE_WITH_DELEGATES) buf.append("xml_del");
else buf.append(">");
buf.append(" serialization of instances of ");
buf.append(getObject().getClass());
return buf.toString();
}
/**
* Get the logic factory used.
* @return a logic factory
*/
public LogicFactory getLogicFactory() {
return lfactory;
}
/**
* Sets the logic factory.
* @param lfactory The logic factory to set
*/
public void setLogicFactory(LogicFactory lfactory) {
this.lfactory = lfactory;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?