KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > parsing > ClasspathInfoTest


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
20 package org.netbeans.modules.java.source.parsing;
21
22 import com.sun.tools.javac.model.JavacElements;
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.EnumSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.jar.JarEntry JavaDoc;
31 import java.util.jar.JarFile JavaDoc;
32 import javax.lang.model.element.PackageElement;
33 import javax.lang.model.element.TypeElement;
34 import javax.lang.model.util.Elements;
35 import javax.tools.DiagnosticListener;
36 import javax.tools.JavaFileManager;
37 import javax.tools.JavaFileObject;
38 import javax.tools.StandardJavaFileManager;
39 import javax.tools.StandardLocation;
40 import junit.framework.*;
41 import org.netbeans.api.java.classpath.ClassPath;
42 import org.netbeans.api.java.source.ClasspathInfo;
43 import org.netbeans.modules.java.source.JavaSourceAccessor;
44 import org.netbeans.modules.java.source.TestUtil;
45 import org.netbeans.modules.java.source.usages.ClasspathInfoAccessor;
46 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
47 import org.openide.filesystems.FileUtil;
48
49 /**
50  *
51  * @author Petr Hrebejk
52  */

53 public class ClasspathInfoTest extends TestCase {
54     
55     private File JavaDoc workDir;
56     private File JavaDoc rtJar;
57     private ClassPath bootPath;
58     private ClassPath classPath;
59     
60     private final String JavaDoc SOURCE =
61                 "package some;" +
62                 "public class MemoryFile<K,V> extends javax.swing.JTable {" +
63                 " public java.util.Map.Entry<K,V> entry;" +
64                 "}";
65     
66     public ClasspathInfoTest(String JavaDoc testName) {
67         super(testName);
68     }
69
70     protected void setUp() throws Exception JavaDoc {
71         workDir = TestUtil.createWorkFolder();
72         TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.RT_JAR );
73         rtJar = FileUtil.normalizeFile(new File JavaDoc( workDir, TestUtil.RT_JAR ));
74         URL JavaDoc url = FileUtil.getArchiveRoot (rtJar.toURI().toURL());
75         this.bootPath = ClassPathSupport.createClassPath (new URL JavaDoc[] {url});
76         this.classPath = ClassPathSupport.createClassPath(new URL JavaDoc[0]);
77     }
78
79     protected void tearDown() throws Exception JavaDoc {
80         TestUtil.removeWorkFolder( workDir );
81     }
82
83     public static Test suite() {
84         TestSuite suite = new TestSuite(ClasspathInfoTest.class);
85         return suite;
86     }
87
88     public void testCreate() {
89         ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null);
90         assertNotNull( "Classpath Info should be created", ci );
91     }
92     
93 //
94
// public void testParse() throws Exception {
95
// final String TEST_FILE = "samples1/EmptyClass.java";
96
//
97
// JavacInterface ji = JavacInterface.create( bootPath, classPath, null);
98
//
99
// TestUtil.copyFiles( workDir, TEST_FILE );
100
// CompilationUnitTree cu = ji.parse( FileObjects.fileFileObject( new File( workDir, TEST_FILE ) ), null );
101
// assertNotNull( "Should produce compilation unit.", cu );
102
// }
103
//
104
// public void testParseString() throws Exception {
105
//
106
// JavacInterface ji = JavacInterface.create( bootPath, classPath, null);
107
//
108
// CompilationUnitTree cu = ji.parse( FileObjects.memoryFileObject( SOURCE, "MemoryFile.java"), null );
109
// assertNotNull( "Should produce compilation unit.", cu );
110
// }
111
//
112
// public void testResolve() {
113
// JavacInterface ji = JavacInterface.create( bootPath, classPath, null);
114
//
115
// CompilationUnitTree cu = ji.parse( FileObjects.memoryFileObject( SOURCE, "MemoryFile.java"), null );
116
// assertNotNull( "Should produce compilation unit.", cu );
117
//
118
// ji.resolveElements( cu );
119
//
120
// }
121
////
122
//// /**
123
//// * Test of resolveEnvironment method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
124
//// */
125
//// public void testResolveEnvironment() {
126
//// System.out.println("testResolveEnvironment");
127
//// // TODO add your test code below by replacing the default call to fail.
128
//// fail("The test case is empty.");
129
//// }
130
//
131

132     public void testGetTypeDeclaration() throws Exception JavaDoc {
133         ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null);
134     JavacElements elements = (JavacElements) JavaSourceAccessor.INSTANCE.createJavacTask(ci, (DiagnosticListener) null, (String JavaDoc) null).getElements();
135     
136         List JavaDoc<String JavaDoc> notFound = new LinkedList JavaDoc<String JavaDoc>();
137         JarFile JavaDoc jf = new JarFile JavaDoc( rtJar );
138         for( Enumeration JavaDoc entries = jf.entries(); entries.hasMoreElements(); ) {
139             JarEntry JavaDoc je = (JarEntry JavaDoc)entries.nextElement();
140             String JavaDoc jeName = je.getName();
141             if ( !je.isDirectory() && jeName.endsWith( ".class" ) ) {
142                 String JavaDoc typeName = jeName.substring( 0, jeName.length() - ".class".length() );
143
144                 typeName = typeName.replace( "/", "." ); //.replace( "$", "." );
145
TypeElement te = elements.getTypeElementByBinaryName( typeName );
146 // assertNotNull( "Declaration for " + typeName + " should not be null.", td );
147
if ( te == null ) {
148                     notFound.add( typeName );
149                 }
150             }
151         }
152         
153         assertTrue( "Should be empty " + notFound, notFound.isEmpty() );
154         
155     }
156     
157     public void testGetPackageDeclaration() throws Exception JavaDoc {
158         ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null);
159         JavaFileManager fm = ClasspathInfoAccessor.INSTANCE.getFileManager(ci);
160         JarFile JavaDoc jf = new JarFile JavaDoc( rtJar );
161         for( Enumeration JavaDoc entries = jf.entries(); entries.hasMoreElements(); ) {
162             JarEntry JavaDoc je = (JarEntry JavaDoc)entries.nextElement();
163             String JavaDoc jeName = je.getName();
164             if ( je.isDirectory() ) {
165                 String JavaDoc packageName = jeName.replace( "/", "." );
166                 if ( !fm.list( StandardLocation.PLATFORM_CLASS_PATH,packageName, EnumSet.of( JavaFileObject.Kind.CLASS ), false).iterator().hasNext() ) {
167                     // empty package
168
continue;
169                 }
170                 PackageElement pd = JavaSourceAccessor.INSTANCE.createJavacTask(ci, (DiagnosticListener) null, (String JavaDoc) null).getElements().getPackageElement( packageName );
171                 assertNotNull( "Declaration for " + packageName + " should not be null.", pd );
172             }
173         }
174     }
175
176 //
177
// /**
178
// * Test of getPackageNames method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
179
// */
180
// public void testGetPackageNames() {
181
// System.out.println("testGetPackageNames");
182
//
183
// // TODO add your test code below by replacing the default call to fail.
184
// fail("The test case is empty.");
185
// }
186
//
187
// /**
188
// * Test of getClassNames method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
189
// */
190
// public void testGetClassNames() {
191
// System.out.println("testGetClassNames");
192
//
193
// // TODO add your test code below by replacing the default call to fail.
194
// fail("The test case is empty.");
195
// }
196
//
197
// /**
198
// * Test of getSourcePositions method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
199
// */
200
// public void testGetSourcePositions() {
201
// System.out.println("testGetSourcePositions");
202
//
203
// // TODO add your test code below by replacing the default call to fail.
204
// fail("The test case is empty.");
205
// }
206
//
207
// /**
208
// * Test of getTypeChecker method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
209
// */
210
// public void testGetTypeChecker() {
211
// System.out.println("testGetTypeChecker");
212
//
213
// // TODO add your test code below by replacing the default call to fail.
214
// fail("The test case is empty.");
215
// }
216
//
217
// /**
218
// * Test of getAttribution method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
219
// */
220
// public void testGetAttribution() {
221
// System.out.println("testGetAttribution");
222
//
223
// // TODO add your test code below by replacing the default call to fail.
224
// fail("The test case is empty.");
225
// }
226
//
227
// /**
228
// * Test of cleanCaches method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
229
// */
230
// public void testCleanCaches() {
231
// System.out.println("testCleanCaches");
232
//
233
// // TODO add your test code below by replacing the default call to fail.
234
// fail("The test case is empty.");
235
// }
236
//
237
// /**
238
// * Test of getClasspath method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
239
// */
240
// public void testGetClasspath() {
241
// System.out.println("testGetClasspath");
242
//
243
// // TODO add your test code below by replacing the default call to fail.
244
// fail("The test case is empty.");
245
// }
246
//
247
// /**
248
// * Test of createContext method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
249
// */
250
// public void testCreateContext() {
251
// System.out.println("testCreateContext");
252
//
253
// // TODO add your test code below by replacing the default call to fail.
254
// fail("The test case is empty.");
255
// }
256
//
257
// /**
258
// * Test of getErrorsFor method, of class org.netbeans.modules.java.search.parsing.JavacInterface.
259
// */
260
// public void testGetErrorsFor() {
261
// System.out.println("testGetErrorsFor");
262
//
263
// // TODO add your test code below by replacing the default call to fail.
264
// fail("The test case is empty.");
265
// }
266

267 }
268
Popular Tags