1 19 package org.netbeans.modules.editor.structure.api; 20 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Vector ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.DefaultEditorKit ; 28 import javax.swing.text.Document ; 29 import org.netbeans.editor.BaseDocument; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.modules.editor.structure.api.DocumentModel.DocumentChange; 32 import org.netbeans.modules.editor.structure.api.DocumentModel.DocumentModelTransactionCancelledException; 33 import org.netbeans.modules.editor.structure.spi.DocumentModelProvider; 34 35 36 40 public class DocumentModelTest extends NbTestCase { 41 42 DocumentModelProvider dmProvider = null; 43 44 public DocumentModelTest() { 45 super("document-model-test"); 46 } 47 48 public void setUp() throws BadLocationException { 49 dmProvider = new FakeDocumentModelProvider(); 50 } 51 52 public void testModelBasis() throws DocumentModelException, BadLocationException { 54 Document doc = new BaseDocument(DefaultEditorKit .class, false); 56 doc.insertString(0,"abcde|fgh|ij|k",null); 58 DocumentModel model = new DocumentModel(doc, dmProvider); 59 60 assertNotNull(model.getDocument()); 61 62 DocumentElement root = model.getRootElement(); 63 assertNotNull(root); 64 65 assertNull(root.getParentElement()); 66 67 assertTrue(root.getContent().equals(doc.getText(0, doc.getLength()))); 69 70 List children = root.getChildren(); 71 assertTrue(children.size() == 4); 72 73 DocumentElement first = root.getElement(0); 74 75 assertTrue(first.getName().equals("element0")); 77 assertTrue(first.getType().equals(FakeDocumentModelProvider.FAKE_ELEMENT_TYPE)); 78 79 assertTrue(first.getContent().equals("abcde")); 81 assertTrue(first.getStartOffset() == 0); 82 assertTrue(first.getEndOffset() == 5); 83 84 assertTrue(first.getElementCount() == 0); 86 87 } 88 89 public void testAddElementEvent() throws DocumentModelException, BadLocationException , InterruptedException { 90 Document doc = new BaseDocument(DefaultEditorKit .class, false); 91 DocumentModel model = new DocumentModel(doc, dmProvider); 92 93 final Vector addedElements = new Vector (); 95 model.addDocumentModelListener(new DocumentModelListenerAdapter() { 96 public void documentElementAdded(DocumentElement de) { 97 addedElements.add(de); 98 } 99 }); 100 101 final Vector addedElements2 = new Vector (); 103 model.getRootElement().addDocumentElementListener(new DocumentElementListenerAdapter() { 104 public void elementAdded(DocumentElementEvent e) { 105 addedElements2.add(e.getChangedChild()); 106 } 107 }); 108 109 doc.insertString(0,"abcde|fgh|ij|k",null); Thread.sleep(1000); 112 assertTrue(addedElements.size() == 4); 113 assertTrue(addedElements2.size() == 4); 114 115 assertTrue(model.getRootElement().getElementCount() == 4); 116 117 } 118 119 public void testRemoveElementEvent() throws DocumentModelException, BadLocationException , InterruptedException { 120 Document doc = new BaseDocument(DefaultEditorKit .class, false); 121 doc.insertString(0,"abcde|fgh|ij|k",null); DocumentModel model = new DocumentModel(doc, dmProvider); 123 124 final Vector removedElements = new Vector (); 126 model.addDocumentModelListener(new DocumentModelListenerAdapter() { 127 public void documentElementRemoved(DocumentElement de) { 128 removedElements.add(de); 129 } 130 }); 131 132 final Vector removedElements2 = new Vector (); 134 model.getRootElement().addDocumentElementListener(new DocumentElementListenerAdapter() { 135 public void elementRemoved(DocumentElementEvent e) { 136 removedElements2.add(e.getChangedChild()); 137 } 138 }); 139 140 doc.remove(0,doc.getLength()); 141 Thread.sleep(1000); 143 assertTrue(removedElements.size() == 4); 144 assertTrue(removedElements2.size() == 4); 145 146 assertTrue(model.getRootElement().getElementCount() == 0); 147 } 148 149 157 private static class FakeDocumentModelProvider implements DocumentModelProvider { 158 159 public void updateModel(DocumentModel.DocumentModelModificationTransaction dtm, 160 DocumentModel model, DocumentChange[] changes) 161 throws DocumentModelException, DocumentModelTransactionCancelledException { 162 try { 163 String text = model.getDocument().getText(0, model.getDocument().getLength()); 164 int lastElementEnd = 0; 165 int elCount = 0; 166 ArrayList foundElements = new ArrayList (); 167 for(int i = 0; i < text.length(); i++) { 168 if(text.charAt(i) == '|' || i == text.length() - 1) { 169 DocumentElement test = model.getDocumentElement(lastElementEnd, i); 171 if( test == null) { 172 foundElements.add(dtm.addDocumentElement("element"+(elCount++), FAKE_ELEMENT_TYPE, Collections.EMPTY_MAP, lastElementEnd, i)); 173 } else { 174 foundElements.add(test); 175 } 176 lastElementEnd = i; 177 } 178 } 179 180 ArrayList deleted = new ArrayList (model.getRootElement().getChildren()); 182 deleted.removeAll(foundElements); 183 Iterator i = deleted.iterator(); 184 while(i.hasNext()) { 185 DocumentElement de = (DocumentElement)i.next(); 186 dtm.removeDocumentElement(de, false); 187 } 188 189 }catch(BadLocationException e) { 190 throw new DocumentModelException("error occured when creating elements",e); 191 } 192 } 193 194 public static final String FAKE_ELEMENT_TYPE = "fake element"; 195 196 } 197 198 private static class DocumentModelListenerAdapter implements DocumentModelListener { 199 public void documentElementAdded(DocumentElement de) { 200 } 201 public void documentElementAttributesChanged(DocumentElement de) { 202 } 203 public void documentElementChanged(DocumentElement de) { 204 } 205 public void documentElementRemoved(DocumentElement de) { 206 } 207 } 208 209 private static class DocumentElementListenerAdapter implements DocumentElementListener { 210 public void attributesChanged(DocumentElementEvent e) { 211 } 212 public void childrenReordered(DocumentElementEvent e) { 213 } 214 public void contentChanged(DocumentElementEvent e) { 215 } 216 public void elementAdded(DocumentElementEvent e) { 217 } 218 public void elementRemoved(DocumentElementEvent e) { 219 } 220 } 221 222 } 223 | Popular Tags |