KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > AnnotationsTest


1
2 package org.netbeans.modules.editor;
3
4 import java.io.IOException JavaDoc;
5 import java.lang.reflect.Field JavaDoc;
6 import java.lang.reflect.Method JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import javax.swing.SwingUtilities JavaDoc;
15 import javax.swing.text.EditorKit JavaDoc;
16 import org.netbeans.editor.AnnotationDesc;
17 import org.netbeans.editor.AnnotationType;
18 import org.netbeans.editor.AnnotationTypes;
19 import org.netbeans.editor.Annotations;
20 import org.netbeans.editor.BaseDocument;
21 import org.netbeans.modules.editor.options.AnnotationTypeProcessor;
22 import org.openide.filesystems.FileObject;
23 import org.openide.filesystems.Repository;
24 import org.openide.text.Annotation;
25
26 /**
27  * Test the annotations attached to the editor.
28  *
29  * @author Miloslav Metelka
30  */

31 public class AnnotationsTest extends BaseDocumentUnitTestCase {
32     
33     public AnnotationsTest(String JavaDoc testMethodName) {
34         super(testMethodName);
35         
36     }
37     
38     protected void setUp() throws Exception JavaDoc {
39         super.setUp();
40     
41         EditorTestLookup.setLookup(
42             new URL JavaDoc[] {
43                 EditorTestConstants.EDITOR_LAYER_URL,
44                 getClass().getClassLoader().getResource(
45                         "org/netbeans/modules/editor/resources/annotations-test-layer.xml")
46             },
47             new Object JavaDoc[] {},
48             getClass().getClassLoader()
49         );
50             
51         AnnotationTypes.getTypes().registerLoader(new AnnotationsLoader());
52     }
53
54     public void testAnnotations() throws Exception JavaDoc {
55         setLoadDocumentText(
56             "a\n"
57           + "b\n"
58           + "c"
59         );
60         
61         BaseDocument doc = getDocument();
62         if (!(doc instanceof NbEditorDocument)) {
63             fail("NbEditorDocument instance expecxted. createEditorKit() redefined?");
64         }
65         
66         final NbEditorDocument nbDoc = (NbEditorDocument)doc;
67         
68         class TestAnnotation extends Annotation {
69             
70             public String JavaDoc getAnnotationType() {
71                 return "testAnnotation";
72             }
73
74             public String JavaDoc getShortDescription() {
75                 return "Test Annotation";
76             }
77
78         }
79
80         // Must run in AWT thread
81
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
82             public void run() {
83                 try {
84                     runE();
85                 } catch (Exception JavaDoc e) {
86                     //e.printStackTrace(getLog());
87
e.printStackTrace();
88                     fail(e.getMessage());
89                 }
90             }
91             
92             private void runE() throws Exception JavaDoc {
93                 // Add annotation to first line
94
Annotation testAnnotation0 = new TestAnnotation();
95                 nbDoc.addAnnotation(nbDoc.createPosition(0), -1, testAnnotation0);
96                 
97                 // Add two annotations to second line
98
Annotation testAnnotation1 = new TestAnnotation();
99                 nbDoc.addAnnotation(nbDoc.createPosition(getLineOffset(1)), -1, testAnnotation1);
100                 Annotation testAnnotation11 = new TestAnnotation();
101                 nbDoc.addAnnotation(nbDoc.createPosition(getLineOffset(1)), -1, testAnnotation11);
102                 
103                 // Check annotations locations
104
List JavaDoc line0Annos = getAnnotations(0);
105                 assertEquals(line0Annos.size(), 1);
106                 assertSame(line0Annos.get(0), testAnnotation0);
107                 
108                 List JavaDoc line1Annos = getAnnotations(1);
109                 assertEquals(line1Annos.size(), 2);
110                 assertSame(line1Annos.get(0), testAnnotation1);
111                 assertSame(line1Annos.get(1), testAnnotation11);
112             }
113         });
114     }
115     
116     private int getLineOffset(int lineIndex) {
117         return getDocument().getDefaultRootElement().getElement(lineIndex).getStartOffset();
118     }
119     
120     protected EditorKit JavaDoc createEditorKit() {
121         return new NbEditorKit();
122     }
123
124     private Annotations getAnnotations() {
125         return getDocument().getAnnotations();
126     }
127
128     private Annotations.LineAnnotations getLineAnnotations(int lineIndex) throws Exception JavaDoc {
129         Method JavaDoc getLineAnnotations = Annotations.class.getDeclaredMethod("getLineAnnotations", new Class JavaDoc[] { Integer.TYPE });
130         getLineAnnotations.setAccessible(true);
131         Annotations.LineAnnotations lineAnnotations = (Annotations.LineAnnotations)
132             getLineAnnotations.invoke(getAnnotations(), new Object JavaDoc[] { new Integer JavaDoc(lineIndex) });
133         return lineAnnotations;
134     }
135     
136     private List JavaDoc getAnnotations(int lineIndex) throws Exception JavaDoc {
137         List JavaDoc annotations;
138         Annotations.LineAnnotations lineAnnotations = getLineAnnotations(lineIndex);
139         if (lineAnnotations != null) {
140             annotations = new ArrayList JavaDoc();
141             Class JavaDoc[] inners = NbEditorDocument.class.getDeclaredClasses();
142             Field JavaDoc delegate = null;
143             for (int i = inners.length - 1; i >= 0; i--) {
144                 Class JavaDoc cls = inners[i];
145                 if (cls.getName().endsWith("AnnotationDescDelegate")) {
146                     delegate = cls.getDeclaredField("delegate");
147                     delegate.setAccessible(true);
148                 }
149             }
150             
151             for (Iterator JavaDoc it = lineAnnotations.getAnnotations(); it.hasNext();) {
152                 AnnotationDesc annoDesc = (AnnotationDesc)it.next();
153                 Annotation anno = (Annotation)delegate.get(annoDesc);
154                 annotations.add(anno);
155             }
156             
157         } else {
158             annotations = Collections.EMPTY_LIST;
159         }
160         return annotations;
161     }
162
163     /*package private*/ static final class AnnotationsLoader implements AnnotationTypes.Loader {
164
165         public void loadTypes() {
166             try {
167                 Map JavaDoc typesInstances = new HashMap JavaDoc();
168                 FileObject typesFolder = Repository.getDefault().getDefaultFileSystem().findResource("Editors/AnnotationTypes");
169                 FileObject[] types = typesFolder.getChildren();
170                 
171                 for (int cntr = 0; cntr < types.length; cntr++) {
172                     AnnotationTypeProcessor proc = new AnnotationTypeProcessor();
173                     
174                     System.err.println("CCC:" + types[cntr].getNameExt());
175                     if (types[cntr].getName().startsWith("testAnnotation") && "xml".equals(types[cntr].getExt())) {
176                         proc.attachTo(types[cntr]);
177                         AnnotationType type = (AnnotationType) proc.instanceCreate();
178                         typesInstances.put(type.getName(), type);
179                     }
180                 }
181                 
182                 AnnotationTypes.getTypes().setTypes(typesInstances);
183             } catch (Exception JavaDoc e) {
184                 e.printStackTrace();
185             }
186         }
187
188         public void loadSettings() {
189         }
190
191         public void saveType(AnnotationType type) {
192         }
193
194         public void saveSetting(String JavaDoc settingName, Object JavaDoc value) {
195         }
196
197     }
198
199 }
200
Popular Tags