1 19 20 package org.netbeans.modules.java.source.parsing; 21 22 import com.sun.tools.javac.model.JavacElements; 23 import java.io.File ; 24 import java.net.URL ; 25 import java.util.Collections ; 26 import java.util.EnumSet ; 27 import java.util.List ; 28 import java.util.Enumeration ; 29 import java.util.LinkedList ; 30 import java.util.jar.JarEntry ; 31 import java.util.jar.JarFile ; 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 53 public class ClasspathInfoTest extends TestCase { 54 55 private File workDir; 56 private File rtJar; 57 private ClassPath bootPath; 58 private ClassPath classPath; 59 60 private final String 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 testName) { 67 super(testName); 68 } 69 70 protected void setUp() throws Exception { 71 workDir = TestUtil.createWorkFolder(); 72 TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.RT_JAR ); 73 rtJar = FileUtil.normalizeFile(new File ( workDir, TestUtil.RT_JAR )); 74 URL url = FileUtil.getArchiveRoot (rtJar.toURI().toURL()); 75 this.bootPath = ClassPathSupport.createClassPath (new URL [] {url}); 76 this.classPath = ClassPathSupport.createClassPath(new URL [0]); 77 } 78 79 protected void tearDown() throws Exception { 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 132 public void testGetTypeDeclaration() throws Exception { 133 ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null); 134 JavacElements elements = (JavacElements) JavaSourceAccessor.INSTANCE.createJavacTask(ci, (DiagnosticListener) null, (String ) null).getElements(); 135 136 List <String > notFound = new LinkedList <String >(); 137 JarFile jf = new JarFile ( rtJar ); 138 for( Enumeration entries = jf.entries(); entries.hasMoreElements(); ) { 139 JarEntry je = (JarEntry )entries.nextElement(); 140 String jeName = je.getName(); 141 if ( !je.isDirectory() && jeName.endsWith( ".class" ) ) { 142 String typeName = jeName.substring( 0, jeName.length() - ".class".length() ); 143 144 typeName = typeName.replace( "/", "." ); TypeElement te = elements.getTypeElementByBinaryName( typeName ); 146 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 { 158 ClasspathInfo ci = ClasspathInfo.create( bootPath, classPath, null); 159 JavaFileManager fm = ClasspathInfoAccessor.INSTANCE.getFileManager(ci); 160 JarFile jf = new JarFile ( rtJar ); 161 for( Enumeration entries = jf.entries(); entries.hasMoreElements(); ) { 162 JarEntry je = (JarEntry )entries.nextElement(); 163 String jeName = je.getName(); 164 if ( je.isDirectory() ) { 165 String packageName = jeName.replace( "/", "." ); 166 if ( !fm.list( StandardLocation.PLATFORM_CLASS_PATH,packageName, EnumSet.of( JavaFileObject.Kind.CLASS ), false).iterator().hasNext() ) { 167 continue; 169 } 170 PackageElement pd = JavaSourceAccessor.INSTANCE.createJavacTask(ci, (DiagnosticListener) null, (String ) null).getElements().getPackageElement( packageName ); 171 assertNotNull( "Declaration for " + packageName + " should not be null.", pd ); 172 } 173 } 174 } 175 176 267 } 268 | Popular Tags |