Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
27   190   28   1,08
0   132   1,04   2,27
25     1,12  
11    
 
  DefaultClassInstantiatorTest       Line # 38 24 0% 18 3 92,3% 0.9230769
  DefaultClassInstantiatorTest.PrimitiveParamClass       Line # 40 0 0% 1 0 100% 1.0
  DefaultClassInstantiatorTest.FinalParamClass       Line # 45 0 0% 1 0 100% 1.0
  DefaultClassInstantiatorTest.ProtectedConstructorClass       Line # 50 0 0% 1 0 100% 1.0
  DefaultClassInstantiatorTest.ProtectedWithPrimitiveConstructorClass       Line # 55 0 0% 1 0 100% 1.0
  DefaultClassInstantiatorTest.ParamClass       Line # 60 0 0% 1 0 100% 1.0
  DefaultClassInstantiatorTest.ObjectParamClass       Line # 65 0 0% 1 0 100% 1.0
  DefaultClassInstantiatorTest.PrivateConstructorClass       Line # 70 0 0% 1 1 0% 0.0
  DefaultClassInstantiatorTest.ConstructorWithCodeClass       Line # 75 1 0% 1 0 100% 1.0
  DefaultClassInstantiatorTest.SerializableClass       Line # 82 1 0% 1 2 0% 0.0
  DefaultClassInstantiatorTest.BadlyDoneSerializableClass       Line # 89 1 0% 1 2 0% 0.0
 
  (24)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16   
17    package org.easymock.tests;
18   
19    import static org.easymock.EasyMock.*;
20    import static org.junit.Assert.*;
21   
22    import java.io.Serializable;
23   
24    import org.easymock.internal.ClassInstantiatorFactory;
25    import org.easymock.internal.DefaultClassInstantiator;
26    import org.junit.AfterClass;
27    import org.junit.BeforeClass;
28    import org.junit.Test;
29   
30    /**
31    * Class testing the default instantiator. I'm cheating a little bit here since
32    * I'm not unit testing directly the class. The reason I'm doing this is that I
33    * want to make sure it works well with the cglib class and not the actual
34    * mocked class.
35    *
36    * @author Henri Tremblay
37    */
 
38    public class DefaultClassInstantiatorTest {
39   
 
40    public static class PrimitiveParamClass {
 
41  2 toggle public PrimitiveParamClass(final int i) {
42    }
43    }
44   
 
45    public static class FinalParamClass {
 
46  4 toggle public FinalParamClass(final String i) {
47    }
48    }
49   
 
50    public static class ProtectedConstructorClass {
 
51  2 toggle protected ProtectedConstructorClass() {
52    }
53    }
54   
 
55    public static class ProtectedWithPrimitiveConstructorClass {
 
56  2 toggle protected ProtectedWithPrimitiveConstructorClass(final int i) {
57    }
58    }
59   
 
60    public static class ParamClass {
 
61  2 toggle public ParamClass(final FinalParamClass f) {
62    }
63    }
64   
 
65    public static class ObjectParamClass {
 
66  2 toggle public ObjectParamClass(final ParamClass c) {
67    }
68    }
69   
 
70    public static class PrivateConstructorClass {
 
71  0 toggle private PrivateConstructorClass() {
72    }
73    }
74   
 
75    public static class ConstructorWithCodeClass {
 
76  2 toggle public ConstructorWithCodeClass() {
77  2 throw new RuntimeException();
78    }
79    }
80   
81    @SuppressWarnings("serial")
 
82    public static class SerializableClass implements Serializable {
 
83  0 toggle public SerializableClass() {
84  0 throw new RuntimeException();
85    }
86    }
87   
88    @SuppressWarnings("serial")
 
89    public static class BadlyDoneSerializableClass implements Serializable {
90   
91    private final long serialVersionUID = 2; // not static
92   
 
93  0 toggle public BadlyDoneSerializableClass() {
94  0 throw new RuntimeException();
95    }
96    }
97   
98    private final String vendor = null;
99   
 
100  2 toggle @BeforeClass
101    public static void setUp() throws Exception {
102    // Set the default instantiator
103  2 ClassInstantiatorFactory.setInstantiator(new DefaultClassInstantiator());
104    }
105   
 
106  2 toggle @AfterClass
107    public static void tearDown() throws Exception {
108    // Set the value back to be clean
109  2 ClassInstantiatorFactory.setDefaultInstantiator();
110    }
111   
 
112  2 toggle @Test
113    public void emptyConstructor() throws Exception {
114  2 checkInstatiation(DefaultClassInstantiator.class);
115    }
116   
 
117  2 toggle @Test
118    public void primitiveType() throws Exception {
119  2 checkInstatiation(PrimitiveParamClass.class);
120    }
121   
 
122  2 toggle @Test
123    public void finalType() throws Exception {
124  2 checkInstatiation(FinalParamClass.class);
125    }
126   
 
127  2 toggle @Test
128    public void protectedConstructor() throws Exception {
129  2 checkInstatiation(ProtectedConstructorClass.class);
130    }
131   
 
132  2 toggle @Test
133    public void protectedWithPrimitiveConstructor() throws Exception {
134  2 checkInstatiation(ProtectedWithPrimitiveConstructorClass.class);
135    }
136   
 
137  2 toggle @Test
138    public void objectParamRecusion() throws Exception {
139  2 checkInstatiation(ObjectParamClass.class);
140    }
141   
 
142  2 toggle @Test
143    public void constructorWithCodeLimitation() {
144  2 try {
145  2 createMock(ConstructorWithCodeClass.class);
146  0 fail("Shouldn't be possible to mock, code in constructor should crash");
147    } catch (final Exception e) {
148    }
149    }
150   
 
151  2 toggle @Test
152    public void privateConstructorLimitation() {
153  2 try {
154  2 createMock(PrivateConstructorClass.class);
155  0 fail("Shouldn't be able to mock a class with a private constructor using DefaultInstantiator");
156    } catch (final Exception e) {
157    }
158    }
159   
 
160  2 toggle @Test
161    public void privateConstructor() {
162  2 final DefaultClassInstantiator instantiator = new DefaultClassInstantiator();
163  2 try {
164  2 instantiator.newInstance(PrivateConstructorClass.class);
165  0 fail("Shouldn't be able to mock a class with a private constructor using DefaultInstantiator");
166    } catch (final Exception e) {
167    }
168    }
169   
 
170  2 toggle @Test
171    public void newInstance() throws Exception {
172  2 checkInstatiation(DefaultClassInstantiator.class);
173    }
174   
 
175  2 toggle @Test
176    public void serializable() {
177  2 checkInstatiation(SerializableClass.class);
178    }
179   
 
180  2 toggle @Test
181    public void badSerializable() throws Exception {
182  2 final DefaultClassInstantiator instantiator = new DefaultClassInstantiator();
183  2 instantiator.newInstance(BadlyDoneSerializableClass.class);
184    }
185   
 
186  16 toggle private <T> void checkInstatiation(final Class<T> clazz) {
187  16 final T mock = createMock(clazz);
188  16 assertTrue(clazz.isAssignableFrom(mock.getClass()));
189    }
190    }