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.BufferedReader;
045: import java.io.File;
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import java.io.InputStreamReader;
049: import java.io.PrintWriter;
050: import java.net.MalformedURLException;
051: import java.net.URL;
052: import java.net.URLConnection;
053: import java.util.StringTokenizer;
054: import org.netbeans.core.startup.layers.NbinstURLMapper;
055: import org.netbeans.core.startup.layers.NbinstURLStreamHandlerFactory;
056: import org.netbeans.junit.MockServices;
057: import org.netbeans.junit.NbTestCase;
058: import org.netbeans.modules.masterfs.MasterURLMapper;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileUtil;
061: import org.openide.filesystems.URLMapper;
062: import org.openide.modules.InstalledFileLocator;
063: import org.openide.util.Lookup;
064:
065: public class NbinstURLMapperTest extends NbTestCase {
066:
067: private static final String FILE_NAME = "test.txt"; //NOI18N
068: private static final String FOLDER_NAME = "modules"; //NOI18N
069:
070: private File testFile;
071: private int expectedLength;
072:
073: public NbinstURLMapperTest(String testName) throws IOException {
074: super (testName);
075: }
076:
077: protected void setUp() throws Exception {
078: super .setUp();
079:
080: MockServices.setServices(TestInstalledFileLocator.class,
081: NbinstURLStreamHandlerFactory.class,
082: NbinstURLMapper.class, MasterURLMapper.class);
083:
084: org.netbeans.core.startup.Main.initializeURLFactory();
085:
086: File f = this .getWorkDir();
087: this .clearWorkDir();
088: Lookup.Result result = Lookup.getDefault().lookupResult(
089: InstalledFileLocator.class);
090: boolean found = false;
091: for (java.util.Iterator it = result.allInstances().iterator(); it
092: .hasNext();) {
093: Object locator = it.next();
094: if (locator instanceof TestInstalledFileLocator) {
095: ((TestInstalledFileLocator) locator).setRoot(f);
096: found = true;
097: }
098: }
099: assertTrue("No TestInstalledFileLocator can be found in "
100: + Lookup.getDefault(), found);
101: f = new File(f, FOLDER_NAME);
102: f.mkdir();
103: f = new File(f, FILE_NAME);
104: f.createNewFile();
105: testFile = f;
106: PrintWriter pw = null;
107: try {
108: pw = new PrintWriter(new FileWriter(f));
109: pw.println(FILE_NAME);
110: } finally {
111: if (pw != null) {
112: pw.close();
113: }
114: }
115: this .expectedLength = (int) f.length();
116: }
117:
118: public void testFindFileObject() throws MalformedURLException,
119: IOException {
120: URL url = new URL("nbinst:///modules/test.txt"); //NOI18N
121: FileObject fo = URLMapper.findFileObject(url);
122: assertNotNull("The nbinst URL was not resolved.", fo);
123: assertEquals("URLMapper returned wrong file.", FileUtil
124: .toFile(fo), testFile);
125: url = new URL("nbinst://test-module/modules/test.txt");
126: fo = URLMapper.findFileObject(url);
127: assertNotNull("The nbinst URL was not resolved.", fo);
128: assertEquals("URLMapper returned wrong file.", FileUtil
129: .toFile(fo), testFile);
130: url = new URL("nbinst://foo-module/modules/test.txt");
131: fo = URLMapper.findFileObject(url);
132: assertNull("The nbinst URL was resolved.", fo);
133: }
134:
135: public void testURLConnection() throws MalformedURLException,
136: IOException {
137: URL url = new URL("nbinst:///modules/test.txt"); //NOI18N
138: URLConnection connection = url.openConnection();
139: assertEquals("URLConnection returned wrong content length.",
140: connection.getContentLength(), expectedLength);
141: BufferedReader in = null;
142: try {
143: in = new BufferedReader(new InputStreamReader(connection
144: .getInputStream()));
145: String line = in.readLine();
146: assertTrue("URLConnection returned invalid InputStream",
147: line.equals(FILE_NAME));
148: } finally {
149: if (in != null) {
150: in.close();
151: }
152: }
153: }
154:
155: public static class TestInstalledFileLocator extends
156: InstalledFileLocator {
157:
158: private File root;
159:
160: public TestInstalledFileLocator() {
161: }
162:
163: public void setRoot(File root) {
164: this .root = root;
165: }
166:
167: public File locate(String relativePath, String codeNameBase,
168: boolean localized) {
169: assert relativePath != null;
170: if (root == null) {
171: return null;
172: }
173: if (codeNameBase != null
174: && !"test-module".equals(codeNameBase)) {
175: return null;
176: }
177: StringTokenizer tk = new StringTokenizer(relativePath, "/");
178: File f = this .root;
179: while (tk.hasMoreTokens()) {
180: String part = tk.nextToken();
181: f = new File(f, part);
182: if (!f.exists()) {
183: return null;
184: }
185: }
186: return f;
187: }
188: }
189:
190: }
|