Source Code Cross Referenced for URLClassLoaderTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » net » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.luni.tests.java.net;
019:
020:        import java.io.File;
021:        import java.io.IOException;
022:        import java.io.InputStream;
023:        import java.net.MalformedURLException;
024:        import java.net.URL;
025:        import java.net.URLClassLoader;
026:        import java.net.URLStreamHandler;
027:        import java.net.URLStreamHandlerFactory;
028:        import java.util.Enumeration;
029:        import java.util.NoSuchElementException;
030:        import java.util.StringTokenizer;
031:        import java.util.Vector;
032:
033:        import junit.framework.TestCase;
034:
035:        import org.apache.harmony.luni.util.InvalidJarIndexException;
036:
037:        import tests.support.Support_Configuration;
038:        import tests.support.resource.Support_Resources;
039:
040:        public class URLClassLoaderTest extends TestCase {
041:
042:            class BogusClassLoader extends ClassLoader {
043:                public URL getResource(String res) {
044:                    try {
045:                        return new URL("http://test/BogusClassLoader");
046:                    } catch (MalformedURLException e) {
047:                        return null;
048:                    }
049:                }
050:            }
051:
052:            public class URLClassLoaderExt extends URLClassLoader {
053:
054:                public URLClassLoaderExt(URL[] urls) {
055:                    super (urls);
056:                }
057:
058:                public Class<?> findClass(String cl)
059:                        throws ClassNotFoundException {
060:                    return super .findClass(cl);
061:                }
062:            }
063:
064:            URLClassLoader ucl;
065:
066:            /**
067:             * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[])
068:             */
069:            public void test_Constructor$Ljava_net_URL() {
070:                URL[] u = new URL[0];
071:                ucl = new URLClassLoader(u);
072:                assertTrue("Failed to set parent", ucl != null
073:                        && ucl.getParent() == URLClassLoader
074:                                .getSystemClassLoader());
075:
076:                URLClassLoader loader = new URLClassLoader(new URL[] { null });
077:                boolean exception = false;
078:                try {
079:                    Class.forName("test", false, loader);
080:                    fail("Should throw ClassNotFoundException");
081:                } catch (ClassNotFoundException e) {
082:                    // expected
083:                }
084:            }
085:
086:            /**
087:             * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[],
088:             *        java.lang.ClassLoader)
089:             */
090:            public void test_Constructor$Ljava_net_URLLjava_lang_ClassLoader() {
091:                ClassLoader cl = new BogusClassLoader();
092:                URL[] u = new URL[0];
093:                ucl = new URLClassLoader(u, cl);
094:                URL res = ucl.getResource("J");
095:                assertNotNull(res);
096:                assertEquals("Failed to set parent", "/BogusClassLoader", res
097:                        .getFile());
098:            }
099:
100:            /**
101:             * @tests java.net.URLClassLoader#findResources(java.lang.String)
102:             */
103:            public void test_findResourcesLjava_lang_String()
104:                    throws IOException {
105:                Enumeration res = null;
106:                String[] resValues = { "This is a test resource file.",
107:                        "This is a resource from a subdir" };
108:
109:                URL[] urls = new URL[2];
110:                urls[0] = new URL(Support_Resources.getResourceURL("/"));
111:                urls[1] = new URL(Support_Resources.getResourceURL("/subdir1/"));
112:                ucl = new URLClassLoader(urls);
113:                res = ucl.findResources("RESOURCE.TXT");
114:                assertNotNull("Failed to locate resources", res);
115:
116:                int i = 0;
117:                while (res.hasMoreElements()) {
118:                    StringBuffer sb = new StringBuffer();
119:                    InputStream is = ((URL) res.nextElement()).openStream();
120:                    int c;
121:                    while ((c = is.read()) != -1) {
122:                        sb.append((char) c);
123:                    }
124:                    assertEquals(
125:                            "Returned incorrect resource/or in wrong order",
126:                            resValues[i++], sb.toString());
127:                }
128:                assertTrue("Incorrect number of resources returned: " + i,
129:                        i == 2);
130:            }
131:
132:            /**
133:             * @tests java.net.URLClassLoader#getURLs()
134:             */
135:            public void test_getURLs() throws MalformedURLException {
136:                URL[] urls = new URL[4];
137:                urls[0] = new URL("http://" + Support_Configuration.HomeAddress);
138:                urls[1] = new URL("http://"
139:                        + Support_Configuration.TestResources + "/");
140:                urls[2] = new URL("ftp://"
141:                        + Support_Configuration.TestResources + "/");
142:                urls[3] = new URL("jar:file:c://"
143:                        + Support_Configuration.TestResources + "!/");
144:                ucl = new URLClassLoader(urls);
145:                URL[] ucUrls = ucl.getURLs();
146:                for (int i = 0; i < urls.length; i++) {
147:                    assertEquals("Returned incorrect URL[]", urls[i], ucUrls[i]);
148:                }
149:            }
150:
151:            /**
152:             * @tests java.net.URLClassLoader#newInstance(java.net.URL[])
153:             */
154:            public void test_newInstance$Ljava_net_URL()
155:                    throws MalformedURLException, ClassNotFoundException {
156:                // Verify that loaded class' have correct permissions
157:                Class cl = null;
158:                URL res = null;
159:                URL[] urls = new URL[1];
160:                urls[0] = new URL(Support_Resources
161:                        .getResourceURL("/UCL/UCL.jar"));
162:                ucl = URLClassLoader.newInstance(urls);
163:                cl = ucl.loadClass("ucl.ResClass");
164:
165:                res = cl.getClassLoader().getResource("XX.class");
166:                assertNotNull("Failed to load class", cl);
167:                assertNotNull(
168:                        "Loaded class unable to access resource from same codeSource",
169:                        res);
170:                cl = null;
171:
172:                urls[0] = new URL("jar:"
173:                        + Support_Resources.getResourceURL("/UCL/UCL.jar!/"));
174:                ucl = URLClassLoader.newInstance(urls);
175:                cl = ucl.loadClass("ucl.ResClass");
176:                assertNotNull("Failed to load class from explicit jar URL", cl);
177:            }
178:
179:            /**
180:             * @tests java.net.URLClassLoader#newInstance(java.net.URL[],
181:             *        java.lang.ClassLoader)
182:             */
183:            public void test_newInstance$Ljava_net_URLLjava_lang_ClassLoader() {
184:                ClassLoader cl = new BogusClassLoader();
185:                URL[] u = new URL[0];
186:                ucl = URLClassLoader.newInstance(u, cl);
187:                URL res = ucl.getResource("J");
188:                assertNotNull(res);
189:                assertEquals("Failed to set parent", "/BogusClassLoader", res
190:                        .getFile());
191:            }
192:
193:            /**
194:             * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[],
195:             *        java.lang.ClassLoader, java.net.URLStreamHandlerFactory)
196:             */
197:            public void test_Constructor$Ljava_net_URLLjava_lang_ClassLoaderLjava_net_URLStreamHandlerFactory() {
198:                class TestFactory implements  URLStreamHandlerFactory {
199:                    public URLStreamHandler createURLStreamHandler(
200:                            String protocol) {
201:                        return null;
202:                    }
203:                }
204:                ClassLoader cl = new BogusClassLoader();
205:                URL[] u = new URL[0];
206:                ucl = new URLClassLoader(u, cl, new TestFactory());
207:                URL res = ucl.getResource("J");
208:                assertNotNull(res);
209:                assertEquals("Failed to set parent", "/BogusClassLoader", res
210:                        .getFile());
211:            }
212:
213:            /**
214:             * @throws ClassNotFoundException
215:             * @throws IOException
216:             * @tests java.net.URLClassLoader#findClass(java.lang.String)
217:             */
218:            public void test_findClassLjava_lang_String()
219:                    throws ClassNotFoundException, IOException {
220:                File resources = Support_Resources.createTempFolder();
221:                String resPath = resources.toString();
222:                if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') {
223:                    resPath = resPath.substring(1);
224:                }
225:
226:                java.net.URL[] urls = new java.net.URL[1];
227:                java.net.URLClassLoader ucl = null;
228:                boolean classFound;
229:                boolean exception;
230:                boolean goodException;
231:                Enumeration en;
232:                boolean resourcesFound;
233:                Support_Resources
234:                        .copyFile(resources, "JarIndex", "hyts_11.jar");
235:                Support_Resources
236:                        .copyFile(resources, "JarIndex", "hyts_12.jar");
237:                Support_Resources
238:                        .copyFile(resources, "JarIndex", "hyts_13.jar");
239:                Support_Resources
240:                        .copyFile(resources, "JarIndex", "hyts_14.jar");
241:                urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_11.jar");
242:                ucl = URLClassLoader.newInstance(urls, null);
243:                URL resURL = ucl.findResource("Test.txt");
244:                URL reference = new URL("jar:file:/"
245:                        + resPath.replace('\\', '/')
246:                        + "/JarIndex/hyts_14.jar!/Test.txt");
247:                assertTrue("Resource not found: " + resURL + " ref: "
248:                        + reference, resURL.equals(reference));
249:
250:                Class c = Class.forName("cpack.CNothing", true, ucl);
251:                assertNotNull(c);
252:
253:                Support_Resources
254:                        .copyFile(resources, "JarIndex", "hyts_21.jar");
255:                Support_Resources
256:                        .copyFile(resources, "JarIndex", "hyts_22.jar");
257:                Support_Resources
258:                        .copyFile(resources, "JarIndex", "hyts_23.jar");
259:                urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_21.jar");
260:                ucl = URLClassLoader.newInstance(urls, null);
261:                en = ucl.findResources("bpack/");
262:
263:                try {
264:                    resourcesFound = true;
265:                    URL url1 = (URL) en.nextElement();
266:                    URL url2 = (URL) en.nextElement();
267:                    System.out.println(url1);
268:                    System.out.println(url2);
269:                    resourcesFound = resourcesFound
270:                            && url1.equals(new URL("jar:file:/"
271:                                    + resPath.replace('\\', '/')
272:                                    + "/JarIndex/hyts_22.jar!/bpack/"));
273:                    resourcesFound = resourcesFound
274:                            && url2.equals(new URL("jar:file:/"
275:                                    + resPath.replace('\\', '/')
276:                                    + "/JarIndex/hyts_23.jar!/bpack/"));
277:                    if (en.hasMoreElements()) {
278:                        resourcesFound = false;
279:                    }
280:                } catch (NoSuchElementException e) {
281:                    resourcesFound = false;
282:                }
283:                assertTrue("Resources not found (1)", resourcesFound);
284:
285:                Class c2 = Class.forName("bpack.Homer", true, ucl);
286:                assertNotNull(c2);
287:
288:                try {
289:                    Class.forName("bpack.Bart", true, ucl);
290:                    fail("InvalidJarIndexException should be thrown");
291:                } catch (InvalidJarIndexException e) {
292:                    // expected
293:                }
294:
295:                try {
296:                    Class.forName("Main4", true, ucl);
297:                    fail("ClassNotFoundException should be thrown");
298:                } catch (ClassNotFoundException e) {
299:                    // Expected
300:                }
301:
302:                Support_Resources.copyFile(resources, "JarIndex",
303:                        "hyts_22-new.jar");
304:                urls[0] = new URL("file:/" + resPath
305:                        + "/JarIndex/hyts_22-new.jar");
306:                ucl = URLClassLoader.newInstance(urls, null);
307:                assertNotNull("Cannot find resource", ucl
308:                        .findResource("cpack/"));
309:                Support_Resources
310:                        .copyFile(resources, "JarIndex", "hyts_11.jar");
311:                urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_31.jar");
312:                ucl = URLClassLoader.newInstance(urls, null);
313:
314:                try {
315:                    Class.forName("cpack.Mock", true, ucl);
316:                    fail("ClassNotFoundException should be thrown");
317:                } catch (ClassNotFoundException e) {
318:                    // Expected
319:                }
320:
321:                // testing circular reference
322:                Support_Resources
323:                        .copyFile(resources, "JarIndex", "hyts_41.jar");
324:                Support_Resources
325:                        .copyFile(resources, "JarIndex", "hyts_42.jar");
326:                urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_41.jar");
327:                ucl = URLClassLoader.newInstance(urls, null);
328:                en = ucl.findResources("bpack/");
329:                resourcesFound = resourcesFound
330:                        && ((URL) en.nextElement()).equals(new URL("jar:file:/"
331:                                + resPath.replace('\\', '/')
332:                                + "/JarIndex/hyts_42.jar!/bpack/"));
333:                assertTrue("Resources not found (2)", resourcesFound);
334:                assertFalse("No more resources expected", en.hasMoreElements());
335:
336:                // Regression test for HARMONY-2357.
337:                try {
338:                    URLClassLoaderExt cl = new URLClassLoaderExt(new URL[557]);
339:                    cl.findClass("0");
340:                    fail("NullPointerException should be thrown");
341:                } catch (NullPointerException npe) {
342:                    // Expected
343:                }
344:
345:                // Regression test for HARMONY-2871.
346:                URLClassLoader cl = new URLClassLoader(new URL[] { new URL(
347:                        "file:/foo.jar") });
348:
349:                try {
350:                    Class.forName("foo.Foo", false, cl);
351:                } catch (Exception ex) {
352:                    // Don't care
353:                }
354:
355:                try {
356:                    Class.forName("foo.Foo", false, cl);
357:                    fail("NullPointerException should be thrown");
358:                } catch (ClassNotFoundException cnfe) {
359:                    // Expected
360:                }
361:            }
362:
363:            /**
364:             * @tests java.net.URLClassLoader#findResource(java.lang.String)
365:             */
366:            public void test_findResourceLjava_lang_String()
367:                    throws MalformedURLException {
368:                URL res = null;
369:
370:                URL[] urls = new URL[2];
371:                urls[0] = new URL("http://" + Support_Configuration.HomeAddress);
372:                urls[1] = new URL(Support_Resources.getResourceURL("/"));
373:                ucl = new URLClassLoader(urls);
374:                res = ucl.findResource("RESOURCE.TXT");
375:                assertNotNull("Failed to locate resource", res);
376:
377:                StringBuffer sb = new StringBuffer();
378:                try {
379:                    java.io.InputStream is = res.openStream();
380:
381:                    int c;
382:                    while ((c = is.read()) != -1) {
383:                        sb.append((char) c);
384:                    }
385:                    is.close();
386:                } catch (IOException e) {
387:                }
388:                assertTrue("Returned incorrect resource", !sb.toString()
389:                        .equals("This is a test resource file"));
390:            }
391:
392:            public void testFindResource_H3461() throws Exception {
393:                File userDir = new File(System.getProperty("user.dir"));
394:                File dir = new File(userDir, "encode#me");
395:                File f, f2;
396:                URLClassLoader loader;
397:                URL dirUrl;
398:
399:                if (!dir.exists()) {
400:                    dir.mkdir();
401:                }
402:                dir.deleteOnExit();
403:                dirUrl = dir.toURI().toURL();
404:                loader = new URLClassLoader(new URL[] { dirUrl });
405:
406:                f = File.createTempFile("temp", ".dat", dir);
407:                f.deleteOnExit();
408:                f2 = File.createTempFile("bad#name#", ".dat", dir);
409:                f2.deleteOnExit();
410:
411:                assertNotNull(
412:                        "Unable to load resource from path with problematic name",
413:                        loader.getResource(f.getName()));
414:                assertEquals("URL was not correctly encoded", f2.toURI()
415:                        .toURL(), loader.getResource(f2.getName()));
416:            }
417:
418:            /**
419:             * @tests java.net.URLClassLoader#getResource(java.lang.String)
420:             */
421:            public void test_getResourceLjava_lang_String()
422:                    throws MalformedURLException {
423:                URL url1 = new URL("file:///");
424:                URLClassLoader loader = new URLClassLoader(new URL[] { url1 },
425:                        null);
426:                long start = System.currentTimeMillis();
427:                // try without the leading /
428:                URL result = loader.getResource("dir1/file1");
429:                long end = System.currentTimeMillis();
430:                long time = end - start;
431:                if (time < 100) {
432:                    time = 100;
433:                }
434:
435:                start = System.currentTimeMillis();
436:                // try with the leading forward slash
437:                result = loader.getResource("/dir1/file1");
438:                end = System.currentTimeMillis();
439:                long uncTime = end - start;
440:                assertTrue("too long. UNC path formed? UNC time: " + uncTime
441:                        + " regular time: " + time, uncTime <= (time * 4));
442:            }
443:
444:            /**
445:             * Regression for Harmony-2237
446:             */
447:            public void test_getResource() throws Exception {
448:                URLClassLoader urlLoader = getURLClassLoader();
449:                assertNull(urlLoader.findResource("XXX")); //$NON-NLS-1$
450:            }
451:
452:            private static URLClassLoader getURLClassLoader() {
453:                String classPath = System.getProperty("java.class.path");
454:                StringTokenizer tok = new StringTokenizer(classPath,
455:                        File.pathSeparator);
456:                Vector<URL> urlVec = new Vector<URL>();
457:                String resPackage = Support_Resources.RESOURCE_PACKAGE;
458:                try {
459:                    while (tok.hasMoreTokens()) {
460:                        String path = tok.nextToken();
461:                        String url;
462:                        if (new File(path).isDirectory())
463:                            url = "file:" + path + resPackage + "subfolder/";
464:                        else
465:                            url = "jar:file:" + path + "!" + resPackage
466:                                    + "subfolder/";
467:                        urlVec.addElement(new URL(url));
468:                    }
469:                } catch (MalformedURLException e) {
470:                    // do nothing
471:                }
472:                URL[] urls = new URL[urlVec.size()];
473:                for (int i = 0; i < urlVec.size(); i++) {
474:                    urls[i] = urlVec.elementAt(i);
475:                }
476:                URLClassLoader loader = new URLClassLoader(urls, null);
477:                return loader;
478:            }
479:
480:            /**
481:             * Regression test for HARMONY-2255
482:             */
483:            public void test_getResourceAsStream() {
484:                InputStream in = this .getClass().getClassLoader()
485:                        .getResourceAsStream(
486:                                "tests/api/java/net/test%.properties");
487:                assertNotNull(in);
488:                in = this .getClass().getClassLoader().getResourceAsStream(
489:                        "tests/api/java/net/test%25.properties");
490:                assertNull(in);
491:            }
492:        }
w__ww___.__j___a__v___a_2__s__.__c___o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.