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.zkb;
019:
020: import java.io.File;
021: import java.util.Comparator;
022: import java.util.Iterator;
023: import org.mandarax.kernel.Clause;
024: import org.mandarax.kernel.ClauseSet;
025: import org.mandarax.kernel.ClauseSetException;
026: import org.mandarax.kernel.ExtendedKnowledgeBase;
027: import org.mandarax.kernel.KnowledgeBase;
028: import org.mandarax.kernel.Rule;
029: import org.mandarax.kernel.validation.TestCase;
030: import org.mandarax.util.logging.LogCategories;
031: import org.mandarax.zkb.BinarySerializationOPS;
032: import org.mandarax.zkb.KnowledgeBasePlusAttachment;
033: import org.mandarax.zkb.ObjectPersistencyService;
034: import org.mandarax.zkb.ZKBDriver;
035: import org.mandarax.zkb.ZKBManager;
036:
037: import test.org.mandarax.testsupport.TestUtils;
038:
039: /**
040: * Abstract test case for ZKB drivers / OPSs.
041: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
042: * @version 3.4 <7 March 05>
043: * @since 2.2
044: */
045: public abstract class ZKBTestCase extends junit.framework.TestCase
046: implements LogCategories {
047: protected ZKBManager zkbMgr = new ZKBManager();
048: //protected DOMBuilder domBuilder = new DOMBuilder();
049: protected String name = null;
050:
051: // the file name used to output / temp storage
052: protected File file = null;
053:
054: protected boolean zipOrFolder = true;
055:
056: /**
057: * Construct a ZKB test case.
058: * @param driver the zkb driver
059: * @param ops the object persistency service
060: * @param name the name of the test case
061: * @param zipOrFolder whether the zkb is stored in a zip file or folder true - zip, false - folder
062: */
063: public ZKBTestCase(ZKBDriver aDriver, ObjectPersistencyService ops,
064: String name, boolean zipOrFolder) {
065: super ("test");
066: zkbMgr.setDriver(aDriver);
067: zkbMgr.setOps(ops);
068: setName(name);
069: this .zipOrFolder = zipOrFolder;
070:
071: // build filename
072: StringBuffer buf = new StringBuffer();
073: buf.append("_test_zkb_");
074: buf.append(getName());
075: buf.append("_");
076: buf.append(ops instanceof BinarySerializationOPS ? "bin"
077: : "xml");
078: buf.append("_4driver_");
079: buf.append(aDriver.getName());
080: if (zipOrFolder)
081: buf.append(".zip");
082:
083: file = zipOrFolder ? TestUtils.getFile(buf.toString())
084: : TestUtils.getFolder(buf.toString());
085: }
086:
087: /**
088: * Get the knowledge base used as test data.
089: * @return a knowledge base
090: */
091: public abstract KnowledgeBase getKB();
092:
093: /**
094: * Get the attachment to be saved with the knowledge base.
095: * @return a knowledge base
096: */
097: public Object getAttachment() {
098: return null;
099: }
100:
101: /**
102: * Compare knowledge bases.
103: * @return a boolean
104: * @param kb1 the first kb
105: * @param kb2 the second kb
106: */
107: public boolean compare(KnowledgeBase kb1, KnowledgeBase kb2) {
108: boolean result = true;
109: try {
110: // compare clauses
111: Iterator iter1 = kb1.clauses();
112: Iterator iter2 = kb2.clauses();
113: while (result && iter1.hasNext() && iter2.hasNext()) {
114: Clause c1 = (Clause) iter1.next();
115: Clause c2 = (Clause) iter2.next();
116: result = result
117: && compare(c1, c2)
118: && (c1.getProperties() == null ? c2
119: .getProperties() == null : c1
120: .getProperties().equals(
121: c2.getProperties()));
122: }
123: // compare queries
124: iter1 = kb1.queries();
125: iter2 = kb2.queries();
126: while (result && iter1.hasNext() && iter2.hasNext()) {
127: result = result && (iter1.next().equals(iter2.next()));
128: }
129: // compare test cases
130: iter1 = kb1.testcases();
131: iter2 = kb2.testcases();
132: while (result && iter1.hasNext() && iter2.hasNext()) {
133: TestCase tc1 = (TestCase) iter1.next();
134: TestCase tc2 = (TestCase) iter2.next();
135: result = result
136: && compare(tc1, tc2)
137: && (tc1.getProperties() == null ? tc2
138: .getProperties() == null : tc1
139: .getProperties().equals(
140: tc2.getProperties()));
141: }
142:
143: // compare comparators
144: if (kb1 instanceof ExtendedKnowledgeBase
145: && kb2 instanceof ExtendedKnowledgeBase) {
146: Comparator comp1 = ((ExtendedKnowledgeBase) kb1)
147: .getComparator();
148: Comparator comp2 = ((ExtendedKnowledgeBase) kb2)
149: .getComparator();
150: result = result
151: && (comp1 == null ? comp2 == null : comp1
152: .equals(comp2));
153: }
154:
155: return result;
156: } catch (ClauseSetException x) {
157: x.printStackTrace();
158: }
159: return false;
160: }
161:
162: /**
163: * Compare clauses.
164: * @return a boolean
165: * @param c1 the first clause
166: * @param c2 the second clause
167: */
168: public boolean compare(Clause c1, Clause c2) {
169: if (c1 instanceof Rule && c2 instanceof Rule)
170: return compare((Rule) c1, (Rule) c2);
171: else
172: return c1.equals(c2);
173: }
174:
175: /**
176: * Compare rules. Special handling for old drivers: the prerequisite interface has only been introduced
177: * in version 2.0 !!
178: * @return a boolean
179: * @param r1 the first rule
180: * @param r2 the second rule
181: */
182: public boolean compare(Rule r1, Rule r2) {
183: return r1.equals(r2);
184: }
185:
186: /**
187: * Compare test cases.
188: * @return a boolean
189: * @param tc1 the first test case
190: * @param tc2 the second test case
191: */
192: public boolean compare(TestCase tc1, TestCase tc2) {
193: return tc1.equals(tc2);
194: }
195:
196: /**
197: * Run the test.
198: */
199: public void test() throws Exception {
200: KnowledgeBase original = getKB();
201: // attach some properties to the clause sets !
202: for (Iterator iter = original.getClauseSets().iterator(); iter
203: .hasNext();) {
204: ClauseSet cs = (ClauseSet) iter.next();
205: cs.setProperty("key1", "value1");
206: cs.setProperty("key2", "value2");
207: }
208: Object attachment = getAttachment();
209: try {
210: zkbMgr.exportKnowledgeBase(file, original, attachment);
211: KnowledgeBasePlusAttachment imported = zkbMgr
212: .importKnowledgeBaseAndAttachment(file);
213: assertTrue(compare(original, imported.getKB())
214: && compareAttachments(attachment, imported
215: .getAttachment()));
216: } catch (Exception x) {
217: LOG_TEST.error("Error running test " + this , x);
218: assertTrue(false);
219: }
220: }
221:
222: /**
223: * Compare attachments
224: * @return a boolean
225: * @param a1 the first attachment
226: * @param a2 the second attachment
227: */
228: public boolean compareAttachments(Object a1, Object a2) {
229: return a1 == null ? a2 == null : a1.equals(a2);
230: }
231:
232: /**
233: * Convert the object to a string.
234: * @return a string
235: */
236: public String toString() {
237: return name + " (zkb driver- " + zkbMgr.getDriver().getClass()
238: + ", ops- " + zkbMgr.getOps().getClass() + ")";
239: }
240:
241: /**
242: * Returns the name.
243: * @return String
244: */
245: public String getName() {
246: return toString();
247: }
248:
249: /**
250: * Sets the name.
251: * @param name The name to set
252: */
253: public void setName(String name) {
254: this.name = name;
255: }
256:
257: }
|