KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > arp > test > WGTestSuite


1 /*
2     (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3     [See end of file]
4     $Id: WGTestSuite.java,v 1.26 2005/02/21 12:11:14 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.arp.test;
8
9 import junit.framework.*;
10 import java.util.zip.*;
11 import java.io.*;
12
13 import com.hp.hpl.jena.rdf.model.*;
14 import com.hp.hpl.jena.rdf.model.impl.*;
15 import java.util.*;
16 import com.hp.hpl.jena.rdf.arp.*;
17 import com.hp.hpl.jena.vocabulary.*;
18 import com.hp.hpl.jena.shared.*;
19 import com.hp.hpl.jena.shared.impl.JenaParameters;
20 import com.hp.hpl.jena.shared.wg.*;
21 import com.hp.hpl.jena.shared.wg.URI;
22
23 import com.hp.hpl.jena.reasoner.test.*;
24 import com.hp.hpl.jena.reasoner.rulesys.*;
25
26 import org.xml.sax.*;
27 /**
28  *
29  * @author jjc
30  */

31 class WGTestSuite extends TestSuite implements ARPErrorNumbers {
32     static private Resource jena2;
33     static private Model testResults;
34     static private void initResults() {
35         logging = true;
36         testResults = ModelFactory.createDefaultModel();
37         jena2 = testResults.createResource(BASE_RESULTS_URI + "#jena2");
38         jena2.addProperty(RDFS.comment,
39             testResults.createLiteral(
40                 "<a xmlns=\"http://www.w3.org/1999/xhtml\" HREF=\"http://jena.sourceforce.net/\">Jena2</a> is a" +
41                 " Semantic Web framework in Java" +
42                 " available from <a xmlns=\"http://www.w3.org/1999/xhtml\" HREF=\"http://www.sourceforce.net/projects/jena\">" +
43                 "sourceforge</a> CVS.",
44                 true)
45         );
46         jena2.addProperty(RDFS.label, "Jena2");
47         testResults.setNsPrefix("results", OWLResults.NS);
48     }
49     static void logResult(Resource test, int type) {
50         if (!logging) return;
51         Resource rslt;
52         switch (type) {
53             case WGReasonerTester.NOT_APPLICABLE:
54             return;
55             case WGReasonerTester.FAIL:
56             rslt = OWLResults.FailingRun;
57             break;
58             case WGReasonerTester.PASS:
59             rslt = OWLResults.PassingRun;
60             break;
61             case WGReasonerTester.INCOMPLETE:
62               rslt = OWLResults.IncompleteRun;
63               break;
64             default:
65             throw new BrokenException("Unknown result type");
66         }
67         Resource result =
68             testResults
69                 .createResource()
70                 .addProperty(RDF.type, OWLResults.TestRun)
71                 .addProperty(RDF.type, rslt)
72                 .addProperty(OWLResults.test, test )
73                 .addProperty(OWLResults.system, jena2);
74     }
75     private static boolean logging = false;
76     private static String JavaDoc BASE_RESULTS_URI = "http://jena.sourceforge.net/data/rdf-results.rdf";
77     static public boolean checkMessages = false;
78     static private boolean doSemanticTests() {
79         return ARPTests.internet;
80     }
81     static private boolean inDevelopment = false;
82      Model loadRDF(InFactoryX in, RDFErrorHandler eh, String JavaDoc base)
83         throws IOException {
84         Model model = ModelFactory.createDefaultModel();
85         JenaReader jr = new JenaReader();
86
87         if (eh != null)
88             jr.setErrorHandler(eh);
89         jr.setProperty("error-mode", "strict");
90         
91         if ( base.indexOf("/xmlns/") != -1
92           || base.indexOf("/comments/") != -1 )
93               jr.setProperty("embedding","true");
94         InputStream inx = in.open();
95         jr.read(model, inx, base);
96         inx.close();
97         return model;
98     }
99     
100     static Model loadNT(InputStream in, String JavaDoc base) throws IOException {
101         Model model = ModelFactory.createDefaultModel();
102         model.read(in, base, "N-TRIPLE");
103         in.close();
104         return model;
105     }
106
107     static private class DummyTest extends TestCase {
108         DummyTest() {
109             super("save results");
110         }
111         public void runTest() throws IOException {
112             if (logging) {
113                 RDFWriter w = testResults.getWriter("RDF/XML-ABBREV");
114                 w.setProperty("xmlbase",BASE_RESULTS_URI );
115                 OutputStream out;
116                 try {
117                 out = new FileOutputStream("/tmp/rdf-results.rdf");
118                 }
119                 catch (Exception JavaDoc e){
120                     out = System.out;
121                 }
122                 w.write(testResults,out,BASE_RESULTS_URI);
123                 out.close();
124             }
125         }
126     }
127     static String JavaDoc testNS =
128         "http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#";
129         
130     static String JavaDoc jjcNS = "http://jcarroll.hpl.hp.com/testSchema#";
131     
132  // static private String approved = "APPROVED";
133
static private Property status;
134     static private Property input;
135     static private Property output;
136     static private Property warning;
137     static private Property errorCodes;
138     
139     static {
140             status = new PropertyImpl(testNS, "status");
141             input = new PropertyImpl(testNS, "inputDocument");
142             output = new PropertyImpl(testNS, "outputDocument");
143             warning = new PropertyImpl(testNS, "warning");
144             errorCodes = new PropertyImpl(jjcNS, "error");
145     }
146     
147     static private Resource rdfxml =
148         new ResourceImpl(testNS, "RDF-XML-Document");
149         
150     static private Resource ntriple = new ResourceImpl(testNS, "NT-Document");
151     // static private Resource falseDoc = new ResourceImpl(testNS, "False-Document");
152

153     private URI testDir;
154     
155     private Act noop = new Act() {
156         public void act(Resource r) {
157         }
158     };
159     
160     private Act semTest = new Act() {
161           public void act(Resource r) {
162             if (doSemanticTests()){
163                 addTest(r, new ReasoningTest(r));
164             }
165           }
166     };
167     
168     TestInputStreamFactory factory;
169     
170     static private Collection misc =
171         Arrays.asList(
172             new String JavaDoc[] { "http://www.w3.org/2000/10/rdf-tests/rdfcore/rdfms-uri-substructure/error001" });
173             
174     private Map behaviours = new HashMap();
175     
176     {
177         behaviours
178             .put(new ResourceImpl(testNS + "PositiveParserTest"), new Act() {
179             public void act(Resource r) {
180                 // if (r.getProperty(status).getString().equals(approved))
181
// if (r.getURI().endsWith("rdfms-xmllang/test004"))
182
if (r.hasProperty(warning)) {
183                     addTest(r, new WarningTest(r));
184                 } else {
185                     addTest(r, new PositiveTest(r));
186                 }
187             }
188         });
189         behaviours
190             .put(new ResourceImpl(testNS + "NegativeParserTest"), new Act() {
191             public void act(Resource r) {
192                 // if (r.getProperty(status).getString().equals(approved))
193
addTest(r, new NegativeTest(r));
194             }
195         });
196             behaviours.put(new ResourceImpl(testNS + "False-Document"), noop);
197         behaviours.put(new ResourceImpl(testNS + "RDF-XML-Document"), noop);
198         behaviours.put(new ResourceImpl(testNS + "NT-Document"), noop);
199         behaviours.put(
200             new ResourceImpl(testNS + "PositiveEntailmentTest"),
201             semTest);
202         behaviours.put(
203             new ResourceImpl(testNS + "NegativeEntailmentTest"),
204                 semTest);
205         behaviours
206             .put(new ResourceImpl(testNS + "MiscellaneousTest"), new Act() {
207             public void act(Resource r) {
208                 String JavaDoc uri = r.getURI();
209                 if (!misc.contains(uri))
210                     System.err.println(
211                         "MiscellaneousTest: " + uri + " - ignored!");
212             }
213         });
214     }
215
216     private Model loadRDF(final TestInputStreamFactory fact,
217       final String JavaDoc file) {
218         Model m = null;
219         String JavaDoc base = fact.getBase().toString();
220         if (!base.endsWith("/"))
221             base = base + "/";
222
223         try {
224             InputStream in = fact.fullyOpen(file);
225             if (in == null )
226                 return null;
227             in.close();
228             m = loadRDF(new InFactoryX(){
229                 public InputStream open() throws IOException {
230                     return fact.fullyOpen(file);
231             } }, null, base + file);
232         } catch (JenaException e) {
233             // System.out.println(e.getMessage());
234
throw e;
235         } catch (Exception JavaDoc e) {
236             // e.printStackTrace();
237
if (file.equals("Manifest.rdf")) {
238                 System.err.println("Failed to open Manifest.rdf");
239                 e.printStackTrace();
240             }
241         }
242         return m;
243     }
244     
245     /** Creates new WGTestSuite
246         This is a private snapshot of the RDF Test Cases Working Draft's
247         data.
248      */

249     String JavaDoc createMe;
250     
251     WGTestSuite(TestInputStreamFactory fact, String JavaDoc name, boolean dynamic) {
252         super(name);
253         factory = fact;
254         testDir = fact.getBase();
255         if (dynamic)
256             try {
257                 String JavaDoc wgDir = ARPTests.wgTestDir.toString();
258                 System.err.println(wgDir);
259                   wgReasoner = new WGReasonerTester("Manifest",wgDir);
260                 createMe =
261                     "new "
262                         + this.getClass().getName()
263                         + "("
264                         + fact.getCreationJava()
265                         + ", \""
266                         + name
267                         + "\", false )";
268                 Model m = loadRDF(fact, "Manifest.rdf");
269                 // System.out.println("OK2");
270
Model extra = loadRDF(fact, "Manifest-extra.rdf");
271                 // System.out.println("OK3");
272
Model wrong = loadRDF(fact, "Manifest-wrong.rdf");
273                 // System.out.println("OK4");
274

275                 if (extra != null)
276                     m = m.add(extra);
277                 if (wrong != null)
278                     m = m.difference(wrong);
279                 // if (m == null)
280
// System.out.println("uggh");
281
StmtIterator si =
282                     m.listStatements( null, RDF.type, (RDFNode) null );
283
284                 while (si.hasNext()) {
285                     Statement st = si.nextStatement();
286                     Act action = (Act) behaviours.get(st.getObject());
287                     if (action == null) {
288                         System.err.println(
289                             "Unknown test class: "
290                                 + ((Resource) st.getObject()).getURI());
291                     } else {
292                         action.act(st.getSubject());
293                     }
294                 }
295                 if ( ARPTests.internet) {
296                     initResults();
297                     addTest(new DummyTest());
298                 }
299             } catch (RuntimeException JavaDoc re) {
300                 re.printStackTrace();
301                 throw re;
302             } catch (Exception JavaDoc e) {
303                 e.printStackTrace();
304                 throw new JenaException( e );
305
306             }
307     }
308     
309    // private ZipFile zip;
310

311     static TestSuite suite(URI testDir, String JavaDoc d, String JavaDoc nm) {
312         return new WGTestSuite(
313             new TestInputStreamFactory(testDir, d),
314             nm,
315             true);
316     }
317
318     static TestSuite suite(URI testDir, URI d, String JavaDoc nm) {
319         return new WGTestSuite(
320             new TestInputStreamFactory(testDir, d),
321             nm,
322             true);
323     }
324
325     private Map parts = new HashMap();
326     
327     private void addTest(Resource key, TestCase test) {
328         String JavaDoc keyName =
329             key.hasProperty(status)
330                 ? key.getRequiredProperty(status).getString()
331                 : "no status";
332         TestSuite sub = (TestSuite) parts.get(keyName);
333         if (sub == null) {
334             if ( keyName.equals("OBSOLETED"))
335               return;
336                   if ( keyName.equals("OBSOLETE"))
337                     return;
338                   if ( keyName.equals("NOT_APPROVED"))
339                     return;
340             sub = new TestSuite();
341             sub.setName(keyName);
342             parts.put(keyName, sub);
343             addTest(sub);
344         }
345         sub.addTest(test);
346     }
347
348     final static String JavaDoc errorLevelName[] =
349         new String JavaDoc[] { "warning", "error", "fatal error" };
350         
351     interface Act {
352         void act(Resource r) ;
353     }
354     private WGReasonerTester wgReasoner;
355     class ReasoningTest extends Test {
356          ReasoningTest(Resource r) {
357             super(r);
358          }
359      protected void runTest() throws IOException {
360            int rslt = WGReasonerTester.FAIL;
361            try {
362                    JenaParameters.enableWhitespaceCheckingOfTypedLiterals = true;
363                     Resource config = ModelFactory.createDefaultModel().createResource()
364                          .addProperty(ReasonerVocabulary.PROPsetRDFSLevel, "full");
365                 rslt = wgReasoner.runTestDetailedResponse(testID.getURI(),
366                 RDFSRuleReasonerFactory.theInstance(),this,config);
367                 } finally {
368                     logResult(testID,rslt);
369             }
370             // assertTrue(rslt>=0);
371
}
372     /* (non-Javadoc)
373          * @see com.hp.hpl.jena.rdf.arp.test.WGTestSuite.Test#createMe()
374      */

375     String JavaDoc createMe() {
376         throw new UnsupportedOperationException JavaDoc();
377     }
378     /* (non-Javadoc)
379      * @see com.hp.hpl.jena.rdf.arp.test.WGTestSuite.Test#reallyRunTest()
380      */

381     void reallyRunTest() {
382         throw new BrokenException("");
383     }
384          
385     }
386     
387     abstract class Test extends TestCase implements RDFErrorHandler {
388         Resource testID;
389         String JavaDoc createURI() {
390             return "\"" + testID.getURI() + "\"";
391         }
392         abstract String JavaDoc createMe();
393         Test(Resource r) {
394             super(
395                 WGTestSuite
396                     .this
397                     .testDir
398                     .relativize(URI.create(r.getURI()))
399                     .toString());
400             testID = r;
401         }
402         String JavaDoc create(Property p) {
403             Resource file = testID.getRequiredProperty(p).getResource();
404             Resource t = file.getRequiredProperty(RDF.type).getResource();
405             if (ntriple.equals(t)) {
406                 return "\"" + file.getURI() + "\",false";
407             } else if (rdfxml.equals(t)) {
408                 return "\"" + file.getURI() + "\",true";
409             } else {
410                 return "Unrecognized file type: " + t;
411             }
412         }
413         Model read(Property p) throws IOException {
414             Resource file = testID.getRequiredProperty(p).getResource();
415             Resource t = file.getRequiredProperty(RDF.type).getResource();
416             final String JavaDoc uri = file.getURI();
417             if (ntriple.equals(t)) {
418                 return loadNT(factory.open(uri),uri);
419             } else if (rdfxml.equals(t)) {
420                 return loadRDF(
421                 new InFactoryX(){
422
423                     public InputStream open() throws IOException {
424                         return factory.open(uri);
425                     }
426                 }
427                 , this, uri);
428             } else {
429                 fail("Unrecognized file type: " + t);
430             }
431             return null;
432         }
433         protected void runTest() throws IOException {
434             int rslt = WGReasonerTester.FAIL;
435             try {
436                 reallyRunTest();
437                 rslt = WGReasonerTester.PASS;
438             }
439             finally {
440                 logResult(testID,rslt);
441             }
442         }
443         abstract void reallyRunTest();
444         public void warning(Exception JavaDoc e) {
445             error(0, e);
446         }
447         public void error(Exception JavaDoc e) {
448             error(1, e);
449         }
450         public void fatalError(Exception JavaDoc e) {
451             error(2, e);
452         }
453         private void error(int level, Exception JavaDoc e) {
454             // println(e.getMessage());
455
if (e instanceof ParseException) {
456                 int eCode = ((ParseException) e).getErrorNumber();
457                 if (eCode == ERR_SYNTAX_ERROR) {
458                     String JavaDoc msg = e.getMessage();
459                     if (msg.indexOf("Unusual") != -1
460                         || msg.indexOf("Internal") != -1) {
461                         System.err.println(testID.getURI());
462                         System.err.println(msg);
463                         fail(msg);
464                     }
465                     if (checkMessages) {
466                         System.err.println(testID.getURI());
467                         System.err.println(msg);
468                     }
469                 }
470                 onError(level, eCode);
471             }
472             /*else if (e instanceof SAXParseException) {
473                 onError(level, ARPErrorNumbers.WARN_BAD_XML);
474             } */

475             else if (e instanceof SAXException) {
476                 fail("Not expecting a SAXException: " + e.getMessage());
477             } else {
478                 fail("Not expecting an Exception: " + e.getMessage());
479             }
480         }
481
482         private void println(String JavaDoc m) {
483             System.err.println(m);
484         }
485         void onError(int level, int num) {
486             String JavaDoc msg =
487                 "Parser reports unexpected "
488                     + errorLevelName[level]
489                     + ": "
490                     + JenaReader.errorCodeName(num);
491             println(msg);
492             fail(msg);
493         }
494     }
495     
496     class PositiveTest extends NegativeTest {
497         String JavaDoc createMe() {
498             return createURI() + "," + create(input) + "," + create(output);
499         }
500         PositiveTest(Resource nm) {
501             super(nm);
502             expectedLevel = -1;
503         }
504         protected void reallyRunTest() {
505             try {
506                 Model m2 = read(output);
507                 super.reallyRunTest();
508                 if (!m1.equals(m2)) {
509                     save(output);
510                     assertTrue(m1.isIsomorphicWith( m2 ) );
511                 }
512             } catch (RuntimeException JavaDoc e) {
513                 throw e;
514             } catch (Exception JavaDoc e) {
515                 fail(e.getMessage());
516             }
517         }
518         void initExpected() {
519             expected = new HashSet();
520         }
521     }
522     
523     class WarningTest extends PositiveTest {
524         String JavaDoc createMe() {
525             return createURI()
526                 + ","
527                 + create(input)
528                 + ","
529                 + create(output)
530                 + ","
531                 + createExpected();
532         }
533         WarningTest(Resource nm) {
534             super(nm);
535             expectedLevel = 0;
536         }
537         void initExpected() {
538             initExpectedFromModel();
539         }
540     }
541     
542     class NegativeTest extends Test {
543         Model m1;
544         Set expected;
545         int expectedLevel = 1;
546         private Set found = new HashSet();
547         private int errorCnt[] = new int[] { 0, 0, 0 };
548         String JavaDoc createExpected() {
549             String JavaDoc rslt = "new int[]{";
550             if ( expected == null)
551                return "null";
552             Iterator it = expected.iterator();
553             while (it.hasNext())
554                 rslt += it.next() + ", ";
555             return rslt + "}";
556         }
557         String JavaDoc createMe() {
558             return createURI() + "," + create(input) + "," + createExpected();
559         }
560         NegativeTest(Resource nm) {
561             super(nm);
562             initExpected();
563         }
564         void save(Property p) {
565             if (factory.savable()) {
566                 String JavaDoc uri = testID.getRequiredProperty(p).getResource().getURI();
567                 int suffix = uri.lastIndexOf('.');
568                 String JavaDoc saveUri = uri.substring(0, suffix) + ".ntx";
569                 // System.out.println("Saving to " + saveUri);
570
try {
571                     OutputStream w = factory.openOutput(saveUri);
572                     m1.write(w, "N-TRIPLE");
573                     w.close();
574                 } catch (IOException e) {
575                     throw new JenaException( e );
576                 }
577             }
578         }
579         void initExpectedFromModel() {
580             StmtIterator si = testID.listProperties(errorCodes);
581             if (si.hasNext()) {
582                 expected = new HashSet();
583                 while (si.hasNext()) {
584                     String JavaDoc uri = si.nextStatement().getResource().getURI();
585                     String JavaDoc fieldName = uri.substring(uri.lastIndexOf('#') + 1);
586                     expected.add(new Integer JavaDoc(JenaReader.errorCode(fieldName)));
587                 }
588             }
589         }
590         void initExpected() {
591             initExpectedFromModel();
592         }
593         protected void reallyRunTest() {
594             try {
595                 m1 = read(input);
596
597                 if (expectedLevel == 1
598                     && expected == null
599                     && errorCnt[2] == 0
600                     && errorCnt[1] == 0)
601                     save(input);
602             } catch (JenaException re) {
603                 if (re.getCause() instanceof SAXException) {
604                     // ignore.
605
} else {
606                     fail(re.getMessage());
607                 }
608             } catch (IOException ioe) {
609                 fail(ioe.getMessage());
610             }
611             if (expected != null && !expected.equals(found)) {
612                 Set dup = new HashSet();
613                 dup.addAll(found);
614                 dup.removeAll(expected);
615                 expected.removeAll(found);
616                 Iterator it = expected.iterator();
617                 while (it.hasNext()) {
618                     int eCode = ((Integer JavaDoc) it.next()).intValue();
619                     String JavaDoc msg =
620                         "Expected error "
621                             + JenaReader.errorCodeName(eCode)
622                             + ", was not detected.";
623                     if (errorCnt[2] == 0)
624                         fail(msg);
625                     else if (
626                         eCode == ERR_SYNTAX_ERROR
627                             && getName().startsWith("rdf-nnn/67_")
628                             && "1234".indexOf(
629                                 getName().charAt("rdf-nnn/67_".length()))
630                                 != -1) {
631                         // ignore
632
// System.err.println("Last message probably reflects a benign race condition on ARP teardown after fatal error that can be ignored.");
633
// System.err.println("It is known to happen with tests rdf-nnn/67_[1234] and ERR_SYNTAX_ERROR.");
634

635                     } else {
636                         System.err.println("Test: " + getName());
637                         System.err.println(msg);
638                     }
639                 }
640                 it = dup.iterator();
641                 while (it.hasNext())
642                     fail(
643                         "Detected error "
644                             + JenaReader.errorCodeName(
645                                 ((Integer JavaDoc) it.next()).intValue())
646                             + ", was not expected.");
647             }
648             for (int j = 2; j >= 0; j--)
649                 if (j == expectedLevel) {
650                     if (errorCnt[j] == 0 && (j != 1 || errorCnt[2] == 0))
651                         fail(
652                             "No "
653                                 + errorLevelName[expectedLevel]
654                                 + " in input file of class "
655                                 + getClass().getName());
656                 } else if (expected == null) {
657                     if (errorCnt[j] != 0)
658                         fail(
659                             "Inappropriate "
660                                 + errorLevelName[j]
661                                 + " in input file of class "
662                                 + getClass().getName());
663                 }
664
665         }
666         void onError(int level, int id) {
667             Integer JavaDoc err = new Integer JavaDoc(id);
668             found.add(err);
669             errorCnt[level]++;
670             if (expected != null) {
671                 if (!expected.contains(err))
672                     super.onError(level, id);
673             } else if (inDevelopment) {
674                 System.err.println(
675                     "<rdf:Description rdf:about='"
676                         + testID.getURI()
677                         + "'>\n"
678                         + "<jjc:error rdf:resource='"
679                         + jjcNS
680                         + JenaReader.errorCodeName(id)
681                         + "'/>\n</rdf:Description>");
682             }
683         }
684     }
685     class Test2 extends TestCase implements RDFErrorHandler {
686         //Resource testID;
687
Test2(String JavaDoc r) {
688             super(
689                 WGTestSuite.this.testDir.relativize(URI.create(r)).toString());
690             // testID = r;
691
}
692         Model read(String JavaDoc file, boolean type) throws IOException {
693             if (!type) {
694                 return loadNT(factory.open(file),file);
695             } else {
696                 final String JavaDoc uri = file;
697                 return loadRDF(
698                 new InFactoryX(){
699
700                     public InputStream open() throws IOException {
701                         return factory.open(uri);
702                     }
703                 }
704                 
705                 , this, uri);
706             }
707         }
708         
709         public void warning(Exception JavaDoc e) {
710             error(0, e);
711         }
712         
713         public void error(Exception JavaDoc e) {
714             error(1, e);
715         }
716         
717         public void fatalError(Exception JavaDoc e) {
718             error(2, e);
719         }
720         
721         private void error(int level, Exception JavaDoc e) {
722             // println(e.getMessage());
723
if (e instanceof ParseException) {
724                 int eCode = ((ParseException) e).getErrorNumber();
725                 if (eCode == ERR_SYNTAX_ERROR) {
726                     String JavaDoc msg = e.getMessage();
727                     if (msg.indexOf("Unusual") != -1
728                         || msg.indexOf("Internal") != -1) {
729                         System.err.println(getName());
730                         System.err.println(msg);
731                         fail(msg);
732                     }
733                     /*
734                     if (checkMessages) {
735                         System.err.println(testID.getURI());
736                         System.err.println(msg);
737                     }
738                     */

739                 }
740                 onError(level, eCode);
741             } /*else if (e instanceof SAXParseException) {
742                 onError(level, ARPErrorNumbers.WARN_BAD_XML);
743             } */

744             else if (e instanceof SAXException) {
745                 fail("Not expecting a SAXException: " + e.getMessage());
746             } else {
747                 fail("Not expecting an Exception: " + e.getMessage());
748             }
749         }
750
751         private void println(String JavaDoc m) {
752             System.err.println(m);
753         }
754         void onError(int level, int num) {
755             String JavaDoc msg =
756                 "Parser reports unexpected "
757                     + errorLevelName[level]
758                     + ": "
759                     + JenaReader.errorCodeName(num);
760             println(msg);
761             fail(msg);
762         }
763     }
764     
765     class PositiveTest2 extends NegativeTest2 {
766         String JavaDoc out;
767         boolean outtype;
768         PositiveTest2(
769             String JavaDoc uri,
770             String JavaDoc in,
771             boolean intype,
772             String JavaDoc out,
773             boolean outtype) {
774             this(uri, in, intype, out, outtype, new int[] {
775             });
776         }
777
778         PositiveTest2(
779             String JavaDoc uri,
780             String JavaDoc in,
781             boolean intype,
782             String JavaDoc out,
783             boolean outtype,
784             int errs[]) {
785             super(uri, in, intype, errs);
786                 expectedLevel = -1;
787             this.out = out;
788             this.outtype = outtype;
789         }
790         protected void runTest() {
791             try {
792                 Model m2 = read(out, outtype);
793                 super.runTest();
794                 if (!m1.isIsomorphicWith(m2)) {
795                     // save(output);
796
System.err.println("=====");
797                     m1.write(System.err,"N-TRIPLE");
798                     System.err.println("=====");
799                     m2.write(System.err,"N-TRIPLE");
800                     System.err.println("=====");
801                     fail("Models were not equal.");
802                 }
803             } catch (RuntimeException JavaDoc e) {
804                 throw e;
805             } catch (Exception JavaDoc e) {
806                 fail(e.getMessage());
807             }
808         }
809         void initExpected() {
810             expected = new HashSet();
811         }
812     }
813     
814     class WarningTest2 extends PositiveTest2 {
815         WarningTest2(
816             String JavaDoc uri,
817             String JavaDoc in,
818             boolean intype,
819             String JavaDoc out,
820             boolean outtype,
821             int errs[]) {
822             super(uri, in, intype, out, outtype, errs);
823                 expectedLevel = 0;
824         }
825     }
826     
827     class NegativeTest2 extends Test2 {
828         Model m1;
829         Set expected;
830         int expectedLevel = 1;
831         String JavaDoc in;
832         boolean intype;
833         private Set found = new HashSet();
834         private int errorCnt[] = new int[] { 0, 0, 0 };
835         NegativeTest2(String JavaDoc uri, String JavaDoc in, boolean intype, int errs[]) {
836             super(uri);
837             this.in = in;
838             this.intype = intype;
839
840             initExpected(errs);
841         }
842         /*
843         void save(Property p) {
844             if (factory.savable()) {
845                 String uri = testID.getProperty(p).getResource().getURI();
846                 int suffix = uri.lastIndexOf('.');
847                 String saveUri = uri.substring(0, suffix) + ".ntx";
848                 // System.out.println("Saving to " + saveUri);
849                 try {
850                     OutputStream w = factory.openOutput(saveUri);
851                     m1.write(w, "N-TRIPLE");
852                     w.close();
853                 } catch (IOException e) {
854                     throw new RuntimeException(e.getMessage());
855                 }
856             }
857         }
858         */

859         void initExpected(int errs[]) {
860             if ( errs == null )
861                return;
862             if (errs.length != 0)
863                 expected = new HashSet();
864             for (int i = 0; i < errs.length; i++) {
865
866                 expected.add(new Integer JavaDoc(errs[i]));
867             }
868         }
869         protected void runTest() {
870             try {
871                 m1 = read(in, intype);
872                 /*
873                                 if (expectedLevel == 1
874                                     && expected == null
875                                     && errorCnt[2] == 0
876                                     && errorCnt[1] == 0)
877                                     save(input);
878                                     */

879             } catch (JenaException re) {
880                 // System.out.println(re.toString());
881
if (re.getCause() instanceof SAXException) {
882                     // ignore.
883
} else {
884                     fail(re.getMessage());
885                 }
886             } catch (IOException ioe) {
887                 fail(ioe.getMessage());
888             }
889             if (expected != null && !expected.equals(found)) {
890                 Set dup = new HashSet();
891                 dup.addAll(found);
892                 dup.removeAll(expected);
893                 expected.removeAll(found);
894                 Iterator it = expected.iterator();
895                 while (it.hasNext()) {
896                     int eCode = ((Integer JavaDoc) it.next()).intValue();
897                     String JavaDoc msg =
898                         "Expected error "
899                             + JenaReader.errorCodeName(eCode)
900                             + ", was not detected.";
901                     if (errorCnt[2] == 0) {
902                         fail(msg);
903                     } else {
904                         System.err.println("Test: " + getName());
905                         System.err.println(msg);
906                     }
907                 }
908                 it = dup.iterator();
909                 while (it.hasNext())
910                     fail(
911                         "Detected error "
912                             + JenaReader.errorCodeName(
913                                 ((Integer JavaDoc) it.next()).intValue())
914                             + ", was not expected.");
915             }
916             for (int j = 2; j >= 0; j--)
917                 if (j == expectedLevel) {
918                     if (errorCnt[j] == 0 && (j != 1 || errorCnt[2] == 0))
919                         fail(
920                             "No "
921                                 + errorLevelName[expectedLevel]
922                                 + " in input file of class "
923                                 + getClass().getName());
924                 } else if (expected == null) {
925                     if (errorCnt[j] != 0)
926                         fail(
927                             "Inappropriate "
928                                 + errorLevelName[j]
929                                 + " in input file of class "
930                                 + getClass().getName());
931                 }
932
933         }
934         void onError(int level, int id) {
935             Integer JavaDoc err = new Integer JavaDoc(id);
936             found.add(err);
937             errorCnt[level]++;
938             if (expected != null) {
939                 if (!expected.contains(err))
940                     super.onError(level, id);
941             }
942             /*else if ( inDevelopment ) {
943                 System.err.println(
944                     "<rdf:Description rdf:about='"
945                         + testID.getURI()
946                         + "'>\n"
947                         + "<jjc:error rdf:resource='"
948                         + jjcNS
949                         + JenaReader.errorCodeName(id)
950                         + "'/>\n</rdf:Description>");
951             }
952             */

953         }
954     }
955     TestCase createPositiveTest(
956         String JavaDoc uri,
957         String JavaDoc in,
958         boolean intype,
959         String JavaDoc out,
960         boolean outtype) {
961         return new PositiveTest2(uri, in, intype, out, outtype);
962     }
963     TestCase createWarningTest(
964         String JavaDoc uri,
965         String JavaDoc in,
966         boolean intype,
967         String JavaDoc out,
968         boolean outtype,
969         int e[]) {
970         return new WarningTest2(uri, in, intype, out, outtype, e);
971     }
972     TestCase createNegativeTest(
973         String JavaDoc uri,
974         String JavaDoc in,
975         boolean intype,
976         int e[]) {
977         return new NegativeTest2(uri, in, intype, e);
978     }
979 }
980 /*
981  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
982  * All rights reserved.
983  *
984  * Redistribution and use in source and binary forms, with or without
985  * modification, are permitted provided that the following conditions
986  * are met:
987  * 1. Redistributions of source code must retain the above copyright
988  * notice, this list of conditions and the following disclaimer.
989  * 2. Redistributions in binary form must reproduce the above copyright
990  * notice, this list of conditions and the following disclaimer in the
991  * documentation and/or other materials provided with the distribution.
992  * 3. The name of the author may not be used to endorse or promote products
993  * derived from this software without specific prior written permission.
994
995  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
996  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
997  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
998  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
999  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1000 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1001 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1002 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1003 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1004 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1005 *
1006 *
1007 * WGTestSuite.java
1008 *
1009 * Created on November 28, 2001, 10:00 AM
1010 */

1011
Popular Tags