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: /**
019: * @author Serguei S.Zapreyev
020: * @version $Revision$
021: */package java.lang;
022:
023: import junit.framework.TestCase;
024:
025: import java.lang.annotation.Documented;
026: import java.lang.annotation.Retention;
027: import java.lang.annotation.Target;
028:
029: /*
030: * Created on 28.05.2005
031: *
032: * This Class1_5Test class is used to test the extention of the
033: * Core API Class class to 1.5
034: *
035: */
036:
037: @Documented
038: @Retention(value=java.lang.annotation.RetentionPolicy.RUNTIME)
039: @Target(value=java.lang.annotation.ElementType.TYPE)
040: @interface zzz {
041: public int id() default 777;
042:
043: public String date() default "[unimplemented]";
044: }
045:
046: public class Class1_5Test extends TestCase {
047:
048: protected void setUp() throws Exception {
049: }
050:
051: protected void tearDown() throws Exception {
052: }
053:
054: /**
055: *
056: */
057: public void test_isAnonymousClass_V() {
058: class e {
059: }
060: ;//Class1_5Test$1e.class
061: assertFalse("Non-anonymous class is indicated as anonymous!",
062: (new e()).getClass().isAnonymousClass());
063: assertTrue("anonymous class is indicated as non-anonymous!",
064: (new e() {
065: int i;
066:
067: int m1() {
068: return i + m2();
069: };
070:
071: int m2() {
072: return i + m1();
073: }
074: }).getClass().isAnonymousClass());
075: }
076:
077: /**
078: *
079: */
080: enum x {
081: aaa;
082: };
083:
084: public void test_isEnum_V() {
085: class e {
086: }
087: ;
088: assertFalse("Non-enum class is indicated as enum!", e.class
089: .isEnum());
090: assertTrue("enum class is indicated as non-enum!", x.class
091: .isEnum());
092: }
093:
094: /**
095: *
096: */
097: public void test_isLocalClass_V() {
098: class e {
099: }
100: ;
101: assertFalse("Non-local class is indicated as local!", x.class
102: .isLocalClass());
103: assertTrue("local class is indicated as non-local!", e.class
104: .isLocalClass());
105: }
106:
107: /**
108: *
109: */
110: public void test_isMemberClass_V() {
111: class e {
112: }
113: ;
114: assertFalse("Non-member class is indicated as member!", e.class
115: .isMemberClass());
116: assertTrue("member class is indicated as non-member!", x.class
117: .isMemberClass());
118: }
119:
120: /**
121: *
122: */
123: public void test_isSynthetic_V() {
124: class e {
125: }
126: ;
127: assertFalse("Non-synthetic class is indicated as synthetic!",
128: e.class.isSynthetic());
129: //assertTrue("synthetic class is indicated as non-synthetic!",
130: // (new e() { int i; }).getClass().isSynthetic());
131: }
132:
133: /**
134: *
135: */
136: public void test_isAnnotation_V() {
137: class e {
138: }
139: ;
140: assertFalse("Non-annotation class is indicated as annotation!",
141: e.class.isAnnotation());
142: assertTrue("annotation class is indicated as non-annotation!",
143: zzz.class.isAnnotation());
144: }
145:
146: /**
147: *
148: */
149: public void test_isAnnotationPresent_Cla() {
150: class e {
151: }
152: ;
153: assertFalse("zzz annotation is not presented for e class!",
154: e.class.isAnnotationPresent(zzz.class));
155: assertFalse("zzz annotation is not presented for zzz class!",
156: zzz.class.isAnnotationPresent(zzz.class));
157: assertTrue(
158: "Target annotation is presented for zzz class!",
159: zzz.class
160: .isAnnotationPresent(java.lang.annotation.Target.class));
161: assertTrue(
162: "Documented annotation is presented for zzz class!",
163: zzz.class
164: .isAnnotationPresent(java.lang.annotation.Documented.class));
165: assertTrue(
166: "Retention annotation is presented for zzz class!",
167: zzz.class
168: .isAnnotationPresent(java.lang.annotation.Retention.class));
169: }
170:
171: /**
172: *
173: */
174: // enum xx{A(77),B(55),C(33);xx(int i) {this.value = i;}};
175: enum xx {
176: A, B, C;
177: };
178:
179: public void test_getEnumConstants_V() {
180: //assertTrue("FAILED: test_getEnumConstants_V.check001: Non-enum class is indicated as enum!", xx.class.getEnumConstants()[0].value() == 77);
181: assertTrue("The first constant should be xx.A!", xx.class
182: .getEnumConstants()[0] == xx.A);
183: assertTrue("The second constant should not be xx.A!", xx.class
184: .getEnumConstants()[1] != xx.A);
185: assertTrue("The third constant should be xx.C!", xx.class
186: .getEnumConstants()[2] == xx.C);
187: }
188:
189: /**
190: *
191: */
192: class e {
193: };
194:
195: //class gg {class gga {class ggb {class ggc {}; class ggg {public int id() {class f {};class ff {};class ff2 {public void vd() {Object ooo = new e() { int i; };}};return 999;}};};};};
196: public class gg {
197: public class gga {
198: public gga() {
199: };
200:
201: public class ggb {
202: public ggb() {
203: };
204:
205: class ggc {
206: };
207:
208: public class ggg {
209: public ggg() {
210: };
211:
212: public Object id() {
213: class f {
214: }
215: ;
216: class ff {
217: }
218: ;
219: class ff2 {
220: public Object vd() {
221: Object ooo = new e() {
222: int i;
223:
224: int m1() {
225: vd();
226: return i + m2();
227: };
228:
229: int m2() {
230: return i + m1();
231: }
232: };
233: return ooo;
234: }
235:
236: public Object vd2() {
237: return vd() == null ? vd3() : vd();
238: }
239:
240: public Object vd3() {
241: return vd() == null ? vd2() : vd();
242: }
243: }
244: ;
245: return new ff2();
246: }
247: };
248:
249: public Object gggO() {
250: return new ggg();
251: }
252: };
253:
254: public Object ggbO() {
255: return new ggb();
256: }
257: };
258:
259: public Object ggaO() {
260: new e() {
261: int i;
262:
263: int m1() {
264: return i + m2();
265: };
266:
267: int m2() {
268: return i + m1();
269: }
270: };
271: return new gga();
272: }
273: };
274:
275: public void test_getSimpleName_V() {
276: class e {
277: }
278: ;
279: try {
280: //##########################################################################################################
281: //##########################################################################################################
282: //\\assertTrue("FAILED: test_getSimpleName_V.check001: Simple name for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1 should be empty!", Class.forName("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1").getSimpleName().equals(""));
283: //\\assertTrue("FAILED: test_getSimpleName_V.check002: Simple name for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2 should be ff2!", Class.forName("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2").getSimpleName().equals("ff2"));
284: // to avoid bug of local classes naming in org.eclipse.jdt.core.JDTCompilerAdapter
285: // ("Class1_5Test$1ff2" instead of "Class1_5Test$gg$gga$ggb$ggg$1ff2")
286: try {
287: Object o1, o2, o3, o4, o5;
288: o1 = new gg();
289: o2 = o1.getClass().getMethod("ggaO")
290: .invoke(o1/*, null*/);
291: o3 = o2.getClass().getMethod("ggbO")
292: .invoke(o2/*, null*/);
293: o4 = o3.getClass().getMethod("gggO")
294: .invoke(o3/*, null*/);
295: //\\assertTrue("FAILED: test_getSimpleName_V.check001: Simple name for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1 should be empty!", Class.forName("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1").getSimpleName().equals(""));
296: assertTrue(
297: "check001: Simple name for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1 should be empty!",
298: (o5 = o4.getClass().getMethod("id")
299: .invoke(o4/*, null*/)).getClass()
300: .getMethod("vd").invoke(o5/*, null*/)
301: .getClass().getSimpleName().equals(""));
302: //\\assertTrue("check002: Simple name for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2 should be ff2!", Class.forName("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2").getSimpleName().equals("ff2"));
303: assertTrue(
304: "check002: Simple name for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2 should be ff2!",
305: (o5 = o4.getClass().getMethod("id")
306: .invoke(o4/*, null*/)).getClass()
307: .getMethod("vd").invoke(o5/*, null*/)
308: .getClass().getEnclosingClass()
309: .getSimpleName().equals("ff2"));
310: } catch (NoSuchMethodException e) {
311: e.printStackTrace();
312: fail("exception check001");
313: } catch (IllegalAccessException e) {
314: e.printStackTrace();
315: fail("exception check002");
316: } catch (java.lang.reflect.InvocationTargetException e) {
317: e.printStackTrace();
318: fail("exception check003");
319: }
320: //##########################################################################################################
321: //##########################################################################################################
322: assertTrue(
323: "check003: Simple name for "
324: + "java.lang.Class1_5Test$gg$gga$ggb$ggc should be ggc!",
325: Class.forName(
326: "java.lang.Class1_5Test$gg$gga$ggb$ggc")
327: .getSimpleName().equals("ggc"));
328: assertTrue("check004: Simple name for "
329: + "java.lang.Class1_5Test$1 should be empty!",
330: Class.forName("java.lang.Class1_5Test$1")
331: .getSimpleName().equals(""));
332: Class c = (new Object() {
333: int i;
334:
335: int m1() {
336: return i + m2();
337: };
338:
339: int m2() {
340: return i + m1();
341: }
342: }).getClass();
343: assertTrue("check005: Simple name for "
344: + "\"(new Object() { int i; })\" should be empty!",
345: c.getSimpleName().equals(""));
346: c = (new e() {
347: int i;
348:
349: int m1() {
350: return i + m2();
351: };
352:
353: int m2() {
354: return i + m1();
355: }
356: }).getClass();
357: assertTrue("check006: Simple name for "
358: + "\"(new e() { int i; })\" should be empty!", c
359: .getSimpleName().equals(""));
360: assertTrue("check007: Simple name for "
361: + "java.lang.Class1_5Test$1e should be e!", e.class
362: .getSimpleName().equals("e"));
363: } catch (Exception e) {
364: e.printStackTrace();
365: fail("check008: Error of the class loading!");
366: }
367: }
368:
369: /**
370: *
371: */
372: public void test_getEnclosingClass_V() {
373: class e {
374: }
375: ;
376: try {
377: //##########################################################################################################
378: //##########################################################################################################
379: // to avoid bug of local classes naming in org.eclipse.jdt.core.JDTCompilerAdapter
380: // ("Class1_5Test$1ff2" instead of "Class1_5Test$gg$gga$ggb$ggg$1ff2")
381: try {
382: Object o1, o2, o3, o4, o5;
383: o1 = new gg();
384: o2 = o1.getClass().getMethod("ggaO")
385: .invoke(o1/*, null*/);
386: o3 = o2.getClass().getMethod("ggbO")
387: .invoke(o2/*, null*/);
388: o4 = o3.getClass().getMethod("gggO")
389: .invoke(o3/*, null*/);
390: //\\//\\if(!(o5 = o4.getClass().getMethod("id").invoke(o4/*, null*/)).getClass().getMethod("vd").invoke(o5/*, null*/).getClass().getName().equals("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1")) fail("test_3, case 019 FAILED");
391: if (!(o5 = o4.getClass().getMethod("id")
392: .invoke(o4/*, null*/)).getClass().getMethod(
393: "vd").invoke(o5/*, null*/).getClass()
394: .getName().matches(
395: "java\\.lang\\.Class1_5Test.*1ff2\\$1"))
396: fail("test_3, case 019 FAILED");
397: if (!(o5 = o4.getClass().getMethod("id")
398: .invoke(o4/*, null*/)).getClass().getMethod(
399: "vd").invoke(o5/*, null*/).getClass()
400: .getEnclosingClass().getName().matches(
401: "java\\.lang\\.Class1_5Test.*1ff2"))
402: fail("test_3, case 019 FAILED");
403: //if(!(o = gg.gga.ggb.ggg.class.getMethod("id").invoke(java.lang.Class1_5Test.gg.gga.ggb.ggg.class.newInstance(), null)).getClass().getMethod("vd").invoke(o, null).getClass().getName().equals("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1")) fail("test_3, case 019 FAILED");
404: //\\//\\assertTrue("FAILED: test_getEnclosingClass_V.check001: Simple name of enclosing class for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1 should be ff2!", Class.forName("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1").getEnclosingClass().getSimpleName().equals("ff2"));
405: assertTrue(
406: "check001: Simple name of enclosing class for "
407: + "java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1"
408: + " should be ff2!", (o5 = o4
409: .getClass().getMethod("id")
410: .invoke(o4/*, null*/)).getClass()
411: .getMethod("vd").invoke(o5/*, null*/)
412: .getClass().getEnclosingClass()
413: .getSimpleName().equals("ff2"));
414: } catch (NoSuchMethodException e) {
415: e.printStackTrace();
416: fail("check003");
417: } catch (IllegalAccessException e) {
418: e.printStackTrace();
419: fail("check004");
420: } catch (java.lang.reflect.InvocationTargetException e) {
421: e.printStackTrace();
422: fail("check005");
423: }
424: //##########################################################################################################
425: //##########################################################################################################
426: } catch (Exception e) {
427: e.printStackTrace();
428: assertTrue("check002: Error of the class loading!", false);
429: }
430: }
431:
432: /**
433: *
434: */
435: public void test_getDeclaredAnnotations_V() {
436: assertFalse(
437: "Target annotation is presented for zzz class!",
438: zzz.class.getDeclaredAnnotations()[0].toString()
439: .indexOf("java.lang.annotation.Documented") == -1);
440: assertFalse(
441: "Documented annotation is presented for zzz class!",
442: zzz.class.getDeclaredAnnotations()[1].toString()
443: .indexOf("java.lang.annotation.Retention") == -1);
444: assertFalse("Retention annotation is presented for zzz class!",
445: zzz.class.getDeclaredAnnotations()[2].toString()
446: .indexOf("java.lang.annotation.Target") == -1);
447: }
448:
449: /**
450: *
451: */
452: @zzz()
453: enum www {
454: aaa;
455: };
456:
457: public void test_Annotations_V() {
458: assertFalse("zzz annotation is presented for www enum!",
459: www.class.getAnnotations()[0].toString().indexOf(
460: "java.lang.zzz") == -1);
461: assertEquals("Only one annotation is presented in enum www!",
462: 1, www.class.getAnnotations().length);
463: }
464:
465: /**
466: *
467: */
468: public void test_getCanonicalName_V() {
469: class e {
470: }
471: ;
472: try {
473: //##########################################################################################################
474: //##########################################################################################################
475: // to avoid bug of local classes naming in org.eclipse.jdt.core.JDTCompilerAdapter
476: // ("Class1_5Test$1ff2" instead of "Class1_5Test$gg$gga$ggb$ggg$1ff2")
477: //\\//\\assertTrue("FAILED: test_getCanonicalName_V.check001: Canonical name for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1 should be null!", Class.forName("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1").getCanonicalName() == null);
478: //\\//\\assertTrue("FAILED: test_getCanonicalName_V.check002: Canonical name for java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2 should be null!", Class.forName("java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2").getCanonicalName() == null);
479: try {
480: Object o1, o2, o3, o4, o5;
481: o1 = new gg();
482: o2 = o1.getClass().getMethod("ggaO")
483: .invoke(o1/*, null*/);
484: o3 = o2.getClass().getMethod("ggbO")
485: .invoke(o2/*, null*/);
486: o4 = o3.getClass().getMethod("gggO")
487: .invoke(o3/*, null*/);
488: assertNull(
489: "check001: Canonical name for "
490: + "java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2$1"
491: + "should be null!", (o5 = o4
492: .getClass().getMethod("id")
493: .invoke(o4/*, null*/)).getClass()
494: .getMethod("vd").invoke(o5/*, null*/)
495: .getClass().getCanonicalName());
496: assertNull("check002: Canonical name for "
497: + "java.lang.Class1_5Test$gg$gga$ggb$ggg$1ff2"
498: + "should be null!", (o5 = o4.getClass()
499: .getMethod("id").invoke(o4/*, null*/))
500: .getClass().getMethod("vd")
501: .invoke(o5/*, null*/).getClass()
502: .getEnclosingClass().getCanonicalName());
503: } catch (NoSuchMethodException e) {
504: e.printStackTrace();
505: fail("exception check001");
506: } catch (IllegalAccessException e) {
507: e.printStackTrace();
508: fail("exception check002");
509: } catch (java.lang.reflect.InvocationTargetException e) {
510: e.printStackTrace();
511: fail("exception check003");
512: }
513: //##########################################################################################################
514: //##########################################################################################################
515: assertEquals("check003: incorrrect canonical name for "
516: + "java.lang.Class1_5Test$gg$gga$ggb$ggc:",
517: "java.lang.Class1_5Test.gg.gga.ggb.ggc",
518: Class.forName(
519: "java.lang.Class1_5Test$gg$gga$ggb$ggc")
520: .getCanonicalName());
521: assertNull("check004: Canonical name for "
522: + "java.lang.Class1_5Test$1 should be null!", Class
523: .forName("java.lang.Class1_5Test$1")
524: .getCanonicalName());
525: Class c = (new Object() {
526: int i;
527:
528: int m1() {
529: return i + m2();
530: };
531:
532: int m2() {
533: return i + m1();
534: }
535: }).getClass();
536: assertNull("check005: Canonical name for "
537: + "\"(new Object() { int i; })\" should be null!",
538: c.getCanonicalName());
539: c = (new e() {
540: int i;
541:
542: int m1() {
543: return i + m2();
544: };
545:
546: int m2() {
547: return i + m1();
548: }
549: }).getClass();
550: assertNull("check006: Canonical name for "
551: + "\"(new e() { int i; })\" should be null!", c
552: .getCanonicalName());
553: assertNull("check007: Canonical name for "
554: + "java.lang.Class1_5Test$1e should be null!",
555: e.class.getCanonicalName());
556: } catch (Exception e) {
557: fail("check008: Error of the class loading!");
558: }
559: }
560:
561: /**
562: *
563: */
564: public void test_getAnnotation_Cla() {
565: class e {
566: }
567: ;
568: assertNull("zzz annotation is not presented in e class!",
569: e.class.getAnnotation(zzz.class));
570: assertNull("zzz annotation is not presented in zzz class!",
571: zzz.class.getAnnotation(zzz.class));
572: assertFalse("Target annotation is presented in zzz class!",
573: zzz.class.getAnnotation(
574: java.lang.annotation.Target.class).toString()
575: .indexOf("java.lang.annotation.Target") == -1);
576: assertFalse(
577: "Documented annotation is presented in zzz class!",
578: zzz.class.getAnnotation(
579: java.lang.annotation.Documented.class)
580: .toString().indexOf(
581: "java.lang.annotation.Documented") == -1);
582: assertFalse("Retention annotation is presented in zzz class!",
583: zzz.class.getAnnotation(
584: java.lang.annotation.Retention.class)
585: .toString().indexOf(
586: "java.lang.annotation.Retention") == -1);
587: }
588:
589: /**
590: *
591: */
592: public void test_cast_Cla() {
593: class e {
594: }
595: ;
596: class ee extends e {
597: }
598: ;
599: class eee extends ee {
600: }
601: ;
602: try {
603: ee.class.cast((Object) new eee());
604: } catch (ClassCastException _) {
605: fail("Object of eee can be casted to Object of ee class!");
606: }
607: try {
608: eee.class.cast((Object) new e());
609: fail("Object of e cannott be casted to Object of eee class");
610: } catch (ClassCastException _) {
611: return;
612: }
613: }
614:
615: /**
616: *
617: */
618: public void test_asSubclass_Cla() {
619: class e {
620: }
621: ;
622: class ee extends e {
623: }
624: ;
625: class eee extends ee {
626: }
627: ;
628: try {
629: eee.class.asSubclass(ee.class);
630: } catch (ClassCastException _) {
631: fail("eee class can be casted to represent a subclass of ee!");
632: }
633: try {
634: e.class.asSubclass(eee.class);
635: fail("e class cannot be casted to represent a subclass of eee!");
636: } catch (ClassCastException _) {
637: return;
638: }
639: }
640:
641: /**
642: *
643: */
644: public void test_getEnclosingMethod_V() {
645: class e {
646: }
647: ;
648: class ee extends e {
649: }
650: ;
651: class eee extends ee {
652: }
653: ;
654: assertFalse("ee class is declared within " + ""
655: + "test_getEnclosingMethod_V method!", eee.class
656: .getEnclosingMethod().toString().indexOf(
657: "test_getEnclosingMethod_V") == -1);
658: }
659:
660: /**
661: *
662: */
663: public void test_getEnclosingConstructor_V() {
664: class eklmn {
665: eklmn() {
666: class ee {
667: }
668: ;
669: assertFalse(
670: "ee class is declared within eklmn constructor!",
671: ee.class.getEnclosingConstructor().toString()
672: .indexOf("eklmn") == -1);
673: }
674: }
675: ;
676: new eklmn();
677: }
678:
679: /**
680: *
681: */
682: public void test_getGenericSuperclass_V() {
683: class e1 {
684: }
685: ;
686: class e2 extends e1 {
687: }
688: ;
689: class e3 extends e2 {
690: }
691: ;
692: assertFalse("e2 is generic superclass for e3!", e3.class
693: .getGenericSuperclass().toString().indexOf("e2") == -1);
694: }
695:
696: /**
697: *
698: */
699: interface e1 {
700: };
701:
702: interface e2 extends e1 {
703: };
704:
705: public void test_getGenericInterfaces_V() {
706: class e3 implements e2 {
707: }
708: ;
709: assertFalse("e2 is genericaly implemented by e3!",
710: e3.class.getGenericInterfaces()[0].toString().indexOf(
711: "e2") == -1);
712: }
713:
714: /**
715: *
716: */
717: public void test_getTypeParameters_V() {
718: class e3<T1, T2> implements e2 {
719: }
720: ;
721: assertFalse("T1 is type parameter for e3!", e3.class
722: .getTypeParameters()[0].toString().indexOf("T1") == -1);
723: assertFalse("T2 is type parameter for e3!", e3.class
724: .getTypeParameters()[1].toString().indexOf("T2") == -1);
725: }
726: }
|