KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > newjvm > NewJVMTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

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 JavaDoc;
47
48 /** Tests the functionality of the new JVM manager.
49  * @version $Id: NewJVMTest.java 3877 2006-06-08 22:29:32Z rcartwright $
50  */

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   /* Lock used to prevent interpreter transactions from interfering with one another. */
57   private final static Object JavaDoc _testLock = new Object JavaDoc();
58   
59   public NewJVMTest(String JavaDoc name) { super(name); }
60
61   protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
70         _jvm = new TestJVMExtension();
71       }
72
73       protected void tearDown() throws Exception JavaDoc { _jvm.killInterpreter(null); }
74     };
75
76     return setup;
77   }
78
79
80   public void testPrintln() throws Throwable JavaDoc {
81     _log.log("NewJVMTest.testPrintln executing");
82     synchronized(_testLock) {
83       _jvm.interpret("System.err.print(\"err\");");
84       _testLock.wait(); // wait for println
85
// _testLock.wait(); // wait for void return
86
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(); // wait for println
94
// _testLock.wait(); // wait for void return
95
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(); // wait for println
103
// _testLock.wait(); // wait for void return
104
assertEquals("system out buffer", "out", _jvm.outBuf);
105       assertEquals("void return flag", true, _jvm.voidReturnFlag);
106     }
107   }
108
109   public void testReturnConstant() throws Throwable JavaDoc {
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 JavaDoc {
119     _log.log("NewJVMTest.testWorksAfterRestartConstant executing");
120
121     // Check that a constant is returned
122
synchronized(_testLock) {
123       _jvm.interpret("5");
124       _testLock.wait();
125       assertEquals("result", "5", _jvm.returnBuf);
126     }
127
128     // Now restart interpreter
129
synchronized(_testLock) {
130       _jvm.killInterpreter(FileOption.NULL_FILE); // "" is not null: start back up
131
_testLock.wait();
132     }
133
134     // Now evaluate another constant
135
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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   /**
199    * Ensure that switching to a non-existant interpreter throws an Exception.
200    */

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 JavaDoc e) {
208       // good, that's what should happen
209
}
210   }
211
212   /**
213    * Ensure that MainJVM can correctly switch the active interpreter used by
214    * the interpreter JVM.
215    */

216   public void testSwitchActiveInterpreter() throws InterruptedException JavaDoc {
217     synchronized(_testLock) {
218       _jvm.interpret("x = 6;");
219       _testLock.wait();
220     }
221     _jvm.addJavaInterpreter("monkey");
222
223     // x should be defined in active interpreter
224
synchronized(_testLock) {
225       _jvm.interpret("x");
226       _testLock.wait();
227       assertEquals("result", "6", _jvm.returnBuf);
228     }
229
230     // switch interpreter
231
_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     // define x to 3 and switch back
240
synchronized(_testLock) {
241       _jvm.interpret("x = 3;");
242       _testLock.wait();
243     }
244     _jvm.setToDefaultInterpreter();
245
246     // x should have its old value
247
synchronized(_testLock) {
248       _jvm.interpret("x");
249       _testLock.wait();
250       assertEquals("result", "6", _jvm.returnBuf);
251     }
252
253     // test syntax error handling
254
// (temporarily disabled until bug 750605 fixed)
255
// synchronized(_testLock) {
256
// _jvm.interpret("x+");
257
// _testLock.wait();
258
// assertTrue("syntax error was reported",
259
// ! _jvm.syntaxErrorMsgBuf.equals("") );
260
// }
261

262   }
263
264   private static class TestJVMExtension extends MainJVM {
265     public volatile String JavaDoc outBuf;
266     public volatile String JavaDoc errBuf;
267     public volatile String JavaDoc returnBuf;
268     public volatile String JavaDoc exceptionClassBuf;
269     public volatile String JavaDoc exceptionMsgBuf;
270     public volatile String JavaDoc exceptionTraceBuf;
271     public volatile String JavaDoc 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 JavaDoc> _testHandler;
279
280     public TestJVMExtension() {
281       super(null);
282       _testHandler = new TestResultHandler();
283       startInterpreterJVM();
284       ensureInterpreterConnected();
285     }
286
287     protected InterpretResultVisitor<Object JavaDoc> 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 JavaDoc s) throws RemoteException JavaDoc {
314       synchronized(_testLock) {
315         //System.out.println("notify err: " + s);
316
errBuf = s;
317 // _testLock.notify();
318
}
319     }
320
321     public void systemOutPrint(String JavaDoc s) throws RemoteException JavaDoc {
322       synchronized(_testLock) {
323         //System.out.println("notify out: " + s);
324
outBuf = s;
325 // _testLock.notify();
326
}
327     }
328
329     private class TestResultHandler implements InterpretResultVisitor<Object JavaDoc> {
330       public Object JavaDoc 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 JavaDoc 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 JavaDoc forExceptionResult(ExceptionResult that) {
347         synchronized(_testLock) {
348           exceptionClassBuf = that.getExceptionClass();
349           exceptionTraceBuf = that.getStackTrace();
350           exceptionMsgBuf = that.getExceptionMessage();
351
352           //System.out.println("notify threw");
353
_testLock.notify();
354           return null;
355         }
356       }
357
358       public Object JavaDoc 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           //System.out.println("notify threw");
366
_testLock.notify();
367           return null;
368         }
369       }
370       
371       public Object JavaDoc forInterpreterBusy(InterpreterBusy that) {
372         throw new UnexpectedException("MainJVM.interpret called when interpreter was busy!");
373       }
374     }
375   }
376 }
377
Popular Tags