KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > test > TestExpressions


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.rdql.test;
7
8 // Quick import of test code into JUnit.
9
// Mega-tests should be broken up. Can use TestCase.setName to change the name while running.
10

11 import java.io.* ;
12
13 import com.hp.hpl.jena.rdql.parser.*;
14
15 import junit.framework.* ;
16
17
18 public class TestExpressions extends TestSuite
19 {
20     static final String JavaDoc testSetName = "RDQL - Expressions" ;
21     
22     public static TestSuite suite()
23     {
24         return new TestExpressions(testSetName) ;
25     }
26     
27     private TestExpressions(String JavaDoc name)
28     {
29         super(name) ;
30         
31         final boolean FAILURE_OK = true ;
32         final boolean NO_FAILURE = false ;
33
34         String JavaDoc xsd = "http://www.w3.org/2001/XMLSchema#" ;
35
36         addTest(new TestNumeric("7", 7)) ;
37         addTest(new TestNumeric("-3", -3)) ;
38         addTest(new TestNumeric("3+4+5", 3+4+5)) ;
39         // Test the trees
40
addTest(new TestNumeric("(3+4)+5", 3+4+5)) ;
41         addTest(new TestNumeric("3+(4+5)", 3+4+5)) ;
42
43         // Precedence
44
addTest(new TestNumeric("3*4+5", 3*4+5)) ;
45         addTest(new TestNumeric("3*(4+5)", 3*(4+5))) ;
46
47         addTest(new TestNumeric("10-3-5", 10-3-5)) ;
48         addTest(new TestNumeric("(10-3)-5", (10-3)-5)) ;
49         addTest(new TestNumeric("10-(3-5)", 10-(3-5))) ;
50         addTest(new TestNumeric("10-3+5", 10-3+5)) ;
51         addTest(new TestNumeric("10-(3+5)", 10-(3+5))) ;
52
53         addTest(new TestNumeric("1<<2", 1<<2)) ;
54         addTest(new TestNumeric("1<<2<<2", 1<<2<<2)) ;
55
56         addTest(new TestNumeric("10000>>2", 10000>>2)) ;
57
58         addTest(new TestNumeric("1.5 + 2.5", 1.5+2.5)) ;
59         addTest(new TestNumeric("1.5 + 2", 1.5+2)) ;
60         
61         // Test longs
62
// A long is over 32bites signed = +2Gig
63
addTest(new TestNumeric("4111222333444", 4111222333444L)) ;
64         addTest(new TestNumeric("1234 + 4111222333444", 1234 + 4111222333444L)) ;
65         
66         // Boolean
67
addTest(new TestBoolean("true", NO_FAILURE, true)) ;
68
69         addTest(new TestBoolean("false", NO_FAILURE, false)) ;
70
71         addTest(new TestBoolean("false || true", NO_FAILURE, true)) ;
72         addTest(new TestBoolean("false && true", NO_FAILURE, false)) ;
73
74         addTest(new TestBoolean("2 < 3", NO_FAILURE, 2 < 3)) ;
75         addTest(new TestBoolean("2 > 3", NO_FAILURE, 2 > 3)) ;
76         addTest(new TestBoolean("(2 < 3) && (3<4)", NO_FAILURE, (2 < 3) && (3<4))) ;
77         addTest(new TestBoolean("(2 < 3) && (3>=4)", NO_FAILURE, (2 < 3) && (3>=4))) ;
78         addTest(new TestBoolean("(2 < 3) || (3>=4)", NO_FAILURE, (2 < 3) || (3>=4))) ;
79         addTest(new TestBoolean("2 == 3", NO_FAILURE, 2 == 3)) ;
80         
81         // Check that strings are coerced if needed
82
addTest(new TestBoolean("2 < '3'", false, 2 < 3)) ;
83
84         addTest(new TestBoolean("\"fred\" ne \"joe\"", NO_FAILURE, true )) ;
85         addTest(new TestBoolean("\"fred\" eq \"joe\"", NO_FAILURE, false )) ;
86         addTest(new TestBoolean("\"fred\" eq \"fred\"", NO_FAILURE, true )) ;
87         addTest(new TestBoolean("\"fred\" eq 'fred'", NO_FAILURE, true )) ;
88         addTest(new TestBoolean("\"fred\" eq 'fr\\ed'", NO_FAILURE, true )) ;
89         addTest(new TestBoolean("\"fred\" ne \"fred\"", NO_FAILURE, false )) ;
90         
91         // Typed literals
92
// Same types.
93
addTest(new TestBoolean("'fred'^^<type1> eq 'fred'^^<type1>", NO_FAILURE, true )) ;
94         addTest(new TestBoolean("'fred'^^<type1> ne 'joe'^^<type1>", NO_FAILURE, true )) ;
95         // Different types.
96
addTest(new TestBoolean("'fred'^^<type1> eq 'fred'^^<type2>", NO_FAILURE, false )) ;
97         addTest(new TestBoolean("'fred'^^<type1> ne 'fred'^^<type2>", NO_FAILURE, true )) ;
98         
99         // true: xsd:string is sameValueAs plain (classic) RDF literal
100
addTest(new TestBoolean("'fred'^^<"+xsd+"string> eq 'fred'", NO_FAILURE, true )) ;
101         // false: parsing created two RDF literals and these are different
102
addTest(new TestBoolean("'fred'^^<type1> eq 'fred'", NO_FAILURE, false )) ;
103         addTest(new TestBoolean("'fred'^^<type1> ne 'fred'", NO_FAILURE, true )) ;
104         
105         // Numeric expessions: ignore typing (compatibility with RDF-99)
106
addTest(new TestBoolean("'21'^^<int> == '21'", NO_FAILURE, true )) ;
107
108         // Escapes in strings
109
addTest(new TestBoolean("\"fred\\1\" eq 'fred1'", NO_FAILURE, true )) ;
110         addTest(new TestBoolean("\"fred2\" eq 'fred\\2'", NO_FAILURE, true )) ;
111         addTest(new TestBoolean("'fred\\\\3' ne \"fred3\"", NO_FAILURE, true )) ;
112
113
114         addTest(new TestBoolean("\"urn:fred\" eq <urn:fred>", NO_FAILURE, true )) ;
115         addTest(new TestBoolean("\"urn:fred\" ne <urn:fred>", NO_FAILURE, false )) ;
116
117         addTest(new TestBoolean("\"urn:fred/1.5\" ne <urn:fred/1.5>", NO_FAILURE, false )) ;
118         
119         // Lang tags - tests code from Zhexuan Song [zsong@fla.fujitsu.com] (Jeff)
120
addTest(new TestBoolean("\"xxx\"@fr langeq 'fr'", NO_FAILURE, true )) ;
121         addTest(new TestBoolean("\"xxx\"@fr langeq \"fr\"", NO_FAILURE, true )) ;
122         addTest(new TestBoolean("\"xxx\"@en-gb langeq 'en'", NO_FAILURE, true )) ;
123         addTest(new TestBoolean("\"xxx\"@en-gb langeq 'EN'", NO_FAILURE, true )) ;
124         
125         addTest(new TestBoolean("\"xxx\"@en-gb langeq 'fr'", NO_FAILURE, false )) ;
126         addTest(new TestBoolean("\"chat\"@fr langeq 'en'", NO_FAILURE, false )) ;
127         
128         // Regular expressions
129
addTest(new TestBoolean("\"aabbcc\" =~ /abbc/", NO_FAILURE, true )) ;
130         addTest(new TestBoolean("\"aabbcc\" =~ /a..c/", NO_FAILURE, true )) ;
131         addTest(new TestBoolean("\"aabbcc\" =~ /^aabb/", NO_FAILURE, true )) ;
132         addTest(new TestBoolean("\"aabbcc\" =~ /cc$/", NO_FAILURE, true )) ;
133         addTest(new TestBoolean("\"aabbcc\" !~ /abbc/", NO_FAILURE, false )) ;
134         
135         addTest(new TestBoolean("\"aab*bcc\" =~ /ab\\*bc/", NO_FAILURE, true )) ;
136         addTest(new TestBoolean("\"aabbcc\" ~~ /ab\\\\*bc/", NO_FAILURE, true )) ;
137         addTest(new TestBoolean("'aabbcc' =~ /B.*B/i", NO_FAILURE, true )) ;
138
139         addTest(new TestBoolean("1.5 < 2", NO_FAILURE, 1.5 < 2 )) ;
140         addTest(new TestBoolean("1.5 > 2", NO_FAILURE, 1.5 > 2 )) ;
141         addTest(new TestBoolean("1.5 < 2.3", NO_FAILURE, 1.5 < 2.3 )) ;
142         addTest(new TestBoolean("1.5 > 2.3", NO_FAILURE, 1.5 > 2.3 )) ;
143         
144         // Longs
145
addTest(new TestBoolean("4111222333444 > 1234", NO_FAILURE, 4111222333444L > 1234)) ;
146         addTest(new TestBoolean("4111222333444 < 1234", NO_FAILURE, 4111222333444L < 1234L)) ;
147         
148         // These are false because a failure should occur
149

150         addTest(new TestBoolean("2 < \"fred\"", FAILURE_OK, false)) ;
151         addTest(new TestBoolean("2 || true", FAILURE_OK, false)) ;
152     }
153
154
155     static class TestNumeric extends TestCase
156     {
157         String JavaDoc s ;
158         boolean isDouble = false ;
159         long rightAnswer ;
160         double rightAnswerDouble ;
161
162         TestNumeric(String JavaDoc _s, long _rightAnswer)
163         {
164             super("Numeric test : "+_s+" ") ;
165             s = _s ;
166             rightAnswer = _rightAnswer ;
167             isDouble = false ;
168         }
169
170         TestNumeric(String JavaDoc _s, double _rightAnswer)
171         {
172             super("Numeric test : "+_s+" ") ;
173             s = _s ;
174             rightAnswerDouble = _rightAnswer ;
175             isDouble = true ;
176         }
177
178         protected void runTest() throws Throwable JavaDoc
179         {
180             long initTime = 0;
181             long parseTime = 0;
182             long startTime = 0;
183             long stopTime = 0;
184
185             ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()) ;
186             RDQLParser parser = new RDQLParser(in) ;
187             startTime = System.currentTimeMillis();
188
189             //parser.CompilationUnit();
190
//System.out.println("Input: "+s);
191

192             try {
193                 parser.Expression() ;
194                 // Be careful, catch ASAP - JUnit uses errors internally
195
}
196             catch (Error JavaDoc e)
197             {
198                 fail("Error thrown in parse: "+e) ;
199             }
200
201             stopTime = System.currentTimeMillis();
202             parseTime = stopTime - startTime;
203             //System.out.println("Time: "+parseTime+"ms") ;
204

205             //parser.top().dump(" ");
206
parser.top().postParse(null) ;
207             Expr e = (Expr)parser.top() ;
208             
209
210             assertTrue("Expression is not ExprNumeric: "+e.getClass().getName() ,
211                        (e instanceof ExprNumeric)) ;
212
213             ExprNumeric n = (ExprNumeric)e ;
214
215             NodeValue v = n.eval(null, null) ;
216
217             if ( ! isDouble )
218                 assertEquals(s+" => "+v.getInt()+" ["+rightAnswer+"]", v.getInt(), rightAnswer ) ;
219             else
220                 assertEquals(s+" => "+v.getDouble()+" ["+rightAnswerDouble+"]", v.getDouble(), rightAnswerDouble, 0.0001 ) ;
221         }
222     } // End of inner class
223

224
225     static class TestBoolean extends TestCase
226     {
227         String JavaDoc s ;
228         boolean failureCorrect ;
229         boolean rightAnswer ;
230
231         TestBoolean(String JavaDoc _s, boolean _failureCorrect, boolean _rightAnswer)
232         {
233             super("Boolean test : "+_s+" ") ;
234             s = _s ;
235             failureCorrect = _failureCorrect ;
236             rightAnswer = _rightAnswer ;
237         }
238
239         protected void runTest() throws Throwable JavaDoc
240         {
241             long initTime = 0;
242             long parseTime = 0;
243             long startTime = 0;
244             long stopTime = 0;
245
246             ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes()) ;
247             RDQLParser parser = new RDQLParser(in) ;
248             startTime = System.currentTimeMillis();
249
250             try {
251                 parser.Expression() ;
252             } catch (Error JavaDoc e)
253             {
254                 super.fail("Error throw in parse: "+s) ;
255             }
256
257             stopTime = System.currentTimeMillis();
258             parseTime = stopTime - startTime;
259             //System.out.println("Time: "+parseTime+"ms") ;
260

261             parser.top().postParse(null) ;
262             Expr e = (Expr)parser.top() ;
263
264             assertTrue("Expression is not ExprBoolean: "+e.getClass().getName(),
265                        (e instanceof ExprBoolean) ) ;
266
267             ExprBoolean n = (ExprBoolean)e ;
268
269             NodeValue v = null ;
270             boolean result = false ;
271             try {
272                 v = n.eval(null, null) ;
273                 result = v.getBoolean() ;
274             } catch (com.hp.hpl.jena.rdql.EvalFailureException evalEx)
275             {
276                 if ( ! failureCorrect )
277                     throw evalEx ;
278                 result = false ;
279
280             }
281             assertEquals(s+" => "+result+" ["+rightAnswer+"]", result, rightAnswer ) ;
282         }
283     }
284 }
285
286 /*
287  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
288  * All rights reserved.
289  *
290  * Redistribution and use in source and binary forms, with or without
291  * modification, are permitted provided that the following conditions
292  * are met:
293  * 1. Redistributions of source code must retain the above copyright
294  * notice, this list of conditions and the following disclaimer.
295  * 2. Redistributions in binary form must reproduce the above copyright
296  * notice, this list of conditions and the following disclaimer in the
297  * documentation and/or other materials provided with the distribution.
298  * 3. The name of the author may not be used to endorse or promote products
299  * derived from this software without specific prior written permission.
300  *
301  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
302  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
303  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
304  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
305  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
306  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
307  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
308  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
309  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
310  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311  */

312
Popular Tags