Source Code Cross Referenced for FieldTest.java in  » Apache-Harmony-Java-SE » java-package » java » lang » reflect » 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 » java package » java.lang.reflect 
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:        /**
019:         * @author Serguei S.Zapreyev
020:         * @version $Revision$
021:         */package java.lang.reflect;
022:
023:        import junit.framework.TestCase;
024:
025:        /*
026:         * Created on 01.28.2006
027:         */
028:
029:        @SuppressWarnings(value={"all"})
030:        public class FieldTest extends TestCase {
031:            static public boolean sfld1 = true;
032:
033:            public boolean ifld1 = false;
034:
035:            static public byte sfld2 = (byte) 7;
036:
037:            public byte ifld2 = (byte) 5;
038:
039:            static public char sfld3 = 'G';
040:
041:            public char ifld3 = 'l';
042:
043:            static public double sfld4 = 777.d;
044:
045:            public double ifld4 = 55.5d;
046:
047:            static public float sfld5 = 7.77f;
048:
049:            public float ifld5 = .555f;
050:
051:            static public int sfld6 = 777;
052:
053:            public int ifld6 = 555;
054:
055:            static public long sfld7 = 777l;
056:
057:            public long ifld7 = 555l;
058:
059:            static public short sfld8 = Short.MAX_VALUE;
060:
061:            public short ifld8 = Short.MIN_VALUE;
062:
063:            /**
064:             *  
065:             */
066:            public void test_equals_Obj() {
067:                class X {
068:                    public int Xfld = 777;
069:                }
070:                class Y {
071:                    public int Xfld = 777;
072:                }
073:                try {
074:                    Field f1 = X.class.getField("Xfld");
075:                    Field f2 = Y.class.getField("Xfld");
076:                    assertEquals("Error: equal fields should coincide", f1, f1);
077:                    assertTrue(
078:                            "Error: coincidence of the unequal fields is detected",
079:                            !f1.equals(f2));
080:                } catch (NoSuchFieldException _) {
081:                    fail("Error: unfound field");
082:                }
083:            }
084:
085:            /**
086:             *  
087:             */
088:            public void test_get_Obj() {
089:                class X1 {
090:                    public int Xfld = 777;
091:                }
092:                X1 x = new X1();
093:                x.Xfld = 333;
094:                try {
095:                    Field f1 = X1.class.getField("Xfld");
096:                    assertTrue("Error1: x.Xfld should be equal 333",
097:                            ((Integer) (f1.get(x))).intValue() == 333);
098:                } catch (Exception e) {
099:                    //e.printStackTrace();
100:                    fail("Error2: " + e.toString());
101:                }
102:                try {
103:                    Field f1 = X1.class.getField("Xfld");
104:                    f1.get(null);
105:                    fail("Error3: NullPointerException should be risen just above");
106:                } catch (NullPointerException _) {
107:                    // The specified object is null and the field is an instance field
108:                } catch (Exception e) {
109:                    fail("Error4: " + e.toString());
110:                }
111:            }
112:
113:            /**
114:             *  
115:             */
116:            public void test_getBoolean_Obj() {
117:                try {
118:                    sfld1 = true;
119:                    Field f1 = FieldTest.class.getField("sfld1");
120:                    assertTrue("Error1", ((Boolean) (f1.get(null)))
121:                            .booleanValue() == true);
122:                    Field f2 = FieldTest.class.getField("ifld1");
123:                    FieldTest ft = new FieldTest();
124:                    ft.ifld1 = true;
125:                    assertTrue("Error2", ((Boolean) (f2.get(ft)))
126:                            .booleanValue());
127:                    assertTrue("Error3", f2.getBoolean(ft));
128:                } catch (Exception e) {
129:                    fail("Error3: " + e.toString());
130:                }
131:            }
132:
133:            /**
134:             *  
135:             */
136:            public void test_getByte_Obj() {
137:                try {
138:                    sfld2 = (byte) 7;
139:                    Field f1 = FieldTest.class.getField("sfld2");
140:                    assertEquals("Error1", 7, ((Byte) (f1.get(null)))
141:                            .byteValue());
142:                    Field f2 = FieldTest.class.getField("ifld2");
143:                    FieldTest ft = new FieldTest();
144:                    ft.ifld2 = 6;
145:                    assertEquals("Error2", 6, ((Byte) (f2.get(ft))).byteValue());
146:                    assertEquals("Error2", 6, f2.getByte(ft));
147:                } catch (Exception e) {
148:                    fail("Error3: " + e.toString());
149:                }
150:            }
151:
152:            /**
153:             *  
154:             */
155:            public void test_getChar_Obj() {
156:                try {
157:                    sfld3 = 'G';
158:                    Field f1 = FieldTest.class.getField("sfld3");
159:                    assertEquals("Error1", 'G', ((Character) (f1.get(null)))
160:                            .charValue());
161:                    Field f2 = FieldTest.class.getField("ifld3");
162:                    FieldTest ft = new FieldTest();
163:                    ft.ifld3 = 'm';
164:                    assertEquals("Error2", 'm', ((Character) (f2.get(ft)))
165:                            .charValue());
166:                    assertEquals("Error2", 'm', f2.getChar(ft));
167:                } catch (Exception e) {
168:                    fail("Error3: " + e.toString());
169:                }
170:            }
171:
172:            /**
173:             *  
174:             */
175:            public void test_getDeclaringClass_V() {
176:                class X {
177:                    public int Xfld = 777;
178:                }
179:                X x = new X();
180:                x.Xfld = 333;
181:                try {
182:                    Field f1 = X.class.getField("Xfld");
183:                    assertEquals("Error1", "java.lang.reflect.FieldTest$2X", f1
184:                            .getDeclaringClass().getName());
185:                } catch (NoSuchFieldException _) {
186:                    fail("Error2");
187:                }
188:            }
189:
190:            /**
191:             *  
192:             */
193:            public void test_getDouble_Obj() {
194:                try {
195:                    sfld4 = 777.d;
196:                    Field f1 = FieldTest.class.getField("sfld4");
197:                    assertEquals("Error1", 777.d, ((Double) (f1.get(null)))
198:                            .doubleValue());
199:                    Field f2 = FieldTest.class.getField("ifld4");
200:                    FieldTest ft = new FieldTest();
201:                    ft.ifld4 = 11.1d;
202:                    assertEquals("Error2", 11.1d, ((Double) (f2.get(ft)))
203:                            .doubleValue());
204:                    assertEquals("Error2", 11.1d, f2.getDouble(ft));
205:                } catch (Exception e) {
206:                    fail("Error3: " + e.toString());
207:                }
208:            }
209:
210:            /**
211:             *  
212:             */
213:            public void test_getFloat_Obj() {
214:                try {
215:                    sfld5 = 7.77f;
216:                    Field f1 = FieldTest.class.getField("sfld5");
217:                    assertEquals("Error1", 7.77f, ((Float) (f1.get(null)))
218:                            .floatValue());
219:                    Field f2 = FieldTest.class.getField("ifld5");
220:                    FieldTest ft = new FieldTest();
221:                    ft.ifld5 = .9999f;
222:                    assertEquals("Error2", .9999f, ((Float) (f2.get(ft)))
223:                            .floatValue());
224:                    assertEquals("Error2", .9999f, f2.getFloat(ft));
225:                } catch (Exception e) {
226:                    fail("Error3: " + e.toString());
227:                }
228:            }
229:
230:            /**
231:             *  
232:             */
233:            public void test_getInt_Obj() {
234:                try {
235:                    sfld6 = 777;
236:                    Field f1 = FieldTest.class.getField("sfld6");
237:                    assertEquals("Error1", 777, ((Integer) (f1.get(null)))
238:                            .intValue());
239:                    Field f2 = FieldTest.class.getField("ifld6");
240:                    FieldTest ft = new FieldTest();
241:                    ft.ifld6 = 222;
242:                    assertEquals("Error2", 222, ((Integer) (f2.get(ft)))
243:                            .intValue());
244:                    assertEquals("Error2", 222, f2.getInt(ft));
245:                } catch (Exception e) {
246:                    fail("Error3: " + e.toString());
247:                }
248:            }
249:
250:            /**
251:             *  
252:             */
253:            public void test_getLong_Obj() {
254:                try {
255:                    sfld7 = 777l;
256:                    Field f1 = FieldTest.class.getField("sfld7");
257:                    assertEquals("Error1", 777l, ((Long) (f1.get(null)))
258:                            .longValue());
259:                    Field f2 = FieldTest.class.getField("ifld7");
260:                    FieldTest ft = new FieldTest();
261:                    ft.ifld7 = 4444l;
262:                    assertEquals("Error2", 4444l, ((Long) (f2.get(ft)))
263:                            .longValue());
264:                    assertEquals("Error2", 4444l, f2.getLong(ft));
265:                } catch (Exception e) {
266:                    fail("Error3: " + e.toString());
267:                }
268:            }
269:
270:            /**
271:             *  
272:             */
273:            public void test_getModifiers_V() {
274:                class X {
275:                    public int Xfld;
276:
277:                    final int Yfld = 777;
278:                }
279:                new X();
280:                try {
281:                    Field f1 = X.class.getField("Xfld");
282:                    assertTrue("Error1", java.lang.reflect.Modifier.isPublic(f1
283:                            .getModifiers()));
284:                    Field af[] = X.class.getDeclaredFields();
285:                    for (int i = 0; i < af.length; i++) {
286:                        if (af[i].getName().equals("Yfld"))
287:                            assertTrue("Error2", java.lang.reflect.Modifier
288:                                    .isFinal(af[i].getModifiers()));
289:                    }
290:                } catch (NoSuchFieldException _) {
291:                    fail("Error3");
292:                }
293:            }
294:
295:            /**
296:             *  
297:             */
298:            public void test_getName_V() {
299:                class X {
300:                    public int Xfld;
301:
302:                    final int Yfld = 777;
303:                }
304:                new X();
305:                try {
306:                    Field f1 = X.class.getField("Xfld");
307:                    assertEquals("Error1", "Xfld", f1.getName());
308:                    Field af[] = X.class.getDeclaredFields();
309:                    int res = 0;
310:                    for (int i = 0; i < af.length; i++) {
311:                        if (af[i].getName().equals("Yfld")
312:                                || af[i].getName().equals("Xfld"))
313:                            res++;
314:                    }
315:                    assertEquals("Error2", 2, res);
316:                } catch (NoSuchFieldException _) {
317:                    fail("Error3");
318:                }
319:            }
320:
321:            /**
322:             *  
323:             */
324:            public void test_getShort_Obj() {
325:                try {
326:                    sfld8 = Short.MAX_VALUE;
327:                    Field f1 = FieldTest.class.getField("sfld8");
328:                    assertEquals("Error1", Short.MAX_VALUE, ((Short) (f1
329:                            .get(null))).shortValue());
330:                    Field f2 = FieldTest.class.getField("ifld8");
331:                    FieldTest ft = new FieldTest();
332:                    ft.ifld8 = Short.MIN_VALUE;
333:                    assertEquals("Error2", Short.MIN_VALUE, ((Short) (f2
334:                            .get(ft))).shortValue());
335:                    assertEquals("Error2", Short.MIN_VALUE, f2.getShort(ft));
336:                } catch (Exception e) {
337:                    fail("Error3: " + e.toString());
338:                }
339:            }
340:
341:            /**
342:             *  
343:             */
344:            public void test_getType_V() {
345:                Field af[] = FieldTest.class.getDeclaredFields();
346:                int res = 0;
347:                for (int i = 0; i < af.length; i++) {
348:                    if (af[i].getType().getName().equals("boolean"))
349:                        res += 1;
350:                    if (af[i].getType().getName().equals("byte"))
351:                        res += 10;
352:                    if (af[i].getType().getName().equals("char"))
353:                        res += 100;
354:                    if (af[i].getType().getName().equals("double"))
355:                        res += 1000;
356:                    if (af[i].getType().getName().equals("float"))
357:                        res += 10000;
358:                    if (af[i].getType().getName().equals("int"))
359:                        res += 100000;
360:                    if (af[i].getType().getName().equals("long"))
361:                        res += 1000000;
362:                    if (af[i].getType().getName().equals("short"))
363:                        res += 10000000;
364:                }
365:                assertEquals("Error1", 22222222, res);
366:            }
367:
368:            /**
369:             *  
370:             */
371:            public void test_hashCode_V() {
372:                try {
373:                    Field f1 = FieldTest.class.getField("sfld8");
374:                    assertEquals("Error1", f1.getDeclaringClass().getName()
375:                            .hashCode()
376:                            ^ f1.getName().hashCode(), f1.hashCode());
377:                } catch (NoSuchFieldException _) {
378:                    fail("Error2");
379:                }
380:            }
381:
382:            /**
383:             *  
384:             */
385:            public void test_set_Obj_Obj() {
386:                class X2 {
387:                    X2 xx;
388:
389:                    int Yfld = 777;
390:
391:                    public int m() {
392:                        return Yfld;
393:                    };
394:                }
395:                X2 x = new X2();
396:                try {
397:                    Field f1 = X2.class.getDeclaredField("Yfld");
398:                    f1.set(x, new Integer(345));
399:                    assertTrue("Error1", x.m() == 345);
400:
401:                    f1 = X2.class.getDeclaredField("xx");
402:                    f1.set(x, x);
403:                    assertTrue("Error2", x.xx.Yfld == 345);
404:                    assertTrue("Error3", x.xx.m() == 345);
405:                    assertTrue("Error4", x.xx.xx.xx.xx.xx.xx.xx.xx.xx.xx
406:                            .equals(x));
407:                    Field f2 = x.xx.xx.xx.xx.xx.xx.xx.xx.getClass()
408:                            .getDeclaredField("Yfld");
409:                    assertTrue("Error5", x.Yfld == ((Integer) f2
410:                            .get(x.xx.xx.xx.xx.xx.xx.xx.xx.xx)).intValue());
411:                } catch (Exception e) {
412:                    //e.printStackTrace();
413:                    fail("Error6: " + e.toString());
414:                }
415:            }
416:
417:            /**
418:             *  
419:             */
420:            static class XX {
421:                static int Yfld = 777;
422:            }
423:
424:            public void test_set_Obj_Obj_2() {
425:                XX x = new XX();
426:                try {
427:                    Field f1 = XX.class.getDeclaredField("Yfld");
428:                    f1.set(x, new Integer(345));
429:                    assertTrue("Error1", x.Yfld == 345);
430:                } catch (Exception e) {
431:                    e.printStackTrace();
432:                    fail("Error2: " + e.toString());
433:                }
434:            }
435:
436:            public void test_set_Null_Negative() throws Throwable {
437:                try {
438:                    Field f1 = XX.class.getDeclaredField("Yfld");
439:                    f1.set(null, null);
440:                    fail("null value should not be unboxed to primitive");
441:                } catch (IllegalArgumentException ok) {
442:                }
443:            }
444:
445:            /**
446:             *  
447:             */
448:            public void test_setBoolean_Obj() {
449:                try {
450:                    Field f1 = FieldTest.class.getField("sfld1");
451:                    f1.setBoolean((Object) null, false);
452:                    assertFalse("Error1", ((Boolean) (f1.get(null)))
453:                            .booleanValue());
454:                } catch (Exception e) {
455:                    fail("Error2: " + e.toString());
456:                }
457:            }
458:
459:            /**
460:             *  
461:             */
462:            public void test_setByte_Obj() {
463:                try {
464:                    Field f1 = FieldTest.class.getField("sfld2");
465:                    f1.setByte((Object) null, (byte) 8);
466:                    assertEquals("Error1", (byte) 8, ((Byte) (f1.get(null)))
467:                            .byteValue());
468:                } catch (Exception e) {
469:                    fail("Error2: " + e.toString());
470:                }
471:            }
472:
473:            /**
474:             *  
475:             */
476:            public void test_setChar_Obj() {
477:                try {
478:                    Field f1 = FieldTest.class.getField("sfld3");
479:                    f1.setChar((Object) null, 'Z');
480:                    assertEquals("Error1", 'Z', ((Character) (f1.get(null)))
481:                            .charValue());
482:                    FieldTest.class.getField("ifld3");
483:                } catch (Exception e) {
484:                    fail("Error2: " + e.toString());
485:                }
486:            }
487:
488:            /**
489:             *  
490:             */
491:            public void test_setDouble_Obj() {
492:                try {
493:                    Field f1 = FieldTest.class.getField("sfld4");
494:                    f1.setDouble((Object) null, 12.3d);
495:                    assertEquals("Error1", 12.3d, ((Double) (f1.get(null)))
496:                            .doubleValue());
497:                } catch (Exception e) {
498:                    fail("Error2: " + e.toString());
499:                }
500:            }
501:
502:            /**
503:             *  
504:             */
505:            public void test_setFloat_Obj() {
506:                try {
507:                    Field f1 = FieldTest.class.getField("sfld5");
508:                    f1.setFloat((Object) null, 0.0057f);
509:                    assertEquals("Error1", 0.0057f, ((Float) (f1.get(null)))
510:                            .floatValue());
511:                } catch (Exception e) {
512:                    fail("Error2: " + e.toString());
513:                }
514:            }
515:
516:            /**
517:             *  
518:             */
519:            public void test_setInt_Obj() {
520:                try {
521:                    Field f1 = FieldTest.class.getField("sfld6");
522:                    f1.setInt((Object) null, 222333444);
523:                    assertEquals("Error1", 222333444,
524:                            ((Integer) (f1.get(null))).intValue());
525:                } catch (Exception e) {
526:                    fail("Error2: " + e.toString());
527:                }
528:            }
529:
530:            /**
531:             *  
532:             */
533:            public void test_setLong_Obj() {
534:                try {
535:                    Field f1 = FieldTest.class.getField("sfld7");
536:                    f1.setLong((Object) null, 99999999999l);
537:                    assertEquals("Error1", 99999999999l,
538:                            ((Long) (f1.get(null))).longValue());
539:                } catch (Exception e) {
540:                    fail("Error2: " + e.toString());
541:                }
542:            }
543:
544:            /**
545:             *  
546:             */
547:            public void test_setShort_Obj() {
548:                try {
549:                    Field f1 = FieldTest.class.getField("sfld8");
550:                    f1.setShort((Object) null,
551:                            (short) (Short.MAX_VALUE + Short.MIN_VALUE));
552:                    assertEquals("Error1",
553:                            (short) (Short.MAX_VALUE + Short.MIN_VALUE),
554:                            ((Short) (f1.get(null))).shortValue());
555:                } catch (Exception e) {
556:                    fail("Error2: " + e.toString());
557:                }
558:            }
559:
560:            /**
561:             *  
562:             */
563:            public void test_toString_Obj() {
564:                class X {
565:                    int Yfld = 777;
566:                }
567:                new X();
568:                try {
569:                    Field f1 = X.class.getDeclaredField("Yfld");
570:                    assertEquals("Error1 ",
571:                            "int java.lang.reflect.FieldTest$5X.Yfld", f1
572:                                    .toString());
573:                } catch (Exception e) {
574:                    fail("Error2: " + e.toString());
575:                }
576:            }
577:
578:            static class RefFld {
579:                public Object o;
580:                public static Object so;
581:                public Integer i;
582:                public static Integer si;
583:            }
584:
585:            public void testSet_Obj() throws Throwable {
586:                Field f = RefFld.class.getField("o");
587:                RefFld obj = new RefFld();
588:                Object val = "dmjb";
589:                f.set(obj, val);
590:                assertSame(val, f.get(obj));
591:                f.set(obj, null);
592:                assertNull(f.get(obj));
593:            }
594:
595:            public void testSet_Obj_Static() throws Throwable {
596:                Field f = RefFld.class.getField("so");
597:                RefFld obj = new RefFld();
598:                Object val = "dmjb";
599:                f.set(obj, val);
600:                assertSame(val, f.get(null));
601:                f.set(null, null);
602:                assertNull(f.get(obj));
603:            }
604:
605:            public void testSet_BoxObj() throws Throwable {
606:                Field f = RefFld.class.getField("i");
607:                RefFld obj = new RefFld();
608:                Object val = new Integer(35434);
609:                f.set(obj, val);
610:                assertSame(val, f.get(obj));
611:                f.set(obj, null);
612:                assertNull(f.get(obj));
613:            }
614:
615:            public void testSet_BoxObj_Static() throws Throwable {
616:                Field f = RefFld.class.getField("si");
617:                RefFld obj = new RefFld();
618:                Object val = new Integer(589878);
619:                f.set(obj, val);
620:                assertSame(val, f.get(null));
621:                f.set(null, null);
622:                assertNull(f.get(obj));
623:            }
624:
625:            public void testSet_Obj_Invalid() throws Throwable {
626:                try {
627:                    Field f = RefFld.class.getField("si");
628:                    f.set(null, "345");
629:                    fail("IllegalArgumentException was not thrown on incompartible value");
630:                } catch (IllegalArgumentException ok) {
631:                }
632:            }
633:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.