1 package net.trajano.commons.testing;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.Method;
5 import java.lang.reflect.Modifier;
6 import java.text.MessageFormat;
7 import java.util.ResourceBundle;
8
9
10
11
12
13
14
15
16 public final class UtilityClassTestUtil {
17
18
19
20
21 private static final ResourceBundle R = ResourceBundle.getBundle("META-INF.Messages");
22
23
24
25
26
27
28
29
30 private static void assertUtilityClassClassWellDefined(final Class<?> clazz) {
31
32 if (!Modifier.isFinal(clazz.getModifiers())) {
33 throw new AssertionError(MessageFormat.format(R.getString("UtilityClassTestUtil.notFinal"),
34 clazz));
35 }
36 if (clazz.getDeclaredConstructors().length != 1) {
37 throw new AssertionError(MessageFormat.format(R.getString("UtilityClassTestUtil.notOneConstructor"),
38 clazz));
39 }
40 }
41
42
43
44
45
46
47
48
49
50 private static void assertUtilityClassConstructorWellDefined(final Constructor<?> constructor) throws ReflectiveOperationException {
51
52 if (constructor.isAccessible() || !Modifier.isPrivate(constructor.getModifiers())) {
53 throw new AssertionError(MessageFormat.format(R.getString("UtilityClassTestUtil.constructorNotPrivate"),
54 constructor));
55 }
56 constructor.setAccessible(true);
57 constructor.newInstance();
58 constructor.setAccessible(false);
59 }
60
61
62
63
64
65
66
67
68 private static void assertUtilityClassMethodsWellDefined(final Class<?> clazz) {
69
70 for (final Method method : clazz.getMethods()) {
71 if (!Modifier.isStatic(method.getModifiers()) && method.getDeclaringClass()
72 .equals(clazz)) {
73 throw new AssertionError(MessageFormat.format(R.getString("UtilityClassTestUtil.methodNotStatic"),
74 method));
75 }
76 }
77 }
78
79
80
81
82
83
84
85
86
87 public static void assertUtilityClassWellDefined(final Class<?> clazz) throws ReflectiveOperationException {
88
89 assertUtilityClassClassWellDefined(clazz);
90 final Constructor<?> constructor = clazz.getDeclaredConstructor();
91 assertUtilityClassConstructorWellDefined(constructor);
92 assertUtilityClassMethodsWellDefined(clazz);
93 }
94
95
96
97
98 private UtilityClassTestUtil() {
99
100 }
101 }