1 5 6 package com.hp.hpl.jena.rdql.test; 7 8 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 testSetName = "RDQL - Expressions" ; 21 22 public static TestSuite suite() 23 { 24 return new TestExpressions(testSetName) ; 25 } 26 27 private TestExpressions(String name) 28 { 29 super(name) ; 30 31 final boolean FAILURE_OK = true ; 32 final boolean NO_FAILURE = false ; 33 34 String 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 addTest(new TestNumeric("(3+4)+5", 3+4+5)) ; 41 addTest(new TestNumeric("3+(4+5)", 3+4+5)) ; 42 43 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 addTest(new TestNumeric("4111222333444", 4111222333444L)) ; 64 addTest(new TestNumeric("1234 + 4111222333444", 1234 + 4111222333444L)) ; 65 66 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 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 addTest(new TestBoolean("'fred'^^<type1> eq 'fred'^^<type1>", NO_FAILURE, true )) ; 94 addTest(new TestBoolean("'fred'^^<type1> ne 'joe'^^<type1>", NO_FAILURE, true )) ; 95 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 addTest(new TestBoolean("'fred'^^<"+xsd+"string> eq 'fred'", NO_FAILURE, true )) ; 101 addTest(new TestBoolean("'fred'^^<type1> eq 'fred'", NO_FAILURE, false )) ; 103 addTest(new TestBoolean("'fred'^^<type1> ne 'fred'", NO_FAILURE, true )) ; 104 105 addTest(new TestBoolean("'21'^^<int> == '21'", NO_FAILURE, true )) ; 107 108 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 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 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 addTest(new TestBoolean("4111222333444 > 1234", NO_FAILURE, 4111222333444L > 1234)) ; 146 addTest(new TestBoolean("4111222333444 < 1234", NO_FAILURE, 4111222333444L < 1234L)) ; 147 148 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 s ; 158 boolean isDouble = false ; 159 long rightAnswer ; 160 double rightAnswerDouble ; 161 162 TestNumeric(String _s, long _rightAnswer) 163 { 164 super("Numeric test : "+_s+" ") ; 165 s = _s ; 166 rightAnswer = _rightAnswer ; 167 isDouble = false ; 168 } 169 170 TestNumeric(String _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 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 192 try { 193 parser.Expression() ; 194 } 196 catch (Error e) 197 { 198 fail("Error thrown in parse: "+e) ; 199 } 200 201 stopTime = System.currentTimeMillis(); 202 parseTime = stopTime - startTime; 203 205 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 } 224 225 static class TestBoolean extends TestCase 226 { 227 String s ; 228 boolean failureCorrect ; 229 boolean rightAnswer ; 230 231 TestBoolean(String _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 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 e) 253 { 254 super.fail("Error throw in parse: "+s) ; 255 } 256 257 stopTime = System.currentTimeMillis(); 258 parseTime = stopTime - startTime; 259 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 312 | Popular Tags |