001: /*
002: * (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: * [See end of file]
005: * $Id: TaintingTests.java,v 1.10 2008/01/02 12:06:50 andy_seaborne Exp $
006: */
007:
008: package com.hp.hpl.jena.rdf.arp.test;
009:
010: import java.io.ByteArrayInputStream;
011: import java.io.ByteArrayOutputStream;
012: import java.io.FileInputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.PrintStream;
016:
017: import junit.framework.Test;
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020:
021: import org.xml.sax.ErrorHandler;
022: import org.xml.sax.SAXParseException;
023:
024: import com.hp.hpl.jena.ontology.OntDocumentManager;
025: import com.hp.hpl.jena.rdf.arp.ARPErrorNumbers;
026: import com.hp.hpl.jena.rdf.arp.NTriple;
027: import com.hp.hpl.jena.rdf.arp.ParseException;
028: import com.hp.hpl.jena.rdf.model.Model;
029: import com.hp.hpl.jena.rdf.model.ModelFactory;
030:
031: /**
032: * @author jjc
033: *
034: */
035: public class TaintingTests extends TestCase implements ErrorHandler,
036: ARPErrorNumbers {
037:
038: static String promoteWarnings[] = { "WARN_UNQUALIFIED_ATTRIBUTE",
039: "WARN_UNKNOWN_RDF_ATTRIBUTE", "WARN_REDEFINITION_OF_ID",
040: "WARN_UNKNOWN_PARSETYPE", "WARN_MALFORMED_URI",
041: "WARN_BAD_NAME", "WARN_RELATIVE_URI", "WARN_BAD_XMLLANG", };
042: static String files[] = {
043:
044: "testing/arp/syntax-errors/error001.rdf",
045: "testing/arp/syntax-errors/error002.rdf",
046: "testing/arp/syntax-errors/error003.rdf",
047: "testing/wg/rdf-charmod-literals/error001.rdf",
048: "testing/wg/rdf-charmod-literals/error002.rdf",
049: "testing/wg/rdf-charmod-uris/error001.rdf",
050: "testing/wg/rdf-containers-syntax-vs-schema/error001.rdf",
051: "testing/wg/rdf-containers-syntax-vs-schema/error002.rdf",
052: "testing/wg/rdf-ns-prefix-confusion/error0001.rdf",
053: "testing/wg/rdf-ns-prefix-confusion/error0002.rdf",
054: "testing/wg/rdf-ns-prefix-confusion/error0003.rdf",
055: "testing/wg/rdf-ns-prefix-confusion/error0004.rdf",
056: "testing/wg/rdf-ns-prefix-confusion/error0005.rdf",
057: "testing/wg/rdf-ns-prefix-confusion/error0006.rdf",
058: "testing/wg/rdf-ns-prefix-confusion/error0007.rdf",
059: "testing/wg/rdf-ns-prefix-confusion/error0008.rdf",
060: "testing/wg/rdf-ns-prefix-confusion/error0009.rdf",
061:
062: "testing/wg/rdfms-abouteach/error001.rdf",
063: "testing/wg/rdfms-abouteach/error002.rdf",
064: "testing/wg/rdfms-difference-between-ID-and-about/error1.rdf",
065: "testing/wg/rdfms-empty-property-elements/error001.rdf",
066: "testing/wg/rdfms-empty-property-elements/error002.rdf",
067: "testing/wg/rdfms-empty-property-elements/error003.rdf",
068: "testing/wg/rdfms-parseType/error001.rdf",
069: "testing/wg/rdfms-parseType/error002.rdf",
070: "testing/wg/rdfms-parseType/error003.rdf",
071: "testing/wg/rdfms-rdf-id/error001.rdf",
072: "testing/wg/rdfms-rdf-id/error002.rdf",
073: "testing/wg/rdfms-rdf-id/error003.rdf",
074: "testing/wg/rdfms-rdf-id/error004.rdf",
075: "testing/wg/rdfms-rdf-id/error005.rdf",
076: "testing/wg/rdfms-rdf-id/error006.rdf",
077: "testing/wg/rdfms-rdf-id/error007.rdf",
078: "testing/wg/rdfms-rdf-names-use/error-001.rdf",
079: "testing/wg/rdfms-rdf-names-use/error-002.rdf",
080: "testing/wg/rdfms-rdf-names-use/error-003.rdf",
081: "testing/wg/rdfms-rdf-names-use/error-004.rdf",
082: "testing/wg/rdfms-rdf-names-use/error-005.rdf",
083: "testing/wg/rdfms-rdf-names-use/error-006.rdf",
084: "testing/wg/rdfms-rdf-names-use/error-007.rdf",
085: "testing/wg/rdfms-rdf-names-use/error-008.rdf",
086: "testing/wg/rdfms-rdf-names-use/error-009.rdf",
087: "testing/wg/rdfms-rdf-names-use/error-010.rdf",
088: "testing/wg/rdfms-rdf-names-use/error-011.rdf",
089: "testing/wg/rdfms-rdf-names-use/error-012.rdf",
090: "testing/wg/rdfms-rdf-names-use/error-013.rdf",
091: "testing/wg/rdfms-rdf-names-use/error-014.rdf",
092: "testing/wg/rdfms-rdf-names-use/error-015.rdf",
093: "testing/wg/rdfms-rdf-names-use/error-016.rdf",
094: "testing/wg/rdfms-rdf-names-use/error-017.rdf",
095: "testing/wg/rdfms-rdf-names-use/error-018.rdf",
096: "testing/wg/rdfms-rdf-names-use/error-019.rdf",
097: "testing/wg/rdfms-rdf-names-use/error-020.rdf",
098: "testing/wg/rdfms-syntax-incomplete/error001.rdf",
099: "testing/wg/rdfms-syntax-incomplete/error002.rdf",
100: "testing/wg/rdfms-syntax-incomplete/error003.rdf",
101: "testing/wg/rdfms-syntax-incomplete/error004.rdf",
102: "testing/wg/rdfms-syntax-incomplete/error005.rdf",
103: "testing/wg/rdfms-syntax-incomplete/error006.rdf",
104: "testing/wg/xmlbase/error001.rdf",
105:
106: "testing/arp/tainting/base.rdf",
107:
108: "testing/arp/tainting/damlcollection.rdf",
109: "testing/arp/tainting/lang.rdf",
110: "testing/arp/tainting/ptUnknown.rdf",
111: "testing/arp/tainting/typedLiteral.rdf",
112:
113: "testing/arp/tainting/typedNode.rdf",
114: "testing/arp/tainting/property.rdf",
115:
116: "testing/arp/tainting/collection.rdf",
117:
118: "testing/arp/tainting/propValueA.rdf",
119: "testing/arp/tainting/propValueB.rdf",
120: "testing/arp/tainting/propValueC.rdf",
121: "testing/arp/tainting/propValueD.rdf",
122: "testing/arp/tainting/propValueE.rdf",
123:
124: };
125:
126: static public Test suite() {
127: TestSuite suite = new TestSuite("ARP Tainting");
128: for (int i = 0; i < files.length; i++)
129: suite.addTest(new TaintingTests(files[i]));
130: suite.addTest(new TaintingTests(
131: "testing/arp/tainting/base.rdf", badBase,
132: "testing/arp/tainting/base-with-bad-base-good.nt",
133: "testing/arp/tainting/base-with-bad-base-bad.nt"));
134: return suite;
135: }
136:
137: final String fileName;
138: final String base;
139: final String goodTriples;
140: final String badTriples;
141: static final String badBase = "http://ww^w/";
142:
143: public TaintingTests(String s) {
144: this (s, "http://example.org/", makeGood(s), makeBad(s));
145: }
146:
147: static String makeGood(String s) {
148: return s.substring(0, s.length() - 4) + "-good.nt";
149: }
150:
151: static String makeBad(String s) {
152: return s.substring(0, s.length() - 4) + "-bad.nt";
153: }
154:
155: public TaintingTests(String s, String b, String good, String bad) {
156: super (s.substring(8));
157: fileName = s;
158: base = b;
159: goodTriples = good;
160: badTriples = bad;
161: }
162:
163: protected Model createMemModel() {
164: return ModelFactory.createDefaultModel();
165: }
166:
167: public void setUp() {
168: // ensure the ont doc manager is in a consistent state
169: OntDocumentManager.getInstance().reset(true);
170: }
171:
172: public void runTest() throws IOException {
173:
174: ByteArrayOutputStream goodBytes = new ByteArrayOutputStream();
175: ByteArrayOutputStream badBytes = new ByteArrayOutputStream();
176: PrintStream oldOut = System.out;
177: PrintStream oldErr = System.err;
178: // Model m = createMemModel();
179: try {
180: PrintStream out = new PrintStream(goodBytes);
181: PrintStream err = new PrintStream(badBytes);
182: // PrintStream out = new PrintStream(new FileOutputStream(goodTriples));
183: // PrintStream err = new PrintStream(new FileOutputStream(badTriples));
184: System.setOut(out);
185: System.setErr(err);
186: NTriple.mainEh(new String[] { "-e",
187: "102,136,105,103,108,107,116,106,004,131", "-E",
188: "-b", base, fileName }, this , null);
189: out.close();
190: err.close();
191: } finally {
192: System.setErr(oldErr);
193: System.setOut(oldOut);
194: }
195: InputStream good = new ByteArrayInputStream(goodBytes
196: .toByteArray());
197: InputStream bad = new ByteArrayInputStream(badBytes
198: .toByteArray());
199: compare(good, goodTriples);
200: compare(bad, badTriples);
201: }
202:
203: private void compare(InputStream in, String filen)
204: throws IOException {
205: Model m1 = this .createMemModel();
206: Model m2 = this .createMemModel();
207: m1.read(in, "", "N-TRIPLES");
208: m2.read(new FileInputStream(filen), "", "N-TRIPLES");
209: boolean isomorphicWith = m1.isIsomorphicWith(m2);
210: if (!isomorphicWith) {
211: System.err.println("Found Triples:");
212: System.err.println("===");
213: m1.write(System.err, "N-TRIPLES");
214: System.err.println("===");
215: System.err.println("Expected Triples:");
216: System.err.println("===");
217: m2.write(System.err, "N-TRIPLES");
218: System.err.println("===");
219: }
220: assertTrue("Triples were not as expected.", isomorphicWith);
221: }
222:
223: static public boolean seen[] = new boolean[400];
224:
225: public void warning(SAXParseException e) {
226: int eNo = ((ParseException) e).getErrorNumber();
227: if (!seen[eNo]) {
228: seen[((ParseException) e).getErrorNumber()] = true;
229: System.out.print(eNo + ", ");
230: System.err.println(e.getMessage());
231: }
232: }
233:
234: public void error(SAXParseException e) {
235: }
236:
237: public void fatalError(SAXParseException e) {
238: }
239:
240: }
241:
242: /*
243: * (c) Copyright 2000-2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
244: * All rights reserved.
245: *
246: * Redistribution and use in source and binary forms, with or without
247: * modification, are permitted provided that the following conditions are met:
248: *
249: * 1. Redistributions of source code must retain the above copyright notice,
250: * this list of conditions and the following disclaimer.
251: *
252: * 2. Redistributions in binary form must reproduce the above copyright notice,
253: * this list of conditions and the following disclaimer in the documentation
254: * and/or other materials provided with the distribution.
255: *
256: * 3. The name of the author may not be used to endorse or promote products
257: * derived from this software without specific prior written permission.
258: *
259: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
260: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
261: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
262: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
263: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
264: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
265: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
266: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
267: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
268: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269: */
|