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 SwingDocumentTest extends DrJavaTestCase { 45 protected SwingDocument _doc; 46 47 public void setUp() throws Exception { 48 super.setUp(); 49 _doc = 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.insertString(2, "1", null); 95 assertEquals("insertString 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.remove(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 } 112 } 113 | Popular Tags |