perfsonar-dev - [pS-dev] [GEANT/SA2/ps-java-services] r6375 - trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest
Subject: perfsonar development work
List archive
[pS-dev] [GEANT/SA2/ps-java-services] r6375 - trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest
Chronological Thread
- From:
- To:
- Subject: [pS-dev] [GEANT/SA2/ps-java-services] r6375 - trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest
- Date: Mon, 13 May 2013 10:12:34 +0100 (BST)
- Authentication-results: sfpop-ironport01.merit.edu; dkim=neutral (message not signed) header.i=none
Author: psnc.pietrzak
Date: 2013-05-13 10:12:34 +0100 (Mon, 13 May 2013)
New Revision: 6375
Modified:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTestRunner.java
Log:
Changing to sequential runner.
Modified:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTestRunner.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTestRunner.java
2013-05-12 22:10:40 UTC (rev 6374)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTestRunner.java
2013-05-13 09:12:34 UTC (rev 6375)
@@ -12,148 +12,141 @@
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
+/**
+ * Runs self tests.
+ *
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class SelfTestRunner implements MethodInterceptor {
- private static String name;
- private static Map<String, SelfTest> tests = new Hashtable<String,
SelfTest>();
- private static Map<String, SelfTest> parameterizedTests = new
Hashtable<String, SelfTest>();
- private static Map<Class<?>, Method> testData = new Hashtable<Class<?>,
Method>();
+ private static String name;
+ private static Map<String, SelfTest> tests = new Hashtable<String,
SelfTest>();
+ private static Map<String, SelfTest> parameterizedTests = new
Hashtable<String, SelfTest>();
+ private static Map<Class<?>, Method> testData = new
Hashtable<Class<?>, Method>();
+
+ private static String successMessage;
+ private static Method testDataMethod;
- private static String successMessage;
- private static Method testDataMethod;
+ public void runSuite(final Scheduler scheduler, final SelfTestLogger
logger) throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException, InstantiationException {
+ proccessParameterizedTests();
+ final List<Runnable> tasks = new ArrayList<Runnable>();
+ for (final String testName : tests.keySet()) {
+ final Runnable task = createTask(testName, logger);
+ task.run();
+ //scheduler.runInParallel(task);
+ //tasks.add(task);
+ }
+// try {
+// scheduler.waitToFinish(tasks.toArray(new
Runnable[0]));
+// } catch (Exception ex) {
+// throw new RuntimeException("Cannot run self tests:",
ex);
+// }
+ }
+
+ public void runTest(final String testName, final Scheduler scheduler,
final SelfTestLogger logger) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException, InstantiationException {
+ proccessParameterizedTests();
+ final Runnable task = createTask(testName, logger);
+ scheduler.runInParallel(task);
+ try {
+ scheduler.waitToFinish(task);
+ } catch (Exception ex) {
+ logger.failure(testName, ex.getMessage());
+ }
+ }
- public void runSuite(final Scheduler scheduler, final SelfTestLogger
logger) throws
- IllegalAccessException, InvocationTargetException,
InstantiationException {
- proccessParameterizedTests();
- final List<Runnable> tasks = new ArrayList<Runnable>();
- for (final String testName : tests.keySet()) {
- final Runnable task = createTask(testName, logger);
- scheduler.runInParallel(task);
- tasks.add(task);
- }
- try {
- scheduler.waitToFinish(tasks.toArray(new Runnable[0]));
- } catch (Exception ex) {
- throw new RuntimeException("Cannot run self tests:", ex);
- }
- }
+ private Runnable createTask(final String testName, final
SelfTestLogger logger) {
+ return new Runnable(){
+ @Override
+ public void run() {
+ final SelfTest test = tests.get(testName);
+ if (test == null)
+ throw new RuntimeException("Cannot
find self test named: " + testName);
+ test.run(logger);
+ }
+ };
+ }
+
+ private void proccessParameterizedTests() throws
IllegalArgumentException, IllegalAccessException, InvocationTargetException,
InstantiationException {
+ for (SelfTest test : parameterizedTests.values()) {
+ Method data = testData.get(test.getParameterType());
+ if (data == null)
+ throw new RuntimeException("Self test without
self test data: " + test.getName());
+
+ int i = 0;
+ for (Object arg : (List<?>)
data.invoke(newInstance(data))) {
+ final SelfTest temp = new
SelfTest(test.getName(), test.getSuccessMessage(), test.getMethod(), i++,
arg);
+ tests.put(temp.getName(), temp);
+ }
+ }
+
+ parameterizedTests.clear();
+ }
- public void runTest(final String testName, final Scheduler scheduler,
final SelfTestLogger logger)
- throws IllegalAccessException, InvocationTargetException,
InstantiationException {
- proccessParameterizedTests();
- final Runnable task = createTask(testName, logger);
- scheduler.runInParallel(task);
- try {
- scheduler.waitToFinish(task);
- } catch (Exception ex) {
- logger.failure(testName, ex.getMessage());
- }
- }
+ private Object newInstance(Method m) throws InstantiationException,
IllegalAccessException {
+ return m.getDeclaringClass().newInstance();
+ }
- private Runnable createTask(final String testName, final SelfTestLogger
logger) {
- return new Runnable() {
- @Override
- public void run() {
- final SelfTest test = tests.get(testName);
- if (test == null) {
- throw new RuntimeException("Cannot find self test named:
" + testName);
- }
- test.run(logger);
- }
- };
- }
+ @SuppressWarnings("unchecked")
+ public static <T> T addSelfTest(String name, String successMessage,
Class<T> test) {
+ SelfTestRunner.name = name;
+ SelfTestRunner.successMessage = successMessage;
+ return (T) Enhancer.create(test, new SelfTestRunner());
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> T selfTestData(Class<T> testData) {
+ SelfTestRunner.name = null;
+ SelfTestRunner.successMessage = null;
+ return (T) Enhancer.create(testData, new SelfTestRunner());
+ }
+
+ public static <T> void configureForType(Class<T> type, List<T>
methodInvocation) {
+ SelfTestRunner.testData.put(type,
SelfTestRunner.testDataMethod);
+ SelfTestRunner.testDataMethod = null;
+ }
+
+ public static <T> T any(Class<T> type) {
+ return null;
+ }
- private void proccessParameterizedTests() throws IllegalAccessException,
- InvocationTargetException, InstantiationException {
- for (SelfTest test : parameterizedTests.values()) {
- Method data = testData.get(test.getParameterType());
- if (data == null) {
- throw new RuntimeException("Self test without self test
data: " + test.getName());
- }
+ @SuppressWarnings("rawtypes")
+ @Override
+ public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
+ if (method.getName().equals("toString") ||
method.getName().equals("hashCode")) {
+ return proxy.invokeSuper(obj, args);
+ }
+
+ if (!method.getReturnType().equals(Void.TYPE) &&
!isSelfTestData())
+ throw new RuntimeException(method.toString() + ":
Self test method should be of type void.");
+ else if (isSelfTestData()) {
+ if (!method.getReturnType().isInstance(new
ArrayList()))
+ throw new RuntimeException(method.toString()
+ ": Self test data method must be of List<E> type where E is type of self
test's argument type.");
+ if (method.getParameterTypes().length != 0)
+ throw new RuntimeException(method.toString()
+ ": Self test data method cannot have any arguments.");
+ SelfTestRunner.testDataMethod = method;
+ return null;
+ }
+
+ if (method.getParameterTypes().length > 1)
+ throw new RuntimeException(method.toString() + ";
Self test method can have zero or one arguments.");
+
+ if (method.getParameterTypes().length == 1)
+
SelfTestRunner.parameterizedTests.put(SelfTestRunner.name, new
SelfTest(SelfTestRunner.name, SelfTestRunner.successMessage, method));
+ else
+ SelfTestRunner.tests.put(SelfTestRunner.name, new
SelfTest(SelfTestRunner.name, SelfTestRunner.successMessage, method));
+ return null;
+ }
- int i = 0;
- for (Object arg : (List<?>) data.invoke(newInstance(data))) {
- final SelfTest temp = new SelfTest(test.getName(),
test.getSuccessMessage(), test.getMethod(), i++, arg);
- tests.put(temp.getName(), temp);
- }
- }
+ private boolean isSelfTestData() {
+ return SelfTestRunner.name == null &&
SelfTestRunner.successMessage == null;
+ }
- parameterizedTests.clear();
- }
-
- private Object newInstance(Method m) throws InstantiationException,
IllegalAccessException {
- return m.getDeclaringClass().newInstance();
- }
-
- @SuppressWarnings("unchecked")
- public static <T> T addSelfTest(String name, String successMessage,
Class<T> test) {
- SelfTestRunner.name = name;
- SelfTestRunner.successMessage = successMessage;
- return (T) Enhancer.create(test, new SelfTestRunner());
- }
-
- @SuppressWarnings("unchecked")
- public static <T> T selfTestData(Class<T> testData) {
- SelfTestRunner.name = null;
- SelfTestRunner.successMessage = null;
- return (T) Enhancer.create(testData, new SelfTestRunner());
- }
-
- public static <T> void configureForType(Class<T> type, List<T>
methodInvocation) {
- SelfTestRunner.testData.put(type, SelfTestRunner.testDataMethod);
- SelfTestRunner.testDataMethod = null;
- }
-
- public static <T> T any(Class<T> type) {
- return null;
- }
-
- @SuppressWarnings("rawtypes")
- @Override
- public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
- if (method.getName().equals("toString") ||
method.getName().equals("hashCode")) {
- return proxy.invokeSuper(obj, args);
- }
-
- if (!method.getReturnType().equals(Void.TYPE) && !isSelfTestData()) {
- throw new RuntimeException(method.toString() + ": Self test
method should be of type void.");
- } else if (isSelfTestData()) {
- if (!method.getReturnType().isInstance(new ArrayList())) {
- throw new RuntimeException(
- method.toString()
- + ": Self test data method must be of
List<E> type where E is type of self test's argument type.");
- }
- if (method.getParameterTypes().length != 0) {
- throw new RuntimeException(method.toString() + ": Self test
data method cannot have any arguments.");
- }
- SelfTestRunner.testDataMethod = method;
- return null;
- }
-
- if (method.getParameterTypes().length > 1) {
- throw new RuntimeException(method.toString() + "; Self test
method can have zero or one arguments.");
- }
-
- if (method.getParameterTypes().length == 1) {
- SelfTestRunner.parameterizedTests.put(SelfTestRunner.name, new
SelfTest(SelfTestRunner.name,
- SelfTestRunner.successMessage, method));
- } else {
- SelfTestRunner.tests.put(SelfTestRunner.name, new
SelfTest(SelfTestRunner.name,
- SelfTestRunner.successMessage, method));
- }
- return null;
- }
-
- private boolean isSelfTestData() {
- return SelfTestRunner.name == null && SelfTestRunner.successMessage
== null;
- }
-
- public static void clearConfiguration() {
- SelfTestRunner.parameterizedTests.clear();
- SelfTestRunner.name = null;
- SelfTestRunner.successMessage = null;
- SelfTestRunner.testData.clear();
- SelfTestRunner.testDataMethod = null;
- SelfTestRunner.tests.clear();
- }
+ public static void clearConfiguration() {
+ SelfTestRunner.parameterizedTests.clear();
+ SelfTestRunner.name = null;
+ SelfTestRunner.successMessage = null;
+ SelfTestRunner.testData.clear();
+ SelfTestRunner.testDataMethod = null;
+ SelfTestRunner.tests.clear();
+ }
}
- [pS-dev] [GEANT/SA2/ps-java-services] r6375 - trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest, svn-noreply, 05/13/2013
Archive powered by MHonArc 2.6.16.