1 package com.thoughtworks.xstream.converters.reflection; 2 3 import junit.framework.TestCase; 4 5 import java.util.Iterator ; 6 import java.lang.reflect.Field ; 7 8 public class FieldDictionaryTest extends TestCase { 9 10 private FieldDictionary fieldDictionary; 11 12 protected void setUp() throws Exception { 13 super.setUp(); 14 fieldDictionary = new FieldDictionary(); 15 } 16 17 static class SomeClass { 18 private String a; 19 private String c; 20 private String b; 21 } 22 23 public void testListsFieldsInClassInDefinitionOrder() { 24 Iterator fields = fieldDictionary.serializableFieldsFor(SomeClass.class); 25 assertEquals("a", ((Field )fields.next()).getName()); 26 assertEquals("c", ((Field )fields.next()).getName()); 27 assertEquals("b", ((Field )fields.next()).getName()); 28 assertFalse("No more fields should be present", fields.hasNext()); 29 } 30 31 static class SpecialClass extends SomeClass { 32 private String brilliant; 33 } 34 35 public void testIncludesFieldsInSuperClasses() { 36 Iterator fields = fieldDictionary.serializableFieldsFor(SpecialClass.class); 37 assertEquals("brilliant", ((Field )fields.next()).getName()); 38 assertEquals("a", ((Field )fields.next()).getName()); 39 assertEquals("c", ((Field )fields.next()).getName()); 40 assertEquals("b", ((Field )fields.next()).getName()); 41 assertFalse("No more fields should be present", fields.hasNext()); 42 } 43 44 class InnerClass { private String someThing; 46 } 47 48 public void testIncludesOuterClassReferenceForInnerClass() { 49 Iterator fields = fieldDictionary.serializableFieldsFor(InnerClass.class); 50 assertEquals("someThing", ((Field )fields.next()).getName()); 51 Field innerField = ((Field )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 |