001: /*
002: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * [See end of file]
004: */
005:
006: package com.hp.hpl.jena.n3.test;
007:
008: import com.hp.hpl.jena.n3.*;
009: import junit.framework.*;
010: import java.io.*;
011:
012: /**
013: * @author Andy Seaborne
014: * @version $Id: N3InternalTests.java,v 1.16 2008/01/02 12:06:53 andy_seaborne Exp $
015: */
016: public class N3InternalTests extends TestSuite {
017: /* JUnit swingUI needed this */
018: static public TestSuite suite() {
019: return new N3InternalTests();
020: }
021:
022: static public final String QUOTE3 = "\"\"\"";
023: static public boolean VERBOSE = false;
024: PrintWriter pw = null;
025:
026: N3InternalTests() {
027: super ("N3 Parser Syntactic tests");
028:
029: if (VERBOSE)
030: pw = new PrintWriter(System.out);
031:
032: // ---- Debug testing
033: //addTest(new Test("<thing> b:px.b:py [] . ")) ;
034: // if ( true ) return ;
035: // ---- Debug testing
036:
037: // Make sure basic things, at least, parse.
038:
039: // URIs, qnames, statements, prefixes
040:
041: // End of statement (and whitespace)
042: addTest(new Test("a:subj a:prop a:d ."));
043: addTest(new Test("a:subj a:prop a:d . "));
044: addTest(new Test("a:subj a:prop a:d."));
045: addTest(new Test("a:subj a:prop a:d. "));
046:
047: addTest(new Test("rdf: rdf:type :_."));
048: addTest(new Test("@prefix start: <somewhere>."));
049: addTest(new Test(
050: "<http://here/subj> <http://here/prep> <http://here/obj>."));
051:
052: // Whitespace, comments
053: addTest(new Test("a:subj\ta:prop\ta:d.\t"));
054: addTest(new Test(" a:subj\ta:prop\ta:d. "));
055: addTest(new Test("a:subj a:prop a:d. "));
056: addTest(new Test(""));
057: addTest(new Test(" #Comment"));
058: addTest(new Test("a:subj a:prop a:d. # Comment"));
059: addTest(new Test("a:subj a:prop a:d.# Comment"));
060:
061: // Literal: strings
062: addTest(new Test("a:subj a:prop 'string1'."));
063: addTest(new Test("a:subj a:prop \"string2\"."));
064: addTest(new Test("a:subj a:prop '''string3'''."));
065: addTest(new Test("a:subj a:prop " + QUOTE3 + "string3" + QUOTE3
066: + "."));
067:
068: // Literals: datatypes
069: addTest(new Test("a:subj a:prop 'string1'^^x:dt."));
070: addTest(new Test("a:subj a:prop 'string1'^^<uriref>."));
071:
072: // Literals: numbers.
073: addTest(new Test("a: :p 2."));
074: addTest(new Test("a: :p +2."));
075: addTest(new Test("a: :p -2 ."));
076: addTest(new Test("a: :p 2e6."));
077: addTest(new Test("a: :p 2e-6."));
078: addTest(new Test("a: :p -2e-6."));
079: addTest(new Test("a: :p 2.0e-6."));
080: addTest(new Test("a: :p 2.0."));
081:
082: // Test numbers in qnames
083: addTest(new Test("a: _: 2:."));
084: addTest(new Test("2.9 9:p 2.0."));
085: addTest(new Test("_:a :2 :_2 ."));
086: //addTest(new Test("2. :p 2")) ;
087: addTest(new Test("2.0 :p 2.0."));
088:
089: // The "unusual" cases
090: addTest(new Test("a:subj 'prop'^^<uriref> 'string'."));
091: addTest(new Test("a:subj a:prop 'string1'^^'stringDT'."));
092:
093: addTest(new Test("a:subj a:prop1 ?x ^^ x:dt."));
094: addTest(new Test("a:subj a:prop2 ?x ^^ ?x."));
095:
096: // Quotes in string
097: addTest(new Test("a:subj a:prop \"\\'string2\\'\"."));
098: addTest(new Test("a:subj a:prop \"\\\"string2\\\"\"."));
099: addTest(new Test("a:subj a:prop '\\'string1\\'\'."));
100: addTest(new Test("a:subj a:prop '\\\"string1\\\"\'."));
101:
102: addTest(new Test("a:q21 a:prop " + QUOTE3 + "start\"finish"
103: + QUOTE3 + "."));
104: addTest(new Test("a:q22 a:prop " + QUOTE3 + "start\"\"finish"
105: + QUOTE3 + "."));
106: addTest(new Test("a:q2e3 a:prop " + QUOTE3
107: + "start\\\"\\\"\\\"finish" + QUOTE3 + "."));
108: addTest(new Test("a:q13 a:prop " + QUOTE3 + "start'''finish"
109: + QUOTE3 + "."));
110:
111: addTest(new Test("a:q11 a:prop '''start'finish'''."));
112: addTest(new Test("a:q12 a:prop '''start''finish'''."));
113: addTest(new Test("a:q12 a:prop '''start\\'\\'\\'finish'''."));
114: addTest(new Test("a:q23 a:prop '''start\"\"\"finish'''."));
115:
116: // Keywords and syntactic sugar
117: addTest(new Test("this a:prop x:y ."));
118: addTest(new Test("a:subj a x:y ."));
119: addTest(new Test("a:subj = x:y ."));
120: addTest(new Test("a:subj => x:y ."));
121: addTest(new Test("a:subj <= x:y ."));
122: // <=> is not legal : it would mean "implies and is implied by"
123: // addTest(new Test("a:subj <=> x:y .")) ;
124: addTest(new Test("a:subj >- x:y -> 'value' ."));
125: addTest(new Test("a:subj >- x:y -> 'value1', 'value2' ."));
126:
127: // Not keywords
128: addTest(new Test("a:subj <a> x:y ."));
129: addTest(new Test("<this> a x:y ."));
130: addTest(new Test("@prefix has: <uri>."));
131: addTest(new Test("has:s a:a :of."));
132:
133: addTest(new Test("<> has a:prop x:y ."));
134: addTest(new Test("x:v is a:prop of <>.")); // Reverses subject and object
135: addTest(new Test("x:v is a:prop of a:s1, a:s2.")); // Reverses subject and object
136: addTest(new Test("<> a:prop x:y ."));
137: addTest(new Test("<#> a:prop x:y ."));
138:
139: // Object lists
140: addTest(new Test("a:subj a:prop a:d, a:e."));
141: addTest(new Test("a:subj a:prop a:d, '123'."));
142: addTest(new Test("a:subj a:prop '123', a:e."));
143: addTest(new Test("a:subj a:prop '123',.")); // Null object list
144: addTest(new Test("a:subj a:prop '123', '456', .")); // Null object list
145:
146: // Property lists
147: addTest(new Test("a:subj a:p1 a:v1 ; a:p2 a:v2 ."));
148: addTest(new Test(
149: "a:subj a:p1 a:v1, a:v2 ; a:p2 a:v2 ; a:p3 'v4' ,'v5' ."));
150: addTest(new Test("a:subj a:p1 a:v1; .")); // Null property list
151: addTest(new Test("a:subj a:p1 a:v1; a:v2; .")); // Null property list
152:
153: // anon nodes
154: addTest(new Test("[a:prop a:val]."));
155: addTest(new Test("[] a:prop a:val."));
156: addTest(new Test("[] a:prop []."));
157:
158: // formulae
159: // The final dot (statement terminator of outer statement) is necessary
160: // Inside formulae, it is not.
161: addTest(new Test("{:x :y :z} => {:x :y :z}."));
162: addTest(new Test("{:x :y :z} => {:x :y :z . }."));
163: addTest(new Test("{:x :y :z. } => {:x :y :z}."));
164:
165: // Variables
166: addTest(new Test("?who ?knows ?what ."));
167: addTest(new Test(
168: "{?who ?knows ?what} => {'somesort' 'of' 'logic'}."));
169:
170: // Formulae do not need the trailing '.'
171: addTest(new Test(
172: "{ this a \"string2\". } => { this a 'string1'} ."));
173:
174: // And they can have directives in.
175: addTest(new Test("{ @prefix : <a> } => { this a 'string1'} ."));
176: addTest(new Test(
177: "{ @prefix : <a> . a:x <b> 'c'} => { this a 'string1'} ."));
178:
179: // RDF collections
180: addTest(new Test("() ."));
181: addTest(new Test("<here> <list> ()."));
182: addTest(new Test(" ( a:i1 a:i2 a:i3 ) a daml:list."));
183:
184: // Paths
185: addTest(new Test(":x!:y <prop> []."));
186: addTest(new Test(":x!:y!:z <prop> []."));
187: addTest(new Test(":x^:y <prop> []."));
188: addTest(new Test(":x^:y^:z <prop> []."));
189: addTest(new Test("[] <prop> :x!:y^:z."));
190: addTest(new Test("[] :x^:y!:z []."));
191:
192: // Paths - using . (dot)
193: addTest(new Test(":x.:y <prop> []."));
194: addTest(new Test(":x.:y.:z <prop> []."));
195: addTest(new Test("[] <prop> :a.:c."));
196: addTest(new Test("<thing>.:y <prop> []."));
197: addTest(new Test("x:x.<thing>.:y <prop> []."));
198: addTest(new Test("<thing>.:y^:z <prop> []."));
199: addTest(new Test(":y.<thing>.:z <prop> []."));
200: addTest(new Test("<thing> :px.:py.:pz [] . "));
201: addTest(new Test("<thing> :px!:py!:pz [] . "));
202:
203: // Paths and formulae
204: addTest(new Test("{ :a.:b.:c . }."));
205: addTest(new Test("{ :a.:b.<c>.}."));
206:
207: // Named things
208: addTest(new Test("_:anon :- [a:p a:v] ."));
209: addTest(new Test("<uri> :- [a:p [ a:p a:v] ] ."));
210: // Named list: Not supported by cwm (as of 2001, 2002, 2003/09) but needed for printing shared
211: addTest(new Test("_:anon :- (\"1\") ."));
212: // Named formulae: Not supported by cwm (as of 2001, 2002, 2003/09)
213: addTest(new Test("_:anon :- { ?a ?b ?c } ."));
214:
215: // Datatypes
216: addTest(new Test("a:subj a:prop '123'^^xsd:integer ."));
217: addTest(new Test("a:subj a:prop '123'^^<uri> ."));
218: addTest(new Test(
219: "a:subj a:prop '<tag>text</tag>'^^rdf:XMLLiteral ."));
220:
221: // Numbers
222: addTest(new Test("a:subj a:prop 123 ."));
223: // addTest(new Test("a:subj a:prop 123.")) ; Illegal N3
224: addTest(new Test("a:subj a:prop 123.1 ."));
225: addTest(new Test("a:subj a:prop -123.1 ."));
226: addTest(new Test("a:subj a:prop 123.1e3 ."));
227: addTest(new Test("a:subj a:prop 123.1e-3 ."));
228: addTest(new Test("a:subj a:prop 123.1E3 ."));
229: addTest(new Test("a:subj a:prop 123.1E-3 ."));
230:
231: // Language tags
232: addTest(new Test("a:subj a:prop 'text'@en ."));
233: // Illegal in N-Triples
234: addTest(new Test("a:subj a:prop 'text'^^a:lang@en ."));
235: addTest(new Test("a:subj a:prop 'text'@en^^a:lang ."));
236:
237: // XML Literal
238: addTest(new Test(
239: "a:subj a:prop '<tag>text</tag>'@fr^^rdf:XMLLiteral ."));
240: addTest(new Test(
241: "a:subj a:prop '<tag>text</tag>'^^rdf:XMLLiteral@fr ."));
242:
243: addTest(new Test("a:subj a:prop ?x^^xsd:integer ."));
244: addTest(new Test("a:subj a:prop '123'^^?x ."));
245: addTest(new Test("a:subj a:prop ?x^^?y ."));
246:
247: // Unicode 00E9 is e-acute
248: // Unicode 03B1 is alpha
249: addTest(new Test("a:subj a:prop '\u00E9'."));
250: addTest(new Test("a:subj a:prop '\u003B1'."));
251:
252: addTest(new Test("\u00E9:subj a:prop '\u00E9'."));
253: addTest(new Test("a:subj-\u00E9 a:prop '\u00E9'."));
254:
255: addTest(new Test("\u03B1:subj a:prop '\u03B1'."));
256: addTest(new Test("a:subj-\u03B1 a:prop '\u03B1'."));
257: }
258:
259: class Test extends TestCase {
260: N3ParserEventHandler handler;
261: String testString;
262: boolean stoppingPoint = false;
263:
264: Test(String s) {
265: this (s, false);
266: }
267:
268: Test(String s, boolean leaveStoppingPoint) {
269: // Some earlier Eclipse versions have problems with comma in CVS test names.
270: // super("N3 Internal test: "+(s!=null?s.replace(',','_'):"<skipped test>")) ;
271: super ("N3 Internal test: "
272: + (s != null ? s : "<skipped test>"));
273: testString = s;
274: if (VERBOSE)
275: handler = new N3EventPrinter(pw);
276: else
277: handler = new NullN3EventHandler();
278: stoppingPoint = leaveStoppingPoint;
279: }
280:
281: protected void runTest() throws Throwable {
282: if (testString == null) {
283: if (pw != null)
284: pw.println("Skipped test");
285: return;
286: }
287:
288: if (stoppingPoint)
289: // No-op that will not be removed.
290: // i.e. it does nothing really but the compiler does not know.
291: stoppingPoint = false;
292:
293: if (pw != null)
294: pw.println("Input: " + testString);
295: N3Parser n3Parser = new N3Parser(new StringReader(
296: testString), handler);
297: n3Parser.parse();
298: if (pw != null) {
299: pw.println();
300: pw.flush();
301: }
302: }
303: }
304: }
305:
306: /*
307: * (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
308: * All rights reserved.
309: *
310: * Redistribution and use in source and binary forms, with or without
311: * modification, are permitted provided that the following conditions
312: * are met:
313: * 1. Redistributions of source code must retain the above copyright
314: * notice, this list of conditions and the following disclaimer.
315: * 2. Redistributions in binary form must reproduce the above copyright
316: * notice, this list of conditions and the following disclaimer in the
317: * documentation and/or other materials provided with the distribution.
318: * 3. The name of the author may not be used to endorse or promote products
319: * derived from this software without specific prior written permission.
320: *
321: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
322: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
323: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
324: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
325: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
326: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
327: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
328: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
329: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
330: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
331: */
|