KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > reflection > FieldDictionaryTest


1 package com.thoughtworks.xstream.converters.reflection;
2
3 import junit.framework.TestCase;
4
5 import java.util.Iterator JavaDoc;
6 import java.lang.reflect.Field JavaDoc;
7
8 public class FieldDictionaryTest extends TestCase {
9
10     private FieldDictionary fieldDictionary;
11
12     protected void setUp() throws Exception JavaDoc {
13         super.setUp();
14         fieldDictionary = new FieldDictionary();
15     }
16
17     static class SomeClass {
18         private String JavaDoc a;
19         private String JavaDoc c;
20         private String JavaDoc b;
21     }
22
23     public void testListsFieldsInClassInDefinitionOrder() {
24         Iterator JavaDoc fields = fieldDictionary.serializableFieldsFor(SomeClass.class);
25         assertEquals("a", ((Field JavaDoc)fields.next()).getName());
26         assertEquals("c", ((Field JavaDoc)fields.next()).getName());
27         assertEquals("b", ((Field JavaDoc)fields.next()).getName());
28         assertFalse("No more fields should be present", fields.hasNext());
29     }
30
31     static class SpecialClass extends SomeClass {
32         private String JavaDoc brilliant;
33     }
34
35     public void testIncludesFieldsInSuperClasses() {
36         Iterator JavaDoc fields = fieldDictionary.serializableFieldsFor(SpecialClass.class);
37         assertEquals("brilliant", ((Field JavaDoc)fields.next()).getName());
38         assertEquals("a", ((Field JavaDoc)fields.next()).getName());
39         assertEquals("c", ((Field JavaDoc)fields.next()).getName());
40         assertEquals("b", ((Field JavaDoc)fields.next()).getName());
41         assertFalse("No more fields should be present", fields.hasNext());
42     }
43
44     class InnerClass { // note: no static makes this an inner class, not nested class.
45
private String JavaDoc someThing;
46     }
47
48     public void testIncludesOuterClassReferenceForInnerClass() {
49         Iterator JavaDoc fields = fieldDictionary.serializableFieldsFor(InnerClass.class);
50         assertEquals("someThing", ((Field JavaDoc)fields.next()).getName());
51         Field JavaDoc innerField = ((Field JavaDoc)fields.next());
52         assertEquals("this$0", innerField.getName());
53         assertEquals(FieldDictionaryTest.class, innerField.getType());
54         assertFalse("No more fields should be present", fields.hasNext());
55     }
56 }
57
Popular Tags