1 package junitx.util; 2 3 import junit.framework.TestCase; 4 import junitx.example.PrivateAccessorExample; 5 6 10 public class PrivateAccessorTest 11 extends TestCase { 12 13 private PrivateAccessorExample example; 14 private ExampleSubclass example2; 15 16 public PrivateAccessorTest(String name) { 17 super(name); 18 } 19 20 public void setUp() { 21 this.example = new PrivateAccessorExample(); 22 this.example2 = new ExampleSubclass(); 23 } 24 25 public void testGetSetIntViaMethod() 26 throws Throwable { 27 Object res = PrivateAccessor.invoke(this.example, "setIntValue", new Class []{int.class}, new Object []{new Integer (1)}); 28 assertNull(res); 29 30 res = PrivateAccessor.invoke(this.example, "getIntValue", new Class [0], new Object [0]); 31 assertNotNull(res); 32 assertEquals(new Integer (1), res); 33 } 34 35 public void testGetSetIntDirect() 36 throws Exception { 37 PrivateAccessor.setField(this.example, "intvalue", new Integer (1)); 38 39 Object res = PrivateAccessor.getField(this.example, "intvalue"); 40 assertNotNull(res); 41 assertEquals(new Integer (1), res); 42 } 43 44 public void testGetSetDoubleViaMethod() 45 throws Throwable { 46 Object res = PrivateAccessor.invoke(PrivateAccessorExample.class, "setDoubleValue", new Class []{double.class}, new Object []{new Double (1234.567)}); 47 assertNull(res); 48 49 res = PrivateAccessor.invoke(PrivateAccessorExample.class, "getDoubleValue", new Class [0], new Object [0]); 50 assertNotNull(res); 51 assertEquals(1234.567, ((Double ) res).doubleValue(), 0); 52 } 53 54 public void testGetSetDoubleDirect() 55 throws Exception { 56 PrivateAccessor.setField(PrivateAccessorExample.class, "doublevalue", new Double (1234.567)); 57 58 Object res = PrivateAccessor.getField(PrivateAccessorExample.class, "doublevalue"); 59 assertNotNull(res); 60 assertEquals(1234.567, ((Double ) res).doubleValue(), 0); 61 } 62 63 public void testGetSetBoolViaMethod() 64 throws Throwable { 65 Object res = PrivateAccessor.invoke(this.example, "setBoolValue", new Class []{Boolean .class}, new Object []{new Boolean (false)}); 66 assertNull(res); 67 68 res = PrivateAccessor.invoke(this.example, "getBoolValue", new Class [0], new Object [0]); 69 assertNotNull(res); 70 assertEquals(new Boolean (false), res); 71 } 72 73 public void testGetSetBoolDirect() 74 throws Exception { 75 PrivateAccessor.setField(this.example, "boolvalue", new Boolean (true)); 76 77 Object res = PrivateAccessor.getField(this.example, "boolvalue"); 78 assertNotNull(res); 79 assertEquals(new Boolean (true), res); 80 } 81 82 public void testSubclassGetSetIntViaMethod() 83 throws Throwable { 84 Object res = PrivateAccessor.invoke(this.example2, "setIntValue", new Class []{int.class}, new Object []{new Integer (8)}); 85 assertNull(res); 86 87 res = PrivateAccessor.invoke(this.example2, "getIntValue", new Class [0], new Object [0]); 88 assertNotNull(res); 89 assertEquals(new Integer (10), res); 90 } 91 92 public void testSubclassGetSetIntDirect() 93 throws Exception { 94 PrivateAccessor.setField(this.example2, "intvalue", new Integer (8)); 95 96 Object res = PrivateAccessor.getField(this.example2, "intvalue"); 97 assertNotNull(res); 98 assertEquals(new Integer (8), res); 99 } 100 101 public void testSubclassGetSetBoolViaMethod() 102 throws Throwable { 103 Object res = PrivateAccessor.invoke(this.example2, "setBoolValue", new Class []{Boolean .class}, new Object []{new Boolean (false)}); 104 assertNull(res); 105 106 res = PrivateAccessor.invoke(this.example2, "getBoolValue", new Class [0], new Object [0]); 107 assertNotNull(res); 108 assertEquals(new Boolean (false), res); 109 } 110 111 public void testSubclassGetSetBoolDirect() 112 throws Exception { 113 PrivateAccessor.setField(this.example2, "boolvalue", new Boolean (true)); 114 115 Object res = PrivateAccessor.getField(this.example2, "boolvalue"); 116 assertNotNull(res); 117 assertEquals(new Boolean (true), res); 118 } 119 120 public void testSubclassThrowingException() 121 throws Throwable { 122 try { 123 PrivateAccessor.invoke(this.example2, "throwingException", new Class [0], new Object [0]); 124 } catch (NullPointerException e) { 125 return; 126 } 127 fail(); 128 } 129 130 public class ExampleSubclass 131 extends PrivateAccessorExample { 132 133 private int intvalue; 134 135 private int getIntValue() { 136 return this.intvalue + 2; 137 } 138 139 private void setIntValue(int intvalue) { 140 this.intvalue = intvalue; 141 } 142 143 private void throwingException() { 144 throw new NullPointerException (); 145 } 146 } 147 148 } 149 | Popular Tags |