KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > n3 > test > N3InternalTests


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

5
6 package com.hp.hpl.jena.n3.test;
7
8 import com.hp.hpl.jena.n3.* ;
9 import junit.framework.* ;
10 import java.io.* ;
11
12 /**
13  * @author Andy Seaborne
14  * @version $Id: N3InternalTests.java,v 1.13 2005/02/21 12:04:09 andy_seaborne Exp $
15  */

16 public class N3InternalTests extends TestSuite
17 {
18     /* JUnit swingUI needed this */
19     static public TestSuite suite() {
20         return new N3InternalTests() ;
21     }
22     
23
24     static public final String JavaDoc QUOTE3 = "\"\"\"" ;
25     static public boolean VERBOSE = false ;
26     PrintWriter pw = null ;
27     
28     N3InternalTests()
29     {
30         super("N3 Parser Syntactic tests") ;
31         
32         if ( VERBOSE )
33             pw = new PrintWriter(System.out) ;
34         
35         // ---- Debug testing
36
//addTest(new Test("<thing> b:px.b:py [] . ")) ;
37
// if ( true ) return ;
38
// ---- Debug testing
39

40         // Make sure basic things, at least, parse.
41

42         // URIs, qnames, statements, prefixes
43

44         // End of statement (and whitespace)
45
addTest(new Test("a:subj a:prop a:d .")) ;
46         addTest(new Test("a:subj a:prop a:d . ")) ;
47         addTest(new Test("a:subj a:prop a:d.")) ;
48         addTest(new Test("a:subj a:prop a:d. ")) ;
49
50         addTest(new Test("rdf: rdf:type :_.")) ;
51         addTest(new Test("@prefix start: <somewhere>.")) ;
52         addTest(new Test("<http://here/subj> <http://here/prep> <http://here/obj>.")) ;
53         
54         // Whitespace, comments
55
addTest(new Test("a:subj\ta:prop\ta:d.\t")) ;
56         addTest(new Test(" a:subj\ta:prop\ta:d. ")) ;
57         addTest(new Test("a:subj a:prop a:d. ")) ;
58         addTest(new Test("")) ;
59         addTest(new Test(" #Comment")) ;
60         addTest(new Test("a:subj a:prop a:d. # Comment")) ;
61         addTest(new Test("a:subj a:prop a:d.# Comment")) ;
62
63         // Literal: strings
64
addTest(new Test("a:subj a:prop 'string1'.")) ;
65         addTest(new Test("a:subj a:prop \"string2\".")) ;
66         addTest(new Test("a:subj a:prop '''string3'''.")) ;
67         addTest(new Test("a:subj a:prop "+QUOTE3+"string3"+QUOTE3+".")) ;
68         
69         // Literals: datatypes
70
addTest(new Test("a:subj a:prop 'string1'^^x:dt.")) ;
71         addTest(new Test("a:subj a:prop 'string1'^^<uriref>.")) ;
72         
73         // Literals: numbers.
74
addTest(new Test("a: :p 2.")) ;
75         addTest(new Test("a: :p +2.")) ;
76         addTest(new Test("a: :p -2 .")) ;
77         addTest(new Test("a: :p 2e6.")) ;
78         addTest(new Test("a: :p 2e-6.")) ;
79         addTest(new Test("a: :p -2e-6.")) ;
80         addTest(new Test("a: :p 2.0e-6.")) ;
81         addTest(new Test("a: :p 2.0.")) ;
82         
83         // Test numbers in qnames
84
addTest(new Test("a: _: 2:.")) ;
85         addTest(new Test("2.9 9:p 2.0.")) ;
86         addTest(new Test("_:a :2 :_2 .")) ;
87         //addTest(new Test("2. :p 2")) ;
88
addTest(new Test("2.0 :p 2.0.")) ;
89
90         // The "unusual" cases
91
addTest(new Test("a:subj 'prop'^^<uriref> 'string'.")) ;
92         addTest(new Test("a:subj a:prop 'string1'^^'stringDT'.")) ;
93
94         addTest(new Test("a:subj a:prop1 ?x ^^ x:dt.")) ;
95         addTest(new Test("a:subj a:prop2 ?x ^^ ?x.")) ;
96
97         // Quotes in string
98
addTest(new Test("a:subj a:prop \"\\'string2\\'\".")) ;
99         addTest(new Test("a:subj a:prop \"\\\"string2\\\"\".")) ;
100         addTest(new Test("a:subj a:prop '\\'string1\\'\'.")) ;
101         addTest(new Test("a:subj a:prop '\\\"string1\\\"\'.")) ;
102         
103         addTest(new Test("a:q21 a:prop "+QUOTE3+"start\"finish"+QUOTE3+".")) ;
104         addTest(new Test("a:q22 a:prop "+QUOTE3+"start\"\"finish"+QUOTE3+".")) ;
105         addTest(new Test("a:q2e3 a:prop "+QUOTE3+"start\\\"\\\"\\\"finish"+QUOTE3+".")) ;
106         addTest(new Test("a:q13 a:prop "+QUOTE3+"start'''finish"+QUOTE3+".")) ;
107         
108         addTest(new Test("a:q11 a:prop '''start'finish'''.")) ;
109         addTest(new Test("a:q12 a:prop '''start''finish'''.")) ;
110         addTest(new Test("a:q12 a:prop '''start\\'\\'\\'finish'''.")) ;
111         addTest(new Test("a:q23 a:prop '''start\"\"\"finish'''.")) ;
112         
113         // Keywords and syntactic sugar
114
addTest(new Test("this a:prop x:y .")) ;
115         addTest(new Test("a:subj a x:y .")) ;
116         addTest(new Test("a:subj = x:y .")) ;
117         addTest(new Test("a:subj => x:y .")) ;
118         addTest(new Test("a:subj <= x:y .")) ;
119         // <=> is not legal : it would mean "implies and is implied by"
120
// addTest(new Test("a:subj <=> x:y .")) ;
121
addTest(new Test("a:subj >- x:y -> 'value' .")) ;
122         addTest(new Test("a:subj >- x:y -> 'value1', 'value2' .")) ;
123         
124         // Not keywords
125
addTest(new Test("a:subj <a> x:y .")) ;
126         addTest(new Test("<this> a x:y .")) ;
127         addTest(new Test("@prefix has: <uri>.")) ;
128         addTest(new Test("has:s a:a :of.")) ;
129         
130         addTest(new Test("<> has a:prop x:y .")) ;
131         addTest(new Test("x:v is a:prop of <>.")) ; // Reverses subject and object
132
addTest(new Test("x:v is a:prop of a:s1, a:s2.")) ; // Reverses subject and object
133
addTest(new Test("<> a:prop x:y .")) ;
134         addTest(new Test("<#> a:prop x:y .")) ;
135         
136         // Object lists
137
addTest(new Test("a:subj a:prop a:d, a:e.")) ;
138         addTest(new Test("a:subj a:prop a:d, '123'.")) ;
139         addTest(new Test("a:subj a:prop '123', a:e.")) ;
140         addTest(new Test("a:subj a:prop '123',.")) ; // Null object list
141
addTest(new Test("a:subj a:prop '123', '456', .")) ; // Null object list
142

143         // Property lists
144
addTest(new Test("a:subj a:p1 a:v1 ; a:p2 a:v2 .")) ;
145         addTest(new Test("a:subj a:p1 a:v1, a:v2 ; a:p2 a:v2 ; a:p3 'v4' ,'v5' .")) ;
146         addTest(new Test("a:subj a:p1 a:v1; .")) ; // Null property list
147
addTest(new Test("a:subj a:p1 a:v1; a:v2; .")) ; // Null property list
148

149         
150         // anon nodes
151
addTest(new Test("[a:prop a:val].")) ;
152         addTest(new Test("[] a:prop a:val.")) ;
153         addTest(new Test("[] a:prop [].")) ;
154         
155         // formulae
156
// The final dot (statement terminator of outer statement) is necessary
157
// Inside formulae, it is not.
158
addTest(new Test("{:x :y :z} => {:x :y :z}.")) ;
159         addTest(new Test("{:x :y :z} => {:x :y :z . }.")) ;
160         addTest(new Test("{:x :y :z. } => {:x :y :z}.")) ;
161         
162         // Variables
163
addTest(new Test("?who ?knows ?what .")) ;
164         addTest(new Test("{?who ?knows ?what} => {'somesort' 'of' 'logic'}." )) ;
165         
166         // Formulae do not need the trailing '.'
167
addTest(new Test("{ this a \"string2\". } => { this a 'string1'} .")) ;
168         
169         // And they can have directives in.
170
addTest(new Test("{ @prefix : <a> } => { this a 'string1'} .")) ;
171         addTest(new Test("{ @prefix : <a> . a:x <b> 'c'} => { this a 'string1'} .")) ;
172         
173         // RDF collections
174
addTest(new Test("() .")) ;
175         addTest(new Test("<here> <list> ().")) ;
176         addTest(new Test(" ( a:i1 a:i2 a:i3 ) a daml:list.")) ;
177         
178         // Paths
179
addTest(new Test(":x!:y <prop> [].")) ;
180         addTest(new Test(":x!:y!:z <prop> [].")) ;
181         addTest(new Test(":x^:y <prop> [].")) ;
182         addTest(new Test(":x^:y^:z <prop> [].")) ;
183         addTest(new Test("[] <prop> :x!:y^:z.")) ;
184         addTest(new Test("[] :x^:y!:z [].")) ;
185         
186         // Paths - using . (dot)
187
addTest(new Test(":x.:y <prop> [].")) ;
188         addTest(new Test(":x.:y.:z <prop> [].")) ;
189         addTest(new Test("[] <prop> :a.:c.")) ;
190         addTest(new Test("<thing>.:y <prop> [].")) ;
191         addTest(new Test("x:x.<thing>.:y <prop> [].")) ;
192         addTest(new Test("<thing>.:y^:z <prop> [].")) ;
193         addTest(new Test(":y.<thing>.:z <prop> [].")) ;
194         addTest(new Test("<thing> :px.:py.:pz [] . ")) ;
195         addTest(new Test("<thing> :px!:py!:pz [] . ")) ;
196         
197         // Paths and formulae
198
addTest(new Test("{ :a.:b.:c . }.")) ;
199         addTest(new Test("{ :a.:b.<c>.}.")) ;
200         
201         // Named things
202
addTest(new Test("_:anon :- [a:p a:v] .")) ;
203         addTest(new Test("<uri> :- [a:p [ a:p a:v] ] .")) ;
204         // Named list: Not supported by cwm (as of 2001, 2002, 2003/09) but needed for printing shared
205
addTest(new Test("_:anon :- (\"1\") .")) ;
206         // Named formulae: Not supported by cwm (as of 2001, 2002, 2003/09)
207
addTest(new Test("_:anon :- { ?a ?b ?c } .")) ;
208         
209         // Datatypes
210
addTest(new Test("a:subj a:prop '123'^^xsd:integer .")) ;
211         addTest(new Test("a:subj a:prop '123'^^<uri> .")) ;
212         addTest(new Test("a:subj a:prop '<tag>text</tag>'^^rdf:XMLLiteral .")) ;
213         
214         // Numbers
215
addTest(new Test("a:subj a:prop 123 .")) ;
216         // addTest(new Test("a:subj a:prop 123.")) ; Illegal N3
217
addTest(new Test("a:subj a:prop 123.1 .")) ;
218         addTest(new Test("a:subj a:prop -123.1 .")) ;
219         addTest(new Test("a:subj a:prop 123.1e3 .")) ;
220         addTest(new Test("a:subj a:prop 123.1e-3 .")) ;
221         addTest(new Test("a:subj a:prop 123.1E3 .")) ;
222         addTest(new Test("a:subj a:prop 123.1E-3 .")) ;
223
224         // Language tags
225
addTest(new Test("a:subj a:prop 'text'@en .")) ;
226         // Illegal in N-Triples
227
addTest(new Test("a:subj a:prop 'text'^^a:lang@en .")) ;
228         addTest(new Test("a:subj a:prop 'text'@en^^a:lang .")) ;
229         
230         // XML Literal
231
addTest(new Test("a:subj a:prop '<tag>text</tag>'@fr^^rdf:XMLLiteral .")) ;
232         addTest(new Test("a:subj a:prop '<tag>text</tag>'^^rdf:XMLLiteral@fr .")) ;
233
234         addTest(new Test("a:subj a:prop ?x^^xsd:integer .")) ;
235         addTest(new Test("a:subj a:prop '123'^^?x .")) ;
236         addTest(new Test("a:subj a:prop ?x^^?y .")) ;
237         
238         // Unicode 00E9 is e-acute
239
// Unicode 03B1 is alpha
240
addTest(new Test("a:subj a:prop '\u00E9'.")) ;
241         addTest(new Test("a:subj a:prop '\u003B1'.")) ;
242         
243         addTest(new Test("\u00E9:subj a:prop '\u00E9'.")) ;
244         addTest(new Test("a:subj-\u00E9 a:prop '\u00E9'.")) ;
245         
246         addTest(new Test("\u03B1:subj a:prop '\u03B1'.")) ;
247         addTest(new Test("a:subj-\u03B1 a:prop '\u03B1'.")) ;
248     }
249     
250     class Test extends TestCase
251     {
252         N3ParserEventHandler handler ;
253         String JavaDoc testString ;
254         boolean stoppingPoint = false ;
255         
256         Test(String JavaDoc s)
257         {
258             this(s, false) ;
259         }
260         
261         Test(String JavaDoc s, boolean leaveStoppingPoint)
262         {
263             // Some earlier Eclipse versions have problems with comma in CVS test names.
264
// super("N3 Internal test: "+(s!=null?s.replace(',','_'):"<skipped test>")) ;
265
super("N3 Internal test: "+(s!=null?s:"<skipped test>")) ;
266             testString = s ;
267             if ( VERBOSE )
268                 handler = new N3EventPrinter(pw) ;
269             else
270                 handler = new NullN3EventHandler() ;
271             stoppingPoint = leaveStoppingPoint ;
272         }
273     
274         
275         
276         protected void runTest() throws Throwable JavaDoc
277         {
278             if ( testString == null )
279             {
280                 if ( pw != null )
281                     pw.println("Skipped test") ;
282                     return ;
283             }
284             
285             if ( stoppingPoint )
286                 // No-op that will not be removed.
287
// i.e. it does nothing really but the compiler does not know.
288
stoppingPoint = false ;
289             
290             if ( pw != null )
291                 pw.println("Input: "+testString) ;
292             N3Parser n3Parser =
293                 new N3Parser(new StringReader(testString), handler);
294             n3Parser.parse();
295             if ( pw != null )
296             {
297                 pw.println() ;
298                 pw.flush() ;
299             }
300         }
301     }
302 }
303
304 /*
305  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
306  * All rights reserved.
307  *
308  * Redistribution and use in source and binary forms, with or without
309  * modification, are permitted provided that the following conditions
310  * are met:
311  * 1. Redistributions of source code must retain the above copyright
312  * notice, this list of conditions and the following disclaimer.
313  * 2. Redistributions in binary form must reproduce the above copyright
314  * notice, this list of conditions and the following disclaimer in the
315  * documentation and/or other materials provided with the distribution.
316  * 3. The name of the author may not be used to endorse or promote products
317  * derived from this software without specific prior written permission.
318  *
319  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
320  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
321  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
322  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
323  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
324  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
325  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
326  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
327  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
328  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
329  */

330
Popular Tags