Source Code Cross Referenced for ClassLoaderTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » lang » 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.lang 
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.lang;
019:
020:        import java.io.File;
021:        import java.io.FileOutputStream;
022:        import java.io.InputStream;
023:        import java.security.CodeSource;
024:        import java.security.Permission;
025:        import java.security.PermissionCollection;
026:        import java.security.Policy;
027:        import java.security.ProtectionDomain;
028:        import java.security.SecurityPermission;
029:        import junit.framework.TestCase;
030:
031:        public class ClassLoaderTest extends TestCase {
032:
033:            public static volatile int flag;
034:
035:            /**
036:             * Tests that Classloader.defineClass() assigns appropriate 
037:             * default domains to the defined classes.
038:             */
039:            public void test_defineClass_defaultDomain() throws Exception {
040:                // Regression for HARMONY-765 
041:                DynamicPolicy plc = new DynamicPolicy();
042:                Policy back = Policy.getPolicy();
043:                try {
044:                    Policy.setPolicy(plc);
045:
046:                    Class<?> a = new Ldr().define();
047:
048:                    Permission p = new SecurityPermission("abc");
049:                    assertFalse("impossible! misconfiguration?", a
050:                            .getProtectionDomain().implies(p));
051:
052:                    plc.pc = p.newPermissionCollection();
053:                    plc.pc.add(p);
054:                    assertTrue("default domain is not dynamic", a
055:                            .getProtectionDomain().implies(p));
056:                } finally {
057:                    Policy.setPolicy(back);
058:                }
059:            }
060:
061:            static class SyncTestClassLoader extends ClassLoader {
062:                Object lock;
063:                volatile int numFindClassCalled;
064:
065:                SyncTestClassLoader(Object o) {
066:                    this .lock = o;
067:                    numFindClassCalled = 0;
068:                }
069:
070:                /*
071:                 * Byte array of bytecode equivalent to the following source code:
072:                 * public class TestClass {
073:                 * }
074:                 */
075:                private byte[] classData = new byte[] { -54, -2, -70, -66, 0,
076:                        0, 0, 49, 0, 13, 10, 0, 3, 0, 10, 7, 0, 11, 7, 0, 12,
077:                        1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41,
078:                        86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110,
079:                        101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101,
080:                        1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108,
081:                        101, 1, 0, 14, 84, 101, 115, 116, 67, 108, 97, 115,
082:                        115, 46, 106, 97, 118, 97, 12, 0, 4, 0, 5, 1, 0, 9, 84,
083:                        101, 115, 116, 67, 108, 97, 115, 115, 1, 0, 16, 106,
084:                        97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106,
085:                        101, 99, 116, 0, 33, 0, 2, 0, 3, 0, 0, 0, 0, 0, 1, 0,
086:                        1, 0, 4, 0, 5, 0, 1, 0, 6, 0, 0, 0, 29, 0, 1, 0, 1, 0,
087:                        0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 1, 0, 7, 0, 0, 0,
088:                        6, 0, 1, 0, 0, 0, 1, 0, 1, 0, 8, 0, 0, 0, 2, 0, 9 };
089:
090:                protected Class findClass(String name)
091:                        throws ClassNotFoundException {
092:                    try {
093:                        while (flag != 2) {
094:                            synchronized (lock) {
095:                                lock.wait();
096:                            }
097:                        }
098:                    } catch (InterruptedException ie) {
099:                    }
100:
101:                    if (name.equals("TestClass")) {
102:                        numFindClassCalled++;
103:                        return defineClass(null, classData, 0, classData.length);
104:                    } else {
105:                        throw new ClassNotFoundException("Class " + name
106:                                + " not found.");
107:                    }
108:                }
109:            }
110:
111:            static class SyncLoadTestThread extends Thread {
112:                volatile boolean started;
113:                ClassLoader cl;
114:                Class cls;
115:
116:                SyncLoadTestThread(ClassLoader cl) {
117:                    this .cl = cl;
118:                }
119:
120:                public void run() {
121:                    try {
122:                        started = true;
123:                        cls = Class.forName("TestClass", false, cl);
124:                    } catch (Exception ex) {
125:                        ex.printStackTrace();
126:                    }
127:                }
128:            }
129:
130:            /**
131:             * Regression test for HARMONY-1939:
132:             * 2 threads simultaneously run Class.forName() method for the same classname 
133:             * and the same classloader. It is expected that both threads succeed but
134:             * class must be defined just once.  
135:             */
136:            public void test_loadClass_concurrentLoad() throws Exception {
137:                Object lock = new Object();
138:                SyncTestClassLoader cl = new SyncTestClassLoader(lock);
139:                SyncLoadTestThread tt1 = new SyncLoadTestThread(cl);
140:                SyncLoadTestThread tt2 = new SyncLoadTestThread(cl);
141:                flag = 1;
142:                tt1.start();
143:                tt2.start();
144:
145:                while (!tt1.started && !tt2.started) {
146:                    Thread.sleep(100);
147:                }
148:
149:                flag = 2;
150:                synchronized (lock) {
151:                    lock.notifyAll();
152:                }
153:                tt1.join();
154:                tt2.join();
155:
156:                assertSame("Bad or redefined class", tt1.cls, tt2.cls);
157:                assertEquals("Both threads tried to define class", 1,
158:                        cl.numFindClassCalled);
159:            }
160:
161:            /**
162:             * @tests java.lang.ClassLoader#getResource(java.lang.String)
163:             */
164:            public void test_getResourceLjava_lang_String() {
165:                // Test for method java.net.URL
166:                // java.lang.ClassLoader.getResource(java.lang.String)
167:                java.net.URL u = ClassLoader.getSystemClassLoader()
168:                        .getResource("hyts_Foo.c");
169:                assertNotNull("Unable to find resource", u);
170:                java.io.InputStream is = null;
171:                try {
172:                    is = u.openStream();
173:                    assertNotNull("Resource returned is invalid", is);
174:                    is.close();
175:                } catch (java.io.IOException e) {
176:                    fail("IOException getting stream for resource : "
177:                            + e.getMessage());
178:                }
179:            }
180:
181:            /**
182:             * @tests java.lang.ClassLoader#getResourceAsStream(java.lang.String)
183:             */
184:            public void test_getResourceAsStreamLjava_lang_String() {
185:                // Test for method java.io.InputStream
186:                // java.lang.ClassLoader.getResourceAsStream(java.lang.String)
187:                // Need better test...
188:
189:                java.io.InputStream is = null;
190:                assertNotNull("Failed to find resource: hyts_Foo.c",
191:                        (is = ClassLoader.getSystemClassLoader()
192:                                .getResourceAsStream("hyts_Foo.c")));
193:                try {
194:                    is.close();
195:                } catch (java.io.IOException e) {
196:                    fail("Exception during getResourceAsStream: "
197:                            + e.toString());
198:                }
199:            }
200:
201:            /**
202:             * @tests java.lang.ClassLoader#getSystemClassLoader()
203:             */
204:            public void test_getSystemClassLoader() {
205:                // Test for method java.lang.ClassLoader
206:                // java.lang.ClassLoader.getSystemClassLoader()
207:                ClassLoader cl = ClassLoader.getSystemClassLoader();
208:                java.io.InputStream is = cl.getResourceAsStream("hyts_Foo.c");
209:                assertNotNull("Failed to find resource from system classpath",
210:                        is);
211:                try {
212:                    is.close();
213:                } catch (java.io.IOException e) {
214:                }
215:
216:            }
217:
218:            /**
219:             * @tests java.lang.ClassLoader#getSystemResource(java.lang.String)
220:             */
221:            public void test_getSystemResourceLjava_lang_String() {
222:                // Test for method java.net.URL
223:                // java.lang.ClassLoader.getSystemResource(java.lang.String)
224:                // Need better test...
225:                assertNotNull("Failed to find resource: hyts_Foo.c",
226:                        ClassLoader.getSystemResource("hyts_Foo.c"));
227:            }
228:
229:            //Regression Test for JIRA-2047
230:            public void test_getResourceAsStream_withSharpChar()
231:                    throws Exception {
232:                InputStream in = this .getClass().getClassLoader()
233:                        .getResourceAsStream(ClassTest.FILENAME);
234:                assertNotNull(in);
235:                in.close();
236:            }
237:        }
238:
239:        class DynamicPolicy extends Policy {
240:
241:            public PermissionCollection pc;
242:
243:            @Override
244:            public PermissionCollection getPermissions(ProtectionDomain pd) {
245:                return pc;
246:            }
247:
248:            @Override
249:            public PermissionCollection getPermissions(CodeSource codesource) {
250:                return pc;
251:            }
252:
253:            @Override
254:            public void refresh() {
255:            }
256:        }
257:
258:        class A {
259:        }
260:
261:        class Ldr extends ClassLoader {
262:            @SuppressWarnings("deprecation")
263:            public Class<?> define() throws Exception {
264:                Package p = getClass().getPackage();
265:                String path = p == null ? "" : p.getName().replace('.',
266:                        File.separatorChar)
267:                        + File.separator;
268:                InputStream is = getResourceAsStream(path + "A.class");
269:                byte[] buf = new byte[512];
270:                int len = is.read(buf);
271:                return defineClass(buf, 0, len);
272:            }
273:        }
ww__w__.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.