1 19 20 package org.openide.filesystems; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.OutputStream ; 25 import java.net.URL ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.jar.JarEntry ; 29 import java.util.jar.JarOutputStream ; 30 import java.util.zip.CRC32 ; 31 import java.util.zip.ZipEntry ; 32 import org.netbeans.junit.NbTestCase; 33 34 38 public class URLMapperTest extends NbTestCase { 39 40 public URLMapperTest(String name) { 41 super(name); 42 } 43 44 48 public void testJarMapping() throws Exception { 49 clearWorkDir(); 50 File workdir = getWorkDir(); 51 File jar = new File (workdir, "test.jar"); 52 String textPath = "x.txt"; 53 OutputStream os = new FileOutputStream (jar); 54 try { 55 JarOutputStream jos = new JarOutputStream (os); 56 jos.setMethod(ZipEntry.STORED); 57 JarEntry entry = new JarEntry (textPath); 58 entry.setSize(0L); 59 entry.setTime(System.currentTimeMillis()); 60 entry.setCrc(new CRC32 ().getValue()); 61 jos.putNextEntry(entry); 62 jos.flush(); 63 jos.close(); 64 } finally { 65 os.close(); 66 } 67 assertTrue("JAR was created", jar.isFile()); 68 assertTrue("JAR is not empty", jar.length() > 0L); 69 JarFileSystem jfs = new JarFileSystem(); 70 jfs.setJarFile(jar); 71 Repository.getDefault().addFileSystem(jfs); 72 FileObject rootFO = jfs.getRoot(); 73 FileObject textFO = jfs.findResource(textPath); 74 assertNotNull("JAR contains a/b.txt", textFO); 75 String rootS = "jar:" + jar.toURI() + "!/"; 76 URL rootU = new URL (rootS); 77 URL textU = new URL (rootS + textPath); 78 assertEquals("correct FO -> URL for root", rootU, URLMapper.findURL(rootFO, URLMapper.EXTERNAL)); 79 assertEquals("correct FO -> URL for " + textPath, textU, URLMapper.findURL(textFO, URLMapper.EXTERNAL)); 80 assertTrue("correct URL -> FO for root", Arrays.asList(URLMapper.findFileObjects(rootU)).contains(rootFO)); 81 assertTrue("correct URL -> FO for " + textPath, Arrays.asList(URLMapper.findFileObjects(textU)).contains(textFO)); 82 } 83 84 } 85 | Popular Tags |