0001: /*
0002: (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
0003: [See end of file]
0004: $Id: WGTestSuite.java,v 1.36 2008/01/02 12:06:50 andy_seaborne Exp $
0005: */
0006:
0007: package com.hp.hpl.jena.rdf.arp.test;
0008:
0009: import java.io.FileOutputStream;
0010: import java.io.IOException;
0011: import java.io.InputStream;
0012: import java.io.OutputStream;
0013: import java.util.Arrays;
0014: import java.util.Collection;
0015: import java.util.HashMap;
0016: import java.util.HashSet;
0017: import java.util.Iterator;
0018: import java.util.Map;
0019: import java.util.Set;
0020:
0021: import junit.framework.TestCase;
0022: import junit.framework.TestSuite;
0023:
0024: import org.xml.sax.SAXException;
0025:
0026: import com.hp.hpl.jena.iri.IRI;
0027: import com.hp.hpl.jena.iri.IRIFactory;
0028: import com.hp.hpl.jena.rdf.arp.ARPErrorNumbers;
0029: import com.hp.hpl.jena.rdf.arp.JenaReader;
0030: import com.hp.hpl.jena.rdf.arp.ParseException;
0031: import com.hp.hpl.jena.rdf.model.Model;
0032: import com.hp.hpl.jena.rdf.model.ModelFactory;
0033: import com.hp.hpl.jena.rdf.model.Property;
0034: import com.hp.hpl.jena.rdf.model.RDFErrorHandler;
0035: import com.hp.hpl.jena.rdf.model.RDFNode;
0036: import com.hp.hpl.jena.rdf.model.RDFWriter;
0037: import com.hp.hpl.jena.rdf.model.Resource;
0038: import com.hp.hpl.jena.rdf.model.Statement;
0039: import com.hp.hpl.jena.rdf.model.StmtIterator;
0040: import com.hp.hpl.jena.rdf.model.impl.PropertyImpl;
0041: import com.hp.hpl.jena.rdf.model.impl.ResourceImpl;
0042: import com.hp.hpl.jena.reasoner.rulesys.RDFSRuleReasonerFactory;
0043: import com.hp.hpl.jena.reasoner.test.WGReasonerTester;
0044: import com.hp.hpl.jena.shared.BrokenException;
0045: import com.hp.hpl.jena.shared.JenaException;
0046: import com.hp.hpl.jena.shared.impl.JenaParameters;
0047: import com.hp.hpl.jena.shared.wg.TestInputStreamFactory;
0048: import com.hp.hpl.jena.vocabulary.OWLResults;
0049: import com.hp.hpl.jena.vocabulary.RDF;
0050: import com.hp.hpl.jena.vocabulary.RDFS;
0051: import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
0052:
0053: /**
0054: *
0055: * @author jjc
0056: */
0057: class WGTestSuite extends TestSuite implements ARPErrorNumbers {
0058: static private Resource jena2;
0059: static private Model testResults;
0060:
0061: static private void initResults() {
0062: logging = true;
0063: testResults = ModelFactory.createDefaultModel();
0064: jena2 = testResults.createResource(BASE_RESULTS_URI + "#jena2");
0065: jena2
0066: .addProperty(
0067: RDFS.comment,
0068: testResults
0069: .createLiteral(
0070: "<a xmlns=\"http://www.w3.org/1999/xhtml\" href=\"http://jena.sourceforce.net/\">Jena2</a> is a"
0071: + " Semantic Web framework in Java"
0072: + " available from <a xmlns=\"http://www.w3.org/1999/xhtml\" href=\"http://www.sourceforce.net/projects/jena\">"
0073: + "sourceforge</a> CVS.",
0074: true));
0075: jena2.addProperty(RDFS.label, "Jena2");
0076: testResults.setNsPrefix("results", OWLResults.NS);
0077: }
0078:
0079: static void logResult(Resource test, int type) {
0080: // if (!logging) return;
0081: // Resource rslt;
0082: // switch (type) {
0083: // case WGReasonerTester.NOT_APPLICABLE:
0084: // return;
0085: // case WGReasonerTester.FAIL:
0086: // rslt = OWLResults.FailingRun;
0087: // break;
0088: // case WGReasonerTester.PASS:
0089: // rslt = OWLResults.PassingRun;
0090: // break;
0091: // case WGReasonerTester.INCOMPLETE:
0092: // rslt = OWLResults.IncompleteRun;
0093: // break;
0094: // default:
0095: // throw new BrokenException("Unknown result type");
0096: // }
0097: // Resource result =
0098: // testResults
0099: // .createResource()
0100: // .addProperty(RDF.type, OWLResults.TestRun)
0101: // .addProperty(RDF.type, rslt)
0102: // .addProperty(OWLResults.test, test )
0103: // .addProperty(OWLResults.system, jena2);
0104: }
0105:
0106: private static boolean logging = false;
0107: private static String BASE_RESULTS_URI = "http://jena.sourceforge.net/data/rdf-results.rdf";
0108: static public boolean checkMessages = false;
0109:
0110: static private boolean doSemanticTests() {
0111: return ARPTests.internet;
0112: }
0113:
0114: static private boolean inDevelopment = false;
0115:
0116: Model loadRDF(InFactoryX in, RDFErrorHandler eh, String base)
0117: throws IOException {
0118: Model model = ModelFactory.createDefaultModel();
0119: JenaReader jr = new JenaReader();
0120:
0121: if (eh != null)
0122: jr.setErrorHandler(eh);
0123: jr.setProperty("error-mode", "strict");
0124:
0125: if (base.indexOf("/xmlns/") != -1
0126: || base.indexOf("/comments/") != -1)
0127: jr.setProperty("embedding", "true");
0128: InputStream inx = in.open();
0129: jr.read(model, inx, base);
0130: inx.close();
0131: return model;
0132: }
0133:
0134: static Model loadNT(InputStream in, String base) throws IOException {
0135: Model model = ModelFactory.createDefaultModel();
0136: model.read(in, base, "N-TRIPLE");
0137: in.close();
0138: return model;
0139: }
0140:
0141: static private class DummyTest extends TestCase {
0142: DummyTest() {
0143: super ("save results");
0144: }
0145:
0146: public void runTest() throws IOException {
0147: if (logging) {
0148: RDFWriter w = testResults.getWriter("RDF/XML-ABBREV");
0149: w.setProperty("xmlbase", BASE_RESULTS_URI);
0150: OutputStream out;
0151: try {
0152: out = new FileOutputStream("/tmp/rdf-results.rdf");
0153: } catch (Exception e) {
0154: out = System.out;
0155: }
0156: w.write(testResults, out, BASE_RESULTS_URI);
0157: out.close();
0158: }
0159: }
0160: }
0161:
0162: static String testNS = "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#";
0163:
0164: static String jjcNS = "http://jcarroll.hpl.hp.com/testSchema#";
0165:
0166: // static private String approved = "APPROVED";
0167: static private Property status;
0168: static private Property input;
0169: static private Property output;
0170: static private Property warning;
0171: static private Property errorCodes;
0172:
0173: static {
0174: status = new PropertyImpl(testNS, "status");
0175: input = new PropertyImpl(testNS, "inputDocument");
0176: output = new PropertyImpl(testNS, "outputDocument");
0177: warning = new PropertyImpl(testNS, "warning");
0178: errorCodes = new PropertyImpl(jjcNS, "error");
0179: }
0180:
0181: static private Resource rdfxml = new ResourceImpl(testNS,
0182: "RDF-XML-Document");
0183:
0184: static private Resource ntriple = new ResourceImpl(testNS,
0185: "NT-Document");
0186: // static private Resource falseDoc = new ResourceImpl(testNS, "False-Document");
0187:
0188: private IRI testDir;
0189:
0190: private Act noop = new Act() {
0191: public void act(Resource r) {
0192: }
0193: };
0194:
0195: private Act semTest = new Act() {
0196: public void act(Resource r) {
0197: if (doSemanticTests()) {
0198: // addTest(r, new ReasoningTest(r));
0199: }
0200: }
0201: };
0202:
0203: TestInputStreamFactory factory;
0204:
0205: static private Collection misc = Arrays
0206: .asList(new String[] { "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-uri-substructure/error001" });
0207:
0208: private Map behaviours = new HashMap();
0209:
0210: {
0211: behaviours.put(new ResourceImpl(testNS + "PositiveParserTest"),
0212: new Act() {
0213: public void act(Resource r) {
0214: // if (r.getProperty(status).getString().equals(approved))
0215: // if (r.getURI().endsWith("rdfms-xmllang/test004"))
0216: if (r.hasProperty(warning)) {
0217: addTest(r, new WarningTest(r));
0218: } else {
0219: addTest(r, new PositiveTest(r));
0220: }
0221: }
0222: });
0223: behaviours.put(new ResourceImpl(testNS + "NegativeParserTest"),
0224: new Act() {
0225: public void act(Resource r) {
0226: // if (r.getProperty(status).getString().equals(approved))
0227: addTest(r, new NegativeTest(r));
0228: }
0229: });
0230: behaviours.put(new ResourceImpl(testNS + "False-Document"),
0231: noop);
0232: behaviours.put(new ResourceImpl(testNS + "RDF-XML-Document"),
0233: noop);
0234: behaviours.put(new ResourceImpl(testNS + "NT-Document"), noop);
0235: behaviours.put(new ResourceImpl(testNS
0236: + "PositiveEntailmentTest"), semTest);
0237: behaviours.put(new ResourceImpl(testNS
0238: + "NegativeEntailmentTest"), semTest);
0239: behaviours.put(new ResourceImpl(testNS + "MiscellaneousTest"),
0240: new Act() {
0241: public void act(Resource r) {
0242: String uri = r.getURI();
0243: if (!misc.contains(uri))
0244: System.err.println("MiscellaneousTest: "
0245: + uri + " - ignored!");
0246: }
0247: });
0248: }
0249:
0250: private Model loadRDF(final TestInputStreamFactory fact,
0251: final String file) {
0252: Model m = null;
0253: String base = fact.getBase().toString();
0254: if (!base.endsWith("/"))
0255: base = base + "/";
0256:
0257: try {
0258: InputStream in = fact.fullyOpen(file);
0259: if (in == null)
0260: return null;
0261: in.close();
0262: m = loadRDF(new InFactoryX() {
0263: public InputStream open() throws IOException {
0264: return fact.fullyOpen(file);
0265: }
0266: }, null, base + file);
0267: } catch (JenaException e) {
0268: // System.out.println(e.getMessage());
0269: throw e;
0270: } catch (Exception e) {
0271: // e.printStackTrace();
0272: if (file.equals("Manifest.rdf")) {
0273: System.err.println("Failed to open Manifest.rdf");
0274: e.printStackTrace();
0275: }
0276: }
0277: return m;
0278: }
0279:
0280: /** Creates new WGTestSuite
0281: This is a private snapshot of the RDF Test Cases Working Draft's
0282: data.
0283: */
0284: String createMe;
0285:
0286: WGTestSuite(TestInputStreamFactory fact, String name,
0287: boolean dynamic) {
0288: super (name);
0289: factory = fact;
0290: testDir = fact.getBase();
0291: if (dynamic)
0292: try {
0293: // String wgDir = ARPTests.wgTestDir.toString();
0294: System.err.println(testDir + ", " + fact.getMapBase());
0295: wgReasoner = new WGReasonerTester("Manifest.rdf",
0296: "testing/wg/");
0297: // wgDir);
0298: createMe = "new " + this .getClass().getName() + "("
0299: + fact.getCreationJava() + ", \"" + name
0300: + "\", false )";
0301: Model m = loadRDF(fact, "Manifest.rdf");
0302: // System.out.println("OK2");
0303: Model extra = loadRDF(fact, "Manifest-extra.rdf");
0304: // System.out.println("OK3");
0305: Model wrong = loadRDF(fact, "Manifest-wrong.rdf");
0306: // System.out.println("OK4");
0307:
0308: if (extra != null)
0309: m = m.add(extra);
0310: if (wrong != null)
0311: m = m.difference(wrong);
0312: // if (m == null)
0313: // System.out.println("uggh");
0314: StmtIterator si = m.listStatements(null, RDF.type,
0315: (RDFNode) null);
0316:
0317: while (si.hasNext()) {
0318: Statement st = si.nextStatement();
0319: Act action = (Act) behaviours.get(st.getObject());
0320: if (action == null) {
0321: System.err.println("Unknown test class: "
0322: + ((Resource) st.getObject()).getURI());
0323: } else {
0324: action.act(st.getSubject());
0325: }
0326: }
0327: if (ARPTests.internet) {
0328: initResults();
0329: addTest(new DummyTest());
0330: }
0331: } catch (RuntimeException re) {
0332: re.printStackTrace();
0333: throw re;
0334: } catch (Exception e) {
0335: e.printStackTrace();
0336: throw new JenaException(e);
0337:
0338: }
0339: }
0340:
0341: // private ZipFile zip;
0342:
0343: static TestSuite suite(IRI testDir, String d, String nm) {
0344: return new WGTestSuite(new TestInputStreamFactory(testDir, d),
0345: nm, true);
0346: }
0347:
0348: static TestSuite suite(IRI testDir, IRI d, String nm) {
0349: return new WGTestSuite(new TestInputStreamFactory(testDir, d),
0350: nm, true);
0351: }
0352:
0353: private Map parts = new HashMap();
0354:
0355: private void addTest(Resource key, TestCase test) {
0356: String keyName = key.hasProperty(status) ? key
0357: .getRequiredProperty(status).getString() : "no status";
0358: TestSuite sub = (TestSuite) parts.get(keyName);
0359: if (sub == null) {
0360: if (keyName.equals("OBSOLETED"))
0361: return;
0362: if (keyName.equals("OBSOLETE"))
0363: return;
0364: if (keyName.equals("NOT_APPROVED"))
0365: return;
0366: sub = new TestSuite();
0367: sub.setName(keyName);
0368: parts.put(keyName, sub);
0369: addTest(sub);
0370: }
0371: sub.addTest(test);
0372: }
0373:
0374: final static String errorLevelName[] = new String[] { "warning",
0375: "error", "fatal error" };
0376:
0377: interface Act {
0378: void act(Resource r);
0379: }
0380:
0381: private WGReasonerTester wgReasoner;
0382:
0383: class ReasoningTest extends Test {
0384: ReasoningTest(Resource r) {
0385: super (r);
0386: }
0387:
0388: protected void runTest() throws IOException {
0389: int rslt = WGReasonerTester.FAIL;
0390: try {
0391: JenaParameters.enableWhitespaceCheckingOfTypedLiterals = true;
0392: Resource config = ModelFactory.createDefaultModel()
0393: .createResource().addProperty(
0394: ReasonerVocabulary.PROPsetRDFSLevel,
0395: "full");
0396: rslt = wgReasoner.runTestDetailedResponse(testID
0397: .getURI(), RDFSRuleReasonerFactory
0398: .theInstance(), this , config);
0399: } finally {
0400: logResult(testID, rslt);
0401: }
0402: // assertTrue(rslt>=0);
0403: }
0404:
0405: /* (non-Javadoc)
0406: * @see com.hp.hpl.jena.rdf.arp.test.WGTestSuite.Test#createMe()
0407: */
0408: String createMe() {
0409: throw new UnsupportedOperationException();
0410: }
0411:
0412: /* (non-Javadoc)
0413: * @see com.hp.hpl.jena.rdf.arp.test.WGTestSuite.Test#reallyRunTest()
0414: */
0415: void reallyRunTest() {
0416: throw new BrokenException("");
0417: }
0418:
0419: }
0420:
0421: abstract class Test extends TestCase implements RDFErrorHandler {
0422: Resource testID;
0423:
0424: String createURI() {
0425: return "\"" + testID.getURI() + "\"";
0426: }
0427:
0428: abstract String createMe();
0429:
0430: Test(Resource r) {
0431: super (WGTestSuite.this .testDir.relativize(
0432: IRIFactory.iriImplementation().create(r.getURI()),
0433: IRI.CHILD).toString());
0434: testID = r;
0435: }
0436:
0437: String create(Property p) {
0438: Resource file = testID.getRequiredProperty(p).getResource();
0439: Resource t = file.getRequiredProperty(RDF.type)
0440: .getResource();
0441: if (ntriple.equals(t)) {
0442: return "\"" + file.getURI() + "\",false";
0443: } else if (rdfxml.equals(t)) {
0444: return "\"" + file.getURI() + "\",true";
0445: } else {
0446: return "Unrecognized file type: " + t;
0447: }
0448: }
0449:
0450: Model read(Property p) throws IOException {
0451: Resource file = testID.getRequiredProperty(p).getResource();
0452: Resource t = file.getRequiredProperty(RDF.type)
0453: .getResource();
0454: final String uri = file.getURI();
0455: if (ntriple.equals(t)) {
0456: return loadNT(factory.open(uri), uri);
0457: } else if (rdfxml.equals(t)) {
0458: return loadRDF(new InFactoryX() {
0459:
0460: public InputStream open() throws IOException {
0461: return factory.open(uri);
0462: }
0463: }, this , uri);
0464: } else {
0465: fail("Unrecognized file type: " + t);
0466: }
0467: return null;
0468: }
0469:
0470: protected void runTest() throws IOException {
0471: int rslt = WGReasonerTester.FAIL;
0472: try {
0473: reallyRunTest();
0474: rslt = WGReasonerTester.PASS;
0475: } finally {
0476: logResult(testID, rslt);
0477: }
0478: }
0479:
0480: abstract void reallyRunTest();
0481:
0482: public void warning(Exception e) {
0483: error(0, e);
0484: }
0485:
0486: public void error(Exception e) {
0487: error(1, e);
0488: }
0489:
0490: public void fatalError(Exception e) {
0491: error(2, e);
0492: }
0493:
0494: private void error(int level, Exception e) {
0495: // println(e.getMessage());
0496: if (e instanceof ParseException) {
0497: int eCode = ((ParseException) e).getErrorNumber();
0498: if (eCode == ERR_SYNTAX_ERROR) {
0499: String msg = e.getMessage();
0500: if (msg.indexOf("Unusual") != -1
0501: || msg.indexOf("Internal") != -1) {
0502: System.err.println(testID.getURI());
0503: System.err.println(msg);
0504: fail(msg);
0505: }
0506: if (checkMessages) {
0507: System.err.println(testID.getURI());
0508: System.err.println(msg);
0509: }
0510: }
0511: onError(level, eCode);
0512: }
0513: /*else if (e instanceof SAXParseException) {
0514: onError(level, ARPErrorNumbers.WARN_BAD_XML);
0515: } */
0516: else if (e instanceof SAXException) {
0517: fail("Not expecting a SAXException: " + e.getMessage());
0518: } else {
0519: fail("Not expecting an Exception: " + e.getMessage());
0520: }
0521: }
0522:
0523: private void println(String m) {
0524: System.err.println(m);
0525: }
0526:
0527: void onError(int level, int num) {
0528: String msg = "Parser reports unexpected "
0529: + errorLevelName[level] + ": "
0530: + ParseException.errorCodeName(num);
0531: println(msg);
0532: fail(msg);
0533: }
0534: }
0535:
0536: class PositiveTest extends NegativeTest {
0537: String createMe() {
0538: return createURI() + "," + create(input) + ","
0539: + create(output);
0540: }
0541:
0542: PositiveTest(Resource nm) {
0543: super (nm);
0544: expectedLevel = -1;
0545: }
0546:
0547: protected void reallyRunTest() {
0548: try {
0549: Model m2 = read(output);
0550: super .reallyRunTest();
0551: if (!m1.equals(m2)) {
0552: save(output);
0553: assertTrue(m1.isIsomorphicWith(m2));
0554: }
0555: } catch (RuntimeException e) {
0556: throw e;
0557: } catch (Exception e) {
0558: fail(e.getMessage());
0559: }
0560: }
0561:
0562: void initExpected() {
0563: expected = new HashSet();
0564: }
0565: }
0566:
0567: class WarningTest extends PositiveTest {
0568: String createMe() {
0569: return createURI() + "," + create(input) + ","
0570: + create(output) + "," + createExpected();
0571: }
0572:
0573: WarningTest(Resource nm) {
0574: super (nm);
0575: expectedLevel = 0;
0576: }
0577:
0578: void initExpected() {
0579: initExpectedFromModel();
0580: }
0581: }
0582:
0583: class NegativeTest extends Test {
0584: Model m1;
0585: Set expected;
0586: int expectedLevel = 1;
0587: private Set found = new HashSet();
0588: private int errorCnt[] = new int[] { 0, 0, 0 };
0589:
0590: String createExpected() {
0591: String rslt = "new int[]{";
0592: if (expected == null)
0593: return "null";
0594: Iterator it = expected.iterator();
0595: while (it.hasNext())
0596: rslt += it.next() + ", ";
0597: return rslt + "}";
0598: }
0599:
0600: String createMe() {
0601: return createURI() + "," + create(input) + ","
0602: + createExpected();
0603: }
0604:
0605: NegativeTest(Resource nm) {
0606: super (nm);
0607: initExpected();
0608: }
0609:
0610: void save(Property p) {
0611: if (factory.savable()) {
0612: String uri = testID.getRequiredProperty(p)
0613: .getResource().getURI();
0614: int suffix = uri.lastIndexOf('.');
0615: String saveUri = uri.substring(0, suffix) + ".ntx";
0616: // System.out.println("Saving to " + saveUri);
0617: try {
0618: OutputStream w = factory.openOutput(saveUri);
0619: m1.write(w, "N-TRIPLE");
0620: w.close();
0621: } catch (IOException e) {
0622: throw new JenaException(e);
0623: }
0624: }
0625: }
0626:
0627: void initExpectedFromModel() {
0628: StmtIterator si = testID.listProperties(errorCodes);
0629: if (si.hasNext()) {
0630: expected = new HashSet();
0631: while (si.hasNext()) {
0632: String uri = si.nextStatement().getResource()
0633: .getURI();
0634: String fieldName = uri.substring(uri
0635: .lastIndexOf('#') + 1);
0636: expected.add(new Integer(ParseException
0637: .errorCode(fieldName)));
0638: }
0639: }
0640: }
0641:
0642: void initExpected() {
0643: initExpectedFromModel();
0644: }
0645:
0646: protected void reallyRunTest() {
0647: try {
0648: m1 = read(input);
0649:
0650: if (expectedLevel == 1 && expected == null
0651: && errorCnt[2] == 0 && errorCnt[1] == 0)
0652: save(input);
0653: } catch (JenaException re) {
0654: if (re.getCause() instanceof SAXException) {
0655: // ignore.
0656: } else {
0657: fail(re.getMessage());
0658: }
0659: } catch (IOException ioe) {
0660: fail(ioe.getMessage());
0661: }
0662: if (expected != null && !expected.equals(found)) {
0663: Set dup = new HashSet();
0664: dup.addAll(found);
0665: dup.removeAll(expected);
0666: expected.removeAll(found);
0667: Iterator it = expected.iterator();
0668: while (it.hasNext()) {
0669: int eCode = ((Integer) it.next()).intValue();
0670: String msg = "Expected error "
0671: + ParseException.errorCodeName(eCode)
0672: + ", was not detected.";
0673: if (errorCnt[2] == 0)
0674: fail(msg);
0675: else if (eCode == ERR_SYNTAX_ERROR
0676: && getName().startsWith("rdf-nnn/67_")
0677: && "1234".indexOf(getName().charAt(
0678: "rdf-nnn/67_".length())) != -1) {
0679: // ignore
0680: // System.err.println("Last message probably reflects a benign race condition on ARP teardown after fatal error that can be ignored.");
0681: // System.err.println("It is known to happen with tests rdf-nnn/67_[1234] and ERR_SYNTAX_ERROR.");
0682:
0683: } else {
0684: System.err.println("Test: " + getName());
0685: System.err.println(msg);
0686: }
0687: }
0688: it = dup.iterator();
0689: while (it.hasNext())
0690: fail("Detected error "
0691: + ParseException
0692: .errorCodeName(((Integer) it.next())
0693: .intValue())
0694: + ", was not expected.");
0695: }
0696: for (int j = 2; j >= 0; j--)
0697: if (j == expectedLevel) {
0698: if (errorCnt[j] == 0
0699: && (j != 1 || errorCnt[2] == 0))
0700: fail("No " + errorLevelName[expectedLevel]
0701: + " in input file of class "
0702: + getClass().getName());
0703: } else if (expected == null) {
0704: if (errorCnt[j] != 0)
0705: fail("Inappropriate " + errorLevelName[j]
0706: + " in input file of class "
0707: + getClass().getName());
0708: }
0709:
0710: }
0711:
0712: void onError(int level, int id) {
0713: Integer err = new Integer(id);
0714: found.add(err);
0715: errorCnt[level]++;
0716: if (expected != null) {
0717: if (!expected.contains(err))
0718: super .onError(level, id);
0719: } else if (inDevelopment) {
0720: System.err.println("<rdf:Description rdf:about='"
0721: + testID.getURI() + "'>\n"
0722: + "<jjc:error rdf:resource='" + jjcNS
0723: + ParseException.errorCodeName(id)
0724: + "'/>\n</rdf:Description>");
0725: }
0726: }
0727: }
0728:
0729: class Test2 extends TestCase implements RDFErrorHandler {
0730: //Resource testID;
0731: Test2(String r) {
0732: super (WGTestSuite.this .testDir.relativize(r, IRI.CHILD)
0733: .toString());
0734: // testID = r;
0735: }
0736:
0737: Model read(String file, boolean type) throws IOException {
0738: if (!type) {
0739: return loadNT(factory.open(file), file);
0740: }
0741: final String uri = file;
0742: return loadRDF(new InFactoryX() {
0743:
0744: public InputStream open() throws IOException {
0745: return factory.open(uri);
0746: }
0747: }
0748:
0749: , this , uri);
0750:
0751: }
0752:
0753: public void warning(Exception e) {
0754: error(0, e);
0755: }
0756:
0757: public void error(Exception e) {
0758: error(1, e);
0759: }
0760:
0761: public void fatalError(Exception e) {
0762: error(2, e);
0763: }
0764:
0765: private void error(int level, Exception e) {
0766: // println(e.getMessage());
0767: if (e instanceof ParseException) {
0768: int eCode = ((ParseException) e).getErrorNumber();
0769: if (eCode == ERR_SYNTAX_ERROR) {
0770: String msg = e.getMessage();
0771: if (msg.indexOf("Unusual") != -1
0772: || msg.indexOf("Internal") != -1) {
0773: System.err.println(getName());
0774: System.err.println(msg);
0775: fail(msg);
0776: }
0777: /*
0778: if (checkMessages) {
0779: System.err.println(testID.getURI());
0780: System.err.println(msg);
0781: }
0782: */
0783: }
0784: onError(level, eCode);
0785: } /*else if (e instanceof SAXParseException) {
0786: onError(level, ARPErrorNumbers.WARN_BAD_XML);
0787: } */
0788: else if (e instanceof SAXException) {
0789: fail("Not expecting a SAXException: " + e.getMessage());
0790: } else {
0791: fail("Not expecting an Exception: " + e.getMessage());
0792: }
0793: }
0794:
0795: private void println(String m) {
0796: System.err.println(m);
0797: }
0798:
0799: void onError(int level, int num) {
0800: String msg = "Parser reports unexpected "
0801: + errorLevelName[level] + ": "
0802: + ParseException.errorCodeName(num);
0803: println(msg);
0804: fail(msg);
0805: }
0806: }
0807:
0808: class PositiveTest2 extends NegativeTest2 {
0809: String out;
0810: boolean outtype;
0811:
0812: PositiveTest2(String uri, String in, boolean intype,
0813: String out, boolean outtype) {
0814: this (uri, in, intype, out, outtype, new int[] {});
0815: }
0816:
0817: PositiveTest2(String uri, String in, boolean intype,
0818: String out, boolean outtype, int errs[]) {
0819: super (uri, in, intype, errs);
0820: expectedLevel = -1;
0821: this .out = out;
0822: this .outtype = outtype;
0823: }
0824:
0825: protected void runTest() {
0826: try {
0827: Model m2 = read(out, outtype);
0828: super .runTest();
0829: if (!m1.isIsomorphicWith(m2)) {
0830: // save(output);
0831: System.err.println("=====");
0832: m1.write(System.err, "N-TRIPLE");
0833: System.err.println("=====");
0834: m2.write(System.err, "N-TRIPLE");
0835: System.err.println("=====");
0836: fail("Models were not equal.");
0837: }
0838: } catch (RuntimeException e) {
0839: throw e;
0840: } catch (Exception e) {
0841: fail(e.getMessage());
0842: }
0843: }
0844:
0845: void initExpected() {
0846: expected = new HashSet();
0847: }
0848: }
0849:
0850: class WarningTest2 extends PositiveTest2 {
0851: WarningTest2(String uri, String in, boolean intype, String out,
0852: boolean outtype, int errs[]) {
0853: super (uri, in, intype, out, outtype, errs);
0854: expectedLevel = 0;
0855: }
0856: }
0857:
0858: class NegativeTest2 extends Test2 {
0859: Model m1;
0860: Set expected;
0861: int expectedLevel = 1;
0862: String in;
0863: boolean intype;
0864: private Set found = new HashSet();
0865: private int errorCnt[] = new int[] { 0, 0, 0 };
0866:
0867: NegativeTest2(String uri, String in, boolean intype, int errs[]) {
0868: super (uri);
0869: this .in = in;
0870: this .intype = intype;
0871:
0872: initExpected(errs);
0873: }
0874:
0875: /*
0876: void save(Property p) {
0877: if (factory.savable()) {
0878: String uri = testID.getProperty(p).getResource().getURI();
0879: int suffix = uri.lastIndexOf('.');
0880: String saveUri = uri.substring(0, suffix) + ".ntx";
0881: // System.out.println("Saving to " + saveUri);
0882: try {
0883: OutputStream w = factory.openOutput(saveUri);
0884: m1.write(w, "N-TRIPLE");
0885: w.close();
0886: } catch (IOException e) {
0887: throw new RuntimeException(e.getMessage());
0888: }
0889: }
0890: }
0891: */
0892: void initExpected(int errs[]) {
0893: if (errs == null)
0894: return;
0895: if (errs.length != 0)
0896: expected = new HashSet();
0897: for (int i = 0; i < errs.length; i++) {
0898:
0899: expected.add(new Integer(errs[i]));
0900: }
0901: }
0902:
0903: protected void runTest() {
0904: try {
0905: m1 = read(in, intype);
0906: /*
0907: if (expectedLevel == 1
0908: && expected == null
0909: && errorCnt[2] == 0
0910: && errorCnt[1] == 0)
0911: save(input);
0912: */
0913: } catch (JenaException re) {
0914: // System.out.println(re.toString());
0915: if (re.getCause() instanceof SAXException) {
0916: // ignore.
0917: } else {
0918: fail(re.getMessage());
0919: }
0920: } catch (IOException ioe) {
0921: fail(ioe.getMessage());
0922: }
0923: // TODO: not for 2.3. Tidy up this code a bit, I don't understand it.
0924: HashSet ex2 = expected == null ? null : new HashSet(
0925: expected);
0926: if (expected == null)
0927: for (int j = 2; j >= 0; j--)
0928: if (j != expectedLevel) {
0929: if (errorCnt[j] != 0)
0930: ex2 = new HashSet();
0931: }
0932: if (ex2 != null && !ex2.equals(found)) {
0933: Set dup = new HashSet();
0934: dup.addAll(found);
0935: dup.removeAll(ex2);
0936: ex2.removeAll(found);
0937: if (expected != null)
0938: expected.removeAll(found);
0939: Iterator it = ex2.iterator();
0940: while (it.hasNext()) {
0941: int eCode = ((Integer) it.next()).intValue();
0942: String msg = "Expected error "
0943: + ParseException.errorCodeName(eCode)
0944: + ", was not detected.";
0945: if (errorCnt[2] == 0) {
0946: fail(msg);
0947: } else {
0948: System.err.println("Test: " + getName());
0949: System.err.println(msg);
0950: }
0951: }
0952: it = dup.iterator();
0953: while (it.hasNext())
0954: fail("Detected error "
0955: + ParseException
0956: .errorCodeName(((Integer) it.next())
0957: .intValue())
0958: + ", was not expected.");
0959: }
0960: for (int j = 2; j >= 0; j--)
0961: if (j == expectedLevel) {
0962: if (errorCnt[j] == 0
0963: && (j != 1 || errorCnt[2] == 0))
0964: fail("No " + errorLevelName[expectedLevel]
0965: + " in input file of class "
0966: + getClass().getName());
0967: } else if (expected == null) {
0968: if (errorCnt[j] != 0)
0969: fail("Inappropriate " + errorLevelName[j]
0970: + " in input file of class "
0971: + getClass().getName());
0972: }
0973:
0974: }
0975:
0976: void onError(int level, int id) {
0977: Integer err = new Integer(id);
0978: found.add(err);
0979: errorCnt[level]++;
0980: if (expected != null) {
0981: if (!expected.contains(err))
0982: super .onError(level, id);
0983: }
0984: /*else if ( inDevelopment ) {
0985: System.err.println(
0986: "<rdf:Description rdf:about='"
0987: + testID.getURI()
0988: + "'>\n"
0989: + "<jjc:error rdf:resource='"
0990: + jjcNS
0991: + JenaReader.errorCodeName(id)
0992: + "'/>\n</rdf:Description>");
0993: }
0994: */
0995: }
0996: }
0997:
0998: TestCase createPositiveTest(String uri, String in, boolean intype,
0999: String out, boolean outtype) {
1000: return new PositiveTest2(uri, in, intype, out, outtype);
1001: }
1002:
1003: TestCase createWarningTest(String uri, String in, boolean intype,
1004: String out, boolean outtype, int e[]) {
1005: return new WarningTest2(uri, in, intype, out, outtype, e);
1006: }
1007:
1008: TestCase createNegativeTest(String uri, String in, boolean intype,
1009: int e[]) {
1010: return new NegativeTest2(uri, in, intype, e);
1011: }
1012: }
1013: /*
1014: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
1015: * All rights reserved.
1016: *
1017: * Redistribution and use in source and binary forms, with or without
1018: * modification, are permitted provided that the following conditions
1019: * are met:
1020: * 1. Redistributions of source code must retain the above copyright
1021: * notice, this list of conditions and the following disclaimer.
1022: * 2. Redistributions in binary form must reproduce the above copyright
1023: * notice, this list of conditions and the following disclaimer in the
1024: * documentation and/or other materials provided with the distribution.
1025: * 3. The name of the author may not be used to endorse or promote products
1026: * derived from this software without specific prior written permission.
1027:
1028: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1029: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1030: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1031: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1033: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1037: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1038: *
1039: *
1040: * WGTestSuite.java
1041: *
1042: * Created on November 28, 2001, 10:00 AM
1043: */
|