KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > text > ConsoleDocumentTest


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.util.text;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37
38 import javax.swing.text.AttributeSet JavaDoc;
39 import javax.swing.text.BadLocationException JavaDoc;
40
41 /** Tests ConsoleDocument.
42  * @version $Id: ConsoleDocumentTest.java 3526 2006-02-12 03:39:30Z rcartwright $
43  */

44 public class ConsoleDocumentTest extends DrJavaTestCase {
45   protected ConsoleDocument _doc;
46   
47   public void setUp() throws Exception JavaDoc {
48     super.setUp();
49     _doc = new ConsoleDocument(new SwingDocument());
50   }
51   
52   public void tearDown() throws Exception JavaDoc {
53     _doc = null;
54     super.tearDown();
55   }
56   
57   /** Tests basic interactions with a Swing Document. */
58   public void testBasicDocOps() throws EditDocumentException {
59     _doc.insertText(0, "one", null);
60     assertEquals("first doc contents", "one", _doc.getText());
61     
62     _doc.insertText(_doc.getLength(), " three", null);
63     assertEquals("second doc contents", "one three", _doc.getText());
64     
65     _doc.removeText(0, 3);
66     _doc.insertText(0, "two", null);
67     assertEquals("third doc contents", "two thr", _doc.getDocText(0, 7));
68     
69     _doc.append(" four", (String JavaDoc) null);
70     assertEquals("fourth doc contents", "two three four", _doc.getText());
71   }
72   
73   /** Tests that a EditDocumentException is thrown when it should be. */
74   public void testException() {
75     try {
76       _doc.insertText(5, "test", null);
77       fail("should have thrown an exception");
78     }
79     catch (EditDocumentException e) { /* Expected. Silently succeed. */ }
80   }
81   
82   /** Tests that a SwingDocument can receive an object that determines whether certain edits are legal. */
83   public void testEditCondition() throws EditDocumentException, BadLocationException JavaDoc {
84     DocumentEditCondition c = new DocumentEditCondition() {
85       public boolean canInsertText(int offs) { return (offs > 5); }
86       public boolean canRemoveText(int offs) { return (offs == 1); }
87     };
88     _doc.insertText(0, "initial", null);
89     assertEquals("first doc contents", "initial", _doc.getDocText(0, _doc.getLength()));
90     
91     _doc.setEditCondition(c);
92     _doc.insertText(4, "1", null);
93     assertEquals("insertText should be rejected", "initial", _doc.getText());
94     _doc.insertText(2, "1", null);
95     assertEquals("insertText should be rejected", "initial", _doc.getText());
96     _doc.insertText(6, "2", null);
97     assertEquals("insertText should be accepted", "initia2l", _doc.getText());
98     _doc.forceInsertText(2, "3", null);
99     assertEquals("forceInsertText should be accepted", "in3itia2l", _doc.getText());
100     
101     _doc.removeText(3, 1);
102     assertEquals("removeText should be rejected", "in3itia2l", _doc.getText());
103     _doc.removeText(6, 1);
104     assertEquals("remove should be rejected", "in3itia2l", _doc.getText());
105     _doc.removeText(1, 2);
106     assertEquals("removeText should be accepted", "iitia2l", _doc.getText());
107     _doc.forceRemoveText(6, 1);
108     assertEquals("forceRemove should be accepted", "iitia2", _doc.getText());
109     _doc.append("THE END", (String JavaDoc) null);
110     assertEquals("forceRemove should be accepted", "iitia2THE END", _doc.getText());
111     _doc.reset("");
112     assertEquals("promptPos reset when doc is reset", 0, _doc.getPromptPos());
113     _doc.setEditCondition(new DocumentEditCondition());
114     _doc.append("THE END", null);
115     assertEquals("append to reset document should be accepted", "THE END", _doc.getText());
116     _doc.setPromptPos(_doc.getLength());
117     assertEquals("promptPos is character position at end of document", _doc.getLength(), _doc.getPromptPos());
118   }
119 }
120
Popular Tags