KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > hints > JavaHintsProviderTest


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
20 package org.netbeans.modules.java.hints;
21
22 import java.beans.PropertyVetoException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FilenameFilter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import org.netbeans.modules.editor.NbEditorDocument;
28 import org.netbeans.spi.editor.hints.ErrorDescription;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.netbeans.api.java.source.CompilationInfo;
32 import org.netbeans.api.java.source.JavaSource;
33 import org.netbeans.api.java.source.JavaSource.Phase;
34 import org.netbeans.api.java.source.SourceUtilsTestUtil;
35 import org.netbeans.editor.BaseDocument;
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.modules.editor.java.JavaKit;
38 import org.netbeans.modules.java.source.TestUtil;
39 import org.openide.cookies.EditorCookie;
40 import org.openide.filesystems.LocalFileSystem;
41 import org.openide.filesystems.Repository;
42 import org.openide.loaders.DataObject;
43
44 /**
45  *
46  * @author Jan Lahoda
47  */

48 public class JavaHintsProviderTest extends NbTestCase {
49     
50     public JavaHintsProviderTest(String JavaDoc testName) {
51         super(testName);
52     }
53
54 // public static Test suite() {
55
// TestSuite suite = new TestSuite(JavaHintsProviderTest.class);
56
//
57
// return suite;
58
// }
59

60     private FileObject testSource;
61     private JavaSource js;
62     private CompilationInfo info;
63     
64     private static File JavaDoc cache;
65     private static FileObject cacheFO;
66     
67     protected void setUp() throws Exception JavaDoc {
68         SourceUtilsTestUtil.prepareTest(new String JavaDoc[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object JavaDoc[0]);
69         
70         if (cache == null) {
71             cache = TestUtil.createWorkFolder();
72             cacheFO = FileUtil.toFileObject(cache);
73             
74             cache.deleteOnExit();
75         }
76     }
77     
78     private void prepareTest(String JavaDoc capitalizedName) throws Exception JavaDoc {
79         FileObject workFO = makeScratchDir(this);
80         
81         assertNotNull(workFO);
82         
83         FileObject sourceRoot = workFO.createFolder("src");
84         FileObject buildRoot = workFO.createFolder("build");
85 // FileObject cache = workFO.createFolder("cache");
86
FileObject packageRoot = FileUtil.createFolder(sourceRoot, "javahints");
87         
88         SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cacheFO);
89         
90         String JavaDoc testPackagePath = "javahints/";
91         File JavaDoc testPackageFile = new File JavaDoc(getDataDir(), testPackagePath);
92         
93         String JavaDoc[] names = testPackageFile.list(new FilenameFilter JavaDoc() {
94             public boolean accept(File JavaDoc dir, String JavaDoc name) {
95                 if (name.endsWith(".java"))
96                     return true;
97                 
98                 return false;
99             }
100         });
101         
102         String JavaDoc[] files = new String JavaDoc[names.length];
103         
104         for (int cntr = 0; cntr < files.length; cntr++) {
105             files[cntr] = testPackagePath + names[cntr];
106         }
107         
108         TestUtil.copyFiles(getDataDir(), FileUtil.toFile(sourceRoot), files);
109         
110         packageRoot.refresh();
111         
112         testSource = packageRoot.getFileObject(capitalizedName + ".java");
113         
114         assertNotNull(testSource);
115         
116         js = JavaSource.forFileObject(testSource);
117         
118         assertNotNull(js);
119         
120         info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);
121         
122         assertNotNull(info);
123     }
124     
125     /**Copied from org.netbeans.api.project.
126      * Create a scratch directory for tests.
127      * Will be in /tmp or whatever, and will be empty.
128      * If you just need a java.io.File use clearWorkDir + getWorkDir.
129      */

130     public static FileObject makeScratchDir(NbTestCase test) throws IOException JavaDoc {
131         test.clearWorkDir();
132         File JavaDoc root = test.getWorkDir();
133         assert root.isDirectory() && root.list().length == 0;
134         FileObject fo = FileUtil.toFileObject(root);
135         if (fo != null) {
136             // Presumably using masterfs.
137
return fo;
138         } else {
139             // For the benefit of those not using masterfs.
140
LocalFileSystem lfs = new LocalFileSystem();
141             try {
142                 lfs.setRootDirectory(root);
143             } catch (PropertyVetoException JavaDoc e) {
144                 assert false : e;
145             }
146             Repository.getDefault().addFileSystem(lfs);
147             return lfs.getRoot();
148         }
149     }
150     
151     private void performTest(String JavaDoc name) throws Exception JavaDoc {
152         prepareTest(name);
153         
154         DataObject testData = DataObject.find(testSource);
155         EditorCookie ec = (EditorCookie) testData.getCookie(EditorCookie.class);
156         Document JavaDoc doc = ec.openDocument();
157         
158         BaseDocument bdoc = new NbEditorDocument(JavaKit.class);
159         
160         bdoc.putProperty("mimeType", "text/x-java");
161         bdoc.putProperty(Document.StreamDescriptionProperty, testData);
162         bdoc.insertString(0, doc.getText(0, doc.getLength()), null);
163         
164         for (ErrorDescription ed : new JavaHintsProvider(testSource).computeErrors(info, doc))
165             ref(ed.toString());
166         
167         compareReferenceFiles();
168     }
169     
170     public void testShortErrors1() throws Exception JavaDoc {
171         performTest("TestShortErrors1");
172     }
173     
174     public void testShortErrors2() throws Exception JavaDoc {
175         performTest("TestShortErrors2");
176     }
177     
178     public void testShortErrors3() throws Exception JavaDoc {
179         performTest("TestShortErrors3");
180     }
181
182     public void testShortErrors4() throws Exception JavaDoc {
183         performTest("TestShortErrors4");
184     }
185     
186     public void testShortErrors5() throws Exception JavaDoc {
187         performTest("TestShortErrors5");
188     }
189     
190     public void testShortErrors6() throws Exception JavaDoc {
191         performTest("TestShortErrors6");
192     }
193     
194     public void testShortErrors7() throws Exception JavaDoc {
195         performTest("TestShortErrors7");
196     }
197     
198     public void testShortErrors8() throws Exception JavaDoc {
199         performTest("TestShortErrors8");
200     }
201     
202     public void testShortErrors9() throws Exception JavaDoc {
203         performTest("TestShortErrors9");
204     }
205     
206 }
207
Popular Tags