1 19 20 package org.netbeans.core.startup; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.net.URLConnection ; 29 import java.net.URLStreamHandler ; 30 import java.net.URLStreamHandlerFactory ; 31 import java.util.Arrays ; 32 import java.util.Enumeration ; 33 import java.util.Locale ; 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 42 46 public class NbURLStreamHandlerFactoryTest extends NbTestCase { 47 48 private static final String [] 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 name) { 75 super(name); 76 } 77 78 protected void setUp() throws Exception { 79 Main.initializeURLFactory(); 80 super.setUp(); 81 MockServices.setServices(TestClassLoader.class, TestURLStreamHandlerFactory.class); 82 } 83 84 public void testNbResourceStreamHandlerAndURLStreamHandlerFactoryMerging() throws Exception { 85 checkNbres("/test/something.html"); 87 checkNbres("/test/something.html?unique=123456789", "/test/something.html"); 89 checkNbres("/test/something"); 91 checkNbres("/test/something.html.template"); 93 URL u = new URL ("nbres:/bogus"); 95 try { 96 u.openConnection().connect(); 97 fail("Should not be able to connect to a bogus nbres URL!"); 98 } catch (IOException e) { 99 } 101 checkNbresLoc("/test/something", ".html"); 103 checkNbresLoc("/test/something", ""); 105 checkNbresLoc("/test/something.html", ".template"); 107 checkNbresLoc("/test.dir/something", ".html"); 109 checkNbresLoc("/test.dir/something", ""); 110 u = new URL ("nbresloc:/bogus"); 112 try { 113 u.openConnection().connect(); 114 fail("Should not be able to connect to a bogus nbresloc URL!"); 115 } catch (IOException e) { 116 } 118 } 119 120 public void testFastGetResourceLoc() throws Exception { 123 TestClassLoader.firstFindRequest(); URL u = new URL ("nbresloc:/test/who"); 125 NbBundle.setBranding("one"); 127 Locale.setDefault(Locale.US); 128 u.openConnection().connect(); 129 assertEquals("test/who", TestClassLoader.firstFindRequest()); 130 u = new URL ("nbresloc:/test/yes"); 131 u.openConnection().connect(); 132 assertEquals("test/yes", TestClassLoader.firstFindRequest()); 133 } 134 135 private static void checkNbres(String path) throws Exception { 136 checkNbres(path, path); 137 } 138 139 private static void checkNbres(String path, String file) throws Exception { 140 URL u = new URL ("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 base, String ext) throws Exception { 147 String path = base + ext; 148 String type = ext.equals(".html") ? "text/html" : null; 149 URL u = new URL ("nbresloc:" + path); 150 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 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 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 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 suck(URL u) throws Exception { 178 URLConnection conn = u.openConnection(); 179 InputStream is = conn.getInputStream(); 180 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 181 FileUtil.copy(is, baos); 182 is.close(); 183 return baos.toString("UTF-8"); 184 } 185 186 private static String contentType(URL u) throws Exception { 187 URLConnection conn = u.openConnection(); 188 return conn.getContentType(); 189 } 190 191 private static int contentLength(URL u) throws Exception { 192 URLConnection conn = u.openConnection(); 193 return conn.getContentLength(); 194 } 195 196 200 public static final class TestClassLoader extends ClassLoader { 201 202 static String first; 203 204 static String firstFindRequest() { 205 String f = first; 206 first = null; 207 return f; 208 } 209 210 protected URL findResource(String 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 ("testurl:" + name); 221 } catch (MalformedURLException e) { 222 assert false : e; 223 return null; 224 } 225 } else { 226 return null; 227 } 228 } 229 230 protected Enumeration <URL > findResources(String name) throws IOException { 231 URL 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 { 242 243 public URLStreamHandler createURLStreamHandler(String 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 { 254 255 protected URLConnection openConnection(URL u) throws IOException { 256 return new TestURLConnection(u); 257 } 258 259 } 260 261 266 private static final class TestURLConnection extends URLConnection { 267 268 private final String path; 269 270 public TestURLConnection(URL u) { 271 super(u); 272 assertEquals("testurl", u.getProtocol()); 273 this.path = u.getPath(); 274 } 275 276 public void connect() throws IOException {} 277 278 public int getContentLength() { 279 return path.length(); 280 } 281 282 public String getContentType() { 283 if (path.endsWith(".html")) { 284 return "text/html"; 285 } else { 286 return null; 287 } 288 } 289 290 public InputStream getInputStream() throws IOException { 291 return new ByteArrayInputStream (path.getBytes("UTF-8")); 292 } 293 294 } 295 296 } 297 | Popular Tags |