001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.repl.newjvm;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040: import edu.rice.cs.drjava.config.FileOption;
041:
042: import edu.rice.cs.util.Log;
043: import edu.rice.cs.util.UnexpectedException;
044:
045: import junit.extensions.TestSetup;
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: import java.rmi.RemoteException;
050:
051: /** Tests the functionality of the new JVM manager.
052: * @version $Id: NewJVMTest.java 4255 2007-08-28 19:17:37Z mgricken $
053: */
054: public final class NewJVMTest extends DrJavaTestCase {
055: private static final Log _log = new Log("MasterSlave.txt", false);
056:
057: private static volatile TestJVMExtension _jvm;
058:
059: /* Lock used to prevent interpreter transactions from interfering with one another. */
060: private final static Object _testLock = new Object();
061:
062: public NewJVMTest(String name) {
063: super (name);
064: }
065:
066: protected void setUp() throws Exception {
067: super .setUp();
068: _jvm.resetFlags();
069: }
070:
071: public static Test suite() {
072: TestSuite suite = new TestSuite(NewJVMTest.class);
073: TestSetup setup = new TestSetup(suite) {
074: protected void setUp() throws Exception {
075: _jvm = new TestJVMExtension();
076: }
077:
078: protected void tearDown() throws Exception {
079: _jvm.killInterpreter(null);
080: }
081: };
082:
083: return setup;
084: }
085:
086: public void testPrintln() throws Throwable {
087: _log.log("NewJVMTest.testPrintln executing");
088: synchronized (_testLock) {
089: _jvm.interpret("System.err.print(\"err\");");
090: _testLock.wait(); // wait for println
091: // _testLock.wait(); // wait for void return
092: assertEquals("system err buffer", "err", _jvm.errBuf);
093: assertEquals("void return flag", true, _jvm.voidReturnFlag);
094: _jvm.resetFlags();
095: }
096:
097: synchronized (_testLock) {
098: _jvm.interpret("System.err.print(\"err2\");");
099: _testLock.wait(); // wait for println
100: // _testLock.wait(); // wait for void return
101: assertEquals("system err buffer", "err2", _jvm.errBuf);
102: assertEquals("void return flag", true, _jvm.voidReturnFlag);
103: _jvm.resetFlags();
104: }
105:
106: synchronized (_testLock) {
107: _jvm.interpret("System.out.print(\"out\");");
108: _testLock.wait(); // wait for println
109: // _testLock.wait(); // wait for void return
110: assertEquals("system out buffer", "out", _jvm.outBuf);
111: assertEquals("void return flag", true, _jvm.voidReturnFlag);
112: }
113: }
114:
115: public void testReturnConstant() throws Throwable {
116: _log.log("NewJVMTest.testReturnConstant executing");
117: synchronized (_testLock) {
118: _jvm.interpret("5");
119: _testLock.wait();
120: assertEquals("result", "5", _jvm.returnBuf);
121: }
122: }
123:
124: public void testWorksAfterRestartConstant() throws Throwable {
125: _log.log("NewJVMTest.testWorksAfterRestartConstant executing");
126:
127: // Check that a constant is returned
128: synchronized (_testLock) {
129: _jvm.interpret("5");
130: _testLock.wait();
131: assertEquals("result", "5", _jvm.returnBuf);
132: }
133:
134: // Now restart interpreter
135: synchronized (_testLock) {
136: _jvm.killInterpreter(FileOption.NULL_FILE); // "" is not null: start back up
137: _testLock.wait();
138: }
139:
140: // Now evaluate another constant
141: synchronized (_testLock) {
142: _jvm.interpret("4");
143: _testLock.wait();
144: assertEquals("result", "4", _jvm.returnBuf);
145: }
146: }
147:
148: public void testThrowRuntimeException() throws Throwable {
149: _log.log("NewJVMTest.testThrowRuntimeException executing");
150: synchronized (_testLock) {
151: _jvm.interpret("throw new RuntimeException();");
152: _testLock.wait();
153: assertEquals("exception class",
154: "java.lang.RuntimeException",
155: _jvm.exceptionClassBuf);
156: }
157: }
158:
159: public void testToStringThrowsRuntimeException() throws Throwable {
160: _log
161: .log("NewJVMTest.testToStringThrowsRuntimeException executing");
162: synchronized (_testLock) {
163: _jvm
164: .interpret("class A { public String toString() { throw new RuntimeException(); } };"
165: + "new A()");
166: _testLock.wait();
167: assertTrue("exception should have been thrown by toString",
168: _jvm.exceptionClassBuf != null);
169: }
170: }
171:
172: public void testThrowNPE() throws Throwable {
173: _log.log("NewJVMTest.testThrowNPE executing");
174: synchronized (_testLock) {
175: _jvm.interpret("throw new NullPointerException();");
176:
177: while (_jvm.exceptionClassBuf == null) {
178: _testLock.wait();
179: }
180:
181: assertEquals("exception class",
182: "java.lang.NullPointerException",
183: _jvm.exceptionClassBuf);
184: }
185: }
186:
187: public void testStackTraceEmptyTrace() throws Throwable {
188: _log.log("NewJVMTest.testStackTraceEmptyTrace executing");
189: synchronized (_testLock) {
190: _jvm.interpret("null.toString()");
191:
192: while (_jvm.exceptionClassBuf == null) {
193: _testLock.wait();
194: }
195:
196: assertEquals("exception class",
197: "java.lang.NullPointerException",
198: _jvm.exceptionClassBuf);
199: assertEquals("stack trace", InterpreterJVM.EMPTY_TRACE_TEXT
200: .trim(), _jvm.exceptionTraceBuf.trim());
201: }
202: }
203:
204: /**
205: * Ensure that switching to a non-existant interpreter throws an Exception.
206: */
207: public void testSwitchToNonExistantInterpreter() {
208: try {
209: _jvm.setActiveInterpreter("thisisabadname");
210: System.out.println("outbuf: " + _jvm.outBuf);
211: fail("Should have thrown an exception!");
212: } catch (IllegalArgumentException e) {
213: // good, that's what should happen
214: }
215: }
216:
217: /**
218: * Ensure that MainJVM can correctly switch the active interpreter used by
219: * the interpreter JVM.
220: */
221: public void testSwitchActiveInterpreter()
222: throws InterruptedException {
223: synchronized (_testLock) {
224: _jvm.interpret("x = 6;");
225: _testLock.wait();
226: }
227: _jvm.addJavaInterpreter("monkey");
228:
229: // x should be defined in active interpreter
230: synchronized (_testLock) {
231: _jvm.interpret("x");
232: _testLock.wait();
233: assertEquals("result", "6", _jvm.returnBuf);
234: }
235:
236: // switch interpreter
237: _jvm.setActiveInterpreter("monkey");
238: synchronized (_testLock) {
239: _jvm.interpret("x");
240: _testLock.wait();
241: assertTrue("exception was thrown", !_jvm.exceptionClassBuf
242: .equals(""));
243: }
244:
245: // define x to 3 and switch back
246: synchronized (_testLock) {
247: _jvm.interpret("x = 3;");
248: _testLock.wait();
249: }
250: _jvm.setToDefaultInterpreter();
251:
252: // x should have its old value
253: synchronized (_testLock) {
254: _jvm.interpret("x");
255: _testLock.wait();
256: assertEquals("result", "6", _jvm.returnBuf);
257: }
258:
259: // test syntax error handling
260: // (temporarily disabled until bug 750605 fixed)
261: // synchronized(_testLock) {
262: // _jvm.interpret("x+");
263: // _testLock.wait();
264: // assertTrue("syntax error was reported",
265: // ! _jvm.syntaxErrorMsgBuf.equals("") );
266: // }
267:
268: }
269:
270: private static class TestJVMExtension extends MainJVM {
271: public volatile String outBuf;
272: public volatile String errBuf;
273: public volatile String returnBuf;
274: public volatile String exceptionClassBuf;
275: public volatile String exceptionMsgBuf;
276: public volatile String exceptionTraceBuf;
277: public volatile String syntaxErrorMsgBuf;
278: public volatile int syntaxErrorStartRow;
279: public volatile int syntaxErrorStartCol;
280: public volatile int syntaxErrorEndRow;
281: public volatile int syntaxErrorEndCol;
282: public volatile boolean voidReturnFlag;
283:
284: private volatile InterpretResultVisitor<Object> _testHandler;
285:
286: public TestJVMExtension() throws RemoteException {
287: super (null);
288: _testHandler = new TestResultHandler();
289: startInterpreterJVM();
290: ensureInterpreterConnected();
291: }
292:
293: protected InterpretResultVisitor<Object> getResultHandler() {
294: return _testHandler;
295: }
296:
297: public void resetFlags() {
298: outBuf = null;
299: errBuf = null;
300: returnBuf = null;
301: exceptionClassBuf = null;
302: exceptionMsgBuf = null;
303: exceptionTraceBuf = null;
304: voidReturnFlag = false;
305: syntaxErrorMsgBuf = null;
306: syntaxErrorStartRow = 0;
307: syntaxErrorStartCol = 0;
308: syntaxErrorEndRow = 0;
309: syntaxErrorEndCol = 0;
310: }
311:
312: protected void handleSlaveQuit(int status) {
313: synchronized (_testLock) {
314: _testLock.notify();
315: super .handleSlaveQuit(status);
316: }
317: }
318:
319: public void systemErrPrint(String s) throws RemoteException {
320: synchronized (_testLock) {
321: //System.out.println("notify err: " + s);
322: errBuf = s;
323: // _testLock.notify();
324: }
325: }
326:
327: public void systemOutPrint(String s) throws RemoteException {
328: synchronized (_testLock) {
329: //System.out.println("notify out: " + s);
330: outBuf = s;
331: // _testLock.notify();
332: }
333: }
334:
335: private class TestResultHandler implements
336: InterpretResultVisitor<Object> {
337: public Object forVoidResult(VoidResult that) {
338: synchronized (_testLock) {
339: voidReturnFlag = true;
340: _log
341: .log("NewJVMTest: void returned by interpretResult callback");
342: _testLock.notify();
343: return null;
344: }
345: }
346:
347: public Object forValueResult(ValueResult that) {
348: synchronized (_testLock) {
349: returnBuf = that.getValueStr();
350: _log.log("NewJVMTest: " + returnBuf
351: + " returned by interpretResult callback");
352: _testLock.notify();
353: return null;
354: }
355: }
356:
357: public Object forExceptionResult(ExceptionResult that) {
358: synchronized (_testLock) {
359: exceptionClassBuf = that.getExceptionClass();
360: exceptionTraceBuf = that.getStackTrace();
361: exceptionMsgBuf = that.getExceptionMessage();
362:
363: //System.out.println("notify threw");
364: _testLock.notify();
365: return null;
366: }
367: }
368:
369: public Object forSyntaxErrorResult(SyntaxErrorResult that) {
370: synchronized (_testLock) {
371: syntaxErrorMsgBuf = that.getErrorMessage();
372: syntaxErrorStartRow = that.getStartRow();
373: syntaxErrorStartCol = that.getStartCol();
374: syntaxErrorEndRow = that.getEndRow();
375: syntaxErrorEndCol = that.getEndCol();
376: //System.out.println("notify threw");
377: _testLock.notify();
378: return null;
379: }
380: }
381:
382: public Object forInterpreterBusy(InterpreterBusy that) {
383: throw new UnexpectedException(
384: "MainJVM.interpret called when interpreter was busy!");
385: }
386: }
387: }
388: }
|