KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > NbURLStreamHandlerFactoryTest


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.startup;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLConnection JavaDoc;
29 import java.net.URLStreamHandler JavaDoc;
30 import java.net.URLStreamHandlerFactory JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Locale JavaDoc;
34 import org.netbeans.junit.MockServices;
35 import org.netbeans.junit.NbTestCase;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.util.Enumerations;
38 import org.openide.util.NbBundle;
39
40 // XXX testNbfsURLStreamHandler
41

42 /**
43  * Test functionality of internal URLs.
44  * @author Jesse Glick
45  */

46 public class NbURLStreamHandlerFactoryTest extends NbTestCase {
47     
48     private static final String JavaDoc[] RESOURCES = {
49         "/test/who",
50         "/test/who_one",
51         "/test/yes",
52         "/test/something",
53         "/test/something_ja",
54         "/test/something_foo",
55         "/test/something_foo_ja",
56         "/test/something.html",
57         "/test/something_ja.html",
58         "/test/something_foo.html",
59         "/test/something_foo_ja.html",
60         "/test/something.html.template",
61         "/test/something.html_ja.template",
62         "/test/something.html_foo.template",
63         "/test/something.html_foo_ja.template",
64         "/test.dir/something.html",
65         "/test.dir/something_ja.html",
66         "/test.dir/something_foo.html",
67         "/test.dir/something_foo_ja.html",
68         "/test.dir/something",
69         "/test.dir/something_ja",
70         "/test.dir/something_foo",
71         "/test.dir/something_foo_ja",
72     };
73     
74     public NbURLStreamHandlerFactoryTest(String JavaDoc name) {
75         super(name);
76     }
77     
78     protected void setUp() throws Exception JavaDoc {
79         Main.initializeURLFactory();
80         super.setUp();
81         MockServices.setServices(TestClassLoader.class, TestURLStreamHandlerFactory.class);
82     }
83     
84     public void testNbResourceStreamHandlerAndURLStreamHandlerFactoryMerging() throws Exception JavaDoc {
85         // Basic usage of nbres.
86
checkNbres("/test/something.html");
87         // Usage of nbres with artificial param.
88
checkNbres("/test/something.html?unique=123456789", "/test/something.html");
89         // Test usage with no extension.
90
checkNbres("/test/something");
91         // Check usage with multiple extensions.
92
checkNbres("/test/something.html.template");
93         // Check bad URL.
94
URL JavaDoc u = new URL JavaDoc("nbres:/bogus");
95         try {
96             u.openConnection().connect();
97             fail("Should not be able to connect to a bogus nbres URL!");
98         } catch (IOException JavaDoc e) {
99             // OK.
100
}
101         // Now test nbresloc. First, basic usage.
102
checkNbresLoc("/test/something", ".html");
103         // Check bare resources.
104
checkNbresLoc("/test/something", "");
105         // And, double-extension resources (cf. #53130).
106
checkNbresLoc("/test/something.html", ".template");
107         // Check also dots in the path prefix.
108
checkNbresLoc("/test.dir/something", ".html");
109         checkNbresLoc("/test.dir/something", "");
110         // Check bad URL.
111
u = new URL JavaDoc("nbresloc:/bogus");
112         try {
113             u.openConnection().connect();
114             fail("Should not be able to connect to a bogus nbresloc URL!");
115         } catch (IOException JavaDoc e) {
116             // OK.
117
}
118     }
119     
120     // tests that loading of localized resources tries unlocalized version first
121
// to optimize for more common case (where class loader cache are more efficient too)
122
public void testFastGetResourceLoc() throws Exception JavaDoc {
123         TestClassLoader.firstFindRequest(); // reset
124
URL JavaDoc u = new URL JavaDoc("nbresloc:/test/who");
125         // Should initially be getting an unlocalized version:
126
NbBundle.setBranding("one");
127         Locale.setDefault(Locale.US);
128         u.openConnection().connect();
129         assertEquals("test/who", TestClassLoader.firstFindRequest());
130         u = new URL JavaDoc("nbresloc:/test/yes");
131         u.openConnection().connect();
132         assertEquals("test/yes", TestClassLoader.firstFindRequest());
133     }
134     
135     private static void checkNbres(String JavaDoc path) throws Exception JavaDoc {
136         checkNbres(path, path);
137     }
138     
139     private static void checkNbres(String JavaDoc path, String JavaDoc file) throws Exception JavaDoc {
140         URL JavaDoc u = new URL JavaDoc("nbres:" + path);
141         assertEquals(file, suck(u));
142         assertEquals(file.endsWith(".html") ? "text/html" : null, contentType(u));
143         assertEquals(file.length(), contentLength(u));
144     }
145     
146     private static void checkNbresLoc(String JavaDoc base, String JavaDoc ext) throws Exception JavaDoc {
147         String JavaDoc path = base + ext;
148         String JavaDoc type = ext.equals(".html") ? "text/html" : null;
149         URL JavaDoc u = new URL JavaDoc("nbresloc:" + path);
150         // Should initially be getting an unlocalized version:
151
NbBundle.setBranding(null);
152         Locale.setDefault(Locale.US);
153         assertEquals(path, suck(u));
154         assertEquals(type, contentType(u));
155         assertEquals(path.length(), contentLength(u));
156         // Make sure branding works.
157
NbBundle.setBranding("foo");
158         path = base + "_foo" + ext;
159         assertEquals(path, suck(u));
160         assertEquals(type, contentType(u));
161         assertEquals(path.length(), contentLength(u));
162         // Check unbranded but localized resources.
163
NbBundle.setBranding(null);
164         Locale.setDefault(Locale.JAPAN);
165         path = base + "_ja" + ext;
166         assertEquals(path, suck(u));
167         assertEquals(type, contentType(u));
168         assertEquals(path.length(), contentLength(u));
169         // Check both together.
170
NbBundle.setBranding("foo");
171         path = base + "_foo_ja" + ext;
172         assertEquals(path, suck(u));
173         assertEquals(type, contentType(u));
174         assertEquals(path.length(), contentLength(u));
175     }
176     
177     private static String JavaDoc suck(URL JavaDoc u) throws Exception JavaDoc {
178         URLConnection JavaDoc conn = u.openConnection();
179         InputStream JavaDoc is = conn.getInputStream();
180         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
181         FileUtil.copy(is, baos);
182         is.close();
183         return baos.toString("UTF-8");
184     }
185     
186     private static String JavaDoc contentType(URL JavaDoc u) throws Exception JavaDoc {
187         URLConnection JavaDoc conn = u.openConnection();
188         return conn.getContentType();
189     }
190     
191     private static int contentLength(URL JavaDoc u) throws Exception JavaDoc {
192         URLConnection JavaDoc conn = u.openConnection();
193         return conn.getContentLength();
194     }
195     
196     /**
197      * Only serves requests for resources in {@link #RESOURCES}.
198      * "/foo" is serviced with the URL "testurl:/foo".
199      */

200     public static final class TestClassLoader extends ClassLoader JavaDoc {
201         
202         static String JavaDoc first;
203         
204         static String JavaDoc firstFindRequest() {
205             String JavaDoc f = first;
206             first = null;
207             return f;
208         }
209         
210         protected URL JavaDoc findResource(String JavaDoc name) {
211             if (first == null) {
212                 first = name;
213             }
214             
215             if (!name.startsWith("/")) {
216                 name = "/" + name;
217             }
218             if (Arrays.asList(RESOURCES).contains(name)) {
219                 try {
220                     return new URL JavaDoc("testurl:" + name);
221                 } catch (MalformedURLException JavaDoc e) {
222                     assert false : e;
223                     return null;
224                 }
225             } else {
226                 return null;
227             }
228         }
229
230         protected Enumeration JavaDoc<URL JavaDoc> findResources(String JavaDoc name) throws IOException JavaDoc {
231             URL JavaDoc u = findResource(name);
232             if (u != null) {
233                 return Enumerations.singleton(u);
234             } else {
235                 return Enumerations.empty();
236             }
237         }
238         
239     }
240     
241     public static final class TestURLStreamHandlerFactory implements URLStreamHandlerFactory JavaDoc {
242         
243         public URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
244             if (protocol.equals("testurl")) {
245                 return new TestURLStreamHandler();
246             } else {
247                 return null;
248             }
249         }
250         
251     }
252     
253     private static final class TestURLStreamHandler extends URLStreamHandler JavaDoc {
254         
255         protected URLConnection JavaDoc openConnection(URL JavaDoc u) throws IOException JavaDoc {
256             return new TestURLConnection(u);
257         }
258         
259     }
260     
261     /**
262      * Serves up a URL by just returning the path as its text.
263      * Content length should match contents.
264      * Content type is text/html for *.html, null for all else.
265      */

266     private static final class TestURLConnection extends URLConnection JavaDoc {
267         
268         private final String JavaDoc path;
269         
270         public TestURLConnection(URL JavaDoc u) {
271             super(u);
272             assertEquals("testurl", u.getProtocol());
273             this.path = u.getPath();
274         }
275
276         public void connect() throws IOException JavaDoc {}
277         
278         public int getContentLength() {
279             return path.length();
280         }
281
282         public String JavaDoc getContentType() {
283             if (path.endsWith(".html")) {
284                 return "text/html";
285             } else {
286                 return null;
287             }
288         }
289
290         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
291             return new ByteArrayInputStream JavaDoc(path.getBytes("UTF-8"));
292         }
293
294     }
295     
296 }
297
Popular Tags