1 package com.thoughtworks.xstream.converters.reflection; 2 3 import org.jmock.Mock; 4 import org.jmock.MockObjectTestCase; 5 6 public abstract class AbstractReflectionProviderTest extends MockObjectTestCase { 7 8 protected ReflectionProvider reflectionProvider; 9 10 public abstract ReflectionProvider createReflectionProvider(); 11 12 protected void setUp() throws Exception { 13 super.setUp(); 14 reflectionProvider = createReflectionProvider(); 15 } 16 17 public void testConstructsStandardClass() { 18 assertCanCreate(OuterClass.class); 19 } 20 21 public void testConstructsStaticInnerClass() { 22 assertCanCreate(PublicStaticInnerClass.class); 23 } 24 25 public static class WithFields { 26 private int a; 27 private int b = 2; 28 29 public int getParentB() { 30 return b; 31 } 32 } 33 34 public void testVisitsEachFieldInClass() { 35 Mock mockBlock = new Mock(ReflectionProvider.Visitor.class); 37 38 mockBlock.expect(once()) 40 .method("visit") 41 .with(eq("a"), eq(int.class), eq(WithFields.class), ANYTHING); 42 mockBlock.expect(once()) 43 .method("visit") 44 .with(eq("b"), eq(int.class), eq(WithFields.class), ANYTHING); 45 46 reflectionProvider.visitSerializableFields(new WithFields(), (ReflectionProvider.Visitor) mockBlock.proxy()); 48 49 mockBlock.verify(); 51 } 52 53 public static class SubClassWithFields extends WithFields { 54 private int c; 55 } 56 57 public void testVisitsEachFieldInHeirarchy() { 58 Mock mockBlock = new Mock(ReflectionProvider.Visitor.class); 60 61 mockBlock.expect(once()) 63 .method("visit") 64 .with(eq("a"), eq(int.class), eq(WithFields.class), ANYTHING); 65 mockBlock.expect(once()) 66 .method("visit") 67 .with(eq("b"), eq(int.class), eq(WithFields.class), ANYTHING); 68 mockBlock.expect(once()) 69 .method("visit") 70 .with(eq("c"), eq(int.class), eq(SubClassWithFields.class), ANYTHING); 71 72 reflectionProvider.visitSerializableFields(new SubClassWithFields(), (ReflectionProvider.Visitor) mockBlock.proxy()); 74 75 mockBlock.verify(); 77 } 78 79 public static class SubClassWithHiddenFields extends WithFields { 80 private int b = 3; 81 82 public int getChildB() { 83 return b; 84 } 85 } 86 87 public void testVisitsFieldsHiddenBySubclass() { 88 Mock mockBlock = new Mock(ReflectionProvider.Visitor.class); 90 91 mockBlock.expect(once()) 93 .method("visit") 94 .with(eq("b"), eq(int.class), eq(SubClassWithHiddenFields.class), ANYTHING) 95 .id("first"); 96 mockBlock.expect(once()) 97 .method("visit") 98 .with(eq("b"), eq(int.class), eq(WithFields.class), ANYTHING) 99 .after("first"); 100 mockBlock.expect(once()) 101 .method("visit") 102 .with(eq("a"), ANYTHING, ANYTHING, ANYTHING); 103 104 reflectionProvider.visitSerializableFields(new SubClassWithHiddenFields(), (ReflectionProvider.Visitor) mockBlock.proxy()); 106 107 mockBlock.verify(); 109 } 110 111 public void testWritesHiddenFields() { 112 SubClassWithHiddenFields o = new SubClassWithHiddenFields(); 113 reflectionProvider.writeField(o, "b", new Integer (10), null); 114 reflectionProvider.writeField(o, "b", new Integer (20), WithFields.class); 115 assertEquals(10, o.getChildB()); 116 assertEquals(20, o.getParentB()); 117 } 118 119 private void assertCanCreate(Class type) { 120 Object result = reflectionProvider.newInstance(type); 121 assertEquals(type, result.getClass()); 122 } 123 124 protected void assertCannotCreate(Class type) { 125 try { 126 reflectionProvider.newInstance(type); 127 fail("Should not have been able to newInstance " + type); 128 } catch (ObjectAccessException goodException) { 129 } 130 } 131 132 public static class PublicStaticInnerClass { 133 } 134 135 } 136 137 class OuterClass { 138 } 139 140 | Popular Tags |