1 33 34 package edu.rice.cs.drjava.model.repl.newjvm; 35 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 import edu.rice.cs.drjava.config.FileOption; 38 39 import edu.rice.cs.util.Log; 40 import edu.rice.cs.util.UnexpectedException; 41 42 import junit.extensions.TestSetup; 43 import junit.framework.Test; 44 import junit.framework.TestSuite; 45 46 import java.rmi.RemoteException ; 47 48 51 public final class NewJVMTest extends DrJavaTestCase { 52 private static final Log _log = new Log("MasterSlave.txt", false); 53 54 private static volatile TestJVMExtension _jvm; 55 56 57 private final static Object _testLock = new Object (); 58 59 public NewJVMTest(String name) { super(name); } 60 61 protected void setUp() throws Exception { 62 super.setUp(); 63 _jvm.resetFlags(); 64 } 65 66 public static Test suite() { 67 TestSuite suite = new TestSuite(NewJVMTest.class); 68 TestSetup setup = new TestSetup(suite) { 69 protected void setUp() throws Exception { 70 _jvm = new TestJVMExtension(); 71 } 72 73 protected void tearDown() throws Exception { _jvm.killInterpreter(null); } 74 }; 75 76 return setup; 77 } 78 79 80 public void testPrintln() throws Throwable { 81 _log.log("NewJVMTest.testPrintln executing"); 82 synchronized(_testLock) { 83 _jvm.interpret("System.err.print(\"err\");"); 84 _testLock.wait(); assertEquals("system err buffer", "err", _jvm.errBuf); 87 assertEquals("void return flag", true, _jvm.voidReturnFlag); 88 _jvm.resetFlags(); 89 } 90 91 synchronized(_testLock) { 92 _jvm.interpret("System.err.print(\"err2\");"); 93 _testLock.wait(); assertEquals("system err buffer", "err2", _jvm.errBuf); 96 assertEquals("void return flag", true, _jvm.voidReturnFlag); 97 _jvm.resetFlags(); 98 } 99 100 synchronized(_testLock) { 101 _jvm.interpret("System.out.print(\"out\");"); 102 _testLock.wait(); assertEquals("system out buffer", "out", _jvm.outBuf); 105 assertEquals("void return flag", true, _jvm.voidReturnFlag); 106 } 107 } 108 109 public void testReturnConstant() throws Throwable { 110 _log.log("NewJVMTest.testReturnConstant executing"); 111 synchronized(_testLock) { 112 _jvm.interpret("5"); 113 _testLock.wait(); 114 assertEquals("result", "5", _jvm.returnBuf); 115 } 116 } 117 118 public void testWorksAfterRestartConstant() throws Throwable { 119 _log.log("NewJVMTest.testWorksAfterRestartConstant executing"); 120 121 synchronized(_testLock) { 123 _jvm.interpret("5"); 124 _testLock.wait(); 125 assertEquals("result", "5", _jvm.returnBuf); 126 } 127 128 synchronized(_testLock) { 130 _jvm.killInterpreter(FileOption.NULL_FILE); _testLock.wait(); 132 } 133 134 synchronized(_testLock) { 136 _jvm.interpret("4"); 137 _testLock.wait(); 138 assertEquals("result", "4", _jvm.returnBuf); 139 } 140 } 141 142 143 public void testThrowRuntimeException() throws Throwable { 144 _log.log("NewJVMTest.testThrowRuntimeException executing"); 145 synchronized(_testLock) { 146 _jvm.interpret("throw new RuntimeException();"); 147 _testLock.wait(); 148 assertEquals("exception class", "java.lang.RuntimeException", _jvm.exceptionClassBuf); 149 } 150 } 151 152 public void testToStringThrowsRuntimeException() throws Throwable { 153 _log.log("NewJVMTest.testToStringThrowsRuntimeException executing"); 154 synchronized(_testLock) { 155 _jvm.interpret( 156 "class A { public String toString() { throw new RuntimeException(); } };" + 157 "new A()"); 158 _testLock.wait(); 159 assertTrue("exception should have been thrown by toString", 160 _jvm.exceptionClassBuf != null); 161 } 162 } 163 164 public void testThrowNPE() throws Throwable { 165 _log.log("NewJVMTest.testThrowNPE executing"); 166 synchronized(_testLock) { 167 _jvm.interpret("throw new NullPointerException();"); 168 169 while (_jvm.exceptionClassBuf == null) { 170 _testLock.wait(); 171 } 172 173 assertEquals("exception class", 174 "java.lang.NullPointerException", 175 _jvm.exceptionClassBuf); 176 } 177 } 178 179 public void testStackTraceEmptyTrace() throws Throwable { 180 _log.log("NewJVMTest.testStackTraceEmptyTrace executing"); 181 synchronized(_testLock) { 182 _jvm.interpret("null.toString()"); 183 184 while (_jvm.exceptionClassBuf == null) { 185 _testLock.wait(); 186 } 187 188 assertEquals("exception class", 189 "java.lang.NullPointerException", 190 _jvm.exceptionClassBuf); 191 assertEquals("stack trace", 192 InterpreterJVM.EMPTY_TRACE_TEXT.trim(), 193 _jvm.exceptionTraceBuf.trim()); 194 } 195 } 196 197 198 201 public void testSwitchToNonExistantInterpreter() { 202 try { 203 _jvm.setActiveInterpreter("thisisabadname"); 204 System.out.println("outbuf: " + _jvm.outBuf); 205 fail("Should have thrown an exception!"); 206 } 207 catch (IllegalArgumentException e) { 208 } 210 } 211 212 216 public void testSwitchActiveInterpreter() throws InterruptedException { 217 synchronized(_testLock) { 218 _jvm.interpret("x = 6;"); 219 _testLock.wait(); 220 } 221 _jvm.addJavaInterpreter("monkey"); 222 223 synchronized(_testLock) { 225 _jvm.interpret("x"); 226 _testLock.wait(); 227 assertEquals("result", "6", _jvm.returnBuf); 228 } 229 230 _jvm.setActiveInterpreter("monkey"); 232 synchronized(_testLock) { 233 _jvm.interpret("x"); 234 _testLock.wait(); 235 assertTrue("exception was thrown", 236 !_jvm.exceptionClassBuf.equals("")); 237 } 238 239 synchronized(_testLock) { 241 _jvm.interpret("x = 3;"); 242 _testLock.wait(); 243 } 244 _jvm.setToDefaultInterpreter(); 245 246 synchronized(_testLock) { 248 _jvm.interpret("x"); 249 _testLock.wait(); 250 assertEquals("result", "6", _jvm.returnBuf); 251 } 252 253 262 } 263 264 private static class TestJVMExtension extends MainJVM { 265 public volatile String outBuf; 266 public volatile String errBuf; 267 public volatile String returnBuf; 268 public volatile String exceptionClassBuf; 269 public volatile String exceptionMsgBuf; 270 public volatile String exceptionTraceBuf; 271 public volatile String syntaxErrorMsgBuf; 272 public volatile int syntaxErrorStartRow; 273 public volatile int syntaxErrorStartCol; 274 public volatile int syntaxErrorEndRow; 275 public volatile int syntaxErrorEndCol; 276 public volatile boolean voidReturnFlag; 277 278 private volatile InterpretResultVisitor<Object > _testHandler; 279 280 public TestJVMExtension() { 281 super(null); 282 _testHandler = new TestResultHandler(); 283 startInterpreterJVM(); 284 ensureInterpreterConnected(); 285 } 286 287 protected InterpretResultVisitor<Object > getResultHandler() { 288 return _testHandler; 289 } 290 291 public void resetFlags() { 292 outBuf = null; 293 errBuf = null; 294 returnBuf = null; 295 exceptionClassBuf = null; 296 exceptionMsgBuf = null; 297 exceptionTraceBuf = null; 298 voidReturnFlag = false; 299 syntaxErrorMsgBuf = null; 300 syntaxErrorStartRow = 0; 301 syntaxErrorStartCol = 0; 302 syntaxErrorEndRow = 0; 303 syntaxErrorEndCol = 0; 304 } 305 306 protected void handleSlaveQuit(int status) { 307 synchronized(_testLock) { 308 _testLock.notify(); 309 super.handleSlaveQuit(status); 310 } 311 } 312 313 public void systemErrPrint(String s) throws RemoteException { 314 synchronized(_testLock) { 315 errBuf = s; 317 } 319 } 320 321 public void systemOutPrint(String s) throws RemoteException { 322 synchronized(_testLock) { 323 outBuf = s; 325 } 327 } 328 329 private class TestResultHandler implements InterpretResultVisitor<Object > { 330 public Object forVoidResult(VoidResult that) { 331 synchronized(_testLock) { 332 voidReturnFlag = true; 333 _log.log("NewJVMTest: void returned by interpretResult callback"); 334 _testLock.notify(); 335 return null; 336 } 337 } 338 public Object forValueResult(ValueResult that) { 339 synchronized(_testLock) { 340 returnBuf = that.getValueStr(); 341 _log.log("NewJVMTest: " + returnBuf + " returned by interpretResult callback"); 342 _testLock.notify(); 343 return null; 344 } 345 } 346 public Object forExceptionResult(ExceptionResult that) { 347 synchronized(_testLock) { 348 exceptionClassBuf = that.getExceptionClass(); 349 exceptionTraceBuf = that.getStackTrace(); 350 exceptionMsgBuf = that.getExceptionMessage(); 351 352 _testLock.notify(); 354 return null; 355 } 356 } 357 358 public Object forSyntaxErrorResult(SyntaxErrorResult that) { 359 synchronized(_testLock) { 360 syntaxErrorMsgBuf = that.getErrorMessage(); 361 syntaxErrorStartRow = that.getStartRow(); 362 syntaxErrorStartCol = that.getStartCol(); 363 syntaxErrorEndRow = that.getEndRow(); 364 syntaxErrorEndCol = that.getEndCol(); 365 _testLock.notify(); 367 return null; 368 } 369 } 370 371 public Object forInterpreterBusy(InterpreterBusy that) { 372 throw new UnexpectedException("MainJVM.interpret called when interpreter was busy!"); 373 } 374 } 375 } 376 } 377 | Popular Tags |