1 package com.zmicer.utils.junit;
2
3 import com.zmicer.utils.InputArgumentUtils;
4 import com.zmicer.utils.LoggingUtils;
5 import com.zmicer.utils.ReflexionUtils;
6 import junit.framework.TestCase;
7 import org.apache.log4j.Logger;
8
9 import java.io.PrintWriter;
10 import java.io.StringWriter;
11 import java.lang.reflect.Method;
12 import java.util.regex.Matcher;
13
14
15
16
17
18
19
20
21
22
23
24
25 public class JUnitUtils extends TestCase
26 {
27
28
29
30 final public static Logger LOG = Logger.getLogger(JUnitUtils.class);
31
32
33
34
35
36
37
38
39
40 public static int getTestMethodsNumber(final Class claz)
41 {
42 InputArgumentUtils.checkObjects(claz);
43 final Method[] methods = claz.getMethods();
44 int result = 0;
45 for (final Method method : methods)
46 {
47 if (method.getName().startsWith("test"))
48 {
49 result++;
50 }
51 }
52 return result;
53 }
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public static void checkOnWrongArgs(final Object object, final String methodName, final boolean[] requiredInfo, final Class[] argsClasses,
81 final Object... args)
82 {
83 InputArgumentUtils.checkObjects(object, methodName, requiredInfo, argsClasses, args);
84 if (requiredInfo.length != args.length)
85 {
86 throw new IllegalArgumentException("The numbers of boolean params in the req. info is not equal to the number of the method " +
87 "param classes");
88 }
89 try
90 {
91 final Class[] classes = new Class[args.length];
92 for (int i = 0; i < args.length; i++)
93 {
94 if (null == args[i])
95 {
96 throw new IllegalArgumentException("The default argument should not contains the null objects");
97 }
98 classes[i] = args[i].getClass();
99 }
100 final Method method = object.getClass().getDeclaredMethod(methodName, argsClasses);
101 assertNotNull(method);
102 for (int counter = 0; counter < args.length; counter++)
103 {
104
105 if (requiredInfo[counter])
106 {
107 final Object[] objects = new Object[args.length];
108 int innerCounter = -1;
109 for (final Object innerParam : args)
110 {
111 innerCounter++;
112 if (innerCounter == counter || !requiredInfo[innerCounter])
113 {
114 objects[innerCounter] = null;
115 }
116 else
117 {
118 objects[innerCounter] = innerParam;
119 }
120 }
121
122 try
123 {
124 method.invoke(object, objects);
125 fail("Exception should have been occuried before this line.");
126 }
127 catch (Exception ex)
128 {
129 assertNotNull(ex.getCause());
130 if (!(ex.getCause() instanceof IllegalArgumentException))
131 {
132 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", object.getClass());
133 }
134 assertTrue(ex.getCause() instanceof IllegalArgumentException);
135 }
136 }
137 }
138 }
139 catch (Exception e)
140 {
141 LoggingUtils.logException(LOG, e, "Smth. went wrong", JUnitUtils.class);
142 fail("Smth. went wrong, Exception message [" + e.getMessage() + "]");
143 }
144 }
145
146
147
148
149
150
151
152
153
154
155
156 public static void runFailure(final Object object, final String methodName, final Class exceptionClaz, final Class[] argsClasses,
157 final Object... arguments)
158 {
159 InputArgumentUtils.checkObjects(object, methodName, exceptionClaz, argsClasses, arguments);
160 try
161 {
162 final Method method = object.getClass().getMethod(methodName, argsClasses);
163 assertNotNull(method);
164 try
165 {
166 method.invoke(object, arguments);
167 fail("Exception should have been occuried before this line.");
168 }
169 catch (Exception ex)
170 {
171 assertNotNull(ex.getCause());
172 if (!ReflexionUtils.instanceOf(ex.getCause(), exceptionClaz))
173 {
174 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", object.getClass());
175 }
176 assertTrue(ReflexionUtils.instanceOf(ex.getCause(), exceptionClaz));
177 }
178 }
179 catch (Exception e)
180 {
181 LoggingUtils.logException(LOG, e, "Smth. went wrong", JUnitUtils.class);
182 fail("Smth. went wrong, Exception message [" + e.getMessage() + "]");
183 }
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197 public static String getRunningTestMethodName()
198 {
199 StringWriter sw = new StringWriter();
200 PrintWriter pw = new PrintWriter(sw);
201 new Exception().printStackTrace(pw);
202
203
204
205 Matcher matcher = ReflexionUtils.PATTERN.matcher(sw.toString());
206 if (matcher.find() && matcher.groupCount() == 1)
207 {
208 final String res = matcher.group(1);
209 LoggingUtils.debug(ReflexionUtils.LOG, ReflexionUtils.class, "Currently running method name [" + res + "].");
210 return res;
211 }
212 LoggingUtils.debug(ReflexionUtils.LOG, ReflexionUtils.class, "Can not find the currently running method name. Smth. wrong in algorithm.");
213 return null;
214 }
215
216
217
218
219
220
221 public static String getRunningMethodName()
222 {
223 final String testMethodName = getRunningTestMethodName();
224 if (null != testMethodName && testMethodName.startsWith("test"))
225 {
226 String temp = testMethodName.substring("test".length());
227
228 return Character.toLowerCase(temp.charAt(0)) + temp.substring(1);
229 }
230
231 return null;
232 }
233 }