Source Code Cross Referenced for ClassTest.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.FileInputStream;
022:        import java.io.FileOutputStream;
023:        import java.io.InputStream;
024:        import java.io.Serializable;
025:        import java.lang.reflect.Constructor;
026:        import java.lang.reflect.Field;
027:        import java.lang.reflect.Member;
028:        import java.lang.reflect.Method;
029:        import java.lang.reflect.Modifier;
030:        import java.net.URL;
031:        import java.net.URLClassLoader;
032:        import java.security.AccessControlContext;
033:        import java.security.AccessController;
034:        import java.security.BasicPermission;
035:        import java.security.DomainCombiner;
036:        import java.security.Permission;
037:        import java.security.PrivilegedAction;
038:        import java.security.ProtectionDomain;
039:        import java.security.Security;
040:        import java.util.Arrays;
041:        import java.util.List;
042:        import java.util.Vector;
043:
044:        import tests.support.resource.Support_Resources;
045:
046:        public class ClassTest extends junit.framework.TestCase {
047:
048:            public static final String FILENAME = ClassTest.class.getPackage()
049:                    .getName().replace('.', '/')
050:                    + "/test#.properties";
051:
052:            static class StaticMember$Class {
053:                class Member2$A {
054:                }
055:            }
056:
057:            class Member$Class {
058:                class Member3$B {
059:                }
060:            }
061:
062:            public static class TestClass {
063:                @SuppressWarnings("unused")
064:                private int privField = 1;
065:
066:                public int pubField = 2;
067:
068:                private Object cValue = null;
069:
070:                public Object ack = new Object();
071:
072:                @SuppressWarnings("unused")
073:                private int privMethod() {
074:                    return 1;
075:                }
076:
077:                public int pubMethod() {
078:                    return 2;
079:                }
080:
081:                public Object cValue() {
082:                    return cValue;
083:                }
084:
085:                public TestClass() {
086:                }
087:
088:                @SuppressWarnings("unused")
089:                private TestClass(Object o) {
090:                }
091:            }
092:
093:            public static class SubTestClass extends TestClass {
094:            }
095:
096:            /**
097:             * @tests java.lang.Class#forName(java.lang.String)
098:             */
099:            public void test_forNameLjava_lang_String() throws Exception {
100:                assertSame("Class for name failed for java.lang.Object",
101:                        Object.class, Class.forName("java.lang.Object"));
102:                assertSame("Class for name failed for [[Ljava.lang.Object;",
103:                        Object[][].class, Class.forName("[[Ljava.lang.Object;"));
104:
105:                assertSame("Class for name failed for [I", int[].class, Class
106:                        .forName("[I"));
107:
108:                try {
109:                    Class.forName("int");
110:                    fail();
111:                } catch (ClassNotFoundException e) {
112:                }
113:
114:                try {
115:                    Class.forName("byte");
116:                    fail();
117:                } catch (ClassNotFoundException e) {
118:                }
119:                try {
120:                    Class.forName("char");
121:                    fail();
122:                } catch (ClassNotFoundException e) {
123:                }
124:
125:                try {
126:                    Class.forName("void");
127:                    fail();
128:                } catch (ClassNotFoundException e) {
129:                }
130:
131:                try {
132:                    Class.forName("short");
133:                    fail();
134:                } catch (ClassNotFoundException e) {
135:                }
136:                try {
137:                    Class.forName("long");
138:                    fail();
139:                } catch (ClassNotFoundException e) {
140:                }
141:
142:                try {
143:                    Class.forName("boolean");
144:                    fail();
145:                } catch (ClassNotFoundException e) {
146:                }
147:                try {
148:                    Class.forName("float");
149:                    fail();
150:                } catch (ClassNotFoundException e) {
151:                }
152:                try {
153:                    Class.forName("double");
154:                    fail();
155:                } catch (ClassNotFoundException e) {
156:                }
157:
158:                //regression test for JIRA 2162
159:                try {
160:                    Class.forName("%");
161:                    fail("should throw ClassNotFoundException.");
162:                } catch (ClassNotFoundException e) {
163:                }
164:
165:                //Regression Test for HARMONY-3332
166:                String securityProviderClassName;
167:                int count = 1;
168:                while ((securityProviderClassName = Security
169:                        .getProperty("security.provider." + count++)) != null) {
170:                    Class.forName(securityProviderClassName);
171:                }
172:            }
173:
174:            /**
175:             * @tests java.lang.Class#getClasses()
176:             */
177:            public void test_getClasses() {
178:                assertEquals("Incorrect class array returned", 2,
179:                        ClassTest.class.getClasses().length);
180:            }
181:
182:            /**
183:             * @tests java.lang.Class#getClasses()
184:             */
185:            public void test_getClasses_subtest0() {
186:                final Permission privCheckPermission = new BasicPermission(
187:                        "Privilege check") {
188:                    private static final long serialVersionUID = 1L;
189:                };
190:
191:                class MyCombiner implements  DomainCombiner {
192:                    boolean combine;
193:
194:                    public ProtectionDomain[] combine(
195:                            ProtectionDomain[] executionDomains,
196:                            ProtectionDomain[] parentDomains) {
197:                        combine = true;
198:                        return new ProtectionDomain[0];
199:                    }
200:
201:                    private boolean recurring = false;
202:
203:                    public boolean isPriviledged() {
204:                        if (recurring) {
205:                            return true;
206:                        }
207:                        try {
208:                            recurring = true;
209:                            combine = false;
210:                            try {
211:                                AccessController
212:                                        .checkPermission(privCheckPermission);
213:                            } catch (SecurityException e) {
214:                            }
215:                            return !combine;
216:                        } finally {
217:                            recurring = false;
218:                        }
219:                    }
220:                }
221:
222:                final MyCombiner combiner = new MyCombiner();
223:                class SecurityManagerCheck extends SecurityManager {
224:                    String reason;
225:
226:                    Class<?> checkClass;
227:
228:                    int checkType;
229:
230:                    int checkPermission;
231:
232:                    int checkMemberAccess;
233:
234:                    int checkPackageAccess;
235:
236:                    public void setExpected(String reason, Class<?> cls,
237:                            int type) {
238:                        this .reason = reason;
239:                        checkClass = cls;
240:                        checkType = type;
241:                        checkPermission = 0;
242:                        checkMemberAccess = 0;
243:                        checkPackageAccess = 0;
244:                    }
245:
246:                    @Override
247:                    public void checkPermission(Permission perm) {
248:                        if (combiner.isPriviledged())
249:                            return;
250:                        checkPermission++;
251:                    }
252:
253:                    @Override
254:                    public void checkMemberAccess(Class<?> cls, int type) {
255:                        if (combiner.isPriviledged())
256:                            return;
257:                        checkMemberAccess++;
258:                        assertEquals(reason + " unexpected class", checkClass,
259:                                cls);
260:                        assertEquals(reason + "unexpected type", checkType,
261:                                type);
262:                    }
263:
264:                    @Override
265:                    public void checkPackageAccess(String packageName) {
266:                        if (combiner.isPriviledged())
267:                            return;
268:                        checkPackageAccess++;
269:                        String name = checkClass.getName();
270:                        int index = name.lastIndexOf('.');
271:                        String checkPackage = name.substring(0, index);
272:                        assertEquals(reason + " unexpected package",
273:                                checkPackage, packageName);
274:                    }
275:
276:                    public void assertProperCalls() {
277:                        assertEquals(reason
278:                                + " unexpected checkPermission count", 0,
279:                                checkPermission);
280:                        assertEquals(reason
281:                                + " unexpected checkMemberAccess count", 1,
282:                                checkMemberAccess);
283:                        assertEquals(reason
284:                                + " unexpected checkPackageAccess count", 1,
285:                                checkPackageAccess);
286:                    }
287:                }
288:
289:                AccessControlContext acc = new AccessControlContext(
290:                        new ProtectionDomain[0]);
291:                AccessControlContext acc2 = new AccessControlContext(acc,
292:                        combiner);
293:
294:                PrivilegedAction<?> action = new PrivilegedAction<Object>() {
295:                    public Object run() {
296:                        File resources = Support_Resources.createTempFolder();
297:                        try {
298:                            Support_Resources.copyFile(resources, null,
299:                                    "hyts_security.jar");
300:                            File file = new File(resources.toString()
301:                                    + "/hyts_security.jar");
302:                            URL url = new URL("file:" + file.getPath());
303:                            ClassLoader loader = new URLClassLoader(
304:                                    new URL[] { url }, null);
305:                            Class<?> cls = Class.forName(
306:                                    "packB.SecurityTestSub", false, loader);
307:                            SecurityManagerCheck sm = new SecurityManagerCheck();
308:                            System.setSecurityManager(sm);
309:                            try {
310:                                sm
311:                                        .setExpected("getClasses", cls,
312:                                                Member.PUBLIC);
313:                                cls.getClasses();
314:                                sm.assertProperCalls();
315:
316:                                sm.setExpected("getDeclaredClasses", cls,
317:                                        Member.DECLARED);
318:                                cls.getDeclaredClasses();
319:                                sm.assertProperCalls();
320:
321:                                sm.setExpected("getConstructor", cls,
322:                                        Member.PUBLIC);
323:                                cls.getConstructor(new Class[0]);
324:                                sm.assertProperCalls();
325:
326:                                sm.setExpected("getConstructors", cls,
327:                                        Member.PUBLIC);
328:                                cls.getConstructors();
329:                                sm.assertProperCalls();
330:
331:                                sm.setExpected("getDeclaredConstructor", cls,
332:                                        Member.DECLARED);
333:                                cls.getDeclaredConstructor(new Class[0]);
334:                                sm.assertProperCalls();
335:
336:                                sm.setExpected("getDeclaredConstructors", cls,
337:                                        Member.DECLARED);
338:                                cls.getDeclaredConstructors();
339:                                sm.assertProperCalls();
340:
341:                                sm.setExpected("getField", cls, Member.PUBLIC);
342:                                cls.getField("publicField");
343:                                sm.assertProperCalls();
344:
345:                                sm.setExpected("getFields", cls, Member.PUBLIC);
346:                                cls.getFields();
347:                                sm.assertProperCalls();
348:
349:                                sm.setExpected("getDeclaredField", cls,
350:                                        Member.DECLARED);
351:                                cls.getDeclaredField("publicField");
352:                                sm.assertProperCalls();
353:
354:                                sm.setExpected("getDeclaredFields", cls,
355:                                        Member.DECLARED);
356:                                cls.getDeclaredFields();
357:                                sm.assertProperCalls();
358:
359:                                sm.setExpected("getDeclaredMethod", cls,
360:                                        Member.DECLARED);
361:                                cls.getDeclaredMethod("publicMethod",
362:                                        new Class[0]);
363:                                sm.assertProperCalls();
364:
365:                                sm.setExpected("getDeclaredMethods", cls,
366:                                        Member.DECLARED);
367:                                cls.getDeclaredMethods();
368:                                sm.assertProperCalls();
369:
370:                                sm.setExpected("getMethod", cls, Member.PUBLIC);
371:                                cls.getMethod("publicMethod", new Class[0]);
372:                                sm.assertProperCalls();
373:
374:                                sm
375:                                        .setExpected("getMethods", cls,
376:                                                Member.PUBLIC);
377:                                cls.getMethods();
378:                                sm.assertProperCalls();
379:
380:                                sm.setExpected("newInstance", cls,
381:                                        Member.PUBLIC);
382:                                cls.newInstance();
383:                                sm.assertProperCalls();
384:                            } finally {
385:                                System.setSecurityManager(null);
386:                            }
387:                        } catch (Exception e) {
388:                            if (e instanceof  RuntimeException)
389:                                throw (RuntimeException) e;
390:                            fail("unexpected exception: " + e);
391:                        }
392:                        return null;
393:                    }
394:                };
395:                AccessController.doPrivileged(action, acc2);
396:            }
397:
398:            /**
399:             * @tests java.lang.Class#getComponentType()
400:             */
401:            public void test_getComponentType() {
402:                assertSame("int array does not have int component type",
403:                        int.class, int[].class.getComponentType());
404:                assertSame("Object array does not have Object component type",
405:                        Object.class, Object[].class.getComponentType());
406:                assertNull("Object has non-null component type", Object.class
407:                        .getComponentType());
408:            }
409:
410:            /**
411:             * @tests java.lang.Class#getConstructor(java.lang.Class[])
412:             */
413:            public void test_getConstructor$Ljava_lang_Class()
414:                    throws NoSuchMethodException {
415:                TestClass.class.getConstructor(new Class[0]);
416:                try {
417:                    TestClass.class.getConstructor(Object.class);
418:                    fail("Found private constructor");
419:                } catch (NoSuchMethodException e) {
420:                    // Correct - constructor with obj is private
421:                }
422:            }
423:
424:            /**
425:             * @tests java.lang.Class#getConstructors()
426:             */
427:            public void test_getConstructors() throws Exception {
428:                Constructor[] c = TestClass.class.getConstructors();
429:                assertEquals("Incorrect number of constructors returned", 1,
430:                        c.length);
431:            }
432:
433:            /**
434:             * @tests java.lang.Class#getDeclaredClasses()
435:             */
436:            public void test_getDeclaredClasses() {
437:                assertEquals("Incorrect class array returned", 2,
438:                        ClassTest.class.getClasses().length);
439:            }
440:
441:            /**
442:             * @tests java.lang.Class#getDeclaredConstructor(java.lang.Class[])
443:             */
444:            public void test_getDeclaredConstructor$Ljava_lang_Class()
445:                    throws Exception {
446:                Constructor<TestClass> c = TestClass.class
447:                        .getDeclaredConstructor(new Class[0]);
448:                assertNull("Incorrect constructor returned", c.newInstance()
449:                        .cValue());
450:                c = TestClass.class.getDeclaredConstructor(Object.class);
451:            }
452:
453:            /**
454:             * @tests java.lang.Class#getDeclaredConstructors()
455:             */
456:            public void test_getDeclaredConstructors() throws Exception {
457:                Constructor[] c = TestClass.class.getDeclaredConstructors();
458:                assertEquals("Incorrect number of constructors returned", 2,
459:                        c.length);
460:            }
461:
462:            /**
463:             * @tests java.lang.Class#getDeclaredField(java.lang.String)
464:             */
465:            public void test_getDeclaredFieldLjava_lang_String()
466:                    throws Exception {
467:                Field f = TestClass.class.getDeclaredField("pubField");
468:                assertEquals("Returned incorrect field", 2, f
469:                        .getInt(new TestClass()));
470:            }
471:
472:            /**
473:             * @tests java.lang.Class#getDeclaredFields()
474:             */
475:            public void test_getDeclaredFields() throws Exception {
476:                Field[] f = TestClass.class.getDeclaredFields();
477:                assertEquals("Returned incorrect number of fields", 4, f.length);
478:                f = SubTestClass.class.getDeclaredFields();
479:                // Declared fields do not include inherited
480:                assertEquals("Returned incorrect number of fields", 0, f.length);
481:            }
482:
483:            /**
484:             * @tests java.lang.Class#getDeclaredMethod(java.lang.String,
485:             *        java.lang.Class[])
486:             */
487:            public void test_getDeclaredMethodLjava_lang_String$Ljava_lang_Class()
488:                    throws Exception {
489:                Method m = TestClass.class.getDeclaredMethod("pubMethod",
490:                        new Class[0]);
491:                assertEquals("Returned incorrect method", 2, ((Integer) (m
492:                        .invoke(new TestClass()))).intValue());
493:                m = TestClass.class.getDeclaredMethod("privMethod",
494:                        new Class[0]);
495:            }
496:
497:            /**
498:             * @tests java.lang.Class#getDeclaredMethods()
499:             */
500:            public void test_getDeclaredMethods() throws Exception {
501:                Method[] m = TestClass.class.getDeclaredMethods();
502:                assertEquals("Returned incorrect number of methods", 3,
503:                        m.length);
504:                m = SubTestClass.class.getDeclaredMethods();
505:                assertEquals("Returned incorrect number of methods", 0,
506:                        m.length);
507:            }
508:
509:            /**
510:             * @tests java.lang.Class#getDeclaringClass()
511:             */
512:            public void test_getDeclaringClass() {
513:                assertEquals(ClassTest.class, TestClass.class
514:                        .getDeclaringClass());
515:            }
516:
517:            /**
518:             * @tests java.lang.Class#getField(java.lang.String)
519:             */
520:            public void test_getFieldLjava_lang_String() throws Exception {
521:                Field f = TestClass.class.getField("pubField");
522:                assertEquals("Returned incorrect field", 2, f
523:                        .getInt(new TestClass()));
524:                try {
525:                    f = TestClass.class.getField("privField");
526:                    fail("Private field access failed to throw exception");
527:                } catch (NoSuchFieldException e) {
528:                    // Correct
529:                }
530:            }
531:
532:            /**
533:             * @tests java.lang.Class#getFields()
534:             */
535:            public void test_getFields() throws Exception {
536:                Field[] f = TestClass.class.getFields();
537:                assertEquals("Incorrect number of fields", 2, f.length);
538:                f = SubTestClass.class.getFields();
539:                // Check inheritance of pub fields
540:                assertEquals("Incorrect number of fields", 2, f.length);
541:            }
542:
543:            /**
544:             * @tests java.lang.Class#getInterfaces()
545:             */
546:            public void test_getInterfaces() {
547:                Class[] interfaces;
548:                List<?> interfaceList;
549:                interfaces = Object.class.getInterfaces();
550:                assertEquals("Incorrect interface list for Object", 0,
551:                        interfaces.length);
552:                interfaceList = Arrays.asList(Vector.class.getInterfaces());
553:                assertTrue("Incorrect interface list for Vector", interfaceList
554:                        .contains(Cloneable.class)
555:                        && interfaceList.contains(Serializable.class)
556:                        && interfaceList.contains(List.class));
557:            }
558:
559:            /**
560:             * @tests java.lang.Class#getMethod(java.lang.String, java.lang.Class[])
561:             */
562:            public void test_getMethodLjava_lang_String$Ljava_lang_Class()
563:                    throws Exception {
564:                Method m = TestClass.class.getMethod("pubMethod", new Class[0]);
565:                assertEquals("Returned incorrect method", 2, ((Integer) (m
566:                        .invoke(new TestClass()))).intValue());
567:                try {
568:                    m = TestClass.class.getMethod("privMethod", new Class[0]);
569:                    fail("Failed to throw exception accessing private method");
570:                } catch (NoSuchMethodException e) {
571:                    // Correct
572:                    return;
573:                }
574:            }
575:
576:            /**
577:             * @tests java.lang.Class#getMethods()
578:             */
579:            public void test_getMethods() throws Exception {
580:                Method[] m = TestClass.class.getMethods();
581:                assertEquals("Returned incorrect number of methods",
582:                        2 + Object.class.getMethods().length, m.length);
583:                m = SubTestClass.class.getMethods();
584:                assertEquals("Returned incorrect number of sub-class methods",
585:                        2 + Object.class.getMethods().length, m.length);
586:                // Number of inherited methods
587:            }
588:
589:            private static final class PrivateClass {
590:            }
591:
592:            /**
593:             * @tests java.lang.Class#getModifiers()
594:             */
595:            public void test_getModifiers() {
596:                int dcm = PrivateClass.class.getModifiers();
597:                assertFalse("default class is public", Modifier.isPublic(dcm));
598:                assertFalse("default class is protected", Modifier
599:                        .isProtected(dcm));
600:                assertTrue("default class is not private", Modifier
601:                        .isPrivate(dcm));
602:
603:                int ocm = Object.class.getModifiers();
604:                assertTrue("public class is not public", Modifier.isPublic(ocm));
605:                assertFalse("public class is protected", Modifier
606:                        .isProtected(ocm));
607:                assertFalse("public class is private", Modifier.isPrivate(ocm));
608:            }
609:
610:            /**
611:             * @tests java.lang.Class#getName()
612:             */
613:            public void test_getName() throws Exception {
614:                String className = Class.forName("java.lang.Object").getName();
615:                assertNotNull(className);
616:
617:                assertEquals("Class getName printed wrong value",
618:                        "java.lang.Object", className);
619:                assertEquals("Class getName printed wrong value", "int",
620:                        int.class.getName());
621:                className = Class.forName("[I").getName();
622:                assertNotNull(className);
623:                assertEquals("Class getName printed wrong value", "[I",
624:                        className);
625:
626:                className = Class.forName("[Ljava.lang.Object;").getName();
627:                assertNotNull(className);
628:
629:                assertEquals("Class getName printed wrong value",
630:                        "[Ljava.lang.Object;", className);
631:            }
632:
633:            /**
634:             * @tests java.lang.Class#getResource(java.lang.String)
635:             */
636:            public void test_getResourceLjava_lang_String() {
637:                final String name = "/org/apache/harmony/luni/tests/test_resource.txt";
638:                URL res = getClass().getResource(name);
639:                assertNotNull(res);
640:            }
641:
642:            /**
643:             * @tests java.lang.Class#getResourceAsStream(java.lang.String)
644:             */
645:            public void test_getResourceAsStreamLjava_lang_String()
646:                    throws Exception {
647:                final String name = "/org/apache/harmony/luni/tests/test_resource.txt";
648:                assertNotNull("the file " + name
649:                        + " can not be found in this directory", getClass()
650:                        .getResourceAsStream(name));
651:
652:                final String nameBadURI = "org/apache/harmony/luni/tests/test_resource.txt";
653:                assertNull("the file " + nameBadURI
654:                        + " should not be found in this directory", getClass()
655:                        .getResourceAsStream(nameBadURI));
656:
657:                InputStream str = Object.class
658:                        .getResourceAsStream("Class.class");
659:                assertNotNull(
660:                        "java.lang.Object couldn't find its class with getResource...",
661:                        str);
662:
663:                assertTrue("Cannot read single byte", str.read() != -1);
664:                assertEquals("Cannot read multiple bytes", 5, str
665:                        .read(new byte[5]));
666:                str.close();
667:
668:                InputStream str2 = getClass().getResourceAsStream(
669:                        "ClassTest.class");
670:                assertNotNull("Can't find resource", str2);
671:                assertTrue("Cannot read single byte", str2.read() != -1);
672:                assertEquals("Cannot read multiple bytes", 5, str2
673:                        .read(new byte[5]));
674:                str2.close();
675:            }
676:
677:            /**
678:             * @tests java.lang.Class#getSuperclass()
679:             */
680:            public void test_getSuperclass() {
681:                assertNull("Object has a superclass???", Object.class
682:                        .getSuperclass());
683:                assertSame("Normal class has bogus superclass",
684:                        InputStream.class, FileInputStream.class
685:                                .getSuperclass());
686:                assertSame("Array class has bogus superclass", Object.class,
687:                        FileInputStream[].class.getSuperclass());
688:                assertNull("Base class has a superclass", int.class
689:                        .getSuperclass());
690:                assertNull("Interface class has a superclass", Cloneable.class
691:                        .getSuperclass());
692:            }
693:
694:            /**
695:             * @tests java.lang.Class#isArray()
696:             */
697:            public void test_isArray() throws ClassNotFoundException {
698:                assertTrue("Non-array type claims to be.", !int.class.isArray());
699:                Class<?> clazz = null;
700:                clazz = Class.forName("[I");
701:                assertTrue("int Array type claims not to be.", clazz.isArray());
702:
703:                clazz = Class.forName("[Ljava.lang.Object;");
704:                assertTrue("Object Array type claims not to be.", clazz
705:                        .isArray());
706:
707:                clazz = Class.forName("java.lang.Object");
708:                assertTrue("Non-array Object type claims to be.", !clazz
709:                        .isArray());
710:            }
711:
712:            /**
713:             * @tests java.lang.Class#isAssignableFrom(java.lang.Class)
714:             */
715:            public void test_isAssignableFromLjava_lang_Class() {
716:                Class<?> clazz1 = null;
717:                Class<?> clazz2 = null;
718:
719:                clazz1 = Object.class;
720:                clazz2 = Class.class;
721:                assertTrue("returned false for superclass", clazz1
722:                        .isAssignableFrom(clazz2));
723:
724:                clazz1 = TestClass.class;
725:                assertTrue("returned false for same class", clazz1
726:                        .isAssignableFrom(clazz1));
727:
728:                clazz1 = Runnable.class;
729:                clazz2 = Thread.class;
730:                assertTrue("returned false for implemented interface", clazz1
731:                        .isAssignableFrom(clazz2));
732:            }
733:
734:            /**
735:             * @tests java.lang.Class#isInterface()
736:             */
737:            public void test_isInterface() throws ClassNotFoundException {
738:                assertTrue("Prim type claims to be interface.", !int.class
739:                        .isInterface());
740:                Class<?> clazz = null;
741:                clazz = Class.forName("[I");
742:                assertTrue("Prim Array type claims to be interface.", !clazz
743:                        .isInterface());
744:
745:                clazz = Class.forName("java.lang.Runnable");
746:                assertTrue("Interface type claims not to be interface.", clazz
747:                        .isInterface());
748:                clazz = Class.forName("java.lang.Object");
749:                assertTrue("Object type claims to be interface.", !clazz
750:                        .isInterface());
751:
752:                clazz = Class.forName("[Ljava.lang.Object;");
753:                assertTrue("Array type claims to be interface.", !clazz
754:                        .isInterface());
755:            }
756:
757:            /**
758:             * @tests java.lang.Class#isPrimitive()
759:             */
760:            public void test_isPrimitive() {
761:                assertFalse("Interface type claims to be primitive.",
762:                        Runnable.class.isPrimitive());
763:                assertFalse("Object type claims to be primitive.", Object.class
764:                        .isPrimitive());
765:                assertFalse("Prim Array type claims to be primitive.",
766:                        int[].class.isPrimitive());
767:                assertFalse("Array type claims to be primitive.",
768:                        Object[].class.isPrimitive());
769:                assertTrue("Prim type claims not to be primitive.", int.class
770:                        .isPrimitive());
771:                assertFalse("Object type claims to be primitive.", Object.class
772:                        .isPrimitive());
773:            }
774:
775:            /**
776:             * @tests java.lang.Class#newInstance()
777:             */
778:            public void test_newInstance() throws Exception {
779:                Class<?> clazz = null;
780:                clazz = Class.forName("java.lang.Object");
781:                assertNotNull("new object instance was null", clazz
782:                        .newInstance());
783:
784:                clazz = Class.forName("java.lang.Throwable");
785:                assertSame("new Throwable instance was not a throwable", clazz,
786:                        clazz.newInstance().getClass());
787:
788:                clazz = Class.forName("java.lang.Integer");
789:                try {
790:                    clazz.newInstance();
791:                    fail("Exception for instantiating a newInstance with no default constructor is not thrown");
792:                } catch (InstantiationException e) {
793:                    // expected
794:                }
795:            }
796:
797:            /**
798:             * @tests java.lang.Class#toString()
799:             */
800:            public void test_toString() throws ClassNotFoundException {
801:                assertEquals("Class toString printed wrong value", "int",
802:                        int.class.toString());
803:                Class<?> clazz = null;
804:                clazz = Class.forName("[I");
805:                assertEquals("Class toString printed wrong value", "class [I",
806:                        clazz.toString());
807:
808:                clazz = Class.forName("java.lang.Object");
809:                assertEquals("Class toString printed wrong value",
810:                        "class java.lang.Object", clazz.toString());
811:
812:                clazz = Class.forName("[Ljava.lang.Object;");
813:                assertEquals("Class toString printed wrong value",
814:                        "class [Ljava.lang.Object;", clazz.toString());
815:            }
816:
817:            // Regression Test for JIRA-2047
818:            public void test_getResourceAsStream_withSharpChar()
819:                    throws Exception {
820:                InputStream in = getClass().getResourceAsStream("/" + FILENAME);
821:                assertNotNull(in);
822:                in.close();
823:
824:                in = getClass().getResourceAsStream(FILENAME);
825:                assertNull(in);
826:
827:                in = this .getClass().getClassLoader().getResourceAsStream(
828:                        FILENAME);
829:                assertNotNull(in);
830:                in.close();
831:            }
832:
833:            /*
834:             * Regression test for HARMONY-2644:
835:             * Load system and non-system array classes via Class.forName()
836:             */
837:            public void test_forName_arrays() throws Exception {
838:                Class c1 = getClass();
839:                String s = c1.getName();
840:                Class a1 = Class.forName("[L" + s + ";");
841:                Class a2 = Class.forName("[[L" + s + ";");
842:                assertSame(c1, a1.getComponentType());
843:                assertSame(a1, a2.getComponentType());
844:                Class l4 = Class.forName("[[[[[J");
845:                assertSame(long[][][][][].class, l4);
846:
847:                try {
848:                    System.out.println(Class.forName("[;"));
849:                    fail("1");
850:                } catch (ClassNotFoundException ok) {
851:                }
852:                try {
853:                    System.out.println(Class.forName("[["));
854:                    fail("2");
855:                } catch (ClassNotFoundException ok) {
856:                }
857:                try {
858:                    System.out.println(Class.forName("[L"));
859:                    fail("3");
860:                } catch (ClassNotFoundException ok) {
861:                }
862:                try {
863:                    System.out.println(Class.forName("[L;"));
864:                    fail("4");
865:                } catch (ClassNotFoundException ok) {
866:                }
867:                try {
868:                    System.out.println(Class.forName(";"));
869:                    fail("5");
870:                } catch (ClassNotFoundException ok) {
871:                }
872:                try {
873:                    System.out.println(Class.forName(""));
874:                    fail("6");
875:                } catch (ClassNotFoundException ok) {
876:                }
877:            }
878:        }
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.