1 5 package org.easymock.classextension.tests; 6 7 import java.io.Serializable ; 8 9 import junit.framework.TestCase; 10 11 import org.easymock.MockControl; 12 import org.easymock.classextension.MockClassControl; 13 import org.easymock.classextension.internal.DefaultClassInstantiator; 14 15 21 public class DefaultClassInstantiatorTest extends TestCase { 22 23 public static class PrimitiveParamClass { 24 public PrimitiveParamClass(int i) { 25 } 26 } 27 28 public static class FinalParamClass { 29 public FinalParamClass(String i) { 30 } 31 } 32 33 public static class ProtectedConstructorClass { 34 protected ProtectedConstructorClass() { 35 } 36 } 37 38 public static class ProtectedWithPrimitiveConstructorClass { 39 protected ProtectedWithPrimitiveConstructorClass(int i) { 40 } 41 } 42 43 public static class ParamClass { 44 public ParamClass(FinalParamClass f) { 45 } 46 } 47 48 public static class ObjectParamClass { 49 public ObjectParamClass(ParamClass c) { 50 } 51 } 52 53 public static class PrivateConstructorClass { 54 private PrivateConstructorClass() { 55 } 56 } 57 58 public static class ConstructorWithCodeClass { 59 public ConstructorWithCodeClass() { 60 throw new RuntimeException (); 61 } 62 } 63 64 public static class SerializableClass implements Serializable { 65 public SerializableClass() { 66 throw new RuntimeException (); 67 } 68 } 69 70 public static class BadlyDoneSerializableClass implements Serializable { 71 72 private long serialVersionUID = 2; 74 public BadlyDoneSerializableClass() { 75 throw new RuntimeException (); 76 } 77 } 78 79 private String vendor = null; 80 81 protected void setUp() throws Exception { 82 super.setUp(); 83 vendor = System.getProperty("java.vm.specification.vendor"); 85 System.setProperty("java.vm.specification.vendor", "SomeJVM"); 86 } 87 88 protected void tearDown() throws Exception { 89 System.setProperty("java.vm.specification.vendor", vendor); 91 super.tearDown(); 92 } 93 94 public void testEmptyConstructor() throws Exception { 95 MockControl ctrl = MockClassControl 96 .createControl(DefaultClassInstantiator.class); 97 assertTrue(ctrl.getMock() instanceof DefaultClassInstantiator); 98 } 99 100 public void testPrimitiveType() throws Exception { 101 MockControl ctrl = MockClassControl 102 .createControl(PrimitiveParamClass.class); 103 assertTrue(ctrl.getMock() instanceof PrimitiveParamClass); 104 } 105 106 public void testFinalType() throws Exception { 107 MockControl ctrl = MockClassControl 108 .createControl(FinalParamClass.class); 109 assertTrue(ctrl.getMock() instanceof FinalParamClass); 110 } 111 112 public void testProtectedConstructor() throws Exception { 113 MockControl ctrl = MockClassControl 114 .createControl(ProtectedConstructorClass.class); 115 assertTrue(ctrl.getMock() instanceof ProtectedConstructorClass); 116 } 117 118 public void testProtectedWithPrimitiveConstructor() throws Exception { 119 MockControl ctrl = MockClassControl 120 .createControl(ProtectedWithPrimitiveConstructorClass.class); 121 assertTrue(ctrl.getMock() instanceof ProtectedWithPrimitiveConstructorClass); 122 } 123 124 public void testObjectParamRecusion() throws Exception { 125 MockControl ctrl = MockClassControl 126 .createControl(ObjectParamClass.class); 127 assertTrue(ctrl.getMock() instanceof ObjectParamClass); 128 } 129 130 public void testConstructorWithCodeLimitation() { 131 try { 132 MockClassControl.createControl(ConstructorWithCodeClass.class); 133 fail("Shouldn't be possible to mock, code in constructor should crash"); 134 } catch (Exception e) { 135 } 136 } 137 138 public void testPrivateConstructorLimitation() { 139 try { 140 MockClassControl.createControl(PrivateConstructorClass.class); 141 fail("Shouldn't be able to mock a class with a private constructor using DefaultInstantiator"); 142 } catch (Exception e) { 143 } 144 } 145 146 public void testPrivateConstructor() { 147 DefaultClassInstantiator instantiator = new DefaultClassInstantiator(); 148 try { 149 instantiator.newInstance(PrivateConstructorClass.class); 150 fail("Shouldn't be able to mock a class with a private constructor using DefaultInstantiator"); 151 } catch (Exception e) { 152 } 153 } 154 155 public void testNewInstance() throws Exception { 156 MockControl ctrl = MockClassControl 157 .createControl(DefaultClassInstantiator.class); 158 assertTrue(ctrl.getMock() instanceof DefaultClassInstantiator); 159 } 160 161 public void testSerializable() { 162 MockControl ctrl = MockClassControl 163 .createControl(SerializableClass.class); 164 assertTrue(ctrl.getMock() instanceof SerializableClass); 165 } 166 167 public void testBadSerializable() throws Exception { 168 DefaultClassInstantiator instantiator = new DefaultClassInstantiator(); 169 instantiator.newInstance(BadlyDoneSerializableClass.class); 170 } 171 } 172 | Popular Tags |