1 19 20 package org.netbeans.core.filesystems; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.text.MessageFormat ; 27 import java.util.jar.JarOutputStream ; 28 import java.util.zip.ZipEntry ; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.URLMapper; 31 import org.netbeans.junit.NbTestCase; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.JarFileSystem; 35 import org.openide.filesystems.LocalFileSystem; 36 import org.openide.filesystems.Repository; 37 41 public class ArchiveURLMapperTest extends NbTestCase { 42 43 private static final String RESOURCE = "test.txt"; private static final String JAR_FILE = "test.jar"; 46 public ArchiveURLMapperTest(String testName) { 47 super(testName); 48 } 49 50 private URL createJarFile () throws IOException { 51 File workDir = FileUtil.normalizeFile(this.getWorkDir()); 52 File jarFile = new File (workDir,JAR_FILE); 53 JarOutputStream out = new JarOutputStream ( new FileOutputStream (jarFile)); 54 ZipEntry entry = new ZipEntry (RESOURCE); 55 out.putNextEntry(entry); 56 out.write (RESOURCE.getBytes()); 57 out.close(); 58 return jarFile.toURI().toURL(); 59 } 60 61 private boolean removeJarFile () { 62 try { 63 File workDir = FileUtil.normalizeFile(this.getWorkDir()); 64 File tmp = new File (workDir,JAR_FILE); 65 tmp.delete(); 66 return true; 67 } catch (IOException ioe) { 68 return false; 69 } 70 } 71 72 private FileSystem mountFs () throws Exception { 73 File f = FileUtil.normalizeFile(this.getWorkDir()); 74 String parentName; 75 while ((parentName=f.getParent())!=null) { 76 f = new File (parentName); 77 } 78 LocalFileSystem lfs = new LocalFileSystem (); 79 lfs.setRootDirectory(f); 80 assertTrue (lfs!=null); 81 Repository.getDefault().addFileSystem(lfs); 82 return lfs; 83 } 84 85 private void umountFs (FileSystem fs) { 86 Repository.getDefault().removeFileSystem (fs); 87 } 88 89 90 public void testURLMapper () throws Exception { 91 URL jarFileURL = createJarFile (); 92 FileSystem fs = mountFs(); 93 assertTrue (jarFileURL != null); 94 URL url = new URL (MessageFormat.format("jar:{0}!/{1}", new Object [] {jarFileURL.toExternalForm(), RESOURCE})); 96 FileObject[] fos = URLMapper.findFileObjects(url); 97 assertEquals ("There is one found file object", 1, fos.length); 98 assertTrue (fos[0].getPath().equals(RESOURCE)); 99 URL newUrl = URLMapper.findURL(fos[0], URLMapper.EXTERNAL); 100 assertEquals(url, newUrl); 101 removeJarFile (); 102 umountFs(fs); 103 } 104 105 public void testArchiveToRootURL () throws Exception { 106 URL jarFileURL = createJarFile (); 107 assertTrue (jarFileURL != null); 108 assertTrue (FileUtil.isArchiveFile(jarFileURL)); 109 URL jarRootURL = FileUtil.getArchiveRoot(jarFileURL); 110 assertTrue ("jar".equals(jarRootURL.getProtocol())); String path = jarRootURL.getPath(); 112 int index = path.lastIndexOf ("!/"); assertTrue (index==path.length()-2); 114 URL innerURL = new URL (path.substring(0,index)); 115 assertTrue (innerURL.equals(jarFileURL)); 116 removeJarFile (); 117 } 118 119 120 public void testArchiveToRootFileObject () throws Exception { 121 FileSystem fs = mountFs (); 122 URL jarFileURL = createJarFile (); 123 FileObject fo = URLMapper.findFileObject(jarFileURL); 124 assertTrue (fo != null); 125 assertTrue (FileUtil.isArchiveFile(fo)); 126 FileObject rootFo = FileUtil.getArchiveRoot (fo); 127 assertTrue (rootFo!=null); 128 assertTrue ("".equals(rootFo.getPath())); assertTrue (rootFo.getFileSystem() instanceof JarFileSystem); 130 File jarFile = ((JarFileSystem)rootFo.getFileSystem()).getJarFile(); 131 assertTrue (jarFileURL.equals(jarFile.toURI().toURL())); 132 removeJarFile (); 133 umountFs (fs); 134 } 135 136 } 137 | Popular Tags |