KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > structure > api > DocumentModelTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.editor.structure.api;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Vector JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.DefaultEditorKit JavaDoc;
28 import javax.swing.text.Document JavaDoc;
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 /** DocumentModel unit tests
37  *
38  * @author Marek Fukala
39  */

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 JavaDoc {
49         dmProvider = new FakeDocumentModelProvider();
50     }
51     
52     //--------- test methods -----------
53
public void testModelBasis() throws DocumentModelException, BadLocationException JavaDoc {
54         //set the document content
55
Document JavaDoc doc = new BaseDocument(DefaultEditorKit JavaDoc.class, false);
56         doc.insertString(0,"abcde|fgh|ij|k",null); //4 elements should be created
57

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         //test if the content of the root elemnt equals the document content
68
assertTrue(root.getContent().equals(doc.getText(0, doc.getLength())));
69         
70         List JavaDoc children = root.getChildren();
71         assertTrue(children.size() == 4);
72         
73         DocumentElement first = root.getElement(0);
74         
75         //check name and type
76
assertTrue(first.getName().equals("element0"));
77         assertTrue(first.getType().equals(FakeDocumentModelProvider.FAKE_ELEMENT_TYPE));
78         
79         //check content and offsets
80
assertTrue(first.getContent().equals("abcde"));
81         assertTrue(first.getStartOffset() == 0);
82         assertTrue(first.getEndOffset() == 5);
83         
84         //check has no children
85
assertTrue(first.getElementCount() == 0);
86         
87     }
88     
89     public void testAddElementEvent() throws DocumentModelException, BadLocationException JavaDoc, InterruptedException JavaDoc {
90         Document JavaDoc doc = new BaseDocument(DefaultEditorKit JavaDoc.class, false);
91         DocumentModel model = new DocumentModel(doc, dmProvider);
92         
93         //listen to model
94
final Vector JavaDoc addedElements = new Vector JavaDoc();
95         model.addDocumentModelListener(new DocumentModelListenerAdapter() {
96             public void documentElementAdded(DocumentElement de) {
97                 addedElements.add(de);
98             }
99         });
100         
101         //listen to element
102
final Vector JavaDoc addedElements2 = new Vector JavaDoc();
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); //4 elements should be created
110
Thread.sleep(1000); //wait for the model update (started after 500ms)
111

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 JavaDoc, InterruptedException JavaDoc {
120         Document JavaDoc doc = new BaseDocument(DefaultEditorKit JavaDoc.class, false);
121         doc.insertString(0,"abcde|fgh|ij|k",null); //4 elements should be created
122
DocumentModel model = new DocumentModel(doc, dmProvider);
123         
124         //listen to model
125
final Vector JavaDoc removedElements = new Vector JavaDoc();
126         model.addDocumentModelListener(new DocumentModelListenerAdapter() {
127             public void documentElementRemoved(DocumentElement de) {
128                 removedElements.add(de);
129             }
130         });
131         
132         //listen to element
133
final Vector JavaDoc removedElements2 = new Vector JavaDoc();
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); //wait for the model update (started after 500ms)
142

143         assertTrue(removedElements.size() == 4);
144         assertTrue(removedElements2.size() == 4);
145         
146         assertTrue(model.getRootElement().getElementCount() == 0);
147     }
148     
149     /**
150      * A Simple testing implementation of DocumentModelProvider interface
151      * used by DocumentModel unit tests.
152      *
153      * Creates elements according to the "|" separators: abcde|fghij|k|l|mnopqrstu|vwxyz
154      *
155      * @author Marek Fukala
156      */

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 JavaDoc text = model.getDocument().getText(0, model.getDocument().getLength());
164                 int lastElementEnd = 0;
165                 int elCount = 0;
166                 ArrayList JavaDoc foundElements = new ArrayList JavaDoc();
167                 for(int i = 0; i < text.length(); i++) {
168                     if(text.charAt(i) == '|' || i == text.length() - 1) {
169                         //create element if doesn't exist
170
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                 //delete unexisting elements
181
ArrayList JavaDoc deleted = new ArrayList JavaDoc(model.getRootElement().getChildren());
182                 deleted.removeAll(foundElements);
183                 Iterator JavaDoc i = deleted.iterator();
184                 while(i.hasNext()) {
185                     DocumentElement de = (DocumentElement)i.next();
186                     dtm.removeDocumentElement(de, false);
187                 }
188                 
189             }catch(BadLocationException JavaDoc e) {
190                 throw new DocumentModelException("error occured when creating elements",e);
191             }
192         }
193         
194         public static final String JavaDoc 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