1 33 34 package edu.rice.cs.drjava.model.repl; 35 36 import edu.rice.cs.drjava.DrJava; 37 import edu.rice.cs.drjava.DrJavaTestCase; 38 import edu.rice.cs.drjava.config.OptionConstants; 39 import edu.rice.cs.drjava.config.OptionListener; 40 import edu.rice.cs.drjava.config.OptionEvent; 41 import edu.rice.cs.drjava.model.repl.newjvm.ClassPathManager; 42 import edu.rice.cs.util.swing.Utilities; 43 44 import junit.framework.*; 45 46 49 public final class JavaInterpreterTest extends DrJavaTestCase { 50 private JavaInterpreter _interpreter; 51 static public boolean testValue; 52 53 54 protected void setUp() throws Exception { 55 super.setUp(); 56 _interpreter = new DynamicJavaAdapter(new ClassPathManager()); 57 testValue = false; 58 } 59 60 64 private void tester(Pair[] cases) throws ExceptionReturnedException { 65 for (int i = 0; i < cases.length; i++) { 66 Object out = _interpreter.interpret(cases[i].first()); 67 assertEquals(cases[i].first() + " interpretation wrong!", cases[i].second(), out); 68 } 69 } 70 71 74 public void testConstants() throws ExceptionReturnedException { 75 Pair[] cases = new Pair[] { 76 Pair.make("5", new Integer (5)), 77 Pair.make("1356", new Integer (1356)), 78 Pair.make("true", Boolean.TRUE), 79 Pair.make("false", Boolean.FALSE), 80 Pair.make("\'c\'", "'" + new Character ('c') + "'"), 81 Pair.make("1.345", new Double (1.345)), 82 Pair.make("\"buwahahahaha!\"", "\"buwahahahaha!\""), 83 Pair.make("\"yah\\\"eh\\\"\"", "\"yah\"eh\"\""), 84 Pair.make("'\\''", "'" + new Character ('\'') + "'") 85 }; 86 tester(cases); 87 } 88 89 90 public void testBooleanOps() throws ExceptionReturnedException { 91 Pair[] cases = new Pair[] { 92 Pair.make("true && false", Boolean.FALSE), Pair.make("true && true", 94 Boolean.TRUE), 95 Pair.make("true || true", Boolean.TRUE), Pair.make("false || true", Boolean.TRUE), 97 Pair.make("false || false", Boolean.FALSE), 98 Pair.make("!true", Boolean.FALSE), Pair.make("!false", Boolean.TRUE), 100 Pair.make("true == true", Boolean.TRUE), Pair.make("false == true", Boolean.FALSE), 102 Pair.make("false == false", Boolean.TRUE), 103 Pair.make("false ^ false", Boolean.valueOf(false ^ false)), Pair.make("false ^ true ", 105 Boolean.valueOf(false ^ true)) 106 }; 107 tester(cases); 108 } 109 110 111 public void testShortCircuit() throws ExceptionReturnedException { 112 Pair[] cases = new Pair[] { 113 Pair.make("false && (3 == 1/0)", Boolean.FALSE), 114 Pair.make("true || (1/0 != 43)", Boolean.TRUE) 115 }; 116 tester(cases); 117 } 118 119 122 public void testIntegerOps() throws ExceptionReturnedException { 123 Pair[] cases = new Pair[] { 124 Pair.make("400 << 5", new Integer (400 << 5)), 148 Pair.make("400 >> 5", new Integer (400 >> 5)), 150 Pair.make("400 >>> 5", new Integer (400 >>> 5)), 152 Pair.make("5 != 6", Boolean.valueOf(5 != 6)), Pair.make("5 != 5", Boolean.valueOf(5 != 5)) 165 }; 166 tester(cases); 167 } 168 169 172 public void testDoubleOps() throws ExceptionReturnedException { 173 Pair[] cases = new Pair[] { 174 Pair.make("5.6 < 6.7", Boolean.valueOf(5.6 < 6.7)), 176 Pair.make("5.6 <= 5.6", Boolean.valueOf(5.6 <= 5.6)), 178 Pair.make("5.6 > 4.5", Boolean.valueOf(5.6 > 4.5)), 180 Pair.make("5.6 >= 56.4", Boolean.valueOf(5.6 >= 56.4)), 182 Pair.make("5.4 == 5.4", Boolean.valueOf(5 == 5)), 184 Pair.make("5.5 != 5.5", Boolean.valueOf(5 != 5)), 186 Pair.make("+5.6", new Double (+5.6)), 188 Pair.make("-5.6", new Double (-5.6)), 190 Pair.make("5.6 * 4.5", new Double (5.6*4.5)), 192 Pair.make("5.6 / 3.4", new Double (5.6/3.4)), 194 Pair.make("5.6 % 3.4", new Double (5.6%3.4)), 196 Pair.make("5.6 + 6.7", new Double (5.6 + 6.7)), 198 Pair.make("4.5 - 3.4", new Double (4.5 - 3.4)), 200 }; 201 tester(cases); 202 } 203 204 207 public void testStringOps() throws ExceptionReturnedException { 208 Pair[] cases = new Pair[] { 209 Pair.make("\"yeah\" + \"and\"", "\"yeah" + "and\""), 211 Pair.make("\"yeah\".equals(\"yeah\")", Boolean.valueOf("yeah".equals("yeah"))), 213 214 }; 215 tester(cases); 216 } 217 218 221 public void testCharacterOps() throws ExceptionReturnedException{ 222 Pair[] cases = new Pair[] { 223 Pair.make("'c' == 'c'", Boolean.valueOf('c' == 'c')) 225 }; 226 tester(cases); 227 } 228 229 233 public void testSemicolon() throws ExceptionReturnedException { 234 Pair[] cases = new Pair[] { 235 Pair.make("'c' == 'c'", Boolean.valueOf('c' == 'c')), 236 Pair.make("'c' == 'c';", JavaInterpreter.NO_RESULT), 237 Pair.make("String s = \"hello\"", JavaInterpreter.NO_RESULT), 238 Pair.make("String x = \"hello\";", JavaInterpreter.NO_RESULT), 239 Pair.make("char c = 'c'", JavaInterpreter.NO_RESULT), 240 Pair.make("Character d = new Character('d')", JavaInterpreter.NO_RESULT), 241 Pair.make("s", "\"hello\""), Pair.make("s;", JavaInterpreter.NO_RESULT), 242 Pair.make("x", "\"hello\""), Pair.make("x;", JavaInterpreter.NO_RESULT), 243 Pair.make("c", "'c'"), Pair.make("d", "'d'") 244 }; 245 tester(cases); 246 } 247 248 251 public void testNullInstanceOf() throws ExceptionReturnedException { 252 Pair[] cases = new Pair[] { 253 Pair.make("null instanceof Object", Boolean.valueOf(null instanceof Object )), 254 Pair.make("null instanceof String", Boolean.valueOf(null instanceof String )) 255 }; 256 tester(cases); 257 } 258 259 263 public void testVariableDefinition() throws ExceptionReturnedException { 264 _interpreter.interpret("int a = 5;"); 265 _interpreter.interpret("int b = a;"); 266 267 _interpreter.interpret("int c = a++;"); 268 } 269 270 273 public void testVariableDefaultValues() throws ExceptionReturnedException { 274 _interpreter.interpret("byte b"); 275 _interpreter.interpret("short s"); 276 _interpreter.interpret("int i"); 277 _interpreter.interpret("long l"); 278 _interpreter.interpret("float f"); 279 _interpreter.interpret("double d"); 280 _interpreter.interpret("char c"); 281 _interpreter.interpret("boolean bool"); 282 _interpreter.interpret("String str"); 283 Pair[] cases = new Pair[] { 284 Pair.make("b", new Byte ((byte)0)), 285 Pair.make("s", new Short ((short)0)), 286 Pair.make("i", new Integer (0)), 287 Pair.make("l", new Long (0L)), 288 Pair.make("f", new Float (0.0f)), 289 Pair.make("d", new Double (0.0d)), 290 Pair.make("c", "'" + new Character ('\u0000') + "'"), Pair.make("bool", Boolean.valueOf(false)), 292 Pair.make("str", null) 293 }; 294 tester(cases); 295 } 296 297 308 public void testVariableRedefinition() throws ExceptionReturnedException{ 309 try { 311 _interpreter.interpret("String s = abc;"); 312 fail("variable definition should have failed"); 313 } 314 catch (ExceptionReturnedException e) { 315 } 317 try { 319 _interpreter.interpret("Vector v = new Vector();"); 320 fail("variable definition should have failed"); 321 } 322 catch (ExceptionReturnedException e) { 323 } 325 try { 326 _interpreter.interpret("File f;"); 327 fail("variable definition should have failed"); 328 } 329 catch (ExceptionReturnedException e) { 330 } 332 try { 333 _interpreter.interpret("import java.util.Vector;"); 335 _interpreter.interpret("Vector v = new Vector();"); 336 _interpreter.interpret("String s = \"abc\";"); 337 _interpreter.interpret("import java.io.File;"); 338 _interpreter.interpret("File f = new File(\"\");"); 339 } 340 catch (ExceptionReturnedException e) { 341 fail("These interpret statements shouldn't cause errors"); 342 } 343 345 try { 347 _interpreter.interpret("String z = new String(Integer.getInteger(\"somebadproperty\").toString());"); 348 fail("variable definition should have failed"); 349 } 350 catch (ExceptionReturnedException e) { 351 } 352 _interpreter.interpret("String z = \"z\";"); 356 357 } 358 359 360 public void testIncompatibleAssignment() throws ExceptionReturnedException { 361 try { 362 _interpreter.interpret("Integer i = new Object()"); 363 fail("incompatible assignment should have failed"); 364 } 365 catch (ExceptionReturnedException e) { 366 } 368 try { 369 _interpreter.interpret("Integer i2 = (Integer)new Object();"); 370 fail("incompatible assignment should have failed"); 371 } 372 catch (ExceptionReturnedException e) { 373 } 375 376 _interpreter.interpret("Object o = new Integer(3)"); 378 } 379 380 383 public void testTypeCheckerExtension() { 384 try { _interpreter.interpret("(false) ? 2/0 : 1 "); } 385 catch(ExceptionReturnedException e) { 386 if ( e.getContainedException() instanceof ArithmeticException ) { 387 fail("testTypeCheckerExtension failed to prevent short circuit DivideByZeroException"); 388 } 389 } 390 391 try { _interpreter.interpret("(false) ? 2%0 : 1 "); } 392 catch(ExceptionReturnedException e) { 393 if ( e.getContainedException() instanceof ArithmeticException ) { 394 fail("testTypeCheckerExtension failed to prevent short circuit DivideByZeroException"); 395 } 396 } 397 } 398 399 402 public void testEvaluationVisitorExtensionNO_RESULT() { 403 try { 404 Object out = _interpreter.interpret("true;"); 405 assertEquals("testEvaluationVisitorExtension", JavaInterpreter.NO_RESULT, out); 406 } 407 catch(ExceptionReturnedException e) { 408 fail("testEvaluationVisitorExtension Exception returned for none exceptional code!" + e); 409 } 410 } 411 412 413 public void testDefineVariableExternally() throws ExceptionReturnedException { 414 _interpreter.defineVariable("foo", "hello"); 415 assertEquals("manipulated externally defined variable", 416 "\"ello\"", _interpreter.interpret("foo.substring(1,5)")); 417 _interpreter.defineVariable("x", 3); 418 assertEquals("externally defined variable x", 419 new Integer (3), _interpreter.interpret("x")); 420 assertEquals("incremented externally defined variable x", 421 new Integer (4), _interpreter.interpret("++x")); 422 } 423 424 425 public void testQueryVariableExternally() { 426 _interpreter.defineVariable("x", 7); 427 assertEquals("external query for x", 429 new Integer (7), _interpreter.getVariable("x")); 430 431 try { 433 _interpreter.getVariable("undefined"); 434 fail("Should have thrown IllegalStateException"); 435 } 436 catch (IllegalStateException e) { 437 } 439 } 440 441 442 public void testDefineConstantExternally() { 443 _interpreter.defineConstant("y", 3); 444 try { 445 _interpreter.interpret("y = 4"); 446 fail("should not be able to assign to a constant"); 447 } 448 catch (ExceptionReturnedException e) { 449 } 451 } 452 453 454 public void testInitializeArrays() throws ExceptionReturnedException { 455 try { 456 _interpreter.interpret("int i[] = new int[]{1,2,3};"); 457 _interpreter.interpret("int j[][] = new int[][]{{1}, {2,3}};"); 458 _interpreter.interpret("int k[][][][] = new int[][][][]{{{{1},{2,3}}}};"); 459 } 460 catch(IllegalArgumentException iae) { 461 fail("Legal array initializations were not accepted."); 462 } 463 } 464 465 466 public void testArrayCloning() throws ExceptionReturnedException { 467 try { _interpreter.interpret("new int[]{0}.clone()"); } 468 catch(RuntimeException e) { fail("Array cloning failed."); } 469 } 470 471 474 public void testAllowPrivateAccess() throws ExceptionReturnedException { 475 DrJava.getConfig().addOptionListener(OptionConstants.ALLOW_PRIVATE_ACCESS, new OptionListener<Boolean >() { 477 public void optionChanged(OptionEvent<Boolean > oce) { 478 _interpreter.setPrivateAccessible(oce.value.booleanValue()); 479 } 480 }); 481 DrJava.getConfig().setSetting(OptionConstants.ALLOW_PRIVATE_ACCESS, Boolean.valueOf(false)); 482 Utilities.clearEventQueue(); 483 try { 485 _interpreter.interpret("class A { private int i = 0; }"); 486 _interpreter.interpret("new A().i"); 487 System.out.println("Private access erroneously succeeded"); 488 fail("Should not have access to the private field i inside class A."); 489 } 490 catch (ExceptionReturnedException ere) { 491 assertTrue(ere.getContainedException() instanceof IllegalAccessException ); 492 } 493 DrJava.getConfig().setSetting(OptionConstants.ALLOW_PRIVATE_ACCESS, Boolean.valueOf(true)); 494 Utilities.clearEventQueue(); 495 assertEquals("Should be able to access private field i whose value should be 0", 496 new Integer (0), 497 _interpreter.interpret("new A().i")); 498 } 499 500 504 public void testDeclareVoidMethod() { 505 try { _interpreter.interpret("void method() {}"); } 506 catch (ExceptionReturnedException ere) { fail("Should be able to declare void methods."); } 507 } 508 509 513 public void testUserDefinedVoidMethod() throws ExceptionReturnedException { 514 Object result = _interpreter.interpret("public void foo() {}; foo()"); 515 assertSame("Should have returned NO_RESULT.", Interpreter.NO_RESULT, result); 516 } 517 } 518 519 525 class Pair extends edu.rice.cs.plt.tuple.Pair<String , Object > { 526 531 public Pair(String f, Object s) { super(f, s); } 532 533 539 public static Pair make(String first, Object second) { return new Pair(first, second); } 540 541 } 542 | Popular Tags |