KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > ConsoleControllerTest


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.ui;
35
36 import edu.rice.cs.drjava.model.GlobalModelTestCase;
37 import edu.rice.cs.drjava.model.repl.InteractionsDJDocument;
38 import edu.rice.cs.util.text.ConsoleDocument;
39 import edu.rice.cs.util.text.EditDocumentException;
40
41 /**
42  * bugs:
43  * EditDocumentExceptions in the interactions from trying to print new prompts or System.out
44  *
45  * verify that input requests go through the console
46  *
47  * @version $Id: ConsoleControllerTest.java 3553 2006-02-20 21:22:09Z rcartwright $
48  */

49 public final class ConsoleControllerTest extends GlobalModelTestCase {
50   /** Document adapter used in the console document. */
51   protected InteractionsDJDocument _adapter;
52
53   /** The console document for the current console controller. */
54   protected ConsoleDocument _doc;
55
56   /** The console pane in use by the current console controller. */
57   protected InteractionsPane _pane;
58
59   /** The current console controller. */
60   protected ConsoleController _controller;
61
62   /** Synchronization object. */
63   protected Object JavaDoc _lock;
64
65   /**
66    * Sets up the fields for the test methods.
67    */

68   public void setUp() throws Exception JavaDoc {
69     super.setUp();
70     _adapter = _model.getSwingConsoleDocument();
71     _doc = _model.getConsoleDocument();
72     _controller = new TestConsoleController(_doc, _adapter);
73     _pane = _controller.getPane();
74     _model.getInteractionsModel().setInputListener(_controller.getInputListener());
75     _lock = _controller.getInputWaitObject(); // convenience alias for use in this test
76
}
77
78   /**
79    * Cleans up the fields after the test methods.
80    */

81   public void tearDown() throws Exception JavaDoc {
82     _adapter = null;
83     _doc = null;
84     _controller = null;
85     _pane = null;
86     super.tearDown();
87   }
88
89   /**
90    * Tests that basic input to the console is correctly redirected to System.in.
91    * We start up an InputGeneratorThread, which waits until input is requested from
92    * the console. It then generates input and puts it into the console, where it is
93    * returned to the interpreter.
94    */

95   public void testBasicConsoleInput()
96     throws EditDocumentException, InterruptedException JavaDoc {
97     Thread JavaDoc inputGenerator = new InputGeneratorThread("a");
98     String JavaDoc result;
99     synchronized(_lock) {
100       inputGenerator.start();
101       _lock.wait(); // wait to be notified by inputGenerator
102
// must reacquire _lock before it can proceed, i.e. inputGenerator must be waiting
103
}
104     result = interpret("System.in.read()");
105
106     String JavaDoc expected = /*DefaultInteractionsModel.INPUT_REQUIRED_MESSAGE + */
107       String.valueOf((int) 'a');
108     assertEquals("read() returns the correct character", expected, result);
109     result = interpret("System.in.read()");
110     assertEquals("second read() should get the end-of-line character",
111                  String.valueOf((int) System.getProperty("line.separator").charAt(0)), result);
112   }
113
114   /**
115    * Class to insert text into the console document after input is requested.
116    */

117   protected class InputGeneratorThread extends Thread JavaDoc {
118     private String JavaDoc _text;
119     /**
120      * @param text the text to be written to the console
121      */

122     public InputGeneratorThread(String JavaDoc text) {
123       _text = text;
124     }
125     public void run() {
126       try {
127         synchronized(_lock) {
128           _lock.notify(); // notifies main test thread that this thread has started
129
_lock.wait(); // wait to be notified by RMI thread that input is needed
130
// cannot proceed until RMI thread starts waiting on _lock (_controller._inputWaitObject)
131
_doc.insertText(_doc.getLength(), _text, ConsoleDocument.DEFAULT_STYLE);
132           _controller.enterAction.actionPerformed(null);
133         }
134       }
135       catch (Exception JavaDoc e) {
136         listenerFail("InputGenerator failed: " + e);
137       }
138     }
139   }
140
141   /**
142    * Overrides the _waitForInput() so that it notifies the test
143    * when input is requested.
144    */

145   protected class TestConsoleController extends ConsoleController {
146     public TestConsoleController(ConsoleDocument doc, InteractionsDJDocument adapter) {
147       super(doc, adapter);
148     }
149
150     protected void _waitForInput() {
151       synchronized(_lock) {
152         _lock.notify(); // notify inputGenerator that input is needed
153
super._waitForInput();
154       }
155     }
156   }
157 }
158
Popular Tags