1 2 6 package com.hp.hpl.jena.rdf.arp.test; 7 import junit.framework.*; 8 import java.io.*; 9 import java.util.*; 10 11 import org.xml.sax.SAXException ; 12 import org.xml.sax.SAXParseException ; 13 14 import com.hp.hpl.jena.rdf.model.*; 15 import com.hp.hpl.jena.rdf.arp.*; 16 import com.hp.hpl.jena.shared.wg.*; 17 import com.hp.hpl.jena.shared.wg.URI; 18 19 27 class NTripleTestSuite extends WGTestSuite { 28 NTripleTestSuite(TestInputStreamFactory fact, String name, boolean b) { 29 super(fact, name, b); 30 } 31 32 static TestSuite suite(URI testDir, String d, String nm) { 33 return new NTripleTestSuite( 34 new TestInputStreamFactory(testDir, d), 35 nm, 36 true); 37 } 38 39 static TestSuite suite(URI testDir, URI d, String nm) { 40 return new NTripleTestSuite( 41 new TestInputStreamFactory(testDir, d), 42 nm, 43 true); 44 } 45 46 static class SimulatedException extends RuntimeException { 47 } 48 static class TestHandler 49 extends ARPSaxErrorHandler 50 implements ARPEventHandler, org.xml.sax.ErrorHandler { 51 TestHandler(RDFErrorHandler eh) { 52 this(eh, 0); 53 } 54 TestHandler(RDFErrorHandler eh, int cnt) { 55 super(eh); 56 countDown = cnt; 57 xCountDown = cnt; 58 } 59 final int xCountDown; 60 Set anon = new HashSet(); 61 Set oldAnon = new HashSet(); 62 int state = 1; int countDown; 64 public void statement(AResource subj, AResource pred, AResource obj) { 65 Assert.assertEquals(state, 2); 66 seeing(subj); 67 seeing(obj); 68 if (--countDown == 0) 69 throw new SimulatedException(); 70 } 71 72 75 private void seeing(AResource subj) { 76 if (subj.isAnonymous()) 77 anon.add(subj); 78 Assert.assertFalse("bnode reuse?", oldAnon.contains(subj)); 79 } 80 83 private void seen(AResource subj) { 84 if (!anon.contains(subj)) 85 Assert.assertTrue( 86 "end-scope for a bnode that had not been used " 87 + subj.getAnonymousID(), 88 anon.contains(subj)); 89 anon.remove(subj); 90 oldAnon.add(subj); 91 } 92 93 public void statement(AResource subj, AResource pred, ALiteral lit) { 94 Assert.assertEquals("no start RDF seen", state, 2); 95 seeing(subj); 96 if (--countDown == 0) 97 throw new SimulatedException(); 98 } 99 100 public void endBNodeScope(AResource bnode) { 101 Assert.assertTrue(bnode.isAnonymous()); 102 switch (state) { 103 case 1 : 104 Assert.fail("Missing startRDF"); 105 case 2 : 106 Assert.assertFalse(bnode.hasNodeID()); 107 seen(bnode); 108 break; 109 case 3 : 110 case 4 : 111 Assert.assertTrue(bnode.hasNodeID()); 112 seen(bnode); 113 state = 4; 114 break; 115 default : 116 Assert.fail("impossible - test logic error"); 117 } 118 119 } 120 121 public void startRDF() { 122 switch (state) { 123 case 2 : 124 case 4 : 125 Assert.fail("Bad state for startRDF " + state); 126 } 127 state = 2; 128 } 129 130 public void endRDF() { 131 Assert.assertEquals(state, 2); 132 state = 3; 133 } 134 135 public void startPrefixMapping(String prefix, String uri) { 136 137 } 138 139 public void endPrefixMapping(String prefix) { 140 141 } 142 143 146 public void atEndOfFile() { 147 if (!anon.isEmpty()) { 148 Iterator it = anon.iterator(); 149 while (it.hasNext()) 150 System.err.print( 151 ((AResource) it.next()).getAnonymousID() + ", "); 152 } 153 Assert.assertTrue("("+xCountDown+") some bnode still in scope ", anon.isEmpty()); 155 switch (state) { 156 case 1 : 157 Assert.fail("end-of-file before anything"); 158 case 2 : 159 Assert.fail("did not see endRDF"); 160 case 3 : 161 case 4 : 162 break; 163 default : 164 Assert.fail("impossible logic error in test"); 165 } 166 } 167 boolean hasErrors = false; 168 169 172 public void error(SAXParseException exception) throws SAXException { 173 hasErrors = true; 174 super.error(exception); 175 176 } 177 178 181 public void fatalError(SAXParseException exception) 182 throws SAXException { 183 hasErrors = true; 184 super.fatalError(exception); 185 186 } 187 190 public int getCount() { 191 return -countDown; 192 } 193 196 public boolean discardNodesWithNodeID() { 197 return false; 198 } 199 200 } 201 202 Model loadRDF(InFactoryX in, RDFErrorHandler eh, String base) 203 throws IOException { 204 return loadRDFx(in, eh, base, true, 0); 205 } 206 static Model loadRDFx( 207 InFactoryX in, 208 RDFErrorHandler eh, 209 String base, 210 boolean wantModel, 211 int cnt) 212 throws IOException { 213 InputStream oldIn = System.in; 214 InputStream ntIn = null; 215 File ntriples = null; 216 217 PrintStream out; 218 TestHandler th; 219 if (wantModel) { 220 ntriples = File.createTempFile("arp", ".nt"); 221 out = new PrintStream(new FileOutputStream(ntriples)); 222 th = new TestHandler(eh); 223 } else { 224 out = new PrintStream(new OutputStream() { 225 226 public void write(int b) throws IOException { 227 } 228 }); 229 th = new TestHandler(eh, cnt); 230 } 231 PrintStream oldOut = System.out; 232 try { 233 System.setIn(in.open()); 234 System.setOut(out); 235 try { 236 NTriple.mainEh(new String [] { "-b", base, "-s" }, th, th); 237 } catch (SimulatedException e) { 238 if (wantModel) 239 throw e; 240 } 241 out.close(); 242 th.atEndOfFile(); 243 244 if (cnt == 0) { 245 for (int i = th.getCount(); i >= 1; i--) 247 loadRDFx(in, TestScope.suppress, base, false, i); 248 } 249 if (wantModel) { 250 ntIn = new FileInputStream(ntriples); 251 return loadNT(ntIn, base); 252 } else { 253 return null; 254 } 255 256 } finally { 257 System.in.close(); 258 System.setIn(oldIn); 259 System.setOut(oldOut); 260 if (ntIn != null) 261 ntIn.close(); 262 if (ntriples != null) 263 ntriples.delete(); 264 } 265 } 266 267 } 268 294 | Popular Tags |