File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\JenkinsMatchers.java.html

1 package org.jvnet.hudson.test;
2
3 import hudson.util.FormValidation;
4 import org.hamcrest.BaseMatcher;
5 import org.hamcrest.CoreMatchers;
6 import org.hamcrest.Description;
7 import org.hamcrest.Matcher;
8
9 import java.lang.reflect.Constructor;
10 import java.lang.reflect.InvocationTargetException;
11 import java.lang.reflect.Modifier;
12
13 /**
14 * Some handy matchers.
15 *
16 * @author Stephen Connolly
17 * @since 1.436
18 */
19 public class JenkinsMatchers {
20
21 public static Matcher<Class> privateConstructorThrows(Class<? extends Throwable> cause) {
22 return new PrivateConstructorThrows(cause);
23 }
24
25 public static Matcher<FormValidation> causedBy(Class<? extends Throwable> cause) {
26 return new FormValidationCauseMatcher(cause);
27 }
28
29 public static Matcher<FormValidation> hasKind(FormValidation.Kind kind) {
30 return new FormValidationKindMatcher(kind);
31 }
32
33 private static class PrivateConstructorThrows extends BaseMatcher<Class> {
34 private final Class<? extends Throwable> cause;
35
36 public PrivateConstructorThrows(Class<? extends Throwable> cause) {
37 this.cause = cause;
38 }
39
40 public boolean matches(Object o) {
41 Class<?> clazz = (Class<?>) o;
42 try {
43 Constructor<?> c = clazz.getDeclaredConstructor();
44 if (!Modifier.isPrivate(c.getModifiers())) {
45 return false;
46 }
47 boolean accessible = c.isAccessible();
48 try {
49 c.setAccessible(true);
50 c.newInstance();
51 } catch (InvocationTargetException e) {
52 Throwable t = e;
53 while (t != null) {
54 if (cause.isInstance(t)) {
55 return true;
56 }
57 t = t.getCause();
58 }
59 } catch (InstantiationException e) {
60 // ignore
61 } catch (IllegalAccessException e) {
62 // ignore
63 } finally {
64 c.setAccessible(accessible);
65 }
66 } catch (NoSuchMethodException e) {
67 // ignore
68 }
69 return false;
70 }
71
72 public void describeTo(Description description) {
73 description.appendText("with a private constructor that throws ");
74 description.appendValue(cause);
75 }
76 }
77
78 private static class FormValidationCauseMatcher extends BaseMatcher<FormValidation> {
79 private final Class<? extends Throwable> cause;
80
81 public FormValidationCauseMatcher(Class<? extends Throwable> cause) {
82 cause.getClass();
83 this.cause = cause;
84 }
85
86 public boolean matches(Object o) {
87 FormValidation v = (FormValidation) o;
88 return v.getMessage().contains(cause.getName());
89 }
90
91 public void describeTo(Description description) {
92 description.appendText("caused by ");
93 description.appendValue(cause);
94 }
95 }
96
97 private static class FormValidationKindMatcher extends BaseMatcher<FormValidation> {
98 private final FormValidation.Kind kind;
99
100 public FormValidationKindMatcher(FormValidation.Kind kind) {
101 kind.getClass();
102 this.kind = kind;
103 }
104
105 public boolean matches(Object o) {
106 FormValidation v = (FormValidation) o;
107 return v.kind == kind;
108 }
109
110 public void describeTo(Description description) {
111 description.appendText("of kind ");
112 description.appendValue(kind);
113 }
114 }
115
116 /**
117 * A matcher which checks that the class is a Utility class (i.e. is final and has only private constructors).
118 *
119 * @return A matcher which checks that the class is a Utility class (i.e. is final and has only private
120 * constructors).
121 */
122 public static Matcher<Class<?>> isUtilityClass() {
123 return CoreMatchers.allOf(isFinalClass(), isClassWithOnlyPrivateConstructors());
124 }
125
126 /**
127 * A matcher which checks that the class has only private constructors.
128 *
129 * @return A matcher which checks that the class has only private constructors.
130 */
131 public static Matcher<Class<?>> isClassWithOnlyPrivateConstructors() {
132 return new IsClassWithOnlyPrivateConstructors();
133 }
134
135 /**
136 * A matcher which checks that the class is final.
137 *
138 * @return A matcher which checks that the class is final.
139 */
140 public static Matcher<Class<?>> isFinalClass() {
141 return new IsFinalClass();
142 }
143
144 /**
145 * A matcher which checks that the class has the default constructor.
146 *
147 * @return A matcher which checks that the class has the default constructor.
148 */
149 public static Matcher<Class<?>> hasDefaultConstructor() {
150 return new HasDefaultConstructor();
151 }
152
153 /**
154 * A matcher that verifies that the a root cause of an exception is of the specified type.
155 *
156 * @param cause the type of exception that caused this.
157 * @return A matcher that verifies that the a root cause of an exception is of the specified type.
158 */
159 public static Matcher<Throwable> hasCause(Class<? extends Throwable> cause) {
160 return new HasCause(cause);
161 }
162
163 public static Matcher<Object> hasImplementedEquals() {
164 return new HasImplementedEquals();
165 }
166
167 public static Matcher<Object> hasReflexiveEquals() {
168 return new HasReflexiveEquals();
169 }
170
171 public static Matcher<Object> hasNonEqualityWithNulls() {
172 return new HasNonNullEquals();
173 }
174
175 public static Matcher<Object> hasSymmetricEquals(Object other) {
176 return new HasSymmetricEquals(other);
177 }
178
179 public static Matcher<Object> hasConsistentEquals(Object other) {
180 return new HasConsistentEquals(other);
181 }
182
183 public static Matcher<Object> hasTransitiveEquals(Object a, Object b) {
184 return new HasTransitiveEquals(a, b);
185 }
186
187 public static Matcher<Object> hasImplementedHashCode() {
188 return new HasImplementedHashCode();
189 }
190
191 public static Matcher<Object> hasHashCodeContract(Object other) {
192 return new HasHashCodeContract(other);
193 }
194
195 private static class HasDefaultConstructor
196 extends BaseMatcher<Class<?>> {
197 public boolean matches(Object item) {
198 Class<?> clazz = (Class<?>) item;
199 try {
200 Constructor<?> constructor = clazz.getConstructor();
201 return Modifier.isPublic(constructor.getModifiers());
202 } catch (NoSuchMethodException e) {
203 return false;
204 }
205 }
206
207 public void describeTo(Description description) {
208 description.appendText("a class with the default constructor");
209 }
210 }
211
212 private static class HasImplementedEquals
213 extends BaseMatcher<Object> {
214 public boolean matches(Object item) {
215 if (item == null) {
216 return false;
217 }
218 Class<?> clazz = item instanceof Class ? (Class<?>) item : item.getClass();
219 try {
220 return !clazz.getMethod("equals", Object.class).equals(Object.class.getMethod("equals", Object.class));
221 } catch (NoSuchMethodException e) {
222 return false;
223 }
224 }
225
226 public void describeTo(Description description) {
227 description.appendText("has overridden the default equals(Object) method");
228 }
229 }
230
231 private static class HasImplementedHashCode
232 extends BaseMatcher<Object> {
233 public boolean matches(Object item) {
234 if (item == null) {
235 return false;
236 }
237 Class<?> clazz = item instanceof Class ? (Class<?>) item : item.getClass();
238 try {
239 return !clazz.getMethod("hashCode").equals(Object.class.getMethod("hashCode"));
240 } catch (NoSuchMethodException e) {
241 return false;
242 }
243 }
244
245 public void describeTo(Description description) {
246 description.appendText("has overridden the default hashCode() method");
247 }
248 }
249
250 private static class IsClassWithOnlyPrivateConstructors
251 extends BaseMatcher<Class<?>> {
252 public boolean matches(Object item) {
253 Class<?> clazz = (Class<?>) item;
254 for (Constructor<?> c : clazz.getConstructors()) {
255 if (!Modifier.isPrivate(c.getModifiers())) {
256 return false;
257 }
258 }
259 return true;
260 }
261
262 public void describeTo(Description description) {
263 description.appendText("a class with only private constructors");
264 }
265 }
266
267 private static class IsFinalClass
268 extends BaseMatcher<Class<?>> {
269 public boolean matches(Object item) {
270 Class<?> clazz = (Class<?>) item;
271 return Modifier.isFinal(clazz.getModifiers());
272 }
273
274 public void describeTo(Description description) {
275 description.appendText("a final class");
276 }
277 }
278
279 private static class HasCause
280 extends BaseMatcher<Throwable> {
281 private final Class<? extends Throwable> cause;
282
283 public HasCause(Class<? extends Throwable> cause) {
284 this.cause = cause;
285 }
286
287 public boolean matches(Object item) {
288 Throwable throwable = (Throwable) item;
289 while (throwable != null && !cause.isInstance(throwable)) {
290 throwable = throwable.getCause();
291 }
292 return cause.isInstance(throwable);
293 }
294
295 public void describeTo(Description description) {
296 description.appendText("was caused by a ").appendValue(cause).appendText(" being thrown");
297 }
298 }
299
300 private static class HasNonNullEquals extends BaseMatcher<Object> {
301
302 public boolean matches(Object o) {
303 return o != null && !o.equals(null);
304 }
305
306 public void describeTo(Description description) {
307 description.appendText("has an equals(Object) method that returns false for null");
308 }
309 }
310
311 private static class HasReflexiveEquals extends BaseMatcher<Object> {
312
313 public boolean matches(Object o) {
314 return o != null && o.equals(o);
315 }
316
317 public void describeTo(Description description) {
318 description.appendText("has a reflexive equals(Object) method");
319 }
320 }
321
322 private static class HasSymmetricEquals extends BaseMatcher<Object> {
323
324 private final Object other;
325
326 public HasSymmetricEquals(Object other) {
327 this.other = other;
328 }
329
330 public boolean matches(Object o) {
331 return o == null ? other == null : (o.equals(other) ? other.equals(o) : !other.equals(o));
332 }
333
334 public void describeTo(Description description) {
335 description.appendText("has a symmetric equals(Object) method with ");
336 description.appendValue(other);
337 }
338 }
339
340 private static class HasConsistentEquals extends BaseMatcher<Object> {
341
342 private final Object other;
343
344 public HasConsistentEquals(Object other) {
345 this.other = other;
346 }
347
348 public boolean matches(Object o) {
349 return o == null ? other == null : (o.equals(other) ? o.equals(other) : !o.equals(other));
350 }
351
352 public void describeTo(Description description) {
353 description.appendText("has a symmetric equals(Object) method with ");
354 description.appendValue(other);
355 }
356 }
357
358 private static class HasTransitiveEquals extends BaseMatcher<Object> {
359
360 private final Object a;
361 private final Object b;
362
363 public HasTransitiveEquals(Object a, Object b) {
364 this.a = a;
365 this.b = b;
366 }
367
368 public boolean matches(Object o) {
369 return o != null && (!(o.equals(a) && a.equals(b)) || o.equals(b));
370 }
371
372 public void describeTo(Description description) {
373 description.appendText("has a transitive equals(Object) method with ");
374 description.appendValue(a);
375 description.appendText(" and ");
376 description.appendValue(b);
377 }
378 }
379
380 private static class HasHashCodeContract extends BaseMatcher<Object> {
381
382 private final Object other;
383
384 public HasHashCodeContract(Object other) {
385 this.other = other;
386 }
387
388 public boolean matches(Object o) {
389 return o == null ? other == null : (!o.equals(other) || o.hashCode() == other.hashCode());
390 }
391
392 public void describeTo(Description description) {
393 description.appendText("follows the hashCode contract when compared to ");
394 description.appendValue(other);
395 }
396 }
397
398 }
399