Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
../../img/srcFileCovDistChart10.png 0% of files have more coverage
26   104   13   5,2
14   52   0,5   5
5     2,6  
1    
 
  ConstructorArgs       Line # 28 26 0% 13 0 100% 1.0
 
  (68)
 
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;
18   
19    import java.lang.reflect.Constructor;
20    import java.lang.reflect.Field;
21   
22    /**
23    * Class wrapping arguments to create a partial class mock that gets
24    * instantiated by calling one of its constructors
25    *
26    * @author Henri Tremblay
27    */
 
28    public class ConstructorArgs {
29   
30    private final Constructor<?> constructor;
31   
32    private final Object[] initArgs;
33   
34    /**
35    * @param constructor
36    * Constructor to be called when creating the mock
37    * @param initArgs
38    * Arguments passed to the constructor
39    */
 
40  108 toggle public ConstructorArgs(final Constructor<?> constructor, final Object... initArgs) {
41  108 this.constructor = constructor;
42  108 this.initArgs = initArgs;
43   
44  108 validateArgs();
45    }
46   
 
47  108 toggle private void validateArgs() {
48   
49  108 final Class<?>[] paramTypes = constructor.getParameterTypes();
50   
51  108 if (initArgs.length != paramTypes.length) {
52  2 throw new IllegalArgumentException("Number of provided arguments doesn't match constructor ones");
53    }
54   
55  156 for (int i = 0; i < initArgs.length; i++) {
56   
57  66 final Class<?> paramType = paramTypes[i];
58  66 final Object arg = initArgs[i];
59   
60  66 if (paramType.isPrimitive()) {
61  44 if (arg == null) {
62  2 throw new IllegalArgumentException("Null argument for primitive param " + i);
63    }
64   
65  42 try {
66  42 final Field field = arg.getClass().getDeclaredField("TYPE");
67  38 final Class<?> argType = (Class<?>) field.get(null);
68   
69  34 if (paramType.equals(argType)) {
70  30 continue;
71    }
72    } catch (final Exception e) {
73  8 throw throwException(paramType, arg);
74    }
75   
76  4 throw throwException(paramType, arg);
77    }
78  22 if (arg == null) {
79  2 continue;
80    }
81  20 if (!paramType.isAssignableFrom(arg.getClass())) {
82  2 throw throwException(paramType, arg);
83    }
84    }
85    }
86   
 
87  14 toggle private IllegalArgumentException throwException(final Class<?> paramType, final Object arg) {
88  14 return new IllegalArgumentException(arg + " isn't of type " + paramType);
89    }
90   
91    /**
92    * @return arguments to be passed to the constructor
93    */
 
94  58 toggle public Object[] getInitArgs() {
95  58 return initArgs;
96    }
97   
98    /**
99    * @return constructor to be called
100    */
 
101  56 toggle public Constructor<?> getConstructor() {
102  56 return constructor;
103    }
104    }