perfsonar-dev - [pS-dev] [GEANT/SA2/ps-java-services] r6351 - in trunk/perfsonar-base/base: . src/main/java/net/geant/perfsonar src/main/java/net/geant/perfsonar/base src/main/java/net/geant/perfsonar/handlers src/main/java/net/geant/perfsonar/response src/main/java/net/geant/perfsonar/selftest src/main/resources/net/geant/perfsonar/response src/test/java/net/geant/perfsonar src/test/java/net/geant/perfsonar/configuration src/test/java/net/geant/perfsonar/helpers
Subject: perfsonar development work
List archive
[pS-dev] [GEANT/SA2/ps-java-services] r6351 - in trunk/perfsonar-base/base: . src/main/java/net/geant/perfsonar src/main/java/net/geant/perfsonar/base src/main/java/net/geant/perfsonar/handlers src/main/java/net/geant/perfsonar/response src/main/java/net/geant/perfsonar/selftest src/main/resources/net/geant/perfsonar/response src/test/java/net/geant/perfsonar src/test/java/net/geant/perfsonar/configuration src/test/java/net/geant/perfsonar/helpers
Chronological Thread
- From:
- To:
- Subject: [pS-dev] [GEANT/SA2/ps-java-services] r6351 - in trunk/perfsonar-base/base: . src/main/java/net/geant/perfsonar src/main/java/net/geant/perfsonar/base src/main/java/net/geant/perfsonar/handlers src/main/java/net/geant/perfsonar/response src/main/java/net/geant/perfsonar/selftest src/main/resources/net/geant/perfsonar/response src/test/java/net/geant/perfsonar src/test/java/net/geant/perfsonar/configuration src/test/java/net/geant/perfsonar/helpers
- Date: Mon, 6 May 2013 14:08:11 +0100 (BST)
- Authentication-results: sfpop-ironport05.merit.edu; dkim=neutral (message not signed) header.i=none
Author: psnc.pietrzak
Date: 2013-05-06 14:08:11 +0100 (Mon, 06 May 2013)
New Revision: 6351
Added:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/response/EchoRequestHandler.java
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/response/SelfTestLoggerImpl.java
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTest.java
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/Handlers.java
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/SampleTests.java
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/SelfTestsTest.java
Removed:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/handlers/selftest/
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/SelfTestRunnerImpl.java
Modified:
trunk/perfsonar-base/base/pom.xml
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/Configuration.java
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/HandlerConfiguration.java
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/NMWGMessage.java
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/PerfSONAR.java
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/base/Scheduler.java
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTestRunner.java
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/PerfSONARTest.java
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/ConfigurationTest.java
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/helpers/PerfSONARClient.java
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/helpers/Response.java
Log:
Improved debugging and fixed bugs.
Modified: trunk/perfsonar-base/base/pom.xml
===================================================================
--- trunk/perfsonar-base/base/pom.xml 2013-05-06 13:01:14 UTC (rev 6350)
+++ trunk/perfsonar-base/base/pom.xml 2013-05-06 13:08:11 UTC (rev 6351)
@@ -5,7 +5,7 @@
<parent>
<groupId>net.geant.perfsonar</groupId>
<artifactId>parent</artifactId>
- <version>1.0.1</version>
+ <version>1.0.1-SNAPSHOT</version>
<relativePath>../../parent/</relativePath>
</parent>
@@ -22,6 +22,21 @@
<url>https://svn.geant.net/fisheye/browse/SA2T3-ps-java-services/trunk/perfsonar-base/base/</url>
</scm>
+ <developers>
+ <developer>
+ <id>blazej.pietrzak</id>
+ <name>Blazej Pietrzak</name>
+
<email></email>
+ <organization>PSNC</organization>
+
<organizationUrl>http://www.man.poznan.pl/</organizationUrl>
+ <roles>
+ <role>architect</role>
+ <role>developer</role>
+ </roles>
+ <timezone>+1</timezone>
+ </developer>
+ </developers>
+
<dependencies>
<dependency>
<groupId>net.geant.perfsonar.base</groupId>
Modified:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/Configuration.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/Configuration.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/Configuration.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -72,7 +72,7 @@
Configuration.method = method;
}
- public static void reset() {
+ public static void clearConfiguration() {
type = null;
method = null;
handlers.clear();
Modified:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/HandlerConfiguration.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/HandlerConfiguration.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/HandlerConfiguration.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -1,6 +1,8 @@
package net.geant.perfsonar;
import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.List;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
@@ -9,6 +11,7 @@
public class HandlerConfiguration implements MethodInterceptor {
private String type;
+ private List<String> restrictedMethods = Arrays.asList("equals",
"hashCode", "finalize", "toString");
public HandlerConfiguration(String type) {
this.type = type;
@@ -22,7 +25,7 @@
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
- if (method.getName().equals("toString") ||
method.getName().equals("hashCode")) {
+ if (restrictedMethods.contains(method.getName())) {
return proxy.invokeSuper(obj, args);
}
Modified:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/NMWGMessage.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/NMWGMessage.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/NMWGMessage.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -10,6 +10,7 @@
private String eventType;
private String id;
private String key;
+ private String testName;
public NMWGMessage() {
}
@@ -28,7 +29,7 @@
return type.equals("EchoRequest") &&
eventType.equals("http://schemas.perfsonar.net/tools/admin/echo/2.0");
}
public boolean isSelfTestRequest() {
- return type.equals("EchoRequest") &&
eventType.equals("http://schemas.perfsonar.net/tools/admin/selftest/1.0");
+ return type.equals("EchoRequest") &&
eventType.startsWith("http://schemas.perfsonar.net/tools/admin/selftest/");
}
public String getId() {
@@ -80,5 +81,20 @@
return true;
}
+ public void setTestName(String name) {
+ this.testName = name;
+ }
+ public String getTestName() {
+ if (isRunSuite())
+ return null;
+
+ return
getEventType().substring(getEventType().lastIndexOf("/") + 1);
+ }
+
+ public boolean isRunSuite() {
+ return getEventType().endsWith("/1.0") ||
getEventType().endsWith("/1.0/");
+ }
+
+
}
Modified:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/PerfSONAR.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/PerfSONAR.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/PerfSONAR.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -1,10 +1,10 @@
package net.geant.perfsonar;
+import static net.geant.perfsonar.Configuration.when;
import static net.geant.perfsonar.messaging.XPathDeserializer.any;
import static net.geant.perfsonar.messaging.XPathDeserializer.declarePrefix;
import static net.geant.perfsonar.messaging.XPathDeserializer.whenXPath;
-import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
@@ -12,6 +12,7 @@
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
@@ -21,11 +22,11 @@
import javax.xml.ws.Provider;
import net.geant.perfsonar.base.Scheduler;
-import net.geant.perfsonar.response.EchoResponse;
-import net.geant.perfsonar.response.SelfTestResponse;
-import net.geant.perfsonar.templates.TemplateEngine;
+import net.geant.perfsonar.messaging.DeserializationException;
import net.geant.perfsonar.messaging.XPathDeserializer;
+import net.geant.perfsonar.response.EchoRequestHandler;
import net.geant.perfsonar.selftest.SelfTestRunner;
+import net.geant.perfsonar.templates.TemplateEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,7 +50,10 @@
*/
protected Logger logger = LoggerFactory.getLogger(getClass());
- static {
+ protected Scheduler scheduler = createThreadPool();
+ protected boolean usingAPT = false;
+
+ public PerfSONAR() {
try {
declarePrefix("nmwg",
"http://ggf.org/ns/nmwg/base/2.0/");
declarePrefix("nmwgr",
"http://ggf.org/ns/nmwg/result/2.0/");
@@ -63,31 +67,23 @@
.then(NMWGMessage.class).setEventType(any(String.class));
whenXPath("nmwg:message/nmwg:metadata/nmwg:key/text()").then(
NMWGMessage.class).setKey(any(String.class));
+
+ Configuration.configure(when("EchoRequest")
+
.then(EchoRequestHandler.class).handleEcho(any(NMWGMessage.class)));
} catch (Exception ex) {
- // FIXME: What should we do with this exception?
+ //No exception will be thrown. It is just for
compiling code.
}
- }
-
- private static SelfTestResponse response;
-
- protected Scheduler scheduler = createThreadPool();
-
- public PerfSONAR() {
+
loadConfiguration();
+ EchoRequestHandler.setScheduler(scheduler);
+ EchoRequestHandler.setServiceName(getServiceName());
+ EchoRequestHandler.setServiceType((getServiceType() == null)
? null : getServiceType().toString());
+ usingAPT =
+ (getXPathAnnotations() != null)
+ || (getLookupServiceRegisterAnnotations() != null)
+ || (getSelfTestsAnnotations() != null);
}
- /**
- * It creates an instance of net.geant.perfsonar.SelfTestRunnerImpl.
If you
- * would like to use a different implementation of SelfTestRunner you
must
- * override this method.
- *
- */
- protected SelfTestRunner newSelfTestRunner() throws
InstantiationException,
- IllegalAccessException, ClassNotFoundException {
- return (SelfTestRunner) Class.forName(
-
"net.geant.perfsonar.SelfTestRunnerImpl").newInstance();
- }
-
private void loadConfiguration() {
try {
configure(getXPathAnnotations());
@@ -105,17 +101,31 @@
} catch (Exception ex) {
// When @LookupServiceRegister annotations are not
found do nothing
}
+
+ try {
+ configure(getSelfTestsAnnotations());
+ } catch (Exception ex) {
+ // When @LookupServiceRegister annotations are not
found do nothing
+ }
}
- protected static Class<?> getLookupServiceRegisterAnnotations() {
+ public Class<?> getSelfTestsAnnotations() {
try {
+ return
Class.forName("net.geant.perfsonar.SelfTestsConfiguration");
+ } catch (ClassNotFoundException ex) {
+ return null;
+ }
+ }
+
+ public Class<?> getLookupServiceRegisterAnnotations() {
+ try {
return
Class.forName("net.geant.perfsonar.LookupServiceRegister");
} catch (ClassNotFoundException ex) {
return null;
}
}
- protected static Class<?> getHandlerAnnotations() {
+ public Class<?> getHandlerAnnotations() {
try {
return Class.forName("net.geant.perfsonar.Handlers");
} catch (ClassNotFoundException ex) {
@@ -123,7 +133,7 @@
}
}
- protected static Class<?> getXPathAnnotations() {
+ public Class<?> getXPathAnnotations() {
try {
return Class.forName("net.geant.perfsonar.XPaths");
} catch (ClassNotFoundException ex) {
@@ -131,14 +141,9 @@
}
}
- protected static boolean isUsingAPT() {
- return (getHandlerAnnotations() != null)
- || (getXPathAnnotations() != null)
- || (getLookupServiceRegisterAnnotations() !=
null);
- }
-
private void configure(Class<?> annotations) throws Exception {
annotations.newInstance();
+ usingAPT = true;
}
private static Scheduler createThreadPool() {
@@ -165,13 +170,20 @@
public void stopAll() {
for (ScheduledFuture<?> task :
tasks.values()) {
- if (task.cancel(false) == false)
- System.out.println("Task: " +
task + " did not stop.");
+ task.cancel(false);
}
pool.shutdownNow();
tasks.clear();
}
+ @Override
+ public void waitToFinish(Runnable... tasks) throws
InterruptedException, ExecutionException {
+ for (Runnable task : tasks) {
+ final ScheduledFuture<?> future =
this.tasks.get(task);
+ if (future == null) continue;
+ future.get();
+ }
+ }
};
}
@@ -191,9 +203,10 @@
try {
objects = (List<Object>) deserializer
.deserialize(toInputSource(request));// .getCharacterStream());
- } catch (Exception e1) {
- logger.error("Could not deserialize the request.",
e1);
- return null;
+ } catch (DeserializationException ex) {
+ throw new RuntimeException(ex.getMessage());
+ } catch (Throwable ex) {
+ throw new RuntimeException("Could not deserialize the
request.", ex);
}
if (deserializer.getObjects(NMWGMessage.class,
objects).size() <= 0)
@@ -203,53 +216,45 @@
final NMWGMessage msg =
deserializer.getObjects(NMWGMessage.class,
objects).get(0);
- final StringWriter writer = new StringWriter();
logger.debug("Dispatching an incoming message");
- if (msg.isEchoRequest()) {
- logger.debug("We received an echo request");
- return handleEcho(msg, writer);
- } else if (msg.isSelfTestRequest()) {
- logger.debug("We received a self test request");
- return handleSelfTest(msg, writer);
- } else {
- try {
- String msgType = msg.getType();
- logger.debug(msgType);
- final Method m =
Configuration.getHandler(msgType);
- if (m.getParameterTypes().length > 1)
- throw new RuntimeException(
- m
- + ":
Controller methods must have zero or one argument.");
+ String msgType = msg.getType();
+ logger.debug(msgType);
+ final Method m = Configuration.getHandler(msgType);
+ if (m == null)
+ throw new RuntimeException("No handler defined for "
+ msgType);
+ if (m.getParameterTypes().length > 1)
+ throw new RuntimeException(m + ": Controller methods
must have zero or one argument.");
- TemplateEngine.put("requestId", msg.getId());
+ TemplateEngine.put("serviceType", getServiceType());
+ TemplateEngine.put("serviceName", getServiceName());
+ TemplateEngine.put("requestId", msg.getId());
- if (m.getParameterTypes().length < 1) {
- TemplateEngine.put("result",
-
m.invoke(m.getDeclaringClass().newInstance()));
- } else {
- final List<Object> result = new
ArrayList<Object>();
- for (Object obj :
deserializer.getObjects(
-
m.getParameterTypes()[0], objects)) {
- result.add(m.invoke(
-
m.getDeclaringClass().newInstance(), obj));
- }
- TemplateEngine.put("result", result);
+ try {
+ if (m.getParameterTypes().length < 1) {
+ TemplateEngine.put("result",
+
m.invoke(m.getDeclaringClass().newInstance()));
+ } else {
+ final List<Object> result = new
ArrayList<Object>();
+ for (Object obj : deserializer.getObjects(
+ m.getParameterTypes()[0],
objects)) {
+
result.add(m.invoke(m.getDeclaringClass().newInstance(), obj));
}
- try {
- TemplateEngine.evaluate(writer,
m.getDeclaringClass(), msg
-
.getType().replace("Request", "Response.template"));
- } catch (IOException e) {
- logger.error(
- "Could not fill in
the template for the response.",
- e);
- }
- } catch (Exception ex) {
- logger.error("Could not process the
request.", ex);
- return null;
+ TemplateEngine.put("result", result);
}
+ } catch (Exception ex) {
+ throw new RuntimeException("Could not process the
request.", ex);
+ }
- return new StreamSource(new
StringReader(writer.toString()));
+ final StringWriter writer = new StringWriter();
+ try {
+ TemplateEngine.evaluate(writer,
m.getDeclaringClass(), msg.getType().replace("Request", "Response.template"));
+ writer.flush();
+ writer.close();
+ } catch (Exception ex) {
+ throw new RuntimeException("Could not serialize
response", ex);
}
+
+ return new StreamSource(new StringReader(writer.toString()));
}
private InputSource toInputSource(StreamSource request) {
@@ -259,41 +264,6 @@
return new InputSource(request.getReader());
}
- private StreamSource handleSelfTest(final NMWGMessage msg,
- final StringWriter writer) {
- try {
- logger.debug("Generating self test response.");
- response = new SelfTestResponse(getServiceType(),
getServiceName());
- } catch (Exception ex) {
- logger.error("Self Test response couldn't be
generated.", ex);
- }
- try {
- logger.debug("Now streaming the response.");
- response.begin(writer, msg.getId());
- newSelfTestRunner().runSuite(scheduler, response);
- } catch (Exception ex) {
- logger.error("Could not run self tests.", ex);
- } finally {
- try {
- response.end();
- } catch (IOException ex) {
- // FIXME: what should we do with this
exception?
- }
- }
-
- return new StreamSource(new StringReader(writer.toString()));
- }
-
- private StreamSource handleEcho(final NMWGMessage msg,
- final StringWriter writer) {
- try {
- EchoResponse.writeEcho(writer, msg.getId());
- } catch (IOException e) {
- return new StreamSource(new
StringReader(e.toString()));
- }
- return new StreamSource(new StringReader(writer.toString()));
- }
-
/**
* @return scheduler that uses thread pool.
*/
@@ -313,5 +283,8 @@
public void stop() {
scheduler.stopAll();
+ Configuration.clearConfiguration();
+ SelfTestRunner.clearConfiguration();
+ XPathDeserializer.clearConfiguration();
}
}
Modified:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/base/Scheduler.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/base/Scheduler.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/base/Scheduler.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -1,5 +1,7 @@
package net.geant.perfsonar.base;
+import java.util.concurrent.ExecutionException;
+
public interface Scheduler {
void runInParallel(Runnable task);
@@ -9,4 +11,6 @@
void stop(Runnable task);
void stopAll();
+
+ void waitToFinish(Runnable... tasks) throws InterruptedException,
ExecutionException;
}
\ No newline at end of file
Added:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/response/EchoRequestHandler.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/response/EchoRequestHandler.java
(rev 0)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/response/EchoRequestHandler.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -0,0 +1,56 @@
+package net.geant.perfsonar.response;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.geant.perfsonar.NMWGMessage;
+import net.geant.perfsonar.base.Scheduler;
+import net.geant.perfsonar.messaging.SelfTestResult;
+import net.geant.perfsonar.selftest.SelfTestRunner;
+
+public class EchoRequestHandler {
+ private static Scheduler scheduler;
+ private static String serviceName;
+ private static String serviceType;
+
+ private SelfTestRunner runner = new SelfTestRunner();
+
+ public List<SelfTestResult> handleEcho(final NMWGMessage msg) {
+ if (msg.isEchoRequest())
+ return new ArrayList<SelfTestResult>();
+
+ final SelfTestLoggerImpl logger = createLogger();
+
+ if (msg.isRunSuite()) {
+ try {
+ runner.runSuite(scheduler, logger);
+ } catch (Exception ex) {
+ throw new RuntimeException("Could not run
self tests.", ex);
+ }
+ } else {
+ try {
+ runner.runTest(msg.getTestName(), scheduler,
logger);
+ } catch (Exception ex) {
+ throw new RuntimeException("Could not run
self tests.", ex);
+ }
+ }
+
+ return logger.getResults();
+ }
+
+ private SelfTestLoggerImpl createLogger() {
+ return new SelfTestLoggerImpl(serviceName, serviceType);
+ }
+
+ public static void setScheduler(Scheduler scheduler) {
+ EchoRequestHandler.scheduler = scheduler;
+ }
+
+ public static void setServiceName(String serviceName) {
+ EchoRequestHandler.serviceName = serviceName;
+ }
+
+ public static void setServiceType(String serviceType) {
+ EchoRequestHandler.serviceType = serviceType;
+ }
+}
Added:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/response/SelfTestLoggerImpl.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/response/SelfTestLoggerImpl.java
(rev 0)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/response/SelfTestLoggerImpl.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -0,0 +1,32 @@
+package net.geant.perfsonar.response;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.geant.perfsonar.messaging.SelfTestResult;
+import net.geant.perfsonar.selftest.SelfTestLogger;
+
+public class SelfTestLoggerImpl implements SelfTestLogger {
+ private List<SelfTestResult> selfTestResults = new
ArrayList<SelfTestResult>();
+ private String serviceName;
+ private String serviceType;
+
+ public SelfTestLoggerImpl(String serviceName, String serviceType) {
+ this.serviceName = serviceName;
+ this.serviceType = serviceType;
+ }
+
+ public List<SelfTestResult> getResults() {
+ return selfTestResults;
+ }
+
+ @Override
+ public void success(String name, String successMessage) {
+ selfTestResults.add(SelfTestResult.success(name,
successMessage, serviceName, serviceType));
+ }
+
+ @Override
+ public void failure(String name, String message) {
+ selfTestResults.add(SelfTestResult.failure(name, message,
serviceName, serviceType));
+ }
+}
Added:
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTest.java
===================================================================
---
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTest.java
(rev 0)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTest.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -0,0 +1,74 @@
+package net.geant.perfsonar.selftest;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import net.geant.perfsonar.templates.TemplateEngine;
+
+public class SelfTest {
+
+ private Method method;
+ private String successMessage;
+ private String name;
+ private Object arg;
+
+ public SelfTest(String name, String successMessage, Method method) {
+ this.name = name;
+ this.method = method;
+ this.successMessage = successMessage;
+ }
+
+ public SelfTest(String name, String successMessage, Method method,
int index, Object arg) {
+ this.name = fillInTemplate(name, index, arg);
+ this.method = method;
+ this.successMessage = fillInTemplate(successMessage, index,
arg);
+ this.arg = arg;
+ }
+
+ public void run(SelfTestLogger logger) {
+ try {
+ final Object test = newInstance(method);
+ if (isParameterized())
+ method.invoke(test, arg);
+ else
+ method.invoke(test);
+ logger.success(name, successMessage);
+ } catch (InvocationTargetException ex) {
+ logger.failure(name,
ex.getTargetException().getMessage());
+ } catch (Exception ex) {
+ logger.failure(name, ex.getMessage());
+ }
+ }
+
+ public boolean isParameterized() {
+ return (method.getParameterTypes().length == 1);
+ }
+
+ public Class<?> getParameterType() {
+ return method.getParameterTypes()[0];
+ }
+
+ private String fillInTemplate(String template, int i, Object arg) {
+ TemplateEngine.put("index", i);
+ TemplateEngine.put("arg", arg);
+ return TemplateEngine.evaluate(template);
+ }
+
+ private Object newInstance(Method m) throws InstantiationException,
+ IllegalAccessException {
+
+ return m.getDeclaringClass().newInstance();
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getSuccessMessage() {
+ return successMessage;
+ }
+
+ public Method getMethod() {
+ return method;
+ }
+}
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-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/main/java/net/geant/perfsonar/selftest/SelfTestRunner.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -1,9 +1,146 @@
package net.geant.perfsonar.selftest;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
import net.geant.perfsonar.base.Scheduler;
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+public class SelfTestRunner implements MethodInterceptor {
-public interface SelfTestRunner {
+ 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;
- void runSuite(Scheduler scheduler, SelfTestLogger logger);
+ 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);
+ 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());
+ }
+ }
+
+ 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();
+ }
+
+ 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();
+ }
}
Deleted:
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template
===================================================================
---
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template
2013-05-06 13:08:11 UTC (rev 6351)
@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<nmwg:message xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/"
id="Resp_${requestId}" messageIdRef="${requestId}" type="EchoResponse">
- <nmwg:metadata id="result-code">
- <nmwg:eventType>echo/success</nmwg:eventType>
- </nmwg:metadata>
- <nmwg:data id="result-code-description" metadataIdRef="result-code">
- <nmwgr:datum xmlns:nmwgr="http://ggf.org/ns/nmwg/result/2.0/">This is
the success echo response from the service.</nmwgr:datum>
- </nmwg:data>
-</nmwg:message>
\ No newline at end of file
Copied:
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template
(from rev 6187,
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template)
===================================================================
---
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template
(rev 0)
+++
trunk/perfsonar-base/base/src/main/resources/net/geant/perfsonar/response/EchoResponse.template
2013-05-06 13:08:11 UTC (rev 6351)
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<nmwg:message xmlns:nmwg="http://ggf.org/ns/nmwg/base/2.0/"
id="Resp_${requestId}" messageIdRef="${requestId}" type="EchoResponse">
+#if (${result.get(0).isEmpty()})
+ <nmwg:metadata id="result-code">
+ <nmwg:eventType>echo/success</nmwg:eventType>
+ </nmwg:metadata>
+ <nmwg:data id="result-code-description" metadataIdRef="result-code">
+ <nmwgr:datum xmlns:nmwgr="http://ggf.org/ns/nmwg/result/2.0/">This is
the success echo response from the service.</nmwgr:datum>
+#else
+ <nmwg:metadata id="meta">
+
<nmwg:eventType>http://schemas.perfsonar.net/tools/admin/selftest/1.0</nmwg:eventType>
+ </nmwg:metadata>
+ <nmwg:data id="data" metadataIdRef="meta">
+#foreach($selftest in ${result.get(0)})
+ <nmwg:metadata id="${selftest.name}">
+
<nmwg:eventType>http://schemas.perfsonar.net/tools/admin/selftest/${serviceType}/${serviceName}/${selftest.name}/${selftest.status}/1.0</nmwg:eventType>
+ </nmwg:metadata>
+ <nmwg:data id="data_${selftest.name}" metadataIdRef="${selftest.name}">
+ <nmwg:datum>${selftest.message}</nmwg:datum>
+ </nmwg:data>
+#end
+#end
+ </nmwg:data>
+</nmwg:message>
\ No newline at end of file
Added:
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/Handlers.java
===================================================================
--- trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/Handlers.java
(rev 0)
+++ trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/Handlers.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -0,0 +1,12 @@
+package net.geant.perfsonar;
+
+import static net.geant.perfsonar.Configuration.*;
+
+public class Handlers {
+
+ static {
+ try {
+
configure(when("TestRequest").then(net.geant.perfsonar.PerfSONARTest.class).dummyMethod());
+ } catch (Exception ex) { }
+ }
+}
Modified:
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/PerfSONARTest.java
===================================================================
---
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/PerfSONARTest.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/PerfSONARTest.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -1,22 +1,31 @@
package net.geant.perfsonar;
-import static net.geant.perfsonar.templates.SelfTestResult.failure;
-import static net.geant.perfsonar.templates.SelfTestResult.success;
+import static java.util.Arrays.asList;
+import static net.geant.perfsonar.messaging.SelfTestResult.failure;
+import static net.geant.perfsonar.messaging.SelfTestResult.success;
+import static net.geant.perfsonar.selftest.SelfTestRunner.addSelfTest;
+import static net.geant.perfsonar.selftest.SelfTestRunner.any;
+import static net.geant.perfsonar.selftest.SelfTestRunner.configureForType;
+import static net.geant.perfsonar.selftest.SelfTestRunner.selfTestData;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
-import net.geant.perfsonar.templates.NMWG;
+import java.util.Arrays;
+import java.util.List;
+
+import net.geant.perfsonar.configuration.SampleTests;
import net.geant.perfsonar.helpers.PerfSONARClient;
import net.geant.perfsonar.helpers.Response;
-import net.geant.perfsonar.templates.SelfTestResult;
+import net.geant.perfsonar.selftest.SelfTestRunner;
+import net.geant.perfsonar.templates.NMWG;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.xml.sax.SAXException;
public class PerfSONARTest {
-
protected Logger logger = LoggerFactory.getLogger(getClass());
static final String REQUEST_ID = "request1";
@@ -36,23 +45,61 @@
@Before
public void givenPerfSONARClient() {
+ addSelfTest("constant", "Success Message.",
SampleTests.class).constantTest();
+ addSelfTest("add test ${index}", "Added ${arg}.",
SampleTests.class).add(any(Integer.class));
+ configureForType(Integer.class,
selfTestData(SampleTests.class).values());
+
client = new PerfSONARClient(service);
}
+
+ @After
+ public void tearDown() {
+ SelfTestRunner.clearConfiguration();
+ }
+ @Test
+ public void shouldLoadConfiguration() throws Exception {
+ assertEquals(PerfSONARTest.class.getMethod("dummyMethod"),
Configuration.getHandler("TestRequest"));
+ }
+
+ public int dummyMethod() {
+ return 0;
+ }
+
+ @Test
+ public void checkEchoConfiguration() throws Exception {
+ assertNotNull(Configuration.getHandler("EchoRequest"));
+ }
@Test
- public void checkEcho() throws SAXException {
+ public void checkEcho() throws Exception {
assertEquals(new Response(NMWG.toEchoResponse(REQUEST_ID)),
client.echo(REQUEST_ID));
}
-
+
@Test
- public void checkSelfTest() throws SAXException {
+ public void checkSelfTest() throws Throwable {
assertEquals(
- new Response(NMWG.toSelfTestResponse(
- REQUEST_ID,
- new SelfTestResult[] {
-
success("test1", "Test passed.", "SSHTELNET", "MP"),
-
failure("test2", "Test failed.", "SSHTELNET", "MP"), })),
- client.selfTest(REQUEST_ID));
+ success("constant", "Success Message.",
service.getServiceName(), service.getServiceType().toString()),
+ client.selfTest(REQUEST_ID, "constant"));
}
+
+ @Test
+ public void checkAllSelfTests() throws Throwable {
+ assertEquals(
+ asList(
+ success("add test 0", "Added 1.",
service.getServiceName(), service.getServiceType().toString()),
+ success("add test 1", "Added 2.",
service.getServiceName(), service.getServiceType().toString()),
+ failure("add test 2", "Value must be >= 0",
service.getServiceName(), service.getServiceType().toString()),
+ success("add test 3", "Added 4.",
service.getServiceName(), service.getServiceType().toString()),
+ success("add test 4", "Added 5.",
service.getServiceName(), service.getServiceType().toString()),
+ success("constant", "Success Message.",
service.getServiceName(), service.getServiceType().toString())),
+ sort(client.selfTest(REQUEST_ID)));
+ }
+
+ @SuppressWarnings({ "unchecked" })
+ private <T> List<T> sort(List<T> list) {
+ final Object[] result = list.toArray();
+ Arrays.sort(result);
+ return (List<T>) asList(result);
+ }
}
Deleted:
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/SelfTestRunnerImpl.java
===================================================================
---
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/SelfTestRunnerImpl.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/SelfTestRunnerImpl.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -1,12 +0,0 @@
-package net.geant.perfsonar;
-
-import net.geant.perfsonar.base.Scheduler;
-import net.geant.perfsonar.selftest.SelfTestLogger;
-import net.geant.perfsonar.selftest.SelfTestRunner;
-
-public class SelfTestRunnerImpl implements SelfTestRunner {
- public void runSuite(Scheduler scheduler, SelfTestLogger logger) {
- logger.success("test1", "Test passed.");
- logger.failure("test2", "Test failed.");
- }
-}
Modified:
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/ConfigurationTest.java
===================================================================
---
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/ConfigurationTest.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/ConfigurationTest.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -16,7 +16,7 @@
@Before
public void setUp() throws Exception {
- Configuration.reset();
+ Configuration.clearConfiguration();
}
@Test
Added:
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/SampleTests.java
===================================================================
---
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/SampleTests.java
(rev 0)
+++
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/SampleTests.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -0,0 +1,33 @@
+package net.geant.perfsonar.configuration;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class SampleTests {
+
+ public void constantTest() { }
+
+ public List<Integer> values() {
+ return Arrays.asList(1, 2, -1, 4, 5);
+ }
+
+ public static int result = 0;
+
+ public void add(Integer value) {
+ if (value < 0)
+ throw new IllegalArgumentException("Value must be >=
0");
+
+ result += value;
+ }
+
+ public int getResult() {
+ return result;
+ }
+
+ public void print(String text) {
+ }
+
+ public List<String> strings() {
+ return Arrays.asList("John", "Doe");
+ }
+}
Added:
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/SelfTestsTest.java
===================================================================
---
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/SelfTestsTest.java
(rev 0)
+++
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/configuration/SelfTestsTest.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -0,0 +1,107 @@
+package net.geant.perfsonar.configuration;
+
+import static java.util.Arrays.asList;
+import static net.geant.perfsonar.messaging.SelfTestResult.failure;
+import static net.geant.perfsonar.messaging.SelfTestResult.success;
+import static net.geant.perfsonar.selftest.SelfTestRunner.addSelfTest;
+import static net.geant.perfsonar.selftest.SelfTestRunner.any;
+import static net.geant.perfsonar.selftest.SelfTestRunner.configureForType;
+import static net.geant.perfsonar.selftest.SelfTestRunner.selfTestData;
+import static org.junit.Assert.assertEquals;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+import net.geant.perfsonar.base.Scheduler;
+import net.geant.perfsonar.messaging.SelfTestResult;
+import net.geant.perfsonar.selftest.SelfTestLogger;
+import net.geant.perfsonar.selftest.SelfTestRunner;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SelfTestsTest {
+ private Scheduler scheduler = new Scheduler() {
+ @Override
+ public void runInParallel(Runnable task) {
+ task.run();
+ }
+
+ @Override
+ public void runEvery(int seconds, Runnable task) { }
+
+ @Override
+ public void stop(Runnable task) { }
+
+ @Override
+ public void stopAll() { }
+
+ @Override
+ public void waitToFinish(Runnable... tasks)
+ throws InterruptedException,
ExecutionException {
+ }
+ };
+
+
+ private SelfTestLogger logger = new SelfTestLogger() {
+ @Override
+ public void success(String name, String successMessage) {
+ testResults.add(SelfTestResult.success(name,
successMessage, "name", "type"));
+ }
+
+ @Override
+ public void failure(String name, String message) {
+ testResults.add(SelfTestResult.failure(name, message,
"name", "type"));
+ }
+
+ };
+
+ private List<SelfTestResult> testResults = new
ArrayList<SelfTestResult>();
+
+ @Before
+ public void givenSelfTests() {
+ addSelfTest("constant", "Success Message",
SampleTests.class).constantTest();
+ addSelfTest("add test ${index}", "Added ${arg}.",
SampleTests.class).add(any(Integer.class));
+ configureForType(Integer.class,
selfTestData(SampleTests.class).values());
+ addSelfTest("string test ${index}", "String ${arg}",
SampleTests.class).print(any(String.class));
+ configureForType(String.class,
selfTestData(SampleTests.class).strings());
+ }
+
+ @After
+ public void clearSelfTests() {
+ SelfTestRunner.clearConfiguration();
+ testResults.clear();
+ }
+
+ @Test
+ public void shouldRunTest() throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException, InstantiationException {
+ new SelfTestRunner().runTest("add test 1", scheduler, logger);
+ assertEquals(asList(success("add test 1", "Added 2.", "name",
"type")), testResults);
+ }
+
+ @Test
+ public void shouldRunSuite() throws Exception {
+ new SelfTestRunner().runSuite(scheduler, logger);
+ assertEquals(Arrays.asList(
+ success("add test 0", "Added 1.", "name", "type"),
+ success("add test 1", "Added 2.", "name", "type"),
+ failure("add test 2", "Value must be >= 0", "name",
"type"),
+ success("add test 3", "Added 4.", "name", "type"),
+ success("add test 4", "Added 5.", "name", "type"),
+ success("constant", "Success Message", "name",
"type"),
+ success("string test 0", "String John", "name",
"type"),
+ success("string test 1", "String Doe", "name", "type")
+ ), sort(testResults));
+ }
+
+ @SuppressWarnings({ "unchecked" })
+ private <T> List<T> sort(List<T> list) {
+ final Object[] result = list.toArray();
+ Arrays.sort(result);
+ return (List<T>) asList(result);
+ }
+}
Modified:
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/helpers/PerfSONARClient.java
===================================================================
---
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/helpers/PerfSONARClient.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/helpers/PerfSONARClient.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -1,13 +1,18 @@
package net.geant.perfsonar.helpers;
import java.io.Reader;
+import java.util.List;
import javax.xml.transform.stream.StreamSource;
import net.geant.perfsonar.PerfSONAR;
+import net.geant.perfsonar.messaging.DeserializationException;
+import net.geant.perfsonar.messaging.SelfTestResult;
+import net.geant.perfsonar.messaging.XPathDeserializer;
import net.geant.perfsonar.mp.sshtelnet.Command;
import net.geant.perfsonar.templates.NMWG;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -22,10 +27,25 @@
return new Response(invoke(NMWG.toEchoRequest(id)));
}
- public Response selfTest(String id) throws SAXException {
- return new Response(invoke(NMWG.toSelfTestRequest(id)));
+ @SuppressWarnings("unchecked")
+ public List<SelfTestResult> selfTest(String id) throws SAXException,
DeserializationException {
+ final Response response = new
Response(invoke(NMWG.toSelfTestRequest(id)));
+ final XPathDeserializer deserializer = new
XPathDeserializer();
+ return deserializer.getObjects(SelfTestResult.class,
(List<Object>) deserializer.deserialize(new
InputSource(response.getReader())));
}
+ public SelfTestResult selfTest(String id, String testName) throws
Throwable {
+ final Response response = new
Response(invoke(NMWG.toSelfTestRequest(id, testName)));
+ final XPathDeserializer deserializer = new
XPathDeserializer();
+ @SuppressWarnings("unchecked")
+ final List<SelfTestResult> result =
deserializer.getObjects(SelfTestResult.class, (List<Object>)
deserializer.deserialize(new InputSource(response.getReader())));
+
+ if (result.size() != 1)
+ throw new SAXException("Invalid number of self test
results: " + result.size());
+
+ return result.get(0);
+ }
+
public StreamSource invoke(Reader request) throws SAXException {
final StreamSource source = service.invoke(new
StreamSource(request));
return source;
Modified:
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/helpers/Response.java
===================================================================
---
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/helpers/Response.java
2013-05-06 13:01:14 UTC (rev 6350)
+++
trunk/perfsonar-base/base/src/test/java/net/geant/perfsonar/helpers/Response.java
2013-05-06 13:08:11 UTC (rev 6351)
@@ -12,22 +12,31 @@
public class Response {
private final Reader input;
- protected Response(InputStream input) {
+ public Response(InputStream input) {
+ if (input == null)
+ throw new NullPointerException("Response cannot be null.");
this.input = new InputStreamReader(input);
}
public Response(StreamSource source) {
- if (source.getInputStream() == null)
+ if (source.getInputStream() == null) {
+ if (source.getReader() == null)
+ throw new NullPointerException("Response cannot be
null.");
this.input = source.getReader();
- else
+ } else
this.input = new InputStreamReader(source.getInputStream());
}
public Response(Reader input) {
+ if (input == null)
+ throw new NullPointerException("Response cannot be null.");
this.input = input;
}
public boolean equals(Object obj) {
+ if (obj == null)
+ throw new NullPointerException("Cannot compare against null
object.");
+
if (obj instanceof Response)
return equals(((Response) obj).input);
@@ -37,8 +46,7 @@
try {
return new Diff((Reader) obj, input).identical();
} catch (Exception ex) {
- ex.printStackTrace();
- return false;
+ throw new RuntimeException("Cannot compare XML
responses.", ex);
}
}
- [pS-dev] [GEANT/SA2/ps-java-services] r6351 - in trunk/perfsonar-base/base: . src/main/java/net/geant/perfsonar src/main/java/net/geant/perfsonar/base src/main/java/net/geant/perfsonar/handlers src/main/java/net/geant/perfsonar/response src/main/java/net/geant/perfsonar/selftest src/main/resources/net/geant/perfsonar/response src/test/java/net/geant/perfsonar src/test/java/net/geant/perfsonar/configuration src/test/java/net/geant/perfsonar/helpers, svn-noreply, 05/06/2013
Archive powered by MHonArc 2.6.16.