KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > easymock > classextension > tests > DefaultClassInstantiatorTest


1 /*
2  * Copyright (c) 2003-2004 OFFIS. This program is made available under the terms of
3  * the MIT License.
4  */

5 package org.easymock.classextension.tests;
6
7 import java.io.Serializable JavaDoc;
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 /**
16  * Class testing the default instantiator. I'm cheating a little bit here since
17  * I'm not unit testing directly the class. The reason I'm doing this is that I
18  * want to make sure it works well with the cglib class and not the actual
19  * mocked class.
20  */

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 JavaDoc 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 JavaDoc();
61         }
62     }
63
64     public static class SerializableClass implements Serializable JavaDoc {
65         public SerializableClass() {
66             throw new RuntimeException JavaDoc();
67         }
68     }
69
70     public static class BadlyDoneSerializableClass implements Serializable JavaDoc {
71
72         private long serialVersionUID = 2; // not static
73

74         public BadlyDoneSerializableClass() {
75             throw new RuntimeException JavaDoc();
76         }
77     }
78
79     private String JavaDoc vendor = null;
80
81     protected void setUp() throws Exception JavaDoc {
82         super.setUp();
83         // Little hack to get a default instantiator
84
vendor = System.getProperty("java.vm.specification.vendor");
85         System.setProperty("java.vm.specification.vendor", "SomeJVM");
86     }
87
88     protected void tearDown() throws Exception JavaDoc {
89         // Set the value back to be clean
90
System.setProperty("java.vm.specification.vendor", vendor);
91         super.tearDown();
92     }
93
94     public void testEmptyConstructor() throws Exception JavaDoc {
95         MockControl ctrl = MockClassControl
96                 .createControl(DefaultClassInstantiator.class);
97         assertTrue(ctrl.getMock() instanceof DefaultClassInstantiator);
98     }
99
100     public void testPrimitiveType() throws Exception JavaDoc {
101         MockControl ctrl = MockClassControl
102                 .createControl(PrimitiveParamClass.class);
103         assertTrue(ctrl.getMock() instanceof PrimitiveParamClass);
104     }
105
106     public void testFinalType() throws Exception JavaDoc {
107         MockControl ctrl = MockClassControl
108                 .createControl(FinalParamClass.class);
109         assertTrue(ctrl.getMock() instanceof FinalParamClass);
110     }
111
112     public void testProtectedConstructor() throws Exception JavaDoc {
113         MockControl ctrl = MockClassControl
114                 .createControl(ProtectedConstructorClass.class);
115         assertTrue(ctrl.getMock() instanceof ProtectedConstructorClass);
116     }
117
118     public void testProtectedWithPrimitiveConstructor() throws Exception JavaDoc {
119         MockControl ctrl = MockClassControl
120                 .createControl(ProtectedWithPrimitiveConstructorClass.class);
121         assertTrue(ctrl.getMock() instanceof ProtectedWithPrimitiveConstructorClass);
122     }
123
124     public void testObjectParamRecusion() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
152         }
153     }
154
155     public void testNewInstance() throws Exception JavaDoc {
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 JavaDoc {
168         DefaultClassInstantiator instantiator = new DefaultClassInstantiator();
169         instantiator.newInstance(BadlyDoneSerializableClass.class);
170     }
171 }
172
Popular Tags