KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > overridden > IsOverriddenAnnotationCreatorTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.editor.overridden;
20
21 import java.beans.PropertyVetoException JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FilenameFilter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.text.StyledDocument JavaDoc;
29 import org.netbeans.api.java.source.CompilationInfo;
30 import org.netbeans.api.java.source.JavaSource;
31 import org.netbeans.api.java.source.JavaSource.Phase;
32 import org.netbeans.api.java.source.SourceUtilsTestUtil;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.modules.java.source.TestUtil;
35 import org.openide.cookies.EditorCookie;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.filesystems.LocalFileSystem;
39 import org.openide.filesystems.Repository;
40 import org.openide.loaders.DataObject;
41
42 /**XXX: tests for multi source root is overridden annotations are missing!
43  *
44  * @author Jan Lahoda
45  */

46 public class IsOverriddenAnnotationCreatorTest extends NbTestCase {
47     
48     private FileObject testSource;
49     private JavaSource js;
50     private CompilationInfo info;
51     
52     private static File JavaDoc cache;
53     private static FileObject cacheFO;
54     
55     public IsOverriddenAnnotationCreatorTest(String JavaDoc testName) {
56         super(testName);
57     }
58
59     protected void setUp() throws Exception JavaDoc {
60         SourceUtilsTestUtil.prepareTest(new String JavaDoc[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object JavaDoc[0]);
61         
62         if (cache == null) {
63             cache = TestUtil.createWorkFolder();
64             cacheFO = FileUtil.toFileObject(cache);
65             
66             cache.deleteOnExit();
67         }
68     }
69     
70     private void prepareTest(String JavaDoc capitalizedName) throws Exception JavaDoc {
71         FileObject workFO = makeScratchDir(this);
72         
73         assertNotNull(workFO);
74         
75         FileObject sourceRoot = workFO.createFolder("src");
76         FileObject buildRoot = workFO.createFolder("build");
77 // FileObject cache = workFO.createFolder("cache");
78
FileObject packageRoot = FileUtil.createFolder(sourceRoot, "org/netbeans/modules/editor/java");
79         
80         SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cacheFO);
81         
82         String JavaDoc testPackagePath = "org/netbeans/modules/editor/java/";
83         File JavaDoc testPackageFile = new File JavaDoc(getDataDir(), testPackagePath);
84         
85         String JavaDoc[] names = testPackageFile.list(new FilenameFilter JavaDoc() {
86             public boolean accept(File JavaDoc dir, String JavaDoc name) {
87                 if (name.endsWith(".java"))
88                     return true;
89                 
90                 return false;
91             }
92         });
93         
94         String JavaDoc[] files = new String JavaDoc[names.length];
95         
96         for (int cntr = 0; cntr < files.length; cntr++) {
97             files[cntr] = testPackagePath + names[cntr];
98         }
99         
100         TestUtil.copyFiles(getDataDir(), FileUtil.toFile(sourceRoot), files);
101         
102         packageRoot.refresh();
103         
104         SourceUtilsTestUtil.compileRecursively(sourceRoot);
105         
106         testSource = packageRoot.getFileObject(capitalizedName + ".java");
107         
108         assertNotNull(testSource);
109         
110         js = JavaSource.forFileObject(testSource);
111         
112         assertNotNull(js);
113         
114         info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);
115         
116         assertNotNull(info);
117     }
118     
119     //does not work as recursive lookup does not work:
120
// public void testExtendsList() throws Exception {
121
// doTest("TestExtendsList");
122
// }
123

124     public void testOverrides() throws Exception JavaDoc {
125         doTest("TestOverrides");
126     }
127     
128     //does not work as recursive lookup does not work:
129
// public void testInterface() throws Exception {
130
// doTest("TestInterface");
131
// }
132

133     public void testInterfaceImplOverride() throws Exception JavaDoc {
134         doTest("TestInterfaceImplOverride");
135     }
136     
137     //the "is overridden" part is currently disabled:
138
// public void testInterfaceImpl() throws Exception {
139
// doTest("TestInterfaceImpl");
140
// }
141
//
142
// public void testInterface2() throws Exception {
143
// doTest("TestInterface2");
144
// }
145

146     public void testHierarchy1() throws Exception JavaDoc {
147         doTest("TestHierarchy1");
148     }
149     
150     public void testHierarchy2() throws Exception JavaDoc {
151         doTest("TestHierarchy2");
152     }
153     
154     public void testBrokenSource() throws Exception JavaDoc {
155         doTest("TestBrokenSource");
156     }
157     
158     private void doTest(String JavaDoc name) throws Exception JavaDoc {
159         prepareTest(name);
160         
161         DataObject testDO = DataObject.find(testSource);
162         EditorCookie ec = testDO.getCookie(EditorCookie.class);
163         
164         assertNotNull(ec);
165         
166         StyledDocument JavaDoc doc = ec.openDocument();
167         
168         List JavaDoc<IsOverriddenAnnotation> annotations = new IsOverriddenAnnotationHandler(testSource).process(info, doc);
169         List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
170         
171         for (IsOverriddenAnnotation annotation : annotations) {
172             result.add(annotation.debugDump());
173         }
174         
175         Collections.sort(result);
176         
177         for (String JavaDoc r : result) {
178             ref(r);
179         }
180         
181         compareReferenceFiles();
182     }
183     
184     /**Copied from org.netbeans.api.project.
185      * Create a scratch directory for tests.
186      * Will be in /tmp or whatever, and will be empty.
187      * If you just need a java.io.File use clearWorkDir + getWorkDir.
188      */

189     @SuppressWarnings JavaDoc("deprecation")
190     public static FileObject makeScratchDir(NbTestCase test) throws IOException JavaDoc {
191         test.clearWorkDir();
192         File JavaDoc root = test.getWorkDir();
193         assert root.isDirectory() && root.list().length == 0;
194         FileObject fo = FileUtil.toFileObject(root);
195         if (fo != null) {
196             // Presumably using masterfs.
197
return fo;
198         } else {
199             // For the benefit of those not using masterfs.
200
LocalFileSystem lfs = new LocalFileSystem();
201             try {
202                 lfs.setRootDirectory(root);
203             } catch (PropertyVetoException JavaDoc e) {
204                 assert false : e;
205             }
206             Repository.getDefault().addFileSystem(lfs);
207             return lfs.getRoot();
208         }
209     }
210 }
211
Popular Tags