001: /*
002: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * See end of file.
004: */
005: package com.hp.hpl.jena.rdf.arp.test;
006:
007: import java.io.File;
008: import java.io.FileInputStream;
009: import java.io.FileOutputStream;
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.io.OutputStream;
013: import java.io.PrintStream;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.Set;
017:
018: import junit.framework.Assert;
019: import junit.framework.TestSuite;
020:
021: import org.xml.sax.SAXException;
022: import org.xml.sax.SAXParseException;
023:
024: import com.hp.hpl.jena.iri.IRI;
025: import com.hp.hpl.jena.rdf.arp.ALiteral;
026: import com.hp.hpl.jena.rdf.arp.ARPEventHandler;
027: import com.hp.hpl.jena.rdf.arp.AResource;
028: import com.hp.hpl.jena.rdf.arp.NTriple;
029: import com.hp.hpl.jena.rdf.arp.impl.ARPResource;
030: import com.hp.hpl.jena.rdf.arp.impl.ARPSaxErrorHandler;
031: import com.hp.hpl.jena.rdf.model.Model;
032: import com.hp.hpl.jena.rdf.model.RDFErrorHandler;
033: import com.hp.hpl.jena.shared.wg.TestInputStreamFactory;
034:
035: /**
036: * A version of the test suite which uses the
037: * ARP internal N-triple writer, and not the
038: * Jena N-triple writer.
039: * @author Jeremy Carroll
040: *
041: *
042: */
043: class NTripleTestSuite extends WGTestSuite {
044: NTripleTestSuite(TestInputStreamFactory fact, String name, boolean b) {
045: super (fact, name, b);
046: }
047:
048: static TestSuite suite(IRI testDir, String d, String nm) {
049: return new NTripleTestSuite(new TestInputStreamFactory(testDir,
050: d), nm, true);
051: }
052:
053: static TestSuite suite(IRI testDir, IRI d, String nm) {
054: return new NTripleTestSuite(new TestInputStreamFactory(testDir,
055: d), nm, true);
056: }
057:
058: static class SimulatedException extends RuntimeException {
059:
060: /**
061: *
062: */
063: private static final long serialVersionUID = -4804213791508445759L;
064: }
065:
066: static class TestHandler extends ARPSaxErrorHandler implements
067: ARPEventHandler, org.xml.sax.ErrorHandler {
068: TestHandler(RDFErrorHandler eh) {
069: this (eh, 0);
070: }
071:
072: TestHandler(RDFErrorHandler eh, int cnt) {
073: super (eh);
074: countDown = cnt;
075: xCountDown = cnt;
076: }
077:
078: final int xCountDown;
079: Set anon = new HashSet();
080: Set oldAnon = new HashSet();
081: int state = 1; // 1 begin, 2 in RDF, 3 after RDF, 4 at end-of-file.
082: int countDown;
083:
084: public void statement(AResource subj, AResource pred,
085: AResource obj) {
086: Assert.assertEquals(state, 2);
087: seeing(subj);
088: seeing(obj);
089: if (--countDown == 0)
090: throw new SimulatedException();
091: }
092:
093: /**
094: * @param subj
095: */
096: private void seeing(AResource subj) {
097: if (subj.isAnonymous())
098: anon.add(subj);
099: Assert.assertFalse("bnode reuse?", oldAnon.contains(subj));
100: }
101:
102: /**
103: * @param subj
104: */
105: private void seen(AResource subj) {
106: if (!anon.contains(subj)) {
107: if (ARPResource.DEBUG) {
108: ((RuntimeException) subj.getUserData())
109: .printStackTrace();
110: }
111: Assert.assertFalse(
112: "end-scope called twice for a bnode: "
113: + subj.getAnonymousID(), oldAnon
114: .contains(subj));
115: Assert.assertTrue(
116: "end-scope for a bnode that had not been used "
117: + subj.getAnonymousID(), anon
118: .contains(subj));
119: }
120: anon.remove(subj);
121: oldAnon.add(subj);
122: }
123:
124: public void statement(AResource subj, AResource pred,
125: ALiteral lit) {
126: Assert.assertEquals("no start RDF seen", state, 2);
127: seeing(subj);
128: if (--countDown == 0)
129: throw new SimulatedException();
130: }
131:
132: public void endBNodeScope(AResource bnode) {
133: Assert.assertTrue(bnode.isAnonymous());
134: switch (state) {
135: case 1:
136: Assert.fail("Missing startRDF");
137: case 2:
138: Assert.assertFalse(bnode.hasNodeID());
139: seen(bnode);
140: break;
141: case 3:
142: case 4:
143: Assert.assertTrue(bnode.hasNodeID());
144: seen(bnode);
145: state = 4;
146: break;
147: default:
148: Assert.fail("impossible - test logic error");
149: }
150:
151: }
152:
153: public void startRDF() {
154: switch (state) {
155: case 2:
156: case 4:
157: Assert.fail("Bad state for startRDF " + state);
158: }
159: state = 2;
160: }
161:
162: public void endRDF() {
163: Assert.assertEquals(state, 2);
164: state = 3;
165: }
166:
167: public void startPrefixMapping(String prefix, String uri) {
168:
169: }
170:
171: public void endPrefixMapping(String prefix) {
172:
173: }
174:
175: /**
176: *
177: */
178: public void atEndOfFile() {
179: if (!anon.isEmpty()) {
180: Iterator it = anon.iterator();
181: while (it.hasNext()) {
182: AResource a = ((AResource) it.next());
183: System.err.print(a.getAnonymousID() + ", ");
184: if (ARPResource.DEBUG) {
185: RuntimeException rte = (RuntimeException) a
186: .getUserData();
187: // throw rte;
188: rte.printStackTrace();
189: }
190: }
191: }
192: Assert.assertTrue("(" + xCountDown
193: + ") some bnode still in scope ", //hasErrors||
194: anon.isEmpty());
195: switch (state) {
196: case 1:
197: Assert.fail("end-of-file before anything");
198: case 2:
199: Assert.fail("did not see endRDF");
200: case 3:
201: case 4:
202: break;
203: default:
204: Assert.fail("impossible logic error in test");
205: }
206: }
207:
208: boolean hasErrors = false;
209:
210: /* (non-Javadoc)
211: * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
212: */
213: public void error(SAXParseException exception)
214: throws SAXException {
215: hasErrors = true;
216: super .error(exception);
217:
218: }
219:
220: /* (non-Javadoc)
221: * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
222: */
223: public void fatalError(SAXParseException exception)
224: throws SAXException {
225: hasErrors = true;
226: super .fatalError(exception);
227:
228: }
229:
230: /**
231: *
232: */
233: public int getCount() {
234: return -countDown;
235: }
236:
237: /* (non-Javadoc)
238: * @see com.hp.hpl.jena.rdf.arp.ExtendedHandler#discardNodesWithNodeID()
239: */
240: public boolean discardNodesWithNodeID() {
241: return false;
242: }
243:
244: }
245:
246: Model loadRDF(InFactoryX in, RDFErrorHandler eh, String base)
247: throws IOException {
248: return loadRDFx(in, eh, base, true, 0);
249: }
250:
251: static Model loadRDFx(InFactoryX in, RDFErrorHandler eh,
252: String base, boolean wantModel, int cnt) throws IOException {
253: InputStream oldIn = System.in;
254: InputStream ntIn = null;
255: File ntriples = null;
256:
257: PrintStream out;
258: TestHandler th;
259: if (wantModel) {
260: ntriples = File.createTempFile("arp", ".nt");
261: out = new PrintStream(new FileOutputStream(ntriples));
262: th = new TestHandler(eh);
263: } else {
264: out = new PrintStream(new OutputStream() {
265:
266: public void write(int b) throws IOException {
267: }
268: });
269: th = new TestHandler(eh, cnt);
270: }
271: PrintStream oldOut = System.out;
272: try {
273: System.setIn(in.open());
274: System.setOut(out);
275: try {
276: NTriple.mainEh(new String[] { "-b", base, "-s" }, th,
277: th);
278: } catch (SimulatedException e) {
279: if (wantModel)
280: throw e;
281: }
282: out.close();
283: th.atEndOfFile();
284:
285: if (cnt == 0) {
286: // retry with sudden death
287: for (int i = th.getCount(); i >= 1; i--)
288: loadRDFx(in, TestScope.suppress, base, false, i);
289: }
290: if (wantModel) {
291: ntIn = new FileInputStream(ntriples);
292: return loadNT(ntIn, base);
293: }
294: return null;
295: } finally {
296: System.in.close();
297: System.setIn(oldIn);
298: System.setOut(oldOut);
299: if (ntIn != null)
300: ntIn.close();
301: if (ntriples != null)
302: ntriples.delete();
303: }
304: }
305:
306: }
307: /*
308: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
309: * All rights reserved.
310: *
311: * Redistribution and use in source and binary forms, with or without
312: * modification, are permitted provided that the following conditions
313: * are met:
314: * 1. Redistributions of source code must retain the above copyright
315: * notice, this list of conditions and the following disclaimer.
316: * 2. Redistributions in binary form must reproduce the above copyright
317: * notice, this list of conditions and the following disclaimer in the
318: * documentation and/or other materials provided with the distribution.
319: * 3. The name of the author may not be used to endorse or promote products
320: * derived from this software without specific prior written permission.
321:
322: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
323: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
324: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
325: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
326: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
327: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
328: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
329: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
330: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
331: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
332: */
|