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 |
Lookup.Result result = Lookup.getDefault().lookup (new Lookup.Template(InstalledFileLocator.class)); |
45 |
for (java.util.Iterator it = result.allInstances().iterator(); it.hasNext();) { |
46 |
Object locator = it.next(); |
47 |
if (locator instanceof TestInstalledFileLocator) { |
48 |
((TestInstalledFileLocator)locator).setRoot(f); |
49 |
} |
50 |
} |
51 |
f = new File (f,FOLDER_NAME); |
52 |
f.mkdir(); |
53 |
f = new File (f,FILE_NAME); |
54 |
f.createNewFile(); |
55 |
PrintWriter pw = null; |
56 |
try { |
57 |
pw = new PrintWriter(new FileWriter(f)); |
58 |
pw.println(FILE_NAME); |
59 |
} finally { |
60 |
if (pw!=null) { |
61 |
pw.close (); |
62 |
} |
63 |
} |
64 |
this.expectedLength = (int) f.length(); |
65 |
this.fs = this.mountFs (); |
66 |
assertNotNull (this.fs); |
67 |
} |
68 |
|
69 |
|
70 |
protected void tearDown() throws Exception { |
71 |
this.umountFs (this.fs); |
72 |
File f = new File (this.getWorkDir(),FOLDER_NAME); |
73 |
cleanUp (f); |
74 |
super.tearDown(); |
75 |
} |
76 |
|
77 |
public void testFindFileObject () throws MalformedURLException, IOException { |
78 |
URL url = new URL (null,"nbinst:///modules/test.txt"); //NOI18N |
79 |
FileObject[] fos = URLMapper.findFileObjects (url); |
80 |
assertTrue (fos!=null); |
81 |
assertTrue (fos.length == 1); |
82 |
} |
83 |
|
84 |
public void testURLConenction () throws MalformedURLException, IOException { |
85 |
URL url = new URL ("nbinst:///modules/test.txt"); //NOI18N |
86 |
URLConnection connection = url.openConnection(); |
87 |
assertTrue (connection.getContentLength()==expectedLength); |
88 |
BufferedReader in = null; |
89 |
try { |
90 |
in = new BufferedReader ( new InputStreamReader (connection.getInputStream())); |
91 |
String line = in.readLine(); |
92 |
assertTrue(line.equals(FILE_NAME)); |
93 |
} finally { |
94 |
if (in != null) { |
95 |
in.close (); |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
private FileSystem mountFs () throws IOException { |
105 |
File f = FileUtil.normalizeFile(this.getWorkDir()); |
106 |
String parentName; |
107 |
while ((parentName=f.getParent())!=null) { |
108 |
f = new File (parentName); |
109 |
} |
110 |
try { |
111 |
LocalFileSystem fs = new LocalFileSystem (); |
112 |
fs.setRootDirectory (f); |
113 |
Repository.getDefault().addFileSystem(fs); |
114 |
return fs; |
115 |
} catch (PropertyVetoException pve) { |
116 |
return null; |
117 |
} |
118 |
} |
119 |
|
120 |
private void umountFs (FileSystem fs) { |
121 |
assertNotNull (fs); |
122 |
Repository.getDefault().removeFileSystem(fs); |
123 |
} |
124 |
|
125 |
private void cleanUp (File f) { |
126 |
if (f.isDirectory()) { |
127 |
File[] files = f.listFiles(); |
128 |
for (int i = 0; i < files.length; i++) { |
129 |
cleanUp(files[i]); |
130 |
} |
131 |
} |
132 |
f.delete(); |
133 |
} |
134 |
|
135 |
public static class TestInstalledFileLocator extends InstalledFileLocator { |
136 |
|
137 |
private File root; |
138 |
|
139 |
public TestInstalledFileLocator () { |
140 |
} |
141 |
|
142 |
|
143 |
public void setRoot (File root) { |
144 |
this.root = root; |
145 |
} |
146 |
|
147 |
public File locate(String relativePath, String codeNameBase, boolean localized) { |
148 |
assert relativePath != null; |
149 |
if (root == null) { |
150 |
return null; |
151 |
} |
152 |
StringTokenizer tk = new StringTokenizer(relativePath,"/"); |
153 |
File f = this.root; |
154 |
while (tk.hasMoreTokens()) { |
155 |
String part = tk.nextToken(); |
156 |
f = new File (f,part); |
157 |
if (!f.exists()) { |
158 |
return null; |
159 |
} |
160 |
} |
161 |
return f; |
162 |
} |
163 |
} |
164 |
|
165 |
} |