KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > URLMapperTest


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.openide.filesystems;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.jar.JarEntry JavaDoc;
29 import java.util.jar.JarOutputStream JavaDoc;
30 import java.util.zip.CRC32 JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import org.netbeans.junit.NbTestCase;
33
34 /**
35  * Test functionality of URLMapper.
36  * @author Jesse Glick
37  */

38 public class URLMapperTest extends NbTestCase {
39
40     public URLMapperTest(String JavaDoc name) {
41         super(name);
42     }
43
44     /**
45      * Check that jar: URLs are correctly mapped back into JarFileSystem resources.
46      * @see "#39190"
47      */

48     public void testJarMapping() throws Exception JavaDoc {
49         clearWorkDir();
50         File JavaDoc workdir = getWorkDir();
51         File JavaDoc jar = new File JavaDoc(workdir, "test.jar");
52         String JavaDoc textPath = "x.txt";
53         OutputStream JavaDoc os = new FileOutputStream JavaDoc(jar);
54         try {
55             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(os);
56             jos.setMethod(ZipEntry.STORED);
57             JarEntry JavaDoc entry = new JarEntry JavaDoc(textPath);
58             entry.setSize(0L);
59             entry.setTime(System.currentTimeMillis());
60             entry.setCrc(new CRC32 JavaDoc().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 JavaDoc rootS = "jar:" + jar.toURI() + "!/";
76         URL JavaDoc rootU = new URL JavaDoc(rootS);
77         URL JavaDoc textU = new URL JavaDoc(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