1 22 package org.jboss.test.jbossmq.test; 23 24 import java.io.ByteArrayInputStream ; 25 import java.util.HashMap ; 26 27 import org.jboss.mq.selectors.ISelectorParser; 28 import org.jboss.mq.selectors.Identifier; 29 import org.jboss.mq.selectors.Operator; 30 import org.jboss.mq.selectors.SelectorParser; 31 import org.jboss.test.JBossTestCase; 32 33 43 public class SelectorParserUnitTestCase extends JBossTestCase 44 { 45 static HashMap identifierMap = new HashMap (); 46 static ISelectorParser parser; 47 48 public SelectorParserUnitTestCase(String name) 49 { 50 super(name); 51 } 52 53 protected void setUp() throws Exception 54 { 55 super.setUp(); 56 identifierMap.clear(); 57 if( parser == null ) 58 { 59 parser = new SelectorParser(new ByteArrayInputStream (new byte[0])); 60 } 61 } 62 63 public void testSimpleUnary() throws Exception 64 { 65 getLog().debug("parse(-12345 = -1 * 12345)"); 67 Operator result = (Operator) parser.parse("-12345 = -1 * 12345", identifierMap); 68 getLog().debug("result -> "+result); 69 Boolean b = (Boolean ) result.apply(); 70 assertTrue("is true", b.booleanValue()); 71 72 getLog().debug("parse(-1 * 12345.67 = -12345.67)"); 74 result = (Operator) parser.parse("-1 * 12345.67 = -12345.67", identifierMap); 75 getLog().debug("result -> "+result); 76 b = (Boolean ) result.apply(); 77 assertTrue("is true", b.booleanValue()); 78 79 getLog().debug("parse(-(1 * 12345.67) = -12345.67)"); 80 result = (Operator) parser.parse("-(1 * 12345.67) = -12345.67", identifierMap); 81 getLog().debug("result -> "+result); 82 b = (Boolean ) result.apply(); 83 assertTrue("is true", b.booleanValue()); 84 } 85 86 public void testPrecedenceNAssoc() throws Exception 87 { 88 getLog().debug("parse(4 + 2 * 3 / 2 = 7)"); 89 Operator result = (Operator) parser.parse("4 + 2 * 3 / 2 = 7", identifierMap); 90 getLog().debug("result -> "+result); 91 Boolean b = (Boolean ) result.apply(); 92 assertTrue("is true", b.booleanValue()); 93 94 getLog().debug("parse(4 + ((2 * 3) / 2) = 7)"); 95 result = (Operator) parser.parse("4 + ((2 * 3) / 2) = 7", identifierMap); 96 getLog().debug("result -> "+result); 97 b = (Boolean ) result.apply(); 98 assertTrue("is true", b.booleanValue()); 99 100 getLog().debug("parse(4 * -2 / -1 - 4 = 4)"); 101 result = (Operator) parser.parse("4 * -2 / -1 - 4 = 4", identifierMap); 102 getLog().debug("result -> "+result); 103 b = (Boolean ) result.apply(); 104 assertTrue("is true", b.booleanValue()); 105 106 getLog().debug("parse(4 * ((-2 / -1) - 4) = -8)"); 107 result = (Operator) parser.parse("4 * ((-2 / -1) - 4) = -8", identifierMap); 108 getLog().debug("result -> "+result); 109 b = (Boolean ) result.apply(); 110 assertTrue("is true", b.booleanValue()); 111 } 112 113 public void testIds() throws Exception 114 { 115 getLog().debug("parse(a + b * c / d = e)"); 116 Operator result = (Operator) parser.parse("a + b * c / d = e", identifierMap); 117 Identifier a = (Identifier) identifierMap.get("a"); 119 a.setValue(new Long (4)); 120 Identifier b = (Identifier) identifierMap.get("b"); 121 b.setValue(new Long (2)); 122 Identifier c = (Identifier) identifierMap.get("c"); 123 c.setValue(new Long (3)); 124 Identifier d = (Identifier) identifierMap.get("d"); 125 d.setValue(new Long (2)); 126 Identifier e = (Identifier) identifierMap.get("e"); 127 e.setValue(new Long (7)); 128 getLog().debug("result -> "+result); 129 Boolean bool = (Boolean ) result.apply(); 130 assertTrue("is true", bool.booleanValue()); 131 132 } 133 134 public void testTrueINOperator() throws Exception 135 { 136 getLog().debug("parse(Status IN ('new', 'cleared', 'acknowledged'))"); 137 Operator result = (Operator) parser.parse("Status IN ('new', 'cleared', 'acknowledged')", identifierMap); 138 Identifier a = (Identifier) identifierMap.get("Status"); 139 a.setValue("new"); 140 getLog().debug("result -> "+result); 141 Boolean bool = (Boolean ) result.apply(); 142 assertTrue("is true", bool.booleanValue()); 143 } 144 public void testFalseINOperator() throws Exception 145 { 146 getLog().debug("parse(Status IN ('new', 'cleared', 'acknowledged'))"); 147 Operator result = (Operator) parser.parse("Status IN ('new', 'cleared', 'acknowledged')", identifierMap); 148 Identifier a = (Identifier) identifierMap.get("Status"); 149 a.setValue("none"); 150 getLog().debug("result -> "+result); 151 Boolean bool = (Boolean ) result.apply(); 152 assertTrue("is false", !bool.booleanValue()); 153 } 154 155 public void testTrueOROperator() throws Exception 156 { 157 getLog().debug("parse((Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged'))"); 158 Operator result = (Operator) parser.parse("(Status = 'new') OR (Status = 'cleared') OR (Status= 'acknowledged')", identifierMap); 159 Identifier a = (Identifier) identifierMap.get("Status"); 160 a.setValue("new"); 161 getLog().debug("result -> "+result); 162 Boolean bool = (Boolean ) result.apply(); 163 assertTrue("is true", bool.booleanValue()); 164 } 165 public void testFalseOROperator() throws Exception 166 { 167 getLog().debug("parse((Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged'))"); 168 Operator result = (Operator) parser.parse("(Status = 'new') OR (Status = 'cleared') OR (Status = 'acknowledged')", identifierMap); 169 Identifier a = (Identifier) identifierMap.get("Status"); 170 a.setValue("none"); 171 getLog().debug("result -> "+result); 172 Boolean bool = (Boolean ) result.apply(); 173 assertTrue("is false", !bool.booleanValue()); 174 } 175 176 public void testInvalidSelector() throws Exception 177 { 178 getLog().debug("parse(definitely not a message selector!)"); 179 try 180 { 181 Object result = parser.parse("definitely not a message selector!", identifierMap); 182 getLog().debug("result -> "+result); 183 fail("Should throw an Exception.\n"); 184 } 185 catch (Exception e) 186 { 187 getLog().info("testInvalidSelector failed as expected", e); 188 } 189 } 190 191 194 public void testApproximateNumericLiteral1() 195 { 196 try 197 { 198 getLog().debug("parse(average = +6.2)"); 199 Object result = parser.parse("average = +6.2", identifierMap); 200 getLog().debug("result -> "+result); 201 } catch (Exception e) 202 { 203 fail(""+e); 204 } 205 } 206 207 public void testApproximateNumericLiteral2() 208 { 209 try 210 { 211 getLog().debug("parse(average = -95.7)"); 212 Object result = parser.parse("average = -95.7", identifierMap); 213 getLog().debug("result -> "+result); 214 } catch (Exception e) 215 { 216 fail(""+e); 217 } 218 } 219 public void testApproximateNumericLiteral3() 220 { 221 try 222 { 223 getLog().debug("parse(average = 7.)"); 224 Object result = parser.parse("average = 7.", identifierMap); 225 getLog().debug("result -> "+result); 226 } catch (Exception e) 227 { 228 fail(""+e); 229 } 230 } 231 232 public void testGTExact() 233 { 234 try 235 { 236 getLog().debug("parse(weight > 2500)"); 237 Operator result = (Operator)parser.parse("weight > 2500", identifierMap); 238 ((Identifier) identifierMap.get("weight")).setValue(new Integer (3000)); 239 getLog().debug("result -> "+result); 240 Boolean bool = (Boolean ) result.apply(); 241 assertTrue("is true", bool.booleanValue()); 242 } catch (Exception e) 243 { 244 getLog().debug("failed", e); 245 fail(""+e); 246 } 247 } 248 249 public void testGTFloat() 250 { 251 try 252 { 253 getLog().debug("parse(weight > 2500)"); 254 Operator result = (Operator)parser.parse("weight > 2500", identifierMap); 255 ((Identifier) identifierMap.get("weight")).setValue(new Float (3000)); 256 getLog().debug("result -> "+result); 257 Boolean bool = (Boolean ) result.apply(); 258 assertTrue("is true", bool.booleanValue()); 259 } catch (Exception e) 260 { 261 getLog().debug("failed", e); 262 fail(""+e); 263 } 264 } 265 266 public void testLTDouble() 267 { 268 try 269 { 270 getLog().debug("parse(weight < 1.5)"); 271 Operator result = (Operator)parser.parse("weight < 1.5", identifierMap); 272 ((Identifier) identifierMap.get("weight")).setValue(new Double (1.2)); 273 getLog().debug("result -> "+result); 274 Boolean bool = (Boolean ) result.apply(); 275 assertTrue("is true", bool.booleanValue()); 276 } catch (Exception e) 277 { 278 getLog().debug("failed", e); 279 fail(""+e); 280 } 281 } 282 283 public void testAndCombination() 284 { 285 try 286 { 287 getLog().debug("parse(JMSType = 'car' AND color = 'blue' AND weight > 2500)"); 288 Operator result = (Operator)parser.parse("JMSType = 'car' AND color = 'blue' AND weight > 2500", identifierMap); 289 ((Identifier) identifierMap.get("JMSType")).setValue("car"); 290 ((Identifier) identifierMap.get("color")).setValue("blue"); 291 ((Identifier) identifierMap.get("weight")).setValue("3000"); 292 293 getLog().debug("result -> "+result); 294 Boolean bool = (Boolean ) result.apply(); 295 assertTrue("is false", !bool.booleanValue()); 296 } catch (Exception e) 297 { 298 getLog().debug("failed", e); 299 fail(""+e); 300 } 301 } 302 303 public void testINANDCombination() 304 { 305 try 306 { 307 getLog().debug("parse(Cateogry IN ('category1') AND Rating >= 2"); 308 Operator result = (Operator)parser.parse("Cateogry IN ('category1') AND Rating >= 2", identifierMap); 309 ((Identifier) identifierMap.get("Cateogry")).setValue("category1"); 310 ((Identifier) identifierMap.get("Rating")).setValue(new Integer (3)); 311 getLog().debug("result -> "+result); 312 Boolean bool = (Boolean ) result.apply(); 313 assertTrue("is true", bool.booleanValue()); 314 } catch (Exception e) 315 { 316 getLog().debug("failed", e); 317 fail(""+e); 318 } 319 } 320 321 324 public void testServerFound() 325 { 326 } 327 328 public static void main(java.lang.String [] args) 329 { 330 junit.textui.TestRunner.run(SelectorParserUnitTestCase.class); 331 } 332 } 333 | Popular Tags |