1 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 32 public class testWriterAndReader 33 extends ModelTestBase implements RDFErrorHandler { 34 static private boolean showProgress = false; 35 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 lang; 43 String test; 44 int fileNumber; 45 int options = 0; 46 testWriterAndReader(String name, String lang, int fName) { 47 super(name); 48 this.lang = lang; 49 this.fileNumber = fName; 50 } 51 testWriterAndReader(String name, String lang, int fName, int options) { 52 super(name); 53 this.lang = lang; 54 this.fileNumber = fName; 55 this.options = options; 56 } 57 public String 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 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 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 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 6,7 })) 118 langsuite.addTest( 119 new testWriterAndReader("testOptions", lang, k, j)); 120 } 121 } 122 } 123 if (lang. 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 137 langsuite.addTest(new TestXMLFeatures("testLiAsProperty1", lang)); 138 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 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 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 [] { 268 }, new Object [] { 269 }); 270 } 271 272 public void testLongId() throws IOException { 273 doTest(new String [] { "longId" }, new Object [] { new Boolean (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 [] { "blockRules" }, new Object [] { blocked }); 294 } 295 296 public void doTest(String [] propNames, Object [] propVals) 297 throws IOException { 298 test(lang, 35, 1, propNames, propVals); 299 } 300 301 static final String 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 316 public void test( 317 String rwLang, 318 int seed, 319 int jjjMax, 320 String [] wopName, 321 Object [] wopVal) 322 throws IOException { 323 324 Model m1 = createMemModel(); 325 Model m2 = createMemModel(); 326 test = "testWriterAndReader lang=" + rwLang + " seed=" + seed; 329 String 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 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 fileName = "t" + (fileNumber * 1000) + ".rdf"; 349 m1 = createMemModel(); 350 String 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 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 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 389 assertTrue( 390 "Comparison of file written out, and file read in.", 391 s1.isIsomorphicWith(s2)); 392 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 410 private void prune(Model m, Random random, int cnt) { 411 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 } 430 431 437 private void expand(Model m, Random random, int cnt) { 438 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 } 480 481 484 public void warning(Exception e) { 485 throw new JenaException( e ); 487 } 488 489 public void error(Exception e) { 490 fail(e.getMessage()); 491 } 492 493 497 public void fatalError(Exception e) { 498 error(e); 499 throw new JenaException(e); 500 } 501 502 511 512 } 513 514 | Popular Tags |