001: package test.org.mandarax.zkb;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.util.Comparator;
024:
025: import org.jdom.Document;
026: import org.jdom.JDOMException;
027: import org.jdom.input.SAXBuilder;
028: import org.jdom.output.XMLOutputter;
029: import org.mandarax.kernel.ClauseSet;
030: import org.mandarax.kernel.Query;
031: import org.mandarax.reference.AdvancedKnowledgeBase;
032: import org.mandarax.util.logging.LogCategories;
033: import org.mandarax.zkb.ObjectPersistencyService;
034: import org.mandarax.zkb.ZKBDriver;
035:
036: import test.org.mandarax.testsupport.TestUtils;
037:
038: /**
039: * Test case to validate ZKB documents.
040: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
041: * @version 3.4 <7 March 05>
042: * @since 2.3
043: */
044: public class ZKBValidationTestCase extends junit.framework.TestCase
045: implements LogCategories {
046: protected static SAXBuilder saxBuilder = new SAXBuilder(true);
047: protected static XMLOutputter outPutter = new XMLOutputter(" ",
048: true);
049: protected ZKBDriver driver = null;
050: protected ObjectPersistencyService ops = null;
051: protected String name = null;
052: protected AdvancedKnowledgeBase kb = new AdvancedKnowledgeBase();
053:
054: // the file name used to output / temp storage
055: protected String file = null;
056:
057: /**
058: * Construct a ZKB test case.
059: * @param driver the zkb driver
060: * @param anOps the object persistency service
061: * @param comparator a comparator
062: * @param clauses clauses to be added to the kb
063: * @param queries queries to be added to the kb
064: * @param name the name of the test case
065: */
066: public ZKBValidationTestCase(ZKBDriver aDriver,
067: ObjectPersistencyService anOps, Comparator comparator,
068: ClauseSet[] clauses, Query[] queries, String name) {
069: super ("test");
070:
071: aDriver.setDtdRefPolicy(ZKBDriver.INTERNAL_DTD_REF);
072: driver = aDriver;
073: ops = anOps;
074: setName(name);
075:
076: // build filename
077: StringBuffer buf = new StringBuffer();
078: buf.append("_test_zkb_valid_");
079: buf.append(getName());
080: buf.append("_4driver_");
081: buf.append(driver.getName());
082: buf.append(".xml");
083:
084: file = TestUtils.getFileName(buf.toString());
085:
086: // init kb
087: if (comparator != null)
088: kb.setComparator(comparator);
089: if (clauses != null) {
090: for (int i = 0; i < clauses.length; i++)
091: kb.add(clauses[i]);
092: }
093: if (queries != null) {
094: for (int i = 0; i < queries.length; i++)
095: kb.addQuery(queries[i]);
096: }
097: }
098:
099: /**
100: * Construct a ZKB test case.
101: * @param driver the zkb driver
102: * @param anOps the object persistency service
103: * @param comparator a comparator
104: * @param clauseSet a clause set to be added to the kb
105: * @param query a query to be added to the kb
106: * @param name the name of the test case
107: */
108: public ZKBValidationTestCase(ZKBDriver aDriver,
109: ObjectPersistencyService anOps, Comparator comparator,
110: ClauseSet clauseSet, Query query, String name) {
111: this (aDriver, anOps, comparator,
112: clauseSet == null ? new ClauseSet[] {}
113: : new ClauseSet[] { clauseSet },
114: query == null ? new Query[] {} : new Query[] { query },
115: name);
116: }
117:
118: /**
119: * Construct a ZKB test case.
120: * @param driver the zkb driver
121: * @param anOps the object persistency service
122: * @param clauseSet a clause set to be added to the kb
123: * @param query a query to be added to the kb
124: * @param name the name of the test case
125: */
126: public ZKBValidationTestCase(ZKBDriver aDriver,
127: ObjectPersistencyService anOps, ClauseSet clauseSet,
128: Query query, String name) {
129: this (aDriver, anOps, null, clauseSet, query, name);
130: }
131:
132: /**
133: * Construct a ZKB test case.
134: * @param driver the zkb driver
135: * @param anOps the object persistency service
136: * @param clauseSet a clause set to be added to the kb
137: * @param name the name of the test case
138: */
139: public ZKBValidationTestCase(ZKBDriver aDriver,
140: ObjectPersistencyService anOps, ClauseSet clauseSet,
141: String name) {
142: this (aDriver, anOps, null, clauseSet, null, name);
143: }
144:
145: /**
146: * Run the test.
147: */
148: public void test() throws Exception {
149: Document doc = driver.exportKnowledgeBase(kb, ops);
150: // write
151: FileOutputStream out = new FileOutputStream(file);
152: outPutter.output(doc, out);
153: out.close();
154:
155: // read and validate !!
156: try {
157: saxBuilder.build(new File(file));
158: } catch (JDOMException x) {
159: LOG_TEST.error("Error running test " + this , x);
160: assertTrue(false);
161: }
162: }
163:
164: /**
165: * Convert the object to a string.
166: * @return a string
167: */
168: public String toString() {
169: return "DTD Validation test case for ZKB - " + name;
170: }
171:
172: /**
173: * Returns the name.
174: * @return String
175: */
176: public String getName() {
177: return toString();
178: }
179:
180: /**
181: * Sets the name.
182: * @param name The name to set
183: */
184: public void setName(String name) {
185: this.name = name;
186: }
187:
188: }
|