Move private constructor call code from
UtilityClassChecker to TestUtils class.
Change-Id: Ibf1e8d688c0ae0775920980d063ae60d9274bce3
diff --git a/src/test/java/net/onrc/onos/core/util/TestUtils.java b/src/test/java/net/onrc/onos/core/util/TestUtils.java
index a3606ed..98d5d60 100644
--- a/src/test/java/net/onrc/onos/core/util/TestUtils.java
+++ b/src/test/java/net/onrc/onos/core/util/TestUtils.java
@@ -4,6 +4,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -116,6 +117,24 @@
}
/**
+ * Triggers an allocation of an object of type <T> and forces a call to
+ * the private constructor.
+ *
+ * @param constructor Constructor to call
+ * @param <T> type of the object to create
+ * @return created object of type <T>
+ */
+ public static <T> T callConstructor(Constructor<T> constructor) {
+ try {
+ constructor.setAccessible(true);
+ return constructor.newInstance();
+ } catch (InstantiationException | IllegalAccessException |
+ InvocationTargetException error) {
+ return null;
+ }
+ }
+
+ /**
* Avoid instantiation.
*/
private TestUtils() {}
diff --git a/src/test/java/net/onrc/onos/core/util/UtilityClassChecker.java b/src/test/java/net/onrc/onos/core/util/UtilityClassChecker.java
index 50d7060..f2ffe1d 100644
--- a/src/test/java/net/onrc/onos/core/util/UtilityClassChecker.java
+++ b/src/test/java/net/onrc/onos/core/util/UtilityClassChecker.java
@@ -4,7 +4,6 @@
import org.hamcrest.StringDescription;
import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -71,15 +70,9 @@
}
- // Trigger an allocation of the object to force the hidden
- // constructor call
- try {
- constructor.setAccessible(true);
- constructor.newInstance();
- constructor.setAccessible(false);
- } catch (InstantiationException|IllegalAccessException|
- InvocationTargetException error) {
- failureReason = error.getCause() + " when calling constructor";
+ final Object newObject = TestUtils.callConstructor(constructor);
+ if (newObject == null) {
+ failureReason = "could not instantiate a new object";
return false;
}
return true;