KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > filesystems > ArchiveURLMapperTest


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.netbeans.core.filesystems;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.jar.JarOutputStream JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
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 /**
38  *
39  * @author tomas zezula
40  */

41 public class ArchiveURLMapperTest extends NbTestCase {
42     
43     private static final String JavaDoc RESOURCE = "test.txt"; //NOI18N
44
private static final String JavaDoc JAR_FILE = "test.jar"; //NOI18N
45

46     public ArchiveURLMapperTest(String JavaDoc testName) {
47         super(testName);
48     }
49     
50     private URL JavaDoc createJarFile () throws IOException JavaDoc {
51     File JavaDoc workDir = FileUtil.normalizeFile(this.getWorkDir());
52         File JavaDoc jarFile = new File JavaDoc(workDir,JAR_FILE);
53         JarOutputStream JavaDoc out = new JarOutputStream JavaDoc ( new FileOutputStream JavaDoc (jarFile));
54         ZipEntry JavaDoc entry = new ZipEntry JavaDoc (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 JavaDoc workDir = FileUtil.normalizeFile(this.getWorkDir());
64             File JavaDoc tmp = new File JavaDoc (workDir,JAR_FILE);
65             tmp.delete();
66             return true;
67         } catch (IOException JavaDoc ioe) {
68             return false;
69         }
70     }
71     
72     private FileSystem mountFs () throws Exception JavaDoc {
73         File JavaDoc f = FileUtil.normalizeFile(this.getWorkDir());
74         String JavaDoc parentName;
75         while ((parentName=f.getParent())!=null) {
76             f = new File JavaDoc (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 JavaDoc {
91         URL JavaDoc jarFileURL = createJarFile ();
92         FileSystem fs = mountFs();
93         assertTrue (jarFileURL != null);
94         URL JavaDoc url = new URL JavaDoc (MessageFormat.format("jar:{0}!/{1}", new Object JavaDoc[] {jarFileURL.toExternalForm(), //NOI18N
95
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 JavaDoc newUrl = URLMapper.findURL(fos[0], URLMapper.EXTERNAL);
100         assertEquals(url, newUrl);
101         removeJarFile ();
102         umountFs(fs);
103     }
104
105     public void testArchiveToRootURL () throws Exception JavaDoc {
106         URL JavaDoc jarFileURL = createJarFile ();
107         assertTrue (jarFileURL != null);
108                 assertTrue (FileUtil.isArchiveFile(jarFileURL));
109                 URL JavaDoc jarRootURL = FileUtil.getArchiveRoot(jarFileURL);
110                 assertTrue ("jar".equals(jarRootURL.getProtocol())); //NOI18N
111
String JavaDoc path = jarRootURL.getPath();
112                 int index = path.lastIndexOf ("!/"); //NOI18N
113
assertTrue (index==path.length()-2);
114                 URL JavaDoc innerURL = new URL JavaDoc(path.substring(0,index));
115                 assertTrue (innerURL.equals(jarFileURL));
116         removeJarFile ();
117     }
118         
119         
120         public void testArchiveToRootFileObject () throws Exception JavaDoc {
121             FileSystem fs = mountFs ();
122             URL JavaDoc 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())); //NOI18N
129
assertTrue (rootFo.getFileSystem() instanceof JarFileSystem);
130             File JavaDoc jarFile = ((JarFileSystem)rootFo.getFileSystem()).getJarFile();
131             assertTrue (jarFileURL.equals(jarFile.toURI().toURL()));
132             removeJarFile ();
133             umountFs (fs);
134         }
135     
136 }
137
Popular Tags