1 2 package org.netbeans.modules.editor; 3 4 import java.io.IOException ; 5 import java.lang.reflect.Field ; 6 import java.lang.reflect.Method ; 7 import java.net.URL ; 8 import java.util.ArrayList ; 9 import java.util.Collections ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 import javax.swing.SwingUtilities ; 15 import javax.swing.text.EditorKit ; 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 31 public class AnnotationsTest extends BaseDocumentUnitTestCase { 32 33 public AnnotationsTest(String testMethodName) { 34 super(testMethodName); 35 36 } 37 38 protected void setUp() throws Exception { 39 super.setUp(); 40 41 EditorTestLookup.setLookup( 42 new URL [] { 43 EditorTestConstants.EDITOR_LAYER_URL, 44 getClass().getClassLoader().getResource( 45 "org/netbeans/modules/editor/resources/annotations-test-layer.xml") 46 }, 47 new Object [] {}, 48 getClass().getClassLoader() 49 ); 50 51 AnnotationTypes.getTypes().registerLoader(new AnnotationsLoader()); 52 } 53 54 public void testAnnotations() throws Exception { 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 getAnnotationType() { 71 return "testAnnotation"; 72 } 73 74 public String getShortDescription() { 75 return "Test Annotation"; 76 } 77 78 } 79 80 SwingUtilities.invokeAndWait(new Runnable () { 82 public void run() { 83 try { 84 runE(); 85 } catch (Exception e) { 86 e.printStackTrace(); 88 fail(e.getMessage()); 89 } 90 } 91 92 private void runE() throws Exception { 93 Annotation testAnnotation0 = new TestAnnotation(); 95 nbDoc.addAnnotation(nbDoc.createPosition(0), -1, testAnnotation0); 96 97 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 List line0Annos = getAnnotations(0); 105 assertEquals(line0Annos.size(), 1); 106 assertSame(line0Annos.get(0), testAnnotation0); 107 108 List 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 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 { 129 Method getLineAnnotations = Annotations.class.getDeclaredMethod("getLineAnnotations", new Class [] { Integer.TYPE }); 130 getLineAnnotations.setAccessible(true); 131 Annotations.LineAnnotations lineAnnotations = (Annotations.LineAnnotations) 132 getLineAnnotations.invoke(getAnnotations(), new Object [] { new Integer (lineIndex) }); 133 return lineAnnotations; 134 } 135 136 private List getAnnotations(int lineIndex) throws Exception { 137 List annotations; 138 Annotations.LineAnnotations lineAnnotations = getLineAnnotations(lineIndex); 139 if (lineAnnotations != null) { 140 annotations = new ArrayList (); 141 Class [] inners = NbEditorDocument.class.getDeclaredClasses(); 142 Field delegate = null; 143 for (int i = inners.length - 1; i >= 0; i--) { 144 Class cls = inners[i]; 145 if (cls.getName().endsWith("AnnotationDescDelegate")) { 146 delegate = cls.getDeclaredField("delegate"); 147 delegate.setAccessible(true); 148 } 149 } 150 151 for (Iterator 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 static final class AnnotationsLoader implements AnnotationTypes.Loader { 164 165 public void loadTypes() { 166 try { 167 Map typesInstances = new HashMap (); 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 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 settingName, Object value) { 195 } 196 197 } 198 199 } 200 | Popular Tags |