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;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040: import edu.rice.cs.util.StringOps;
041: import edu.rice.cs.util.text.EditDocumentException;
042:
043: /** Tests the functionality of the InteractionsDocument. Most history functionality is tested in HistoryTest.
044: * @version $Id: InteractionsDocumentTest.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public final class InteractionsDocumentTest extends DrJavaTestCase {
047: protected InteractionsDocument _doc;
048:
049: final String _testBanner = "This is a test banner";
050:
051: /** Initialize fields for each test. */
052: protected void setUp() throws Exception {
053: super .setUp();
054: // Use System.getProperty("user.dir") as working directory here and in call on reset(...) below
055: _doc = new InteractionsDocument(new InteractionsDJDocument(),
056: _testBanner);
057: }
058:
059: /** Tests that the document prevents editing before the prompt, and beeps if you try. */
060: public void testCannotEditBeforePrompt()
061: throws EditDocumentException {
062: TestBeep testBeep = new TestBeep();
063: _doc.setBeep(testBeep);
064: int origLength = _doc.getLength();
065:
066: // Try to insert into the banner
067: _doc.insertText(1, "text", InteractionsDocument.DEFAULT_STYLE);
068: assertEquals("Number of beeps", 1, testBeep.numBeeps);
069: assertEquals("Doc length", origLength, _doc.getLength());
070: }
071:
072: /** Tests that clear current interaction works. */
073: public void testClearCurrent() throws EditDocumentException {
074: int origLength = _doc.getLength();
075: _doc.insertText(origLength, "text",
076: InteractionsDocument.DEFAULT_STYLE);
077: _doc.insertBeforeLastPrompt("before",
078: InteractionsDocument.DEFAULT_STYLE);
079: assertEquals("Length after inserts", origLength + 10, _doc
080: .getLength()); // orig + "before" + "text"
081: _doc.clearCurrentInteraction();
082: assertEquals("Length after clear", origLength + 6, _doc
083: .getLength()); // orig + "before"
084: }
085:
086: /** Tests that initial contents are the banner and prompt, and that reset works. */
087: public void testContentsAndReset() throws EditDocumentException {
088: String banner = _testBanner;
089: String prompt = _doc.getPrompt();
090: String newBanner = "THIS IS A NEW BANNER\n";
091: assertEquals("Contents before insert", banner + prompt, _doc
092: .getDocText(0, _doc.getLength()));
093: // Insert some text
094: _doc.insertText(_doc.getLength(), "text",
095: InteractionsDocument.DEFAULT_STYLE);
096: _doc.insertBeforeLastPrompt("before",
097: InteractionsDocument.DEFAULT_STYLE);
098: assertEquals("Contents before reset", banner + "before"
099: + prompt + "text", _doc.getDocText(0, _doc.getLength()));
100: _doc.reset(newBanner);
101: assertEquals("Contents after reset", newBanner + prompt, _doc
102: .getDocText(0, _doc.getLength()));
103: }
104:
105: /** Tests that inserting a newline works. */
106: public void testInsertNewLine() throws EditDocumentException {
107: int origLength = _doc.getLength();
108: _doc.insertText(origLength, "command",
109: InteractionsDocument.DEFAULT_STYLE);
110: assertEquals("current interaction before newline", "command",
111: _doc.getCurrentInteraction());
112: _doc.insertNewLine(origLength + 2);
113: assertEquals("current interaction after newline", "co" + "\n" /* formerly StringOps.EOL */
114: + "mmand", _doc.getCurrentInteraction());
115: }
116:
117: /** Tests that recalling commands from the history works. */
118: public void testRecallFromHistory() throws EditDocumentException {
119: String origText = _doc.getDocText(0, _doc.getLength());
120: _doc.addToHistory("command");
121: assertEquals("Contents before recall prev", origText, _doc
122: .getDocText(0, _doc.getLength()));
123:
124: _doc.recallPreviousInteractionInHistory();
125: assertEquals("Contents after recall prev",
126: origText + "command", _doc.getDocText(0, _doc
127: .getLength()));
128:
129: _doc.recallNextInteractionInHistory();
130: assertEquals("Contents after recall next", origText, _doc
131: .getDocText(0, _doc.getLength()));
132: }
133:
134: /** Silent beep for a test class. */
135: public static class TestBeep implements Runnable {
136: int numBeeps = 0;
137:
138: public void run() {
139: numBeeps++;
140: }
141: }
142: }
|