KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /*
3  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
4  * See end of file.
5  */

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 JavaDoc;
12 import org.xml.sax.SAXParseException JavaDoc;
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 /**
20  * A version of the test suite which uses the
21  * ARP internal N-triple writer, and not the
22  * Jena N-triple writer.
23  * @author Jeremy Carroll
24  *
25  *
26  */

27 class NTripleTestSuite extends WGTestSuite {
28     NTripleTestSuite(TestInputStreamFactory fact, String JavaDoc name, boolean b) {
29         super(fact, name, b);
30     }
31
32     static TestSuite suite(URI testDir, String JavaDoc d, String JavaDoc 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 JavaDoc nm) {
40         return new NTripleTestSuite(
41             new TestInputStreamFactory(testDir, d),
42             nm,
43             true);
44     }
45
46     static class SimulatedException extends RuntimeException JavaDoc {
47     }
48     static class TestHandler
49         extends ARPSaxErrorHandler
50         implements ARPEventHandler, org.xml.sax.ErrorHandler JavaDoc {
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; // 1 begin, 2 in RDF, 3 after RDF, 4 at end-of-file.
63
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         /**
73          * @param subj
74          */

75         private void seeing(AResource subj) {
76             if (subj.isAnonymous())
77                 anon.add(subj);
78             Assert.assertFalse("bnode reuse?", oldAnon.contains(subj));
79         }
80         /**
81         * @param subj
82         */

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 JavaDoc prefix, String JavaDoc uri) {
136
137         }
138
139         public void endPrefixMapping(String JavaDoc prefix) {
140
141         }
142
143         /**
144          *
145          */

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 ", //hasErrors||
154
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         /* (non-Javadoc)
170          * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
171          */

172         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
173             hasErrors = true;
174             super.error(exception);
175
176         }
177
178         /* (non-Javadoc)
179          * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
180          */

181         public void fatalError(SAXParseException JavaDoc exception)
182             throws SAXException JavaDoc {
183             hasErrors = true;
184             super.fatalError(exception);
185
186         }
187         /**
188          *
189          */

190         public int getCount() {
191             return -countDown;
192         }
193         /* (non-Javadoc)
194          * @see com.hp.hpl.jena.rdf.arp.ExtendedHandler#discardNodesWithNodeID()
195          */

196         public boolean discardNodesWithNodeID() {
197             return false;
198         }
199
200     }
201
202     Model loadRDF(InFactoryX in, RDFErrorHandler eh, String JavaDoc 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 JavaDoc 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 JavaDoc[] { "-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                 // retry with sudden death
246
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 /*
269  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
270  * All rights reserved.
271  *
272  * Redistribution and use in source and binary forms, with or without
273  * modification, are permitted provided that the following conditions
274  * are met:
275  * 1. Redistributions of source code must retain the above copyright
276  * notice, this list of conditions and the following disclaimer.
277  * 2. Redistributions in binary form must reproduce the above copyright
278  * notice, this list of conditions and the following disclaimer in the
279  * documentation and/or other materials provided with the distribution.
280  * 3. The name of the author may not be used to endorse or promote products
281  * derived from this software without specific prior written permission.
282
283  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
284  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
285  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
286  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
287  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
288  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
289  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
290  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
292  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293  */

294
Popular Tags