001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.filesystems;
043:
044: import java.io.File;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.net.URL;
048: import java.text.MessageFormat;
049: import java.util.jar.JarOutputStream;
050: import java.util.zip.ZipEntry;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.URLMapper;
053: import org.netbeans.junit.NbTestCase;
054: import org.openide.filesystems.FileSystem;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.filesystems.JarFileSystem;
057: import org.openide.filesystems.LocalFileSystem;
058: import org.openide.filesystems.Repository;
059:
060: /**
061: *
062: * @author tomas zezula
063: */
064: public class ArchiveURLMapperTest extends NbTestCase {
065:
066: private static final String RESOURCE = "test.txt"; //NOI18N
067: private static final String JAR_FILE = "test.jar"; //NOI18N
068:
069: public ArchiveURLMapperTest(String testName) {
070: super (testName);
071: }
072:
073: private URL createJarFile() throws IOException {
074: File workDir = FileUtil.normalizeFile(this .getWorkDir());
075: File jarFile = new File(workDir, JAR_FILE);
076: JarOutputStream out = new JarOutputStream(new FileOutputStream(
077: jarFile));
078: ZipEntry entry = new ZipEntry(RESOURCE);
079: out.putNextEntry(entry);
080: out.write(RESOURCE.getBytes());
081: out.close();
082: return jarFile.toURI().toURL();
083: }
084:
085: private boolean removeJarFile() {
086: try {
087: File workDir = FileUtil.normalizeFile(this .getWorkDir());
088: File tmp = new File(workDir, JAR_FILE);
089: tmp.delete();
090: return true;
091: } catch (IOException ioe) {
092: return false;
093: }
094: }
095:
096: private FileSystem mountFs() throws Exception {
097: File f = FileUtil.normalizeFile(this .getWorkDir());
098: String parentName;
099: while ((parentName = f.getParent()) != null) {
100: f = new File(parentName);
101: }
102: LocalFileSystem lfs = new LocalFileSystem();
103: lfs.setRootDirectory(f);
104: assertTrue(lfs != null);
105: Repository.getDefault().addFileSystem(lfs);
106: return lfs;
107: }
108:
109: private void umountFs(FileSystem fs) {
110: Repository.getDefault().removeFileSystem(fs);
111: }
112:
113: public void testURLMapper() throws Exception {
114: URL jarFileURL = createJarFile();
115: FileSystem fs = mountFs();
116: assertTrue(jarFileURL != null);
117: URL url = new URL(MessageFormat.format("jar:{0}!/{1}",
118: new Object[] { jarFileURL.toExternalForm(), //NOI18N
119: RESOURCE }));
120: FileObject[] fos = URLMapper.findFileObjects(url);
121: assertEquals("There is one found file object", 1, fos.length);
122: assertTrue(fos[0].getPath().equals(RESOURCE));
123: URL newUrl = URLMapper.findURL(fos[0], URLMapper.EXTERNAL);
124: assertEquals(url, newUrl);
125: removeJarFile();
126: umountFs(fs);
127: }
128:
129: public void testArchiveToRootURL() throws Exception {
130: URL jarFileURL = createJarFile();
131: assertTrue(jarFileURL != null);
132: assertTrue(FileUtil.isArchiveFile(jarFileURL));
133: URL jarRootURL = FileUtil.getArchiveRoot(jarFileURL);
134: assertTrue("jar".equals(jarRootURL.getProtocol())); //NOI18N
135: String path = jarRootURL.getPath();
136: int index = path.lastIndexOf("!/"); //NOI18N
137: assertTrue(index == path.length() - 2);
138: URL innerURL = new URL(path.substring(0, index));
139: assertTrue(innerURL.equals(jarFileURL));
140: removeJarFile();
141: }
142:
143: public void testArchiveToRootFileObject() throws Exception {
144: FileSystem fs = mountFs();
145: URL jarFileURL = createJarFile();
146: FileObject fo = URLMapper.findFileObject(jarFileURL);
147: assertTrue(fo != null);
148: assertTrue(FileUtil.isArchiveFile(fo));
149: FileObject rootFo = FileUtil.getArchiveRoot(fo);
150: assertTrue(rootFo != null);
151: assertTrue("".equals(rootFo.getPath())); //NOI18N
152: assertTrue(rootFo.getFileSystem() instanceof JarFileSystem);
153: File jarFile = ((JarFileSystem) rootFo.getFileSystem())
154: .getJarFile();
155: assertTrue(jarFileURL.equals(jarFile.toURI().toURL()));
156: removeJarFile();
157: umountFs(fs);
158: }
159:
160: }
|