KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > InteractionsDocumentTest


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;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37 import edu.rice.cs.util.text.EditDocumentException;
38
39 /** Tests the functionality of the InteractionsDocument. Most history functionality is tested in HistoryTest.
40  * @version $Id: InteractionsDocumentTest.java 3816 2006-04-20 19:54:29Z rcartwright $
41  */

42 public final class InteractionsDocumentTest extends DrJavaTestCase {
43   protected InteractionsDocument _doc;
44   
45   final String JavaDoc _testBanner = "This is a test banner";
46   
47   /** Initialize fields for each test. */
48   protected void setUp() throws Exception JavaDoc {
49     super.setUp();
50     // Use System.getProperty("user.dir") as working directory here and in call on reset(...) below
51
_doc = new InteractionsDocument(new InteractionsDJDocument(), _testBanner);
52   }
53
54   /** Tests that the document prevents editing before the prompt, and beeps if you try. */
55   public void testCannotEditBeforePrompt() throws EditDocumentException {
56     TestBeep testBeep = new TestBeep();
57     _doc.setBeep(testBeep);
58     int origLength = _doc.getLength();
59
60     // Try to insert into the banner
61
_doc.insertText(1, "text", InteractionsDocument.DEFAULT_STYLE);
62     assertEquals("Number of beeps", 1, testBeep.numBeeps);
63     assertEquals("Doc length", origLength, _doc.getLength());
64   }
65
66   /** Tests that clear current interaction works. */
67   public void testClearCurrent() throws EditDocumentException {
68     int origLength = _doc.getLength();
69     _doc.insertText(origLength, "text", InteractionsDocument.DEFAULT_STYLE);
70     _doc.insertBeforeLastPrompt("before", InteractionsDocument.DEFAULT_STYLE);
71     assertEquals("Length after inserts", origLength + 10, _doc.getLength()); // orig + "before" + "text"
72
_doc.clearCurrentInteraction();
73     assertEquals("Length after clear", origLength + 6, _doc.getLength()); // orig + "before"
74
}
75
76   /** Tests that initial contents are the banner and prompt, and that reset works. */
77   public void testContentsAndReset() throws EditDocumentException {
78     String JavaDoc banner = _testBanner;
79     String JavaDoc prompt = _doc.getPrompt();
80     String JavaDoc newBanner = "THIS IS A NEW BANNER\n";
81     assertEquals("Contents before insert", banner + prompt, _doc.getDocText(0, _doc.getLength()));
82     // Insert some text
83
_doc.insertText(_doc.getLength(), "text", InteractionsDocument.DEFAULT_STYLE);
84     _doc.insertBeforeLastPrompt("before", InteractionsDocument.DEFAULT_STYLE);
85     assertEquals("Contents before reset", banner + "before" + prompt + "text",
86                  _doc.getDocText(0, _doc.getLength()));
87     _doc.reset(newBanner);
88     assertEquals("Contents after reset", newBanner + prompt, _doc.getDocText(0, _doc.getLength()));
89   }
90
91   /** Tests that inserting a newline works. */
92   public void testInsertNewLine() throws EditDocumentException {
93     int origLength = _doc.getLength();
94     _doc.insertText(origLength, "command", InteractionsDocument.DEFAULT_STYLE);
95     assertEquals("current interaction before newline", "command", _doc.getCurrentInteraction());
96     _doc.insertNewLine(origLength + 2);
97     assertEquals("current interaction after newline", "co" + System.getProperty("line.separator") + "mmand",
98                  _doc.getCurrentInteraction());
99   }
100
101   /** Tests that recalling commands from the history works. */
102   public void testRecallFromHistory() throws EditDocumentException {
103     String JavaDoc origText = _doc.getDocText(0, _doc.getLength());
104     _doc.addToHistory("command");
105     assertEquals("Contents before recall prev", origText, _doc.getDocText(0, _doc.getLength()));
106
107     _doc.recallPreviousInteractionInHistory();
108     assertEquals("Contents after recall prev", origText + "command", _doc.getDocText(0, _doc.getLength()));
109
110     _doc.recallNextInteractionInHistory();
111     assertEquals("Contents after recall next", origText, _doc.getDocText(0, _doc.getLength()));
112   }
113
114
115   /** Silent beep for a test class. */
116   public static class TestBeep implements Runnable JavaDoc {
117     int numBeeps = 0;
118     public void run() { numBeeps++; }
119   }
120 }
121
122
123
124
Popular Tags