1 19 package org.netbeans.modules.editor.guards; 20 import javax.swing.text.BadLocationException ; 21 import javax.swing.text.Position ; 22 import javax.swing.text.StyledDocument ; 23 import junit.framework.TestCase; 24 import org.netbeans.api.editor.guards.Editor; 25 import org.netbeans.api.editor.guards.GuardUtils; 26 import org.netbeans.modules.editor.guards.GuardedSectionsImpl; 27 import org.openide.text.NbDocument; 28 29 33 public class PositionBoundsTest extends TestCase { 34 35 private Editor editor; 36 private GuardedSectionsImpl guardsImpl; 37 38 39 public PositionBoundsTest(String testName) { 40 super(testName); 41 } 42 43 protected void setUp() throws Exception { 44 this.editor = new Editor(); 45 this.guardsImpl = new GuardedSectionsImpl(this.editor); 46 GuardUtils.initManager(this.editor, this.guardsImpl); 47 } 48 49 public void testCreatePositionBounds() throws BadLocationException { 50 editor.doc.insertString(0, "_acd", null); 51 52 PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl); 54 assertEquals("start", 1, bounds.getBegin().getOffset()); 55 assertEquals("end", 3, bounds.getEnd().getOffset()); 56 assertEquals("getText", editor.doc.getText(1, 2), bounds.getText()); 57 assertEquals("getText2", "ac", bounds.getText()); 58 } 59 60 public void testChangesInPositionBounds() throws BadLocationException { 61 editor.doc.insertString(0, "_acd", null); 62 63 PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl); 65 editor.doc.insertString(2, "b", null); 66 assertEquals("start", 1, bounds.getBegin().getOffset()); 67 assertEquals("end", 4, bounds.getEnd().getOffset()); 68 assertEquals("getText", editor.doc.getText(1, 3), bounds.getText()); 69 assertEquals("getText2", "abc", bounds.getText()); 70 } 71 72 public void testSetText() throws BadLocationException { 73 editor.doc.insertString(0, "_abcd", null); 74 PositionBounds bounds = PositionBounds.create(1, 4, guardsImpl); 75 bounds.setText("xy"); 77 assertEquals("start", 1, bounds.getBegin().getOffset()); 78 assertEquals("end", 3, bounds.getEnd().getOffset()); 79 assertEquals("getText", "xy", bounds.getText()); 80 assertEquals("doc length", "_xyd".length(), editor.doc.getLength()); 81 } 82 83 public void testInsertionBeforeBounds() throws BadLocationException { 84 editor.doc.insertString(0, "_xyd", null); 85 PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl); 86 editor.doc.insertString(1, "a", null); 88 assertEquals("start", 2, bounds.getBegin().getOffset()); 89 assertEquals("end", 4, bounds.getEnd().getOffset()); 90 assertEquals("getText", "xy", bounds.getText()); 91 assertEquals("doc length", "_axyd".length(), editor.doc.getLength()); 92 } 93 94 public void testSetEmptyText() throws BadLocationException { 95 editor.doc.insertString(0, "_axyd", null); 96 PositionBounds bounds = PositionBounds.create(2, 4, guardsImpl); 97 98 bounds.setText(""); 100 assertEquals("start", 2, bounds.getBegin().getOffset()); 101 assertEquals("end", 2, bounds.getEnd().getOffset()); 102 assertEquals("getText", "", bounds.getText()); 103 assertEquals("doc length", "_ad".length(), editor.doc.getLength()); 104 105 bounds.setText("xy"); 106 assertEquals("start", 2, bounds.getBegin().getOffset()); 107 assertEquals("end", 4, bounds.getEnd().getOffset()); 108 assertEquals("getText", "xy", bounds.getText()); 109 assertEquals("doc length", "_axyd".length(), editor.doc.getLength()); 110 } 111 112 public void testDocumentClean() throws BadLocationException { 113 editor.doc.insertString(0, "_acd", null); 114 PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl); 115 116 editor.doc.remove(0, editor.doc.getLength()); 117 assertEquals("start", 0, bounds.getBegin().getOffset()); 118 assertEquals("end", 0, bounds.getEnd().getOffset()); 119 assertEquals("getText", "", bounds.getText()); 120 } 121 122 public void testComplexSetText() throws BadLocationException { 123 editor.doc.insertString(0, "_acd", null); 124 125 PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl); 127 assertEquals("start", 1, bounds.getBegin().getOffset()); 128 assertEquals("end", 3, bounds.getEnd().getOffset()); 129 assertEquals("getText", editor.doc.getText(1, 2), bounds.getText()); 130 assertEquals("getText2", "ac", bounds.getText()); 131 132 editor.doc.insertString(2, "b", null); 134 assertEquals("start", 1, bounds.getBegin().getOffset()); 135 assertEquals("end", 4, bounds.getEnd().getOffset()); 136 assertEquals("getText", editor.doc.getText(1, 3), bounds.getText()); 137 assertEquals("getText2", "abc", bounds.getText()); 138 139 bounds.setText("xy"); 141 assertEquals("start", 1, bounds.getBegin().getOffset()); 142 assertEquals("end", 3, bounds.getEnd().getOffset()); 143 assertEquals("getText", "xy", bounds.getText()); 144 assertEquals("doc length", "_xyd".length(), editor.doc.getLength()); 145 146 editor.doc.insertString(1, "a", null); 148 assertEquals("start", 2, bounds.getBegin().getOffset()); 149 assertEquals("end", 4, bounds.getEnd().getOffset()); 150 assertEquals("getText", "xy", bounds.getText()); 151 assertEquals("doc length", "_axyd".length(), editor.doc.getLength()); 152 153 bounds.setText(""); 155 assertEquals("start", 2, bounds.getBegin().getOffset()); 156 assertEquals("end", 2, bounds.getEnd().getOffset()); 157 assertEquals("getText", "", bounds.getText()); 158 assertEquals("doc length", "_ad".length(), editor.doc.getLength()); 159 160 bounds.setText("xy"); 162 assertEquals("start", 2, bounds.getBegin().getOffset()); 163 assertEquals("end", 4, bounds.getEnd().getOffset()); 164 assertEquals("getText", "xy", bounds.getText()); 165 assertEquals("doc length", "_axyd".length(), editor.doc.getLength()); 166 167 editor.doc.remove(0, editor.doc.getLength()); 168 assertEquals("start", 0, bounds.getBegin().getOffset()); 169 assertEquals("end", 0, bounds.getEnd().getOffset()); 170 assertEquals("getText", "", bounds.getText()); 171 172 } 173 174 public void testSetTextWithGuardMarks() throws Throwable { 175 final Throwable [] ts = new Throwable [1]; 176 NbDocument.runAtomic(editor.doc, new Runnable () { 177 public void run() { 178 try { 179 doTestSetTextWithGuardMarks(); 180 } catch (Throwable ex) { 181 ts[0] = ex; 182 } 183 } 184 }); 185 if (ts[0] != null) { 186 throw ts[0]; 187 } 188 } 189 190 private void doTestSetTextWithGuardMarks() throws BadLocationException { 191 StyledDocument doc = editor.doc; 192 doc.insertString(0, "abcdef", null); 193 Position p = doc.createPosition(1); 194 assertTrue(!GuardUtils.isGuarded(doc, 1)); 195 NbDocument.markGuarded(doc, 1, 3); 196 assertTrue(GuardUtils.isGuarded(doc, 1)); 197 198 doc.insertString(1, "x", null); 199 assertEquals(2, p.getOffset()); 200 assertTrue(GuardUtils.isGuarded(doc, 2)); 201 assertTrue(!GuardUtils.isGuarded(doc, 1)); 202 203 doc.insertString(4, "x", null); 204 assertEquals(2, p.getOffset()); 205 assertTrue(GuardUtils.isGuarded(doc, 4)); 206 assertTrue(GuardUtils.isGuarded(doc, 3)); 207 assertTrue(GuardUtils.isGuarded(doc, 5)); 208 assertTrue(GuardUtils.isGuarded(doc, 2)); 209 assertTrue(!GuardUtils.isGuarded(doc, 1)); 210 GuardUtils.dumpGuardedAttr(doc); 211 212 doc.remove(1, 1); 213 assertEquals(1, p.getOffset()); 214 } 215 216 } 217 | Popular Tags |