Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
package org.netbeans.core.filesystems; |
14 |
|
15 |
|
16 |
import java.beans.PropertyVetoException; |
17 |
import java.io.*; |
18 |
import java.net.MalformedURLException; |
19 |
import java.net.URL; |
20 |
import java.net.URLConnection; |
21 |
import java.util.StringTokenizer; |
22 |
import org.openide.filesystems.*; |
23 |
import org.openide.util.Lookup; |
24 |
import org.openide.modules.InstalledFileLocator; |
25 |
import org.netbeans.junit.NbTestCase; |
26 |
|
27 |
|
28 |
public class NbinstURLMapperTest extends NbTestCase { |
29 |
|
30 |
private static final String FILE_NAME = "test.txt"; //NOI18N |
31 |
private static final String FOLDER_NAME = "modules"; //NOI18N |
32 |
|
33 |
private FileSystem fs; |
34 |
private int expectedLength; |
35 |
|
36 |
public NbinstURLMapperTest (String testName) throws IOException { |
37 |
super (testName); |
38 |
} |
39 |
|
40 |
|
41 |
protected void setUp() throws Exception { |
42 |
super.setUp(); |
43 |
File f = this.getWorkDir(); |
44 |
cleanUp (new File (f,FOLDER_NAME)); |
45 |
Lookup.Result result = Lookup.getDefault().lookup (new Lookup.Template(InstalledFileLocator.class)); |
46 |
boolean found = false; |
47 |
for (java.util.Iterator it = result.allInstances().iterator(); it.hasNext();) { |
48 |
Object locator = it.next(); |
49 |
if (locator instanceof TestInstalledFileLocator) { |
50 |
((TestInstalledFileLocator)locator).setRoot(f); |
51 |
found = true; |
52 |
} |
53 |
} |
54 |
assertTrue("The TestInstalledFileLocator can not be found in the default lookup.",found); |
55 |
f = new File (f,FOLDER_NAME); |
56 |
f.mkdir(); |
57 |
f = new File (f,FILE_NAME); |
58 |
f.createNewFile(); |
59 |
PrintWriter pw = null; |
60 |
try { |
61 |
pw = new PrintWriter(new FileWriter(f)); |
62 |
pw.println(FILE_NAME); |
63 |
} finally { |
64 |
if (pw!=null) { |
65 |
pw.close (); |
66 |
} |
67 |
} |
68 |
this.expectedLength = (int) f.length(); |
69 |
this.fs = this.mountFs (); |
70 |
assertNotNull ("Test was not able to mount filesystem.",this.fs); |
71 |
} |
72 |
|
73 |
|
74 |
protected void tearDown() throws Exception { |
75 |
this.umountFs (this.fs); |
76 |
super.tearDown(); |
77 |
} |
78 |
|
79 |
public void testFindFileObject () throws MalformedURLException, IOException { |
80 |
URL url = new URL ("nbinst:///modules/test.txt"); //NOI18N |
81 |
FileObject[] fos = URLMapper.findFileObjects (url); |
82 |
assertTrue ("URLMapper returned null, violation of API contract.",fos!=null); |
83 |
assertTrue ("The nbinst URL was not resolved.",fos.length == 1); |
84 |
url = new URL ("nbinst://test-module/modules/test.txt"); |
85 |
fos = URLMapper.findFileObjects (url); |
86 |
assertTrue ("URLMapper returned null, violation of API contract.",fos!=null); |
87 |
assertTrue ("The nbinst URL was not resolved.",fos.length == 1); |
88 |
url = new URL ("nbinst://foo-module/modules/test.txt"); |
89 |
fos = URLMapper.findFileObjects (url); |
90 |
assertTrue ("URLMapper returned null, violation of API contract.",fos!=null); |
91 |
assertTrue ("The nbinst URL was resolved.",fos.length == 0); |
92 |
} |
93 |
|
94 |
public void testURLConenction () throws MalformedURLException, IOException { |
95 |
URL url = new URL ("nbinst:///modules/test.txt"); //NOI18N |
96 |
URLConnection connection = url.openConnection(); |
97 |
assertEquals ("URLConnection returned wrong content length.",connection.getContentLength(),expectedLength); |
98 |
BufferedReader in = null; |
99 |
try { |
100 |
in = new BufferedReader ( new InputStreamReader (connection.getInputStream())); |
101 |
String line = in.readLine(); |
102 |
assertTrue("URLConnection returned invalid InputStream",line.equals(FILE_NAME)); |
103 |
} finally { |
104 |
if (in != null) { |
105 |
in.close (); |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
private FileSystem mountFs () throws IOException { |
115 |
File f = FileUtil.normalizeFile(this.getWorkDir()); |
116 |
String parentName; |
117 |
while ((parentName=f.getParent())!=null) { |
118 |
f = new File (parentName); |
119 |
} |
120 |
try { |
121 |
LocalFileSystem fs = new LocalFileSystem (); |
122 |
fs.setRootDirectory (f); |
123 |
Repository.getDefault().addFileSystem(fs); |
124 |
return fs; |
125 |
} catch (PropertyVetoException pve) { |
126 |
return null; |
127 |
} |
128 |
} |
129 |
|
130 |
private void umountFs (FileSystem fs) { |
131 |
assertNotNull ("umountFs called with null FileSystem.",fs); |
132 |
Repository.getDefault().removeFileSystem(fs); |
133 |
} |
134 |
|
135 |
private void cleanUp (File f) { |
136 |
if (!f.exists()) { |
137 |
return; |
138 |
} |
139 |
if (f.isDirectory()) { |
140 |
File[] files = f.listFiles(); |
141 |
for (int i = 0; i < files.length; i++) { |
142 |
cleanUp(files[i]); |
143 |
} |
144 |
} |
145 |
f.delete(); |
146 |
} |
147 |
|
148 |
public static class TestInstalledFileLocator extends InstalledFileLocator { |
149 |
|
150 |
private File root; |
151 |
|
152 |
public TestInstalledFileLocator () { |
153 |
} |
154 |
|
155 |
|
156 |
public void setRoot (File root) { |
157 |
this.root = root; |
158 |
} |
159 |
|
160 |
public File locate(String relativePath, String codeNameBase, boolean localized) { |
161 |
assert relativePath != null; |
162 |
if (root == null) { |
163 |
return null; |
164 |
} |
165 |
if (codeNameBase!= null && !"test-module".equals(codeNameBase)) { |
166 |
return null; |
167 |
} |
168 |
StringTokenizer tk = new StringTokenizer(relativePath,"/"); |
169 |
File f = this.root; |
170 |
while (tk.hasMoreTokens()) { |
171 |
String part = tk.nextToken(); |
172 |
f = new File (f,part); |
173 |
if (!f.exists()) { |
174 |
return null; |
175 |
} |
176 |
} |
177 |
return f; |
178 |
} |
179 |
} |
180 |
|
181 |
} |