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.model.repl.DynamicJavaAdapter;
041: import edu.rice.cs.drjava.model.repl.ExceptionReturnedException;
042: import edu.rice.cs.drjava.model.repl.JavaInterpreter;
043:
044: import java.util.Hashtable;
045:
046: /**
047: * simple test suite over InterpreterJVM
048: */
049: public final class InterpreterJVMTest extends DrJavaTestCase {
050: private InterpreterJVM jvm = InterpreterJVM.ONLY;
051: private Hashtable<String, InterpreterData> _debugInterpreters;
052: private JavaInterpreter _interpreter1;
053: private JavaInterpreter _interpreter2;
054: private JavaInterpreter _interpreter3;
055:
056: private void _addInterpreter(String name,
057: JavaInterpreter interpreter) {
058: InterpreterJVM.ONLY.addInterpreter(name, interpreter);
059: }
060:
061: public void setUp() throws Exception {
062: super .setUp();
063: _debugInterpreters = InterpreterJVM.ONLY.getInterpreters();
064: _interpreter1 = new DynamicJavaAdapter(new ClassPathManager());
065: _interpreter2 = new DynamicJavaAdapter(new ClassPathManager());
066: _interpreter3 = new DynamicJavaAdapter(new ClassPathManager());
067: }
068:
069: /**
070: * ensures that our InterpreterJVM adds named debug interpreters correctly
071: */
072: public void testAddNamedDebugInterpreter() {
073: assertTrue(_debugInterpreters.isEmpty());
074: _addInterpreter("interpreter1", _interpreter1);
075: assertSame(_interpreter1, _debugInterpreters
076: .get("interpreter1").getInterpreter());
077: assertTrue(!_debugInterpreters.containsKey("interpreter2"));
078:
079: _addInterpreter("interpreter2", _interpreter2);
080: assertSame(_interpreter1, _debugInterpreters
081: .get("interpreter1").getInterpreter());
082: assertSame(_interpreter2, _debugInterpreters
083: .get("interpreter2").getInterpreter());
084:
085: try {
086: _addInterpreter("interpreter1", _interpreter3);
087: fail();
088: } catch (IllegalArgumentException ex) {
089: assertSame(_interpreter1, _debugInterpreters.get(
090: "interpreter1").getInterpreter());
091: assertSame(_interpreter2, _debugInterpreters.get(
092: "interpreter2").getInterpreter());
093: }
094: }
095:
096: /**
097: * verifies that the InterpreterJVM can switch between active interpreters.
098: */
099: public void testSwitchingActiveInterpreter()
100: throws ExceptionReturnedException {
101: String var0 = "stuff";
102: String var1 = "junk";
103: String var2 = "raargh";
104: Object val0 = new Byte("5");
105: Object val1 = new Short("2");
106: Object val2 = new Long(2782);
107: _addInterpreter("1", _interpreter1);
108: _addInterpreter("2", _interpreter2);
109:
110: JavaInterpreter interpreter = (JavaInterpreter) jvm
111: .getActiveInterpreter();
112: interpreter.defineVariable(var0, val0);
113: assertEquals(val0, interpreter.interpret(var0));
114:
115: jvm.setActiveInterpreter("1");
116: interpreter = (JavaInterpreter) jvm.getActiveInterpreter();
117: try {
118: interpreter.interpret(var0);
119: fail();
120: } catch (ExceptionReturnedException ex) {
121: // correct behavior -- var0 should not be defined
122: }
123: interpreter.defineVariable(var1, val1);
124: assertEquals(val1, interpreter.interpret(var1));
125:
126: jvm.setActiveInterpreter("2");
127: interpreter = (JavaInterpreter) jvm.getActiveInterpreter();
128: try {
129: interpreter.interpret(var0);
130: fail();
131: } catch (ExceptionReturnedException ex) {
132: }
133: try {
134: interpreter.interpret(var1);
135: fail();
136: } catch (ExceptionReturnedException ex) {
137: // correct behavior -- var0 & var1 should not be defined
138: }
139: interpreter.defineVariable(var2, val2);
140: assertEquals(val2, interpreter.interpret(var2));
141:
142: jvm.setToDefaultInterpreter();
143: interpreter = (JavaInterpreter) jvm.getActiveInterpreter();
144: try {
145: interpreter.interpret(var1);
146: fail();
147: } catch (ExceptionReturnedException ex) {
148: }
149: try {
150: interpreter.interpret(var2);
151: fail();
152: } catch (ExceptionReturnedException ex) {
153: // correct behavior -- var1 & var2 should not be defined
154: }
155: assertEquals(val0, jvm.getActiveInterpreter().interpret(var0));
156:
157: jvm.setActiveInterpreter("1");
158: interpreter = (JavaInterpreter) jvm.getActiveInterpreter();
159: try {
160: interpreter.interpret(var0);
161: fail();
162: } catch (ExceptionReturnedException ex) {
163: }
164: try {
165: interpreter.interpret(var2);
166: fail();
167: } catch (ExceptionReturnedException ex) {
168: // correct behavior -- var1 & var2 should not be defined
169: }
170: assertEquals(val1, interpreter.interpret(var1));
171:
172: try {
173: jvm.setActiveInterpreter("not an interpreter");
174: fail();
175: } catch (IllegalArgumentException ex) {
176: assertEquals(
177: "Interpreter 'not an interpreter' does not exist.",
178: ex.getMessage());
179: }
180: }
181: }
|