KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  * $Id: MoreTests.java,v 1.29 2005/04/18 14:11:09 jeremy_carroll Exp $
6  */

7
8 package com.hp.hpl.jena.rdf.arp.test;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import junit.framework.*;
13
14 import com.hp.hpl.jena.ontology.OntDocumentManager;
15 import com.hp.hpl.jena.rdf.arp.*;
16 import com.hp.hpl.jena.rdf.model.*;
17 import com.hp.hpl.jena.vocabulary.RDF;
18 import java.nio.charset.Charset JavaDoc;
19
20 import org.xml.sax.*;
21
22 import java.io.*;
23
24 /**
25  * @author jjc
26  *
27  */

28 public class MoreTests extends TestCase implements RDFErrorHandler,
29         ARPErrorNumbers {
30     static private Log logger = LogFactory.getLog(MoreTests.class);
31
32     static public Test suite() {
33         TestSuite suite = new TestSuite("ARP Plus");
34
35         
36         suite.addTest(TestErrorMsg.suite());
37         suite.addTest(TestScope.suite());
38         suite.addTest(ExceptionTests.suite());
39     
40         suite.addTest(new MoreTests("testIcu"));
41         suite.addTest(new MoreTests("testLatin1"));
42         suite.addTest(new MoreTests("testIcu2"));
43         suite.addTest(new MoreTests("testEncodingMismatch1"));
44         suite.addTest(new MoreTests("testEncodingMismatch2"));
45         suite.addTest(new MoreTests("testNullBaseParamOK"));
46         suite.addTest(new MoreTests("testNullBaseParamError"));
47         suite.addTest(new MoreTests("testEmptyBaseParamOK"));
48         suite.addTest(new MoreTests("testEmptyBaseParamError"));
49         suite.addTest(new MoreTests("testWineDefaultNS"));
50         suite.addTest(new MoreTests("testInterrupt"));
51         suite.addTest(new MoreTests("testToString"));
52         
53 //for (int i=0; i< 20; i++ ) {
54
//suite.addTest(new MoreTests("testTokenGarbage1"));
55
//suite.addTest(new MoreTests("testTokenGarbage2"));
56
// suite.addTest(new MoreTests("testTokenGarbage1"));
57
// suite.addTest(new MoreTests("testTokenGarbage2"));
58
// suite.addTest(new MoreTests("testTokenGarbage1"));
59
// suite.addTest(new MoreTests("testTokenGarbage2"));
60
//}
61
return suite;
62     }
63
64     public MoreTests(String JavaDoc s) {
65         super(s);
66     }
67
68     protected Model createMemModel() {
69         return ModelFactory.createDefaultModel();
70     }
71
72     public void setUp() {
73         // ensure the ont doc manager is in a consistent state
74
OntDocumentManager.getInstance().reset(true);
75     }
76
77     public void testWineDefaultNS() throws IOException {
78         testWineNS(createMemModel());
79         testWineNS(ModelFactory.createOntologyModel());
80     }
81
82     private void testWineNS(Model m) throws FileNotFoundException, IOException {
83         InputStream in = new FileInputStream("testing/arp/xmlns/wine.rdf");
84         m.read(in, "");
85         in.close();
86         assertEquals("http://www.w3.org/TR/2003/CR-owl-guide-20030818/wine#", m
87                 .getNsPrefixURI(""));
88     }
89
90     public void testLatin1() throws IOException {
91         Model m = createMemModel();
92         RDFReader rdr = m.getReader();
93         InputStream r = new FileInputStream(
94                 "testing/arp/i18n/latin1.rdf");
95         
96         rdr.setErrorHandler(this);
97         expected = new int[] { WARN_NONCANONICAL_IANA_NAME };
98         rdr.read(m, r, "http://example.org/");
99         checkExpected();
100     }
101     public void testARPMacRoman() throws IOException {
102         Model m = createMemModel();
103         RDFReader rdr = m.getReader();
104         InputStream r = new FileInputStream(
105                 "testing/arp/i18n/macroman.rdf");
106         
107         rdr.setErrorHandler(this);
108         expected = new int[] { WARN_UNSUPPORTED_ENCODING, WARN_NON_IANA_ENCODING };
109         expected[Charset.isSupported("MacRoman")?0:1]=0;
110 // Only one of the warnings is expected, which depends on Java version
111

112         rdr.read(m, r, "http://example.org/");
113         checkExpected();
114     }
115     public void testARPMacArabic() throws IOException {
116         Model m = createMemModel();
117         RDFReader rdr = m.getReader();
118         InputStream r = new FileInputStream(
119                 "testing/arp/i18n/arabic-macarabic.rdf");
120         
121         rdr.setErrorHandler(this);
122         expected = new int[] { WARN_UNSUPPORTED_ENCODING, WARN_NON_IANA_ENCODING };
123         expected[Charset.isSupported("MacArabic")?0:1]=0;
124 // Only one of the warnings is expected, which depends on Java version
125
rdr.read(m, r, "http://example.org/");
126         checkExpected();
127     }
128     
129
130     public void testEncodingMismatch1() throws IOException {
131         Model m = createMemModel();
132         RDFReader rdr = m.getReader();
133         FileReader r = new FileReader(
134                 "testing/wg/rdfms-syntax-incomplete/test001.rdf");
135         if (r.getEncoding().startsWith("UTF")) {
136             System.err
137                     .println("WARNING: Encoding mismatch tests not executed on platform with default UTF encoding.");
138             return;
139         }
140         rdr.setErrorHandler(this);
141         expected = new int[] { WARN_ENCODING_MISMATCH };
142         rdr.read(m, r, "http://example.org/");
143         //System.err.println(m.size() + " triples read.");
144
checkExpected();
145
146     }
147
148     public void testIcu() throws IOException {
149 // "?????\u0b87\u0ba8\u0bcd\u0ba4\u0bbf// Normalizer. isNormalized(
150
// "?????\u0bcd\u0ba4\u0bbf// ,Normalizer.NFC,0);
151

152         Model m = createMemModel();
153         RDFReader rdr = m.getReader();
154         FileInputStream r = new FileInputStream(
155                 "testing/arp/i18n/icubug.rdf");
156         rdr.setErrorHandler(this);
157         expected = new int[] { WARN_STRING_COMPOSING_CHAR };
158         rdr.read(m, r, "http://example.org/");
159         r.close();
160         checkExpected();
161     
162     }
163     public void testIcu2() throws IOException {
164 // "???????"
165
// Normalizer. isNormalized(
166
// "?????"
167
// ,Normalizer.NFC,0);
168

169             Model m = createMemModel();
170             RDFReader rdr = m.getReader();
171             FileInputStream r = new FileInputStream(
172                     "testing/arp/i18n/icubugtwo.rdf");
173             rdr.setErrorHandler(this);
174             expected = new int[] { WARN_STRING_NOT_NORMAL_FORM_C };
175             rdr.read(m, r, "http://example.org/");
176             r.close();
177             checkExpected();
178         
179         }
180     static class ToStringStatementHandler implements StatementHandler {
181         String JavaDoc obj;
182
183         String JavaDoc subj;
184
185         public void statement(AResource sub, AResource pred, ALiteral lit) {
186             // System.out.println("(" + sub + ", " + pred + ", " + lit + ")");
187
subj = sub.toString();
188         }
189
190         public void statement(AResource sub, AResource pred, AResource ob) {
191             // System.out.println("(" + sub + ", " + pred + ", " + ob + ")");
192
obj = ob.toString();
193         }
194
195     };
196
197     public void testToString() throws IOException, SAXException {
198
199         String JavaDoc testcase = "<rdf:RDF xmlns:music=\"http://www.kanzaki.com/ns/music#\" "
200                 + " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"> "
201                 + "<rdf:Description> "
202                 + " <music:performs rdf:nodeID=\"p1\"/> "
203                 + "</rdf:Description> "
204                 + "<rdf:Description rdf:nodeID=\"p1\"> "
205                 + " <music:opus>op.26</music:opus> "
206                 + "</rdf:Description> "
207                 + "</rdf:RDF>";
208
209         ARP parser = new ARP();
210         ToStringStatementHandler tssh = new ToStringStatementHandler();
211         parser.getHandlers().setStatementHandler(tssh);
212         parser.load(new StringReader(testcase), "http://www.example.com");
213         assertEquals(tssh.subj, tssh.obj);
214     }
215
216     public void testEncodingMismatch2() throws IOException {
217         Model m = createMemModel();
218         RDFReader rdr = m.getReader();
219         FileReader r = new FileReader(
220                 "testing/wg/rdf-charmod-literals/test001.rdf");
221         if (r.getEncoding().startsWith("UTF")) {
222             // see above for warning message.
223
return;
224         }
225         rdr.setErrorHandler(this);
226         expected = new int[] { WARN_ENCODING_MISMATCH, ERR_ENCODING_MISMATCH };
227         rdr.read(m, r, "http://example.org/");
228
229         checkExpected();
230     }
231
232     public void testNullBaseParamOK() throws IOException {
233         Model m = createMemModel();
234         Model m1 = createMemModel();
235         RDFReader rdr = m.getReader();
236         FileInputStream fin = new FileInputStream(
237                 "testing/wg/rdfms-identity-anon-resources/test001.rdf");
238
239         rdr.setErrorHandler(this);
240         expected = new int[] {};
241         rdr.read(m, fin, "http://example.org/");
242         fin.close();
243         fin = new FileInputStream(
244                 "testing/wg/rdfms-identity-anon-resources/test001.rdf");
245         rdr.read(m1, fin, null);
246         fin.close();
247         assertTrue("Base URI should have no effect.", m.isIsomorphicWith(m1));
248         checkExpected();
249     }
250
251     public void testNullBaseParamError() throws IOException {
252         Model m = createMemModel();
253         RDFReader rdr = m.getReader();
254         FileInputStream fin = new FileInputStream(
255                 "testing/wg/rdfms-difference-between-ID-and-about/test1.rdf");
256         rdr.setErrorHandler(this);
257         expected = new int[] { ERR_RESOLVING_URI_AGAINST_NULL_BASE };
258         rdr.read(m, fin, null);
259         fin.close();
260         checkExpected();
261     }
262
263     public void testEmptyBaseParamOK() throws IOException {
264         Model m = createMemModel();
265         Model m1 = createMemModel();
266         RDFReader rdr = m.getReader();
267         FileInputStream fin = new FileInputStream(
268                 "testing/wg/rdfms-identity-anon-resources/test001.rdf");
269
270         rdr.setErrorHandler(this);
271         expected = new int[] {};
272         rdr.read(m, fin, "http://example.org/");
273         fin.close();
274         fin = new FileInputStream(
275                 "testing/wg/rdfms-identity-anon-resources/test001.rdf");
276         rdr.read(m1, fin, "");
277         fin.close();
278         assertTrue("Empty base URI should have no effect.[" + m1.toString()
279                 + "]", m.isIsomorphicWith(m1));
280         checkExpected();
281     }
282
283     public void testEmptyBaseParamError() throws IOException {
284         Model m = createMemModel();
285         RDFReader rdr = m.getReader();
286         FileInputStream fin = new FileInputStream(
287                 "testing/wg/rdfms-difference-between-ID-and-about/test1.rdf");
288         rdr.setErrorHandler(this);
289         expected = new int[] { WARN_RESOLVING_URI_AGAINST_EMPTY_BASE };
290         rdr.read(m, fin, "");
291         fin.close();
292         Model m1 = createMemModel();
293         m1.createResource("#foo").addProperty(RDF.value, "abc");
294         assertTrue("Empty base URI should produce relative URI.["
295                 + m.toString() + "]", m.isIsomorphicWith(m1));
296         checkExpected();
297     }
298
299     public void testInterrupt() throws SAXException, IOException {
300         ARP a = new ARP();
301         InputStream in;
302         long start = System.currentTimeMillis();
303         in = new FileInputStream("testing/wg/miscellaneous/consistent001.rdf");
304         a.getHandlers().setStatementHandler(new StatementHandler() {
305             int countDown = 10;
306
307             public void statement(AResource subj, AResource pred, AResource obj) {
308                 if (countDown-- == 0)
309                     Thread.currentThread().interrupt();
310
311             }
312
313             public void statement(AResource subj, AResource pred, ALiteral lit) {
314
315             }
316         });
317         try {
318             a.load(in);
319             fail("Thread was not interrupted.");
320         } catch (InterruptedIOException e) {
321         } catch (SAXParseException e) {
322         } finally {
323             in.close();
324         }
325         // System.err.println("Finished "+Thread.interrupted());
326

327     }
328
329     private void checkExpected() {
330         for (int i = 0; i < expected.length; i++)
331             if (expected[i] != 0) {
332                 fail("Expected error: " + JenaReader.errorCodeName(expected[i])
333                         + " but it did not occur.");
334             }
335     }
336
337     public void warning(Exception JavaDoc e) {
338         error(0, e);
339     }
340
341     public void error(Exception JavaDoc e) {
342         error(1, e);
343     }
344
345     public void fatalError(Exception JavaDoc e) {
346         error(2, e);
347     }
348
349     private void error(int level, Exception JavaDoc e) {
350         //System.err.println(e.getMessage());
351
if (e instanceof ParseException) {
352             int eCode = ((ParseException) e).getErrorNumber();
353             onError(level, eCode);
354         } else {
355             fail("Not expecting an Exception: " + e.getMessage());
356         }
357     }
358
359     private int expected[];
360
361     private void println(String JavaDoc m) {
362         logger.error(m);
363     }
364
365     void onError(int level, int num) {
366         for (int i = 0; i < expected.length; i++)
367             if (expected[i] == num) {
368                 expected[i] = 0;
369                 return;
370             }
371         String JavaDoc msg = "Parser reports unexpected "
372                 + WGTestSuite.errorLevelName[level] + ": "
373                 + JenaReader.errorCodeName(num);
374         println(msg);
375         fail(msg);
376     }
377
378     private void tokenGarbage(String JavaDoc file) {
379         try {
380             Token.COUNT = true;
381             Token.COUNTTEST = true;
382             Token.reinitHighTide();
383             NTriple.main(new String JavaDoc[] { "-t", file });
384             //System.err.println("["+Token.highTide+"]");
385
assertTrue("Too many tokens used: "+ Token.highTide,
386                     Token.highTide<2000);
387         } finally {
388             Token.COUNT = false;
389             Token.COUNTTEST = false;
390         }
391     }
392
393     public void testTokenGarbage1() {
394         tokenGarbage("testing/ontology/owl/Wine/wine.owl");
395     }
396
397     public void testTokenGarbage2() {
398
399         tokenGarbage("testing/arp/gc/someWordNet.rdf");
400     }
401 }
402
403 /*
404  * (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
405  * All rights reserved.
406  *
407  * Redistribution and use in source and binary forms, with or without
408  * modification, are permitted provided that the following conditions are met:
409  *
410  * 1. Redistributions of source code must retain the above copyright notice,
411  * this list of conditions and the following disclaimer.
412  *
413  * 2. Redistributions in binary form must reproduce the above copyright notice,
414  * this list of conditions and the following disclaimer in the documentation
415  * and/or other materials provided with the distribution.
416  *
417  * 3. The name of the author may not be used to endorse or promote products
418  * derived from this software without specific prior written permission.
419  *
420  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
421  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
422  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
423  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
424  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
425  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
426  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
427  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
428  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
429  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
430  */
Popular Tags