1 33 34 package edu.rice.cs.util.text; 35 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 38 import javax.swing.text.AttributeSet ; 39 import javax.swing.text.BadLocationException ; 40 41 44 public class ConsoleDocumentTest extends DrJavaTestCase { 45 protected ConsoleDocument _doc; 46 47 public void setUp() throws Exception { 48 super.setUp(); 49 _doc = new ConsoleDocument(new SwingDocument()); 50 } 51 52 public void tearDown() throws Exception { 53 _doc = null; 54 super.tearDown(); 55 } 56 57 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 ) null); 70 assertEquals("fourth doc contents", "two three four", _doc.getText()); 71 } 72 73 74 public void testException() { 75 try { 76 _doc.insertText(5, "test", null); 77 fail("should have thrown an exception"); 78 } 79 catch (EditDocumentException e) { } 80 } 81 82 83 public void testEditCondition() throws EditDocumentException, BadLocationException { 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 ) 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 |