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.ui;
038:
039: import edu.rice.cs.drjava.model.GlobalModelTestCase;
040: import edu.rice.cs.drjava.model.repl.InteractionsDJDocument;
041: import edu.rice.cs.util.StringOps;
042: import edu.rice.cs.util.text.ConsoleDocument;
043: import edu.rice.cs.util.text.EditDocumentException;
044:
045: /**
046: * bugs:
047: * EditDocumentExceptions in the interactions from trying to print new prompts or System.out
048: *
049: * verify that input requests go through the console
050: *
051: * @version $Id: ConsoleControllerTest.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public final class ConsoleControllerTest extends GlobalModelTestCase {
054: /** Document adapter used in the console document. */
055: protected InteractionsDJDocument _adapter;
056:
057: /** The console document for the current console controller. */
058: protected ConsoleDocument _doc;
059:
060: /** The console pane in use by the current console controller. */
061: protected InteractionsPane _pane;
062:
063: /** The current console controller. */
064: protected ConsoleController _controller;
065:
066: /** Synchronization object. */
067: protected Object _lock;
068:
069: /**
070: * Sets up the fields for the test methods.
071: */
072: public void setUp() throws Exception {
073: super .setUp();
074: _adapter = _model.getSwingConsoleDocument();
075: _doc = _model.getConsoleDocument();
076: _controller = new TestConsoleController(_doc, _adapter);
077: _pane = _controller.getPane();
078: _model.getInteractionsModel().setInputListener(
079: _controller.getInputListener());
080: _lock = _controller.getInputWaitObject(); // convenience alias for use in this test
081: }
082:
083: /**
084: * Cleans up the fields after the test methods.
085: */
086: public void tearDown() throws Exception {
087: _adapter = null;
088: _doc = null;
089: _controller = null;
090: _pane = null;
091: super .tearDown();
092: }
093:
094: /**
095: * Tests that basic input to the console is correctly redirected to System.in.
096: * We start up an InputGeneratorThread, which waits until input is requested from
097: * the console. It then generates input and puts it into the console, where it is
098: * returned to the interpreter.
099: */
100: public void testBasicConsoleInput() throws EditDocumentException,
101: InterruptedException {
102: Thread inputGenerator = new InputGeneratorThread("a");
103: String result;
104: synchronized (_lock) {
105: inputGenerator.start();
106: _lock.wait(); // wait to be notified by inputGenerator
107: // must reacquire _lock before it can proceed, i.e. inputGenerator must be waiting
108: }
109: result = interpret("System.in.read()");
110:
111: String expected = /*DefaultInteractionsModel.INPUT_REQUIRED_MESSAGE + */
112: String.valueOf((int) 'a');
113: assertEquals("read() returns the correct character", expected,
114: result);
115: result = interpret("System.in.read()");
116: assertEquals(
117: "second read() should get the end-of-line character",
118: String
119: .valueOf((int) '\n' /* formerly StringOps.EOL.charAt(0) */),
120: result);
121: }
122:
123: /**
124: * Class to insert text into the console document after input is requested.
125: */
126: protected class InputGeneratorThread extends Thread {
127: private String _text;
128:
129: /**
130: * @param text the text to be written to the console
131: */
132: public InputGeneratorThread(String text) {
133: _text = text;
134: }
135:
136: public void run() {
137: try {
138: synchronized (_lock) {
139: _lock.notify(); // notifies main test thread that this thread has started
140: _lock.wait(); // wait to be notified by RMI thread that input is needed
141: // cannot proceed until RMI thread starts waiting on _lock (_controller._inputWaitObject)
142: _doc.insertText(_doc.getLength(), _text,
143: ConsoleDocument.DEFAULT_STYLE);
144: _controller.enterAction.actionPerformed(null);
145: }
146: } catch (Exception e) {
147: listenerFail("InputGenerator failed: " + e);
148: }
149: }
150: }
151:
152: /**
153: * Overrides the _waitForInput() so that it notifies the test
154: * when input is requested.
155: */
156: protected class TestConsoleController extends ConsoleController {
157: public TestConsoleController(ConsoleDocument doc,
158: InteractionsDJDocument adapter) {
159: super (doc, adapter);
160: }
161:
162: protected void _waitForInput() {
163: synchronized (_lock) {
164: _lock.notify(); // notify inputGenerator that input is needed
165: super._waitForInput();
166: }
167: }
168: }
169: }
|