KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > xmloutput > test > testWriterAndReader


1 /*
2     (c) Copyright 2001, 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3     All rights reserved.
4     [See end of file]
5     $Id: testWriterAndReader.java,v 1.31 2005/02/21 12:22:53 andy_seaborne Exp $
6 */

7
8 package com.hp.hpl.jena.xmloutput.test;
9
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.rdf.model.test.*;
12 import com.hp.hpl.jena.shared.*;
13 import com.hp.hpl.jena.vocabulary.RDFSyntax;
14 import com.hp.hpl.jena.vocabulary.DAML_OIL;
15
16 import java.io.*;
17 import java.util.*;
18
19 import junit.framework.*;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /**
24  * This will test any Writer and Reader pair.
25  * It writes out a random model, and reads it back in.
26  * The test fails if the models are not 'the same'.
27  * Quite what 'the same' means is debatable.
28  * @author jjc
29  
30  * @version Release='$Name: $' Revision='$Revision: 1.31 $' Date='$Date: 2005/02/21 12:22:53 $'
31  */

32 public class testWriterAndReader
33     extends ModelTestBase implements RDFErrorHandler {
34     static private boolean showProgress = false;
35     //static private boolean errorDetail = false;
36
static private int firstTest = 4;
37     static private int lastTest = 9;
38     static private int repetitionsJ = 6;
39     
40   protected static Log logger = LogFactory.getLog( testWriterAndReader.class );
41     
42     String JavaDoc lang;
43     String JavaDoc test;
44     int fileNumber;
45     int options = 0;
46     testWriterAndReader(String JavaDoc name, String JavaDoc lang, int fName) {
47         super(name);
48         this.lang = lang;
49         this.fileNumber = fName;
50     }
51     testWriterAndReader(String JavaDoc name, String JavaDoc lang, int fName, int options) {
52         super(name);
53         this.lang = lang;
54         this.fileNumber = fName;
55         this.options = options;
56     }
57     public String JavaDoc toString() {
58         return getName()
59             + " "
60             + lang
61             + " t"
62             + fileNumber
63             + "000.rdf"
64             + (options != 0 ? ("[" + options + "]") : "");
65     }
66     
67     static Test suite(String JavaDoc lang) {
68         return suite(lang, false);
69     }
70     
71     static public Test suite() {
72         return suite("special");
73     }
74     
75     static private boolean nBits(int i, int ok[]) {
76         int cnt = 0;
77         while (i > 0) {
78             if ((i & 1) == 1)
79                 cnt++;
80             i >>= 1;
81         }
82         for (int j = 0; j < ok.length; j++)
83             if (cnt == ok[j])
84                 return true;
85         return false;
86     }
87     
88     static Test suite(String JavaDoc lang, boolean lots) {
89         TestSuite langsuite = new TestSuite();
90         langsuite.setName(lang);
91         if (lang.equals("special")) {
92             langsuite.addTest(
93                 new TestXMLFeatures("testNoReification", "RDF/XML-ABBREV"));
94             return langsuite;
95         }
96         /* */
97         langsuite.addTest(new testWriterInterface("testWriting", lang));
98         
99         langsuite.addTest( new testWriterInterface( "testLineSeparator", lang ) );
100         /* */
101         for (int k = firstTest; k <= lastTest; k++) {
102             // if ( k==7 )
103
// continue;
104
/* * /
105             langsuite.addTest(new testWriterAndReader("testRandom", lang, k));
106             /* */

107             if (lang.indexOf("XML") > 0) {
108                 /* */
109                 langsuite.addTest(
110                     new testWriterAndReader("testLongId", lang, k));
111                 /* */
112                 for (int j = 1;
113                     j < (lang.equals("RDF/XML-ABBREV") ? (1<<blockRules.length) : 2);
114                     j++) {
115                     if (lots || nBits(j, new int[] { 1,
116                              // 2,3,4,5,
117
6,7 }))
118                         langsuite.addTest(
119                             new testWriterAndReader("testOptions", lang, k, j));
120                 }
121             }
122         }
123         if (lang.//equals("RDF/XML")) {
124
indexOf("XML") > 0) {
125             /* */
126             langsuite.addTest(
127                 new TestXMLFeatures("testBadURIAsProperty1", lang));
128             /* */
129             langsuite.addTest(
130                 new TestXMLFeatures("testBadURIAsProperty2", lang));
131             /* */
132             langsuite.addTest(new TestXMLFeatures("testBadProperty1", lang));
133             /* * /
134             langsuite.addTest(
135                 new TestXMLFeatures("testBadProperty2", lang));
136             /* */

137             langsuite.addTest(new TestXMLFeatures("testLiAsProperty1", lang));
138             /* * /
139             langsuite.addTest(
140                 new TestXMLFeatures("testLiAsProperty2", lang));
141             /* */

142             langsuite.addTest(
143                 new TestXMLFeatures("testDescriptionAsProperty", lang));
144             /* */
145
146             /* */
147             langsuite.addTest(new TestXMLFeatures("testXMLBase", lang));
148             /* */
149             langsuite.addTest(new TestXMLFeatures("testRelativeAPI", lang));
150             /* * /
151             langsuite.addTest(new TestXMLFeatures("testRelativeURI", lang));
152             /* */

153             langsuite.addTest(new TestXMLFeatures("testRelative", lang));
154             /* */
155             langsuite.addTest(new TestXMLFeatures("testBug696057", lang));
156             /* */
157             langsuite.addTest(new TestXMLFeatures("testPropertyURI", lang));
158             /* */
159             langsuite.addTest(new TestXMLFeatures("testUseNamespace", lang));
160             /* */
161             langsuite.addTest(
162                 new TestXMLFeatures("testUseDefaultNamespace", lang));
163             /* */
164             langsuite.addTest(
165                 new TestXMLFeatures("testUseUnusedNamespace", lang));
166             /* */
167             langsuite.addTest(
168                 new TestXMLFeatures("testBadPrefixNamespace", lang));
169             /* */
170             langsuite.addTest(new TestXMLFeatures("testRDFNamespace", lang));
171             /* */
172             langsuite.addTest(
173                 new TestXMLFeatures(
174                     "testDuplicatePrefixSysPropAndExplicit",
175                     lang));
176             /* */
177             langsuite.addTest(
178                 new TestXMLFeatures("testRDFDefaultNamespace", lang));
179             /* */
180             langsuite.addTest(
181                 new TestXMLFeatures("testDuplicateNamespace", lang));
182             /* */
183             langsuite.addTest(new TestXMLFeatures("testDuplicatePrefix", lang));
184             /* */
185             langsuite.addTest(
186                 new TestXMLFeatures("testUseNamespaceSysProp", lang));
187             /* */
188             langsuite.addTest(
189                 new TestXMLFeatures("testDefaultNamespaceSysProp", lang));
190             /* */
191             langsuite.addTest(
192                 new TestXMLFeatures("testDuplicateNamespaceSysProp", lang));
193             /* */
194             langsuite.addTest(
195                 new TestXMLFeatures("testDuplicatePrefixSysProp", lang));
196             /* */
197             langsuite.addTest(new TestXMLFeatures("testUTF8DeclAbsent", lang));
198             /* */
199             langsuite.addTest(new TestXMLFeatures("testUTF16DeclAbsent", lang));
200             /* */
201             langsuite.addTest(new TestXMLFeatures("testUTF8DeclPresent", lang));
202             /* */
203             langsuite.addTest(
204                 new TestXMLFeatures("testUTF16DeclPresent", lang));
205             /* */
206             langsuite.addTest(
207                 new TestXMLFeatures("testISO8859_1_DeclAbsent", lang));
208             /* */
209             langsuite.addTest(
210                 new TestXMLFeatures("testISO8859_1_DeclPresent", lang));
211             /* */
212             langsuite.addTest(
213                 new TestXMLFeatures("testStringDeclAbsent", lang));
214             /* */
215             langsuite.addTest(
216                 new TestXMLFeatures("testStringDeclPresent", lang));
217             /* */
218             langsuite.addTest(new TestXMLFeatures("testTab", lang));
219             /* */
220             langsuite.addTest(new TestXMLFeatures("testNoLiteral", lang));
221             /* */
222             langsuite.addTest(new TestXMLFeatures("testNoTab", lang));
223             /* */
224             langsuite.addTest(new TestXMLFeatures("testDoubleQuote", lang));
225             /* */
226             langsuite.addTest(new TestXMLFeatures("testSingleQuote", lang));
227             /* */
228             langsuite.addTest( new TestXMLFeatures("testNullBaseWithAbbrev", lang));
229         }
230         if (lang.equals("RDF/XML-ABBREV")) {
231            
232             langsuite.addTest(
233                 new TestXMLFeatures("testNoPropAttr", "RDF/XML-ABBREV"));
234             /* */
235             langsuite.addTest(
236                 new TestXMLFeatures("testNoDamlCollection", "RDF/XML-ABBREV"));
237             /* */
238             langsuite.addTest(
239                 new TestXMLFeatures("testNoRdfCollection", "RDF/XML-ABBREV"));
240             /* */
241             langsuite.addTest(
242                 new TestXMLFeatures("testNoLi", "RDF/XML-ABBREV"));
243             /* */
244             langsuite.addTest(new TestXMLFeatures("testNoID", lang));
245             /* */
246             langsuite.addTest(new TestXMLFeatures("testNoID2", lang));
247             /* */
248             langsuite.addTest(new TestXMLFeatures("testNoResource", lang));
249             /* * /
250             langsuite.addTest(
251                 new TestXMLFeatures("testNoStripes", lang));
252             /* */

253             langsuite.addTest(
254                 new TestXMLFeatures("testNoReification", lang));
255             langsuite.addTest(
256                 new TestXMLFeatures("testNoPropAttrs", lang));
257             langsuite.addTest(
258                 new TestXMLFeatures("testNoCookUp", lang));
259             langsuite.addTest(
260                 new TestXMLFeatures("testPropAttrs", lang));
261             /* */
262         }
263         return langsuite;
264     }
265
266     public void testRandom() throws IOException {
267         doTest(new String JavaDoc[] {
268         }, new Object JavaDoc[] {
269         });
270     }
271     
272     public void testLongId() throws IOException {
273         doTest(new String JavaDoc[] { "longId" }, new Object JavaDoc[] { new Boolean JavaDoc(true)});
274     }
275     
276     static Resource blockRules[] =
277         {
278             RDFSyntax.parseTypeLiteralPropertyElt,
279             RDFSyntax.parseTypeCollectionPropertyElt,
280             RDFSyntax.propertyAttr,
281         RDFSyntax.sectionReification,
282         RDFSyntax.sectionListExpand,
283             RDFSyntax.parseTypeResourcePropertyElt,
284             DAML_OIL.collection };
285     public void testOptions() throws IOException {
286         Vector v = new Vector();
287         for (int i = 0; i < blockRules.length; i++) {
288             if ((options & (1 << i)) != 0)
289                 v.add(blockRules[i]);
290         }
291         Resource blocked[] = new Resource[v.size()];
292         v.copyInto(blocked);
293         doTest(new String JavaDoc[] { "blockRules" }, new Object JavaDoc[] { blocked });
294     }
295     
296     public void doTest(String JavaDoc[] propNames, Object JavaDoc[] propVals)
297         throws IOException {
298         test(lang, 35, 1, propNames, propVals);
299     }
300
301     static final String JavaDoc baseUris[] =
302         {
303             "http://foo.com/Hello",
304             "http://foo.com/Hello",
305             "http://daml.umbc.edu/ontologies/calendar-ont",
306             "http://www.daml.org/2001/03/daml+oil-ex" };
307             
308     /**
309      *
310      * @param rwLang Use Writer for this lang
311      * @param seed A seed for the random number generator
312      * @param jjjMax Number of random variations
313      * @param wopName Property names to set on Writer
314      * @param wopVal Property values to set on Writer
315      */

316     public void test(
317         String JavaDoc rwLang,
318         int seed,
319         int jjjMax,
320         String JavaDoc[] wopName,
321         Object JavaDoc[] wopVal)
322         throws IOException {
323
324         Model m1 = createMemModel();
325         Model m2 = createMemModel();
326         // Model m3 = createMemModel();
327
// Model m4 = createMemModel();
328
test = "testWriterAndReader lang=" + rwLang + " seed=" + seed;
329         String JavaDoc filebase = "modules/rdf/regression/testWriterAndReader/";
330         if (showProgress)
331             System.out.println("Beginning " + test);
332         Random random = new Random(seed);
333
334         RDFReader rdfRdr = m1.getReader(rwLang);
335         RDFWriter rdfWtr = m1.getWriter(rwLang);
336
337         // set any writer options
338

339         if (wopName != null) {
340             for (int i = 0; i < wopName.length; i++) {
341                 rdfWtr.setProperty(wopName[i], wopVal[i]);
342             }
343         }
344
345         rdfRdr.setErrorHandler(this);
346         rdfWtr.setErrorHandler(this);
347         for (int jjj = 0; jjj < jjjMax; jjj++) {
348             String JavaDoc fileName = "t" + (fileNumber * 1000) + ".rdf";
349             m1 = createMemModel();
350             String JavaDoc baseUriRead;
351             if (fileNumber < baseUris.length)
352                 baseUriRead = baseUris[fileNumber];
353             else
354                 baseUriRead = "http://foo.com/Hello";
355             InputStream rdr = new FileInputStream(filebase + fileName);
356             m1.read(rdr, baseUriRead);
357             rdr.close();
358             boolean problem = false;
359             for (int j = 0; j < repetitionsJ; j++) {
360
361                 String JavaDoc baseUriWrite =
362                     j % 2 == 0 ? baseUriRead : "http://bar.com/irrelevant";
363                 int cn = (int) m1.size();
364                 if ((j % 2) == 0 && j > 0)
365                     prune(m1, random, 1 + cn / 10);
366                 if ((j % 2) == 0 && j > 0)
367                     expand(m1, random, 1 + cn / 10);
368                 
369                 ByteArrayOutputStream tmpOut = new ByteArrayOutputStream() ;
370                 rdfWtr.write(m1, tmpOut, baseUriWrite);
371                 tmpOut.flush() ;
372                 tmpOut.close() ;
373                 m2 = createMemModel();
374                 //empty(m2);
375

376                 InputStream in = new ByteArrayInputStream(tmpOut.toByteArray()) ;
377                 rdfRdr.read(m2, in, baseUriWrite);
378                 in.close();
379                 Model s1 = m1;
380                 Model s2 = m2;
381                 /*
382                 System.err.println("m1:");
383                 m1.write(System.err,"N-TRIPLE");
384                 System.err.println("m2:");
385                 
386                 m2.write(System.err,"N-TRIPLE");
387                 System.err.println("=");
388                 */

389                 assertTrue(
390                         "Comparison of file written out, and file read in.",
391                         s1.isIsomorphicWith(s2));
392                 // Free resources explicitily.
393
tmpOut.reset() ;
394                 tmpOut = null ;
395             }
396             if (showProgress) {
397                 System.out.print("+");
398                 System.out.flush();
399             }
400
401         }
402         if (showProgress)
403             System.out.println("End of " + test);
404     }
405   static boolean linuxFileDeleteErrorFlag = false;
406     /**Deletes cnt edges from m chosen by random.
407      * @param cnt The number of statements to delete.
408      * @param m A model with more than cnt statements.
409      */

410     private void prune(Model m, Random random, int cnt) {
411         // System.out.println("Pruning from " + (int)m.size() + " by " + cnt );
412
Statement die[] = new Statement[cnt];
413         int sz = (int) m.size();
414         StmtIterator ss = m.listStatements();
415         try {
416             for (int i = 0; i < cnt; i++)
417                 die[i] = ss.nextStatement();
418             while (ss.hasNext()) {
419                 int ix = random.nextInt(sz);
420                 if (ix < cnt)
421                     die[ix] = ss.nextStatement();
422             }
423         } finally {
424             ss.close();
425         }
426         for (int i = 0; i < cnt; i++)
427             m.remove(die[i]);
428         // System.out.println("Reduced to " + (int)m.size() );
429
}
430     
431     /**
432      * Adds cnt edges to m chosen by random.
433      *
434      * @param cnt The number of statements to add.
435      * @param m A model with more than cnt statements.
436      */

437     private void expand(Model m, Random random, int cnt) {
438         // System.out.println("Expanding from " + (int)m.size() + " by " + cnt );
439
Resource subject[] = new Resource[cnt];
440         Property predicate[] = new Property[cnt];
441         RDFNode object[] = new RDFNode[cnt];
442         int sz = (int) m.size();
443         StmtIterator ss = m.listStatements();
444         try {
445             for (int i = 0; i < cnt; i++) {
446                 Statement s = ss.nextStatement();
447                 subject[i] = s.getSubject();
448                 predicate[i] = s.getPredicate();
449                 object[i] = s.getObject();
450             }
451             while (ss.hasNext()) {
452                 Statement s = ss.nextStatement();
453                 Resource subj = s.getSubject();
454                 RDFNode obj = s.getObject();
455                 int ix = random.nextInt(sz);
456                 if (ix < cnt)
457                     subject[ix] = subj;
458                 ix = random.nextInt(sz);
459                 if (ix < cnt)
460                     object[ix] = subj;
461                 ix = random.nextInt(sz);
462                 if (ix < cnt)
463                     predicate[ix] = s.getPredicate();
464                 ix = random.nextInt(sz);
465                 if (ix < cnt)
466                     object[ix] = obj;
467                 if (obj instanceof Resource) {
468                     ix = random.nextInt(sz);
469                     if (ix < cnt)
470                         subject[ix] = (Resource) obj;
471                 }
472             }
473         } finally {
474             ss.close();
475         }
476         for (int i = 0; i < cnt; i++)
477             m.add(subject[i], predicate[i], object[i]);
478         // System.out.println("Expanded to " + (int)m.size() );
479
}
480
481     /** report a warning
482      * @param e an exception representing the error
483      */

484     public void warning(Exception JavaDoc e) {
485 // logger.warn( toString() + " " + e.getMessage(), e );
486
throw new JenaException( e );
487     }
488     
489     public void error(Exception JavaDoc e) {
490         fail(e.getMessage());
491     }
492
493     /** report a catastrophic error. Must not return.
494      * @param e an exception representing the error
495      * @throws RDFError a generic RDF exception
496      */

497     public void fatalError(Exception JavaDoc e) {
498         error(e);
499         throw new JenaException(e);
500     }
501     
502     /*
503     static public void empty(Model m) {
504         StmtIterator iter = m.listStatements();
505         while (iter.hasNext()) {
506             iter.nextStatement();
507             iter.remove();
508         }
509     }
510     */

511
512 }
513
514 /*
515  * (c) Copyright 2001,2003, 2004, 2005 Hewlett-Packard Development Company, LP
516  * All rights reserved.
517  *
518  * Redistribution and use in source and binary forms, with or without
519  * modification, are permitted provided that the following conditions
520  * are met:
521  * 1. Redistributions of source code must retain the above copyright
522  * notice, this list of conditions and the following disclaimer.
523  * 2. Redistributions in binary form must reproduce the above copyright
524  * notice, this list of conditions and the following disclaimer in the
525  * documentation and/or other materials provided with the distribution.
526  * 3. The name of the author may not be used to endorse or promote products
527  * derived from this software without specific prior written permission.
528
529  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
530  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
531  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
532  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
533  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
534  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
535  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
536  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
537  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
538  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
539  *
540  * $Id: testWriterAndReader.java,v 1.31 2005/02/21 12:22:53 andy_seaborne Exp $
541  */
Popular Tags