1+ /**
2+ * Copyright (c) 2017-present, UtilsVerifier Contributors.
3+ * <p>
4+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+ * compliance with the License. You may obtain a copy of the License at
6+ * <p>
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ * <p>
9+ * Unless required by applicable law or agreed to in writing, software distributed under the License is
10+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+ * the License for the specific language governing permissions and limitations under the License.
12+ */
113package com .github .karczews .utilsverifier ;
214
315import java .lang .reflect .Constructor ;
416import java .lang .reflect .Field ;
517import java .lang .reflect .InvocationTargetException ;
618import java .lang .reflect .Modifier ;
719
20+ /**
21+ * {@code EqualsVerifier} tool can be used in unit tests to verify if certain util class is
22+ * well formed.
23+ * <p>
24+ * By default verifier performs following checks that class:
25+ * <ul>
26+ * <li> is final
27+ * <li> has only one private constructor
28+ * <li> has no instance fields
29+ * </ul>
30+ *
31+ * @param <T> class under test
32+ */
833public class UtilsVerifier <T > {
934
1035 private final Class <T > classUnderTest ;
@@ -20,48 +45,78 @@ private UtilsVerifier(final Class<T> type) {
2045 }
2146
2247 /**
23- * Creates UtilsVerifier for provided type.
48+ * Creates UtilsVerifier instance for provided type.
49+ *
50+ * @param type class type for which verifier will be created
2451 */
2552 public static <T > UtilsVerifier <T > forClass (final Class <T > type ) {
2653 return new UtilsVerifier <T >(type );
2754 }
2855
56+ /**
57+ * Performs verification for provided type.
58+ * {@link AssertionError} will be thrown if provided type is not well formed util class.
59+ */
60+ public void verify () {
61+ checkIfClassIsFinal ();
62+ hasOnlyOneConstructor ();
63+ verifyPrivateConstructor ();
64+ hasNoInstanceFields ();
65+ }
66+
67+ /**
68+ * Sets exception type that will be expected during construction attempt.
69+ *
70+ * @param type expected exception type
71+ */
2972 public UtilsVerifier <T > withConstructorThrowing (final Class <? extends Throwable > type ) {
3073 expectedConstructorException = type ;
3174 return this ;
3275 }
3376
77+ /**
78+ * Suppress final class verification. Use if non-final util class is allowed.
79+ *
80+ * @param suppressCheck true if check should be suppressed, false otherwise
81+ */
3482 public UtilsVerifier <T > suppressFinalClassCheck (final boolean suppressCheck ) {
3583 suppressFinalClassCheck = suppressCheck ;
3684 return this ;
3785 }
3886
87+ /**
88+ * Suppress single constructor verification.
89+ * Use if util class is allowed to have more than one constructor.
90+ *
91+ * @param suppressCheck true if check should be suppressed, false otherwise
92+ */
3993 public UtilsVerifier <T > suppressOnlyOneConstructorCheck (final boolean suppressCheck ) {
4094 suppressOnlyOneConstructorCheck = suppressCheck ;
4195 return this ;
4296 }
4397
98+ /**
99+ * Suppress private constructor verification.
100+ * Use if util class is allowed to have non private constructor.
101+ *
102+ * @param suppressCheck true if check should be suppressed, false otherwise
103+ */
44104 public UtilsVerifier <T > suppressPrivateConstructorCheck (final boolean suppressCheck ) {
45105 suppressPrivateConstructorCheck = suppressCheck ;
46106 return this ;
47107 }
48108
109+ /**
110+ * Suppress instance field verification.
111+ * Use if util class is allowed to have instance fields.
112+ *
113+ * @param suppressCheck true if check should be suppressed, false otherwise
114+ */
49115 public UtilsVerifier <T > suppressInstanceFieldCheck (final boolean suppressCheck ) {
50116 suppressInstanceFieldCheck = suppressCheck ;
51117 return this ;
52118 }
53119
54- /**
55- * Performs verification for provided type.
56- * {@link AssertionError} will be thrown if provided type is not well formed util class.
57- */
58- public void verify () {
59- checkIfClassIsFinal ();
60- hasOnlyOneConstructor ();
61- verifyPrivateConstructor ();
62- hasNoInstanceFields ();
63- }
64-
65120 private void checkIfClassIsFinal () {
66121 if (suppressFinalClassCheck ) return ;
67122 if (!Modifier .isFinal (classUnderTest .getModifiers ())) {
@@ -93,15 +148,13 @@ private void verifyPrivateConstructor() {
93148 if (expectedConstructorException != null ) {
94149 throw new AssertionError ("should not be able to instantiate " + classUnderTest .getSimpleName ());
95150 }
96- } catch (final IllegalAccessException e ) {
97- throw new IllegalStateException (e );
98- } catch (final InstantiationException e ) {
99- throw new IllegalStateException (e );
100151 } catch (final InvocationTargetException e ) {
101152 if (!expectedConstructorException .isInstance (e .getTargetException ())) {
102153 throw new AssertionError ("expected exception: " + expectedConstructorException .getName () +
103154 " got: " + e .getTargetException ().getClass ().getName ());
104155 }
156+ } catch (final ReflectiveOperationException e ) {
157+ throw new IllegalStateException (e );
105158 }
106159 }
107160
@@ -115,5 +168,6 @@ private void hasNoInstanceFields() {
115168 }
116169 }
117170 }
171+ //TODO: add check for non-static methods
118172 //TODO: add check for mutable static fields
119173}
0 commit comments