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.util.text;
038:
039: import edu.rice.cs.drjava.model.repl.InteractionsDJDocument;
040: import edu.rice.cs.drjava.DrJavaTestCase;
041: import javax.swing.text.AttributeSet;
042: import javax.swing.text.BadLocationException;
043:
044: /** Tests ConsoleDocument.
045: * @version $Id: ConsoleDocumentTest.java 4255 2007-08-28 19:17:37Z mgricken $
046: */
047: public class ConsoleDocumentTest extends DrJavaTestCase {
048: protected ConsoleDocument _doc;
049:
050: public void setUp() throws Exception {
051: super .setUp();
052: _doc = new ConsoleDocument(new InteractionsDJDocument());
053: }
054:
055: public void tearDown() throws Exception {
056: _doc = null;
057: super .tearDown();
058: }
059:
060: /** Tests basic interactions with a Swing Document. */
061: public void testBasicDocOps() throws EditDocumentException {
062: _doc.insertText(0, "one", null);
063: assertEquals("first doc contents", "one", _doc.getText());
064:
065: _doc.insertText(_doc.getLength(), " three", null);
066: assertEquals("second doc contents", "one three", _doc.getText());
067:
068: _doc.removeText(0, 3);
069: _doc.insertText(0, "two", null);
070: assertEquals("third doc contents", "two thr", _doc.getDocText(
071: 0, 7));
072:
073: _doc.append(" four", (String) null);
074: assertEquals("fourth doc contents", "two three four", _doc
075: .getText());
076: }
077:
078: /** Tests that a EditDocumentException is thrown when it should be. */
079: public void testException() {
080: try {
081: _doc.insertText(5, "test", null);
082: fail("should have thrown an exception");
083: } catch (EditDocumentException e) { /* Expected. Silently succeed. */
084: }
085: }
086:
087: /** Tests that a SwingDocument can receive an object that determines whether certain edits are legal. */
088: public void testEditCondition() throws EditDocumentException,
089: BadLocationException {
090: DocumentEditCondition c = new DocumentEditCondition() {
091: public boolean canInsertText(int offs) {
092: return (offs > 5);
093: }
094:
095: public boolean canRemoveText(int offs) {
096: return (offs == 1);
097: }
098: };
099: _doc.insertText(0, "initial", null);
100: assertEquals("first doc contents", "initial", _doc.getDocText(
101: 0, _doc.getLength()));
102:
103: _doc.setEditCondition(c);
104: _doc.insertText(4, "1", null);
105: assertEquals("insertText should be rejected", "initial", _doc
106: .getText());
107: _doc.insertText(2, "1", null);
108: assertEquals("insertText should be rejected", "initial", _doc
109: .getText());
110: _doc.insertText(6, "2", null);
111: assertEquals("insertText should be accepted", "initia2l", _doc
112: .getText());
113: _doc.forceInsertText(2, "3", null);
114: assertEquals("forceInsertText should be accepted", "in3itia2l",
115: _doc.getText());
116: // System.err.println(_doc.getText());
117: _doc.removeText(3, 1);
118: // System.err.println(_doc.getText());
119: assertEquals("removeText should be rejected", "in3itia2l", _doc
120: .getText());
121: _doc.removeText(6, 1);
122: assertEquals("remove should be rejected", "in3itia2l", _doc
123: .getText());
124: _doc.removeText(1, 2);
125: assertEquals("removeText should be accepted", "iitia2l", _doc
126: .getText());
127: _doc.forceRemoveText(6, 1);
128: assertEquals("forceRemove should be accepted", "iitia2", _doc
129: .getText());
130: _doc.append("THE END", (String) null);
131: assertEquals("forceRemove should be accepted", "iitia2THE END",
132: _doc.getText());
133: _doc.reset("");
134: assertEquals("promptPos reset when doc is reset", 0, _doc
135: .getPromptPos());
136: _doc.setEditCondition(new DocumentEditCondition());
137: _doc.append("THE END", null);
138: assertEquals("append to reset document should be accepted",
139: "THE END", _doc.getText());
140: _doc.setPromptPos(_doc.getLength());
141: assertEquals(
142: "promptPos is character position at end of document",
143: _doc.getLength(), _doc.getPromptPos());
144: }
145: }
|