001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package test.org.mandarax.ser;
019:
020: import java.io.*;
021: import java.util.Comparator;
022: import java.util.Iterator;
023: import java.util.Properties;
024: import org.mandarax.kernel.*;
025: import org.mandarax.util.xmlser.LogExceptionListener;
026:
027: import test.org.mandarax.testsupport.TestUtils;
028:
029: import java.beans.*;
030:
031: /**
032: * Superclass for testing serialization of objects.
033: * From version 3.1, tests are supported for binary serialization as well as for (jdk 1.4 and later)
034: * xml serialization!
035: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
036: * @version 3.4 <7 March 05>
037: * @since 1.9.1
038: */
039: public abstract class SerializationTestCase extends
040: junit.framework.TestCase {
041: public static final String FILENAME4BIN_MODE = "sertest.ser";
042: public static final String FILENAME4XML_MODE = "sertest.xml";
043: public static final int BIN_SERIALIZE = 0;
044: public static final int XML_SERIALIZE = 1;
045: public static final int XML_SERIALIZE_WITH_DELEGATES = 2;
046:
047: protected LogicFactory lfactory = LogicFactory.getDefaultFactory();
048: // the serialization mode - one of the constants BIN_SERIALIZE, XML_SERIALIZE, XML_SERIALIZE_WITH_DELEGATES
049: private int serializationMode = BIN_SERIALIZE;
050: private Predicate predicate = new SimplePredicate("a predicate",
051: new Class[] { SerializableTestObject.class,
052: SerializableTestObject.class });
053:
054: /**
055: * Constructor.
056: * @param lfactory the logic factory used
057: */
058: public SerializationTestCase(LogicFactory lfactory) {
059: super ("test");
060: this .lfactory = lfactory;
061: }
062:
063: /**
064: * Constructor.
065: */
066: public SerializationTestCase() {
067: super ("test");
068: }
069:
070: /**
071: * Set the serialization mode.
072: * @param serializationMode an int constant defined in the class
073: */
074: public void setSerializationMode(int serMode) {
075: serializationMode = serMode;
076: }
077:
078: /**
079: * Compare the original object and the object that has been recovered from
080: * the serialized file.
081: * @param obj original
082: * @param deserialized the deserialized object
083: * @return a boolean
084: */
085: protected boolean compare(Object original, Object deserialized) {
086: boolean result = true;
087: if (original instanceof PropertiesSupport
088: || deserialized instanceof PropertiesSupport) {
089: Properties p1 = ((PropertiesSupport) original)
090: .getProperties();
091: Properties p2 = ((PropertiesSupport) deserialized)
092: .getProperties();
093: result = result && p1 == null ? p2 == null : p1.equals(p2);
094: }
095: return result && original.equals(deserialized);
096: }
097:
098: /**
099: * Get the object to be tested.
100: * @return an object
101: */
102: protected abstract Object getObject();
103:
104: /**
105: * Perform the test.
106: */
107: public void test() throws Exception {
108: String FILENAME = serializationMode == BIN_SERIALIZE ? FILENAME4BIN_MODE
109: : FILENAME4XML_MODE;
110: FILENAME = TestUtils.getFileName(FILENAME);
111: Object obj = getObject();
112: if (obj instanceof PropertiesSupport) {
113: PropertiesSupport ps = (PropertiesSupport) obj;
114: ps.setProperty("key1", "value1");
115: ps.setProperty("key2", "value2");
116: }
117: Object deserialized = null;
118: if (serializationMode == BIN_SERIALIZE) {
119: // BINARY
120: // serialize
121: ObjectOutputStream out = new ObjectOutputStream(
122: new FileOutputStream(FILENAME));
123: out.writeObject(obj);
124: out.close();
125: // deserialize and compare
126: ObjectInputStream in = new ObjectInputStream(
127: new FileInputStream(FILENAME));
128: deserialized = in.readObject();
129: } else if (serializationMode == XML_SERIALIZE) {
130: // XML
131: // serialize
132: XMLEncoder encoder = new XMLEncoder(new FileOutputStream(
133: FILENAME));
134: encoder.setExceptionListener(new LogExceptionListener());
135: // org.mandarax.util.xmlser.XMLEncoderConfigurator.configure(encoder);
136: encoder.writeObject(obj);
137: encoder.close();
138: // deserialize and compare
139: XMLDecoder decoder = new XMLDecoder(new FileInputStream(
140: FILENAME));
141: decoder.setExceptionListener(new LogExceptionListener());
142: // org.mandarax.util.xmlser.XMLEncoderConfigurator.configure(decoder);
143: deserialized = decoder.readObject();
144: } else if (serializationMode == XML_SERIALIZE_WITH_DELEGATES) {
145: // XML
146: // serialize (use delegates)
147: XMLEncoder encoder = new XMLEncoder(new FileOutputStream(
148: FILENAME));
149: encoder.setExceptionListener(new LogExceptionListener());
150: org.mandarax.util.xmlser.XMLEncoderConfigurator
151: .configure(encoder);
152: encoder.writeObject(obj);
153: encoder.close();
154: // deserialize and compare
155: XMLDecoder decoder = new XMLDecoder(new FileInputStream(
156: FILENAME));
157: decoder.setExceptionListener(new LogExceptionListener());
158: org.mandarax.util.xmlser.XMLEncoderConfigurator
159: .configure(decoder);
160: deserialized = decoder.readObject();
161: }
162: assertTrue(compare(obj, deserialized));
163: }
164:
165: /**
166: * Create a fact,
167: * @return a fact.
168: */
169: protected Fact createFact() {
170: Term t1 = lfactory
171: .createConstantTerm(new SerializableTestObject());
172: Term t2 = lfactory.createVariableTerm("y",
173: SerializableTestObject.class);
174: Term[] terms = { t1, t2 };
175: return lfactory.createFact(predicate, terms);
176: }
177:
178: /**
179: * Create a prerequisite
180: * @return a prerequisite.
181: */
182: protected Prerequisite createPrereq() {
183: Term t1 = lfactory
184: .createConstantTerm(new SerializableTestObject());
185: Term t2 = lfactory.createVariableTerm("y",
186: SerializableTestObject.class);
187: Term[] terms = { t1, t2 };
188: return lfactory.createPrerequisite(predicate, terms, false);
189: }
190:
191: /**
192: * Create a rule.
193: * @return a rule
194: */
195: protected Rule createRule() {
196: Prerequisite f1 = createPrereq();
197: Prerequisite f2 = createPrereq();
198: Fact f3 = createFact();
199: java.util.List body = new java.util.ArrayList();
200: body.add(f1);
201: body.add(f2);
202: return lfactory.createRule(body, f3);
203: }
204:
205: /**
206: * Create a query.
207: * @return a query
208: */
209: protected Query createQuery() {
210: Fact[] queryFacts = { createFact(), createFact() };
211: return lfactory.createQuery(queryFacts, "a query");
212: }
213:
214: /**
215: * Compare knowledge bases.
216: * @param obj original
217: * @param deserialized the deserialized knowledge base
218: * @return a boolean
219: */
220: protected boolean compareKBs(KnowledgeBase kb1, Object deserialized) {
221: if (deserialized == null
222: || !(deserialized instanceof KnowledgeBase))
223: return false;
224: KnowledgeBase kb2 = (KnowledgeBase) deserialized;
225: boolean result = true;
226: try {
227: // compare implementation classes
228: result = result && (kb1.getClass() == kb2.getClass());
229: // compare clauses
230: Iterator iter1 = kb1.clauses();
231: Iterator iter2 = kb2.clauses();
232: while (result && iter1.hasNext() && iter2.hasNext()) {
233: result = result && (iter1.next().equals(iter2.next()));
234: }
235: // compare queries
236: iter1 = kb1.queries();
237: iter2 = kb2.queries();
238: while (result && iter1.hasNext() && iter2.hasNext()) {
239: result = result && (iter1.next().equals(iter2.next()));
240: }
241: // if classes implement ExtendedKnowledgeBase, compare comparators
242: if (kb1 instanceof ExtendedKnowledgeBase
243: && kb2 instanceof ExtendedKnowledgeBase) {
244: ExtendedKnowledgeBase xkb1 = (ExtendedKnowledgeBase) kb1;
245: ExtendedKnowledgeBase xkb2 = (ExtendedKnowledgeBase) kb2;
246: Comparator comp1 = xkb1.getComparator();
247: Comparator comp2 = xkb2.getComparator();
248: result = result
249: && (comp1 == null ? (comp2 == null) : (comp1
250: .equals(comp2)));
251:
252: }
253: return result;
254: } catch (ClauseSetException x) {
255: x.printStackTrace();
256: }
257: return false;
258:
259: }
260:
261: /**
262: * Convert the object to a string.
263: * @return a string
264: */
265: public String toString() {
266: StringBuffer buf = new StringBuffer();
267: buf.append("Test ");
268: if (serializationMode == BIN_SERIALIZE)
269: buf.append("binary");
270: else if (serializationMode == XML_SERIALIZE)
271: buf.append("xml");
272: else if (serializationMode == XML_SERIALIZE_WITH_DELEGATES)
273: buf.append("xml_del");
274: else
275: buf.append(">");
276: buf.append(" serialization of instances of ");
277: buf.append(getObject().getClass());
278: return buf.toString();
279: }
280:
281: /**
282: * Get the logic factory used.
283: * @return a logic factory
284: */
285: public LogicFactory getLogicFactory() {
286: return lfactory;
287: }
288:
289: /**
290: * Sets the logic factory.
291: * @param lfactory The logic factory to set
292: */
293: public void setLogicFactory(LogicFactory lfactory) {
294: this.lfactory = lfactory;
295: }
296:
297: }
|