1 19 20 package org.netbeans.modules.java.source.parsing; 21 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import java.util.jar.JarFile ; 25 import javax.tools.JavaCompiler; 26 import javax.tools.JavaFileManager; 27 import static javax.tools.JavaFileObject.Kind.*; 28 import javax.tools.StandardJavaFileManager; 29 import javax.tools.ToolProvider; 30 import junit.extensions.TestSetup; 31 import junit.framework.*; 32 import java.io.File ; 33 import java.util.zip.ZipFile ; 34 import javax.tools.StandardLocation; 35 import org.netbeans.modules.java.source.TestUtil; 36 37 41 public class FileManagerTest extends TestCase { 42 43 protected static Setup setup; 44 private CachingArchiveProvider archiveProvider; 45 46 public FileManagerTest(String testName) { 47 super(testName); 48 } 49 50 protected void setUp() throws Exception { 51 archiveProvider = new CachingArchiveProvider(); 52 } 53 54 protected void tearDown() throws Exception { 55 } 56 57 public static Test suite() { 58 setup = new Setup( new TestSuite( FileManagerTest.class ) ); 59 return setup; 60 } 61 62 protected JavaFileManagerDescripton[] getDescriptions() throws IOException { 63 64 JavaFileManager tfm = createGoldenJFM( new File [] { setup.rtFolder }, 65 new File [] { setup.srcFolder} ); 66 JavaFileManager gfm = createGoldenJFM( new File [] { setup.rtFile }, 67 new File [] { setup.srcFile } ); 68 return new JavaFileManagerDescripton[] { 69 new JavaFileManagerDescripton( tfm, gfm, setup.srcZipArchive ), 70 }; 71 } 72 73 public static class JavaFileManagerDescripton { 74 75 76 public Archive archive; 77 public JavaFileManager testJFM; 78 public JavaFileManager goldenJFM; 79 80 public JavaFileManagerDescripton( JavaFileManager testJFM, 81 JavaFileManager goldenJFM, 82 Archive archive ) { 83 this.testJFM = testJFM; 84 this.goldenJFM = goldenJFM; 85 this.archive = archive; 86 } 87 88 } 89 90 91 160 162 165 public static JavaFileManager createGoldenJFM( File [] classpath, File [] sourcpath ) throws IOException { 166 167 JavaCompiler jc = ToolProvider.getSystemJavaCompiler(); 168 StandardJavaFileManager fm = jc.getStandardFileManager (null, null, null); 169 170 if ( classpath != null ) { 171 fm.setLocation(StandardLocation.CLASS_PATH,Arrays.asList(classpath)); 172 } 173 174 if ( sourcpath != null ) { 175 fm.setLocation(StandardLocation.SOURCE_PATH,Arrays.asList(sourcpath)); 176 } 177 178 return fm; 179 180 } 181 182 183 185 private static class Setup extends TestSetup { 186 187 public File workDir; 188 public File rtFile, srcFile; 189 public File rtFolder, srcFolder; 190 public CachingArchiveProvider archiveProvider; 191 192 public Archive rtJarArchive; 193 public Archive rtFolderArchive; 194 public Archive srcZipArchive; 195 public Archive srcFolderArchive; 196 197 public Setup( Test test ) { 198 super( test ); 199 } 200 201 protected void tearDown() throws Exception { 202 TestUtil.removeWorkFolder( workDir ); 203 super.tearDown(); 204 } 205 206 protected void setUp() throws Exception { 207 super.setUp(); 208 209 workDir = TestUtil.createWorkFolder(); 210 System.out.println("Workdir " + workDir ); 211 TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.RT_JAR ); 212 TestUtil.copyFiles( TestUtil.getJdkDir(), workDir, TestUtil.SRC_ZIP ); 213 214 rtFile = new File ( workDir, TestUtil.RT_JAR ); 215 JarFile rtJar = new JarFile ( rtFile ); 216 srcFile = new File ( workDir, TestUtil.SRC_ZIP ); 217 ZipFile srcZip = new ZipFile ( srcFile ); 218 219 rtFolder = new File ( workDir, "rtFolder" ); 220 TestUtil.unzip( rtJar, rtFolder ); 221 222 srcFolder = new File ( workDir, "src" ); 223 TestUtil.unzip( srcZip, srcFolder ); 224 225 archiveProvider = CachingArchiveProvider.getDefault(); 227 228 rtJarArchive = archiveProvider.getArchive( rtFile.toURI().toURL(), true ); 229 rtFolderArchive = archiveProvider.getArchive( rtFolder.toURI().toURL(), true ); 230 srcZipArchive = archiveProvider.getArchive( srcFile.toURI().toURL(), true ); 231 srcFolderArchive = archiveProvider.getArchive( srcFolder.toURI().toURL(), true ); 232 233 } 234 } 235 236 } 237 | Popular Tags |