perfsonar-dev - [pS-dev] [GEANT/SA2/ps-java-services] r6372 - in trunk/perfsonar-base/messaging/src: main/java/net/geant/perfsonar/messaging test/java/net/geant/perfsonar/messaging test/java/net/geant/perfsonar/templates
Subject: perfsonar development work
List archive
[pS-dev] [GEANT/SA2/ps-java-services] r6372 - in trunk/perfsonar-base/messaging/src: main/java/net/geant/perfsonar/messaging test/java/net/geant/perfsonar/messaging test/java/net/geant/perfsonar/templates
Chronological Thread
- From:
- To:
- Subject: [pS-dev] [GEANT/SA2/ps-java-services] r6372 - in trunk/perfsonar-base/messaging/src: main/java/net/geant/perfsonar/messaging test/java/net/geant/perfsonar/messaging test/java/net/geant/perfsonar/templates
- Date: Sun, 12 May 2013 20:32:41 +0100 (BST)
- Authentication-results: sfpop-ironport01.merit.edu; dkim=neutral (message not signed) header.i=none
Author: psnc.pietrzak
Date: 2013-05-12 20:32:41 +0100 (Sun, 12 May 2013)
New Revision: 6372
Added:
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodThrowsExceptionTest.java
Modified:
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/DeserializationException.java
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/Method.java
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/SelfTestResult.java
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/XPathDeserializer.java
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/InvalidXPathsTest.java
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodOnObjectTest.java
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodTest.java
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/StaticMethodTest.java
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/XPathDeserializerTest.java
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/templates/TemplateEngineTest.java
Log:
Fixed concurrency problem, added missing javadocs.
Modified:
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/DeserializationException.java
===================================================================
---
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/DeserializationException.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/DeserializationException.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -4,52 +4,55 @@
import java.util.Iterator;
import java.util.List;
+/**
+ * Aggregate exception for exceptions that are thrown when deserializing
objects.
+ *
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class DeserializationException extends Exception implements
Iterable<Throwable> {
- private static final long serialVersionUID = 2627705969628394226L;
- private List<Throwable> exceptions = new ArrayList<Throwable>();
+ private static final long serialVersionUID = 2627705969628394226L;
+ private List<Throwable> exceptions = new ArrayList<Throwable>();
- public DeserializationException(List<Throwable> exceptions) {
- super(toString(exceptions));
- this.exceptions.addAll(exceptions);
- }
+ public DeserializationException(List<Throwable> exceptions) {
+ super(toString(exceptions));
+ this.exceptions.addAll(exceptions);
+ }
- public DeserializationException(Throwable... ex) {
- super(toString(ex));
- for (Throwable t : ex) {
- exceptions.add(t);
- }
- }
+ public DeserializationException(Throwable... ex) {
+ super(toString(ex));
+ for (Throwable t : ex) {
+ exceptions.add(t);
+ }
+ }
+
+ private static String toString(Throwable[] ex) {
+ final StringBuffer text = new StringBuffer();
+ boolean firstTime = true;
+ for (Throwable t : ex) {
+ if (firstTime)
+ firstTime = false;
+ else
+ text.append("\n");
+ text.append(t.getMessage());
+ }
+ return text.toString();
+ }
- private static String toString(Throwable[] ex) {
- final StringBuffer text = new StringBuffer();
- boolean firstTime = true;
- for (Throwable t : ex) {
- if (firstTime) {
- firstTime = false;
- } else {
- text.append("\n");
- }
- text.append(t.getMessage());
- }
- return text.toString();
- }
+ private static String toString(List<Throwable> exceptions) {
+ final StringBuffer text = new StringBuffer();
+ boolean firstTime = true;
+ for (Throwable ex : exceptions) {
+ if (firstTime)
+ firstTime = false;
+ else
+ text.append("\n");
+ text.append(ex.getMessage());
+ }
+ return text.toString();
+ }
- private static String toString(List<Throwable> exceptions) {
- final StringBuffer text = new StringBuffer();
- boolean firstTime = true;
- for (Throwable ex : exceptions) {
- if (firstTime) {
- firstTime = false;
- } else {
- text.append("\n");
- }
- text.append(ex.getMessage());
- }
- return text.toString();
- }
-
- @Override
- public Iterator<Throwable> iterator() {
- return exceptions.iterator();
- }
+ @Override
+ public Iterator<Throwable> iterator() {
+ return exceptions.iterator();
+ }
}
Modified:
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/Method.java
===================================================================
---
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/Method.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/Method.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -6,111 +6,129 @@
import java.util.List;
import java.util.Map;
+/**
+ * Method executed when given XPath is evaluated successfully.
+ *
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class Method {
+ private final java.lang.reflect.Method method;
+ private final java.lang.reflect.Method identifierMethod;
+ private final Object object;
+
+ private int count = 0;
+
+ public Method(java.lang.reflect.Method method) {
+ this(method, null, null);
+ }
- private final java.lang.reflect.Method method;
- private int count = 0;
+ /**
+ * @param method method to be executed when XPath evaluates
successfully
+ * @param identifierMethod method providing object unique identifier
+ */
+ public Method(java.lang.reflect.Method method,
java.lang.reflect.Method identifierMethod) {
+ this(method, identifierMethod, null);
+ }
+
+ /**
+ * @param method method to be executed when XPath evaluates
successfully
+ * @param identifierMethod method providing object unique identifier
+ * @param object object on which given method should be executed
+ */
+ public Method(java.lang.reflect.Method method,
java.lang.reflect.Method identifierMethod, Object object) {
+ this.method = method;
+ this.identifierMethod = identifierMethod;
+ this.object = object;
+ }
+
+ /**
+ * Executes the method. It creates object of the method's declaring
class if needed and adds it to instances
+ * provided in instances argument.
+ * @param arg method argument
+ * @param instances list of instances (execution may add new object)
+ * @throws Throwable if method throws exception or method cannot be
invoked.
+ */
+ public synchronized void execute(String arg, Map<Class<?>,
List<Object>> instances) throws Throwable {
+ final Object instance;
+ try {
+ instance = getInstance(instances);
+ if (getParameterType().equals(String.class)) {
+ method.invoke(instance, arg);
+ } else {
+ final Object reference =
getReference(instances, arg);
+ if (reference == null)
+ return;
+ method.invoke(instance, reference);
+ }
+ } catch (InvocationTargetException ex) {
+ throw new RuntimeException("Method: " + toString(),
ex.getTargetException());
+ }
+
+ putInstance(instances, instance);
+
+ count++;
+ }
+
+ private void putInstance(Map<Class<?>, List<Object>> instances,
Object instance) {
+ if (instances.get(method.getDeclaringClass()) == null) {
+ instances.put(method.getDeclaringClass(), new
ArrayList<Object>());
+ }
+ if (instances.get(method.getDeclaringClass()).size() < count
+ 1)
+
instances.get(method.getDeclaringClass()).add(instance);
+ }
- private final java.lang.reflect.Method identifierMethod;
- private final Object object;
+ public synchronized int getCount() {
+ return count;
+ }
+
+ public synchronized void resetCount() {
+ count = 0;
+ }
+
+ protected synchronized Object getInstance(Map<Class<?>, List<Object>>
instances) throws InstantiationException, IllegalAccessException {
+ if (Modifier.isStatic(method.getModifiers()))
+ throw new IllegalStateException("Static methods are
forbidden for xpath configuration: " + method);
+ if (object != null)
+ return object;
+
+ if (instances.get(method.getDeclaringClass()) == null)
+ return method.getDeclaringClass().newInstance();
+
+ if (instances.get(method.getDeclaringClass()).size() > count)
{
+ return
instances.get(method.getDeclaringClass()).get(count);
+ }
+
+ return method.getDeclaringClass().newInstance();
+ }
+
+ protected Object getReference(Map<Class<?>, List<Object>> instances,
String value) throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
+ if (identifierMethod == null)
+ return null;
+
+ if (instances.get(identifierMethod.getDeclaringClass()) ==
null)
+ return null;
+
+ for (Object instance :
instances.get(identifierMethod.getDeclaringClass())) {
+ try {
+ if
(identifierMethod.invoke(instance).equals(value))
+ return instance;
+ } catch (NullPointerException ex) {
+ throw new NullPointerException("Identifier
value is not set for " + instance
+ + ". Check xpath for setting
identifier or verify whether xpath for reference is correct.");
+ }
+ }
+
+ return null;
+ }
- public Method(java.lang.reflect.Method method) {
- this(method, null, null);
- }
-
- public Method(java.lang.reflect.Method method, java.lang.reflect.Method
identifierMethod) {
- this(method, identifierMethod, null);
- }
-
- public Method(java.lang.reflect.Method method, java.lang.reflect.Method
identifierMethod, Object object) {
- this.method = method;
- this.identifierMethod = identifierMethod;
- this.object = object;
- }
-
- public void execute(String arg, Map<Class<?>, List<Object>> instances)
throws Throwable {
- try {
- if (getParameterType().equals(String.class)) {
- method.invoke(getInstance(instances), arg);
- } else {
- final Object reference = getReference(instances, arg);
- if (reference == null) {
- return;
- }
- method.invoke(getInstance(instances), reference);
- }
- } catch (InvocationTargetException ex) {
- throw new RuntimeException("Method: " + toString(),
ex.getTargetException());
- // throw ex.getTargetException();
- }
-
- count++;
- }
-
- public int getCount() {
- return count;
- }
-
- protected Object getInstance(Map<Class<?>, List<Object>> instances)
throws InstantiationException,
- IllegalAccessException {
- if (Modifier.isStatic(method.getModifiers())) {
- throw new IllegalStateException("Static methods are forbidden
for xpath configuration: " + method);
- }
- if (object != null) {
- return object;
- }
- if (instances.get(method.getDeclaringClass()) == null) {
- instances.put(method.getDeclaringClass(), new
ArrayList<Object>());
- }
- if (instances.get(method.getDeclaringClass()).size() < count + 1) {
-
instances.get(method.getDeclaringClass()).add(method.getDeclaringClass().newInstance());
- }
-
- try {
- return instances.get(method.getDeclaringClass()).get(count);
- } catch (IndexOutOfBoundsException ex) {
- ex.printStackTrace();
- throw ex;
- }
- }
-
- protected Object getReference(Map<Class<?>, List<Object>> instances,
String value) throws IllegalAccessException,
- InvocationTargetException {
- if (identifierMethod == null) {
- return null;
- }
-
- if (instances.get(identifierMethod.getDeclaringClass()) == null) {
- return null;
- }
-
- for (Object instance :
instances.get(identifierMethod.getDeclaringClass())) {
- try {
- if (identifierMethod.invoke(instance).equals(value)) {
- return instance;
- }
- } catch (NullPointerException ex) {
- throw new NullPointerException("Identifier value is not set
for " + instance
- + ". Check xpath for setting identifier or verify
whether xpath for reference is correct.");
- }
- }
-
- return null;
- }
-
- public boolean isParameterType(Class<?> type) {
- return getParameterType().equals(type);
- }
-
- private Class<?> getParameterType() {
- return method.getParameterTypes()[0];
- }
-
- public void resetCount() {
- count = 0;
- }
-
- public String toString() {
- return this.method.toString() + " [" + count + "]";
- }
+ private Class<?> getParameterType() {
+ return method.getParameterTypes()[0];
+ }
+
+ /**
+ * @see java.lang.Object#toString()
+ */
+ public String toString() {
+ return this.method.toString() + " [" + count + "]";
+ }
}
Modified:
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/SelfTestResult.java
===================================================================
---
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/SelfTestResult.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/SelfTestResult.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -4,166 +4,156 @@
import static net.geant.perfsonar.messaging.XPathDeserializer.declarePrefix;
import static net.geant.perfsonar.messaging.XPathDeserializer.whenXPath;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
/**
- * This class is used in unit tests to check the behavior of a service wrt
self
- * tests
- *
- * @author Blazej Pietrzak -
- * @author Antoine Delvaux -
- *
+ * Represents result of running Self Test.
+ *
+ * @author <a
href=mailto:">Blazej
Pietrzak</a>, <a
href="mailto:">Antoine
Delvaux</a>
*/
public class SelfTestResult implements Comparable<SelfTestResult> {
- protected static Logger logger =
LoggerFactory.getLogger("SelfTestResult");
- static {
- declarePrefix("nmwg", "http://ggf.org/ns/nmwg/base/2.0/");
- try {
-
whenXPath("nmwg:message/nmwg:data/nmwg:metadata/@id").then(SelfTestResult.class).setName(any(String.class));
+ static {
+ declarePrefix("nmwg", "http://ggf.org/ns/nmwg/base/2.0/");
+ try {
+
whenXPath("nmwg:message/nmwg:data/nmwg:metadata/@id").then(
+
SelfTestResult.class).setName(any(String.class));
-
whenXPath("nmwg:message/nmwg:data/nmwg:data/nmwg:datum/text()").then(SelfTestResult.class).setMessage(
- any(String.class));
+
whenXPath("nmwg:message/nmwg:data/nmwg:data/nmwg:datum/text()")
+
.then(SelfTestResult.class).setMessage(any(String.class));
-
whenXPath("nmwg:message/nmwg:data/nmwg:metadata/nmwg:eventType/text()").then(SelfTestResult.class)
- .setEventType(any(String.class));
- } catch (Exception ex) {
- logger.debug(ex.getMessage());
- }
- }
+ whenXPath(
+
"nmwg:message/nmwg:data/nmwg:metadata/nmwg:eventType/text()")
+
.then(SelfTestResult.class).setEventType(any(String.class));
+ } catch (Exception ex) {
+ }
+ }
- protected String name;
- protected String message;
- protected String status;
- protected String serviceName;
- protected String serviceType;
+ protected String name;
+ protected String message;
+ protected String status;
+ protected String serviceName;
+ protected String serviceType;
- public SelfTestResult(String name, String message, String status, String
serviceName, String serviceType) {
- this.name = name;
- this.message = message;
- this.status = status;
- this.serviceName = serviceName;
- this.serviceType = serviceType;
- }
+ public SelfTestResult(String name, String message, String status,
+ String serviceName, String serviceType) {
+ this.name = name;
+ this.message = message;
+ this.status = status;
+ this.serviceName = serviceName;
+ this.serviceType = serviceType;
+ }
- public SelfTestResult() {
- }
+ public SelfTestResult() {
+ }
- public static SelfTestResult failure(String name, String message, String
serviceName, String serviceType) {
- return new SelfTestResult(name, message, "failure", serviceName,
serviceType);
- }
+ public static SelfTestResult failure(String name, String message,
+ String serviceName, String serviceType) {
+ return new SelfTestResult(name, message, "failure",
serviceName,
+ serviceType);
+ }
- public static SelfTestResult success(String name, String message, String
serviceName, String serviceType) {
- return new SelfTestResult(name, message, "success", serviceName,
serviceType);
- }
+ public static SelfTestResult success(String name, String message,
+ String serviceName, String serviceType) {
+ return new SelfTestResult(name, message, "success",
serviceName,
+ serviceType);
+ }
- public void setName(String name) {
- this.name = name;
- }
+ public void setName(String name) {
+ this.name = name;
+ }
- public String getName() {
- return name;
- }
+ public String getName() {
+ return name;
+ }
- public void setMessage(String message) {
- this.message = message;
- }
+ public void setMessage(String message) {
+ this.message = message;
+ }
- public String getMessage() {
- return message;
- }
+ public String getMessage() {
+ return message;
+ }
- public void setStatus(String status) {
- this.status = status;
- }
+ public void setStatus(String status) {
+ this.status = status;
+ }
- public String getStatus() {
- return status;
- }
+ public String getStatus() {
+ return status;
+ }
- @Override
- public int compareTo(SelfTestResult arg) {
- return getName().compareTo(arg.getName());
- }
+ @Override
+ public int compareTo(SelfTestResult arg) {
+ return getName().compareTo(arg.getName());
+ }
- public void setEventType(String eventType) {
- if (eventType.contains("success")) {
- status = "success";
- } else {
- status = "failure";
- }
+ public void setEventType(String eventType) {
+ if (eventType.contains("success"))
+ status = "success";
+ else
+ status = "failure";
- String[] elems =
eventType.substring("http://schemas.perfsonar.net/tools/admin/selftest/".length()).split("/");
+ String[] elems = eventType.substring(
+
"http://schemas.perfsonar.net/tools/admin/selftest/".length())
+ .split("/");
+
+ serviceType = elems[0];
+ serviceName = elems[1];
+ }
- serviceType = elems[0];
- serviceName = elems[1];
- }
+ @Override
+ public String toString() {
+ return status + " (name=" + name + ", message=" + message
+ + ", service-name=" + serviceName + ",
service-type="
+ + serviceType + ")";
+ }
- @Override
- public String toString() {
- return status + " (name=" + name + ", message=" + message + ",
service-name=" + serviceName + ", service-type="
- + serviceType + ")";
- }
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((message == null) ? 0 :
message.hashCode());
+ result = prime * result + ((name == null) ? 0 :
name.hashCode());
+ result = prime * result
+ + ((serviceName == null) ? 0 :
serviceName.hashCode());
+ result = prime * result
+ + ((serviceType == null) ? 0 :
serviceType.hashCode());
+ result = prime * result + ((status == null) ? 0 :
status.hashCode());
+ return result;
+ }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((message == null) ? 0 :
message.hashCode());
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- result = prime * result + ((serviceName == null) ? 0 :
serviceName.hashCode());
- result = prime * result + ((serviceType == null) ? 0 :
serviceType.hashCode());
- result = prime * result + ((status == null) ? 0 : status.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- SelfTestResult other = (SelfTestResult) obj;
- if (message == null) {
- if (other.message != null) {
- return false;
- }
- } else if (!message.equals(other.message)) {
- return false;
- }
- if (name == null) {
- if (other.name != null) {
- return false;
- }
- } else if (!name.equals(other.name)) {
- return false;
- }
- if (serviceName == null) {
- if (other.serviceName != null) {
- return false;
- }
- } else if (!serviceName.equals(other.serviceName)) {
- return false;
- }
- if (serviceType == null) {
- if (other.serviceType != null) {
- return false;
- }
- } else if (!serviceType.equals(other.serviceType)) {
- return false;
- }
- if (status == null) {
- if (other.status != null) {
- return false;
- }
- } else if (!status.equals(other.status)) {
- return false;
- }
- return true;
- }
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ SelfTestResult other = (SelfTestResult) obj;
+ if (message == null) {
+ if (other.message != null)
+ return false;
+ } else if (!message.equals(other.message))
+ return false;
+ if (name == null) {
+ if (other.name != null)
+ return false;
+ } else if (!name.equals(other.name))
+ return false;
+ if (serviceName == null) {
+ if (other.serviceName != null)
+ return false;
+ } else if (!serviceName.equals(other.serviceName))
+ return false;
+ if (serviceType == null) {
+ if (other.serviceType != null)
+ return false;
+ } else if (!serviceType.equals(other.serviceType))
+ return false;
+ if (status == null) {
+ if (other.status != null)
+ return false;
+ } else if (!status.equals(other.status))
+ return false;
+ return true;
+ }
}
Modified:
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/XPathDeserializer.java
===================================================================
---
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/XPathDeserializer.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/main/java/net/geant/perfsonar/messaging/XPathDeserializer.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -22,169 +22,180 @@
/**
* Deserializes data from XML stream.
*
- * This class provides API for configuring XPath expressions that trigger
method
- * invocation with values of the expression evaluation. Please note that it
uses
- * streaming XPaths, so only a subset of expressions can be used. Those that
- * require creation of DOM tree are not allowed.
+ * <p>This class provides API for configuring XPath expressions that trigger
+ * method invocation with values of the expression evaluation.
+ * Please note that it uses streaming XPaths, so only a subset of
expressions can
+ * be used. Those that require creation of DOM tree are not allowed.
+ * There are two approaches to configure deserializer: via API or via
annotations.</p>
*
- * There are two approaches to configure deserializer: via API or via
- * annotations. By invoking: <code>declarePrefix("nmwg", uri)</code> one
informs
- * the deserializer about prefixes for various namespaces. By invoking:
- *
<code>whenXPath("/nmwg:message/@id").then(NMWGMessage.class).setId(any(String.class))</code>
- * deserializer will fire setId method from instance of NMWGMesssage class
and
- * provide as an argument the result of XPath expression provided in
whenXPath
- * method. During configuration no real method is executed. The following
- * configuration is developer friendly. It statically checks whether the
method
- * exists and in case of refactoring it will be reconfigured automatically.
+ * <p>By invoking:<br/>
+ * <code>declarePrefix("nmwg", uri)</code><br/>
+ * one informs the deserializer about prefixes for various namespaces.</p>
*
- * By invoking: <code>whenXPath(
- * "nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]/@metadataIdRef")
- * .then(Command.class).setCurrentRouter(
- * getReference(Router.class,
identifier(Router.class).getId()));</code>
+ * <p>By invoking:<br/>
+ *
<code>whenXPath("/nmwg:message/@id").then(NMWGMessage.class).setId(any(String.class))</code><br/>
+ * deserializer will fire setId method from instance of NMWGMesssage class
and provide
+ * as an argument the result of XPath expression provided in whenXPath
method.
+ * During configuration no real method is executed.
+ * The following configuration is developer friendly. It statically checks
whether
+ * the method exists and in case of refactoring it will be reconfigured
automatically.</p>
*
- * deserializer will invoke setCurrentRouter method and will provide a real
- * instance (not string) of Router class identified by getId method in
defined
- * in Router class.
+ * <p>By invoking:<br/>
+ * <code>whenXPath("nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]/@metadataIdRef")
+ * .then(Command.class).setCurrentRouter(
+ * getReference(Router.class,
identifier(Router.class).getId()));</code><br/>
+ *
+ * deserializer will invoke setCurrentRouter method and will provide a real
instance (not string)
+ * of Router class identified by getId method defined in Router class.</p>
*
- * Configuring deserializer via annotations requires psbase3-apt tool to be
- * present on the classpath during compilation. By invoking:
+ * <p>Configuring deserializer via annotations requires
net.geant.perfsonar.base/apt tool to be present on
+ * the classpath during compilation. Namespaces are defined globally, so it
is not needed to define the same
+ * namespace in all xpaths.</p>
+ * <p>When declaring:<br/>
*
<code>@XPath(xpath="nmwg:message/nmwg:metadata[@id='meta']/nmwg:eventType/text()",
- *
namespaces=@Namespace(prefix="nmwg",
uri=NMWG_URI)) public void setEventType(String type)</code>
+ *
namespaces=@Namespace(prefix="nmwg",
uri=NMWG_URI))<br/>
+ * public void setEventType(String type)</code><br/>
+ * setEventType method will be invoked when the xpath will be evaluated.</p>
+ *
+ * <p>One can declare multiple xpaths for the given method using XPaths
annotation.
+ * If any of these xpaths is evaluated successfully then the given method is
invoked. Below is the example:<br/>
+ * <code>@XPaths({<br/>
+
@XPath(xpath="nmwg:message/nmwg:metadata/@metadataIdRef"),<br/>
+ @XPath(xpath="nmwg:message/@id",
namespaces=@Namespace(prefix="nmwg",
uri=NMWG_URI))<br/>
+ })</code><br/>
*
- * setEventType method will be invoked when the xpath will be evaluated.
- *
+ * <p>In order to configure identifier method one must annotate the method
with Identifier annotation
+ * (see example below).<br/>
+ * <code>@Identifier<br/>
+ * public String getId();</code><br/>
+ * </p>
* @author <a
href="mailto:">Blazej
Pietrzak</a>
- *
*/
public class XPathDeserializer {
- private static final DefaultNamespaceContext CONTEXT = new
DefaultNamespaceContext();
- private static final Map<String, Method> XPATHS = new Hashtable<String,
Method>();
- private static final Map<Class<?>, java.lang.reflect.Method> IDENTIFIERS
= new Hashtable<Class<?>, java.lang.reflect.Method>();
+ private final static DefaultNamespaceContext context = new
DefaultNamespaceContext();
+ private final static Map<String, Method> xpaths = new
Hashtable<String, Method>();
+ private final static Map<Class<?>, java.lang.reflect.Method>
identifiers = new Hashtable<Class<?>, java.lang.reflect.Method>();
+
+ public static void declarePrefix(String prefix, String uri) {
+ context.declarePrefix(prefix, uri);
+ }
+
+ public static XPathConfiguration whenXPath(String xpath) {
+ return new XPathConfiguration(xpath);
+ }
+
+ public static <T> T getReference(Class<T> type, String id) throws
InstantiationException, IllegalAccessException {
+ return type.newInstance();
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> T identifier(Class<T> type) {
+ return (T) Enhancer.create(type, new ReferenceInterceptor());
+ }
+
+ public static java.lang.reflect.Method findIdentifier(Class<?> type) {
+ return identifiers.get(type);
+ }
+
+ public static <T> T any(Class<T> type) {
+ return null;
+ }
+
+ public static void putXPath(String xpath, Object object,
java.lang.reflect.Method method) {
+ xpaths.put(xpath, new Method(method,
identifiers.get(method.getParameterTypes()[0]), object));
+ }
+
+ public static synchronized void putXPath(String xpath,
java.lang.reflect.Method method) {
+ xpaths.put(xpath, new Method(method,
identifiers.get(method.getParameterTypes()[0])));
+ }
+
+ protected static void putIdentifier(Class<?> declaringClass,
java.lang.reflect.Method method) {
+ XPathDeserializer.identifiers.put(declaringClass, method);
+ }
+
+ public static void clearConfiguration() {
+ xpaths.clear();
+ }
+
+ private static synchronized Set<String> getXPaths() {
+ return xpaths.keySet();
+ }
+
+ protected static Method getXPathMethod(String xpath) {
+ return xpaths.get(xpath);
+ }
+
+ public static Map<String, Method> getConfiguration() {
+ return xpaths;
+ }
- public static void declarePrefix(String prefix, String uri) {
- CONTEXT.declarePrefix(prefix, uri);
- }
+ public XPathDeserializer() {
+ }
+
+ public static String validate(String xpath) {
+ final XMLDog dog = new XMLDog(context);
+ try {
+ dog.addXPath(xpath);
+ } catch (XPathSyntaxException ex) {
+ return "Invalid XPath: " + ex.getMultilineMessage();
+ } catch (SAXPathException ex) {
+ return "Invalid XPath: " + ex.getMessage();
+ }
+
+ return null;
+ }
- public static XPathConfiguration whenXPath(String xpath) {
- return new XPathConfiguration(xpath);
- }
-
- public static <T> T getReference(Class<T> type, String id) throws
InstantiationException, IllegalAccessException {
- return type.newInstance();
- }
-
- @SuppressWarnings("unchecked")
- public static <T> T identifier(Class<T> type) {
- return (T) Enhancer.create(type, new ReferenceInterceptor());
- }
-
- public static java.lang.reflect.Method findIdentifier(Class<?> type) {
- return IDENTIFIERS.get(type);
- }
-
- public static <T> T any(Class<T> type) {
- return null;
- }
-
- public static void putXPath(String xpath, Object object,
java.lang.reflect.Method method) {
- XPATHS.put(xpath, new Method(method,
IDENTIFIERS.get(method.getParameterTypes()[0]), object));
- }
-
- public static synchronized void putXPath(String xpath,
java.lang.reflect.Method method) {
- XPATHS.put(xpath, new Method(method,
IDENTIFIERS.get(method.getParameterTypes()[0])));
- }
-
- protected static void putIdentifier(Class<?> declaringClass,
java.lang.reflect.Method method) {
- XPathDeserializer.IDENTIFIERS.put(declaringClass, method);
- }
-
- public static void clearConfiguration() {
- XPATHS.clear();
- }
-
- private static synchronized Set<String> getXPaths() {
- return XPATHS.keySet();
- }
-
- protected static Method getXPathMethod(String xpath) {
- return XPATHS.get(xpath);
- }
-
- public static Map<String, Method> getConfiguration() {
- return XPATHS;
- }
-
- public XPathDeserializer() {
- }
-
- public static String validate(String xpath) {
- final XMLDog dog = new XMLDog(CONTEXT);
- try {
- dog.addXPath(xpath);
- } catch (XPathSyntaxException ex) {
- return "Invalid XPath: " + ex.getMultilineMessage();
- } catch (SAXPathException ex) {
- return "Invalid XPath: " + ex.getMessage();
- }
-
- return null;
- }
-
- /**
- * Deserializes objects from xml stream using streaming XPath
expressions.
- *
- * @throws Throwable
- */
- public List<?> deserialize(InputSource reader) throws
DeserializationException {
- final XMLDog dog = new XMLDog(CONTEXT);
- final List<Throwable> exceptions = new ArrayList<Throwable>();
- for (Iterator<String> iter =
XPathDeserializer.getXPaths().iterator(); iter.hasNext();) {
- try {
- dog.addXPath(iter.next());
- } catch (XPathSyntaxException ex) {
- exceptions.add(new XPathException("Invalid XPath: " +
ex.getMultilineMessage()));
- } catch (SAXPathException ex) {
- exceptions.add(new XPathException("Invalid XPath: " +
ex.getMessage()));
- }
- }
-
- if (exceptions.size() > 0) {
- throw new DeserializationException(exceptions);
- }
-
- final Event event = dog.createEvent(); // must be after all
expressions
- // are added.
- event.setXMLBuilder(new DOMBuilder());
-
- final InMemoryInstancesListener listener = new
InMemoryInstancesListener();
-
- event.setListener(listener);
-
- try {
- dog.sniff(event, reader, false/* useSTAX */);
- } catch (XPathException ex) {
- throw new DeserializationException(ex);
- } finally {
- for (Method method : XPATHS.values()) {
- method.resetCount();
- }
- }
-
- if (listener.getExceptions().size() > 0) {
- throw new DeserializationException(listener.getExceptions());
- }
-
- return listener.getObjects();
- }
-
- @SuppressWarnings("unchecked")
- public <T> List<T> getObjects(Class<T> type, List<Object> objects) {
- final List<T> result = new ArrayList<T>();
- for (Object obj : objects) {
- if (obj.getClass().equals(type)) {
- result.add((T) obj);
- }
- }
- return result;
- }
+ /**
+ * Deserializes objects from xml stream using streaming XPath
expressions.
+ * @throws DeserializationException list of exceptions thrown while
deserializing
+ */
+ public List<?> deserialize(InputSource reader) throws
DeserializationException {
+ final XMLDog dog = new XMLDog(context);
+ final List<Throwable> exceptions = new ArrayList<Throwable>();
+ for (Iterator<String> iter =
XPathDeserializer.getXPaths().iterator(); iter.hasNext(); ) {
+ try {
+ dog.addXPath(iter.next());
+ } catch (XPathSyntaxException ex) {
+ exceptions.add(new XPathException("Invalid
XPath: " + ex.getMultilineMessage()));
+ } catch (SAXPathException ex) {
+ exceptions.add(new XPathException("Invalid
XPath: " + ex.getMessage()));
+ }
+ }
+
+ if (exceptions.size() > 0)
+ throw new DeserializationException(exceptions);
+
+ final Event event = dog.createEvent(); // must be after all
expressions are added.
+ event.setXMLBuilder(new DOMBuilder());
+
+ final InMemoryInstancesListener listener = new
InMemoryInstancesListener();
+
+ event.setListener(listener);
+
+ try {
+ dog.sniff(event, reader, false/*useSTAX*/);
+ } catch (XPathException ex) {
+ throw new DeserializationException(ex);
+ } finally {
+ for (Method method : xpaths.values()) {
+ method.resetCount();
+ }
+ }
+
+ if (listener.getExceptions().size() > 0)
+ throw new
DeserializationException(listener.getExceptions());
+
+ return listener.getObjects();
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> List<T> getObjects(Class<T> type, List<Object> objects) {
+ final List<T> result = new ArrayList<T>();
+ for (Object obj : objects) {
+ if (obj.getClass().equals(type)) {
+ result.add((T) obj);
+ }
+ }
+ return result;
+ }
}
Modified:
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/InvalidXPathsTest.java
===================================================================
---
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/InvalidXPathsTest.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/InvalidXPathsTest.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -22,69 +22,76 @@
import org.junit.Test;
import org.xml.sax.InputSource;
+/**
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class InvalidXPathsTest {
- private static final String REQUEST_ID = "1";
+ private static final String REQUEST_ID = "1";
- private Reader reader;
- private XPathDeserializer deserializer;
+ private Reader reader;
+ private XPathDeserializer deserializer;
- @Before
- public void givenMessageAndDeserializationConfigured() throws Exception {
- reader = NMWG.toMetadataKeyResponse(REQUEST_ID,
- new Router("metadata0", "test1",
InetAddress.getAllByName("192.168.1.1")[0], Command.PING_IPV6));
+ @Before
+ public void givenMessageAndDeserializationConfigured() throws
Exception {
+ reader = NMWG.toMetadataKeyResponse(REQUEST_ID, new
Router("metadata0",
+ "test1",
InetAddress.getAllByName("192.168.1.1")[0],
+ Command.PING_IPV6));
- declarePrefix("nmwg", "http://ggf.org/ns/nmwg/base/2.0/");
- deserializer = new XPathDeserializer();
- }
+ declarePrefix("nmwg", "http://ggf.org/ns/nmwg/base/2.0/");
+ deserializer = new XPathDeserializer();
+ }
- @After
- public void clearConfiguration() {
- XPathDeserializer.clearConfiguration();
- }
+ @After
+ public void clearConfiguration() {
+ XPathDeserializer.clearConfiguration();
+ reader = null;
+ deserializer = null;
+ }
- @Test
- public void shouldThrowExceptionWhenInvalidXPathSyntax() throws
Exception {
- whenXPath("invalid
xpath1").then(Router.class).setId(any(String.class));
- whenXPath("invalid
xpath2").then(Router.class).setAddress(any(String.class));
+ @Test
+ public void shouldThrowExceptionWhenInvalidXPathSyntax() throws
Exception {
+ whenXPath("invalid
xpath1").then(Router.class).setId(any(String.class));
+ whenXPath("invalid xpath2").then(Router.class).setAddress(
+ any(String.class));
- try {
- deserializer.deserialize(new InputSource(reader));
- fail("DeserializationException was not thrown");
- } catch (DeserializationException ex) {
- final Iterator<Throwable> iter = ex.iterator();
- assertEquals(XPathException.class, iter.next().getClass());
- assertEquals(XPathException.class, iter.next().getClass());
- assertFalse(iter.hasNext());
- }
- }
+ try {
+ deserializer.deserialize(new InputSource(reader));
+ fail("DeserializationException was not thrown");
+ } catch (DeserializationException ex) {
+ final Iterator<Throwable> iter = ex.iterator();
+ assertEquals(XPathException.class,
iter.next().getClass());
+ assertEquals(XPathException.class,
iter.next().getClass());
+ assertFalse(iter.hasNext());
+ }
+ }
- @Test
- public void shouldThrowExceptionWhenXPathReturnsNull() throws Exception {
-
whenXPath("nmwg:message/nmwg:metadata[nmwg:parameters]/nmwg:subject").then(Router.class).setName(
- any(String.class));
+ @Test
+ public void shouldThrowExceptionWhenXPathReturnsNull() throws
Exception {
+
whenXPath("nmwg:message/nmwg:metadata[nmwg:parameters]/nmwg:subject")
+
.then(Router.class).setName(any(String.class));
- try {
- deserializer.deserialize(new InputSource(reader));
- fail("DeserializationException was not thrown");
- } catch (DeserializationException ex) {
- final Iterator<Throwable> iter = ex.iterator();
- assertEquals(XPathException.class, iter.next().getClass());
- assertFalse(iter.hasNext());
- }
- }
+ try {
+ deserializer.deserialize(new InputSource(reader));
+ fail("DeserializationException was not thrown");
+ } catch (DeserializationException ex) {
+ final Iterator<Throwable> iter = ex.iterator();
+ assertEquals(XPathException.class,
iter.next().getClass());
+ assertFalse(iter.hasNext());
+ }
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenUndeclaredPrefix() throws
Exception {
+
whenXPath("nmwg:message/nmwg:data/nmwgr:datum/nmwg:eventType/text()")
+
.then(Command.class).setEventType(any(String.class));
- @Test
- public void shouldThrowExceptionWhenUndeclaredPrefix() throws Exception {
-
whenXPath("nmwg:message/nmwg:data/nmwgr:datum/nmwg:eventType/text()").then(Command.class).setEventType(
- any(String.class));
-
- try {
- deserializer.deserialize(new InputSource(reader));
- fail("DeserializationException was not thrown");
- } catch (DeserializationException ex) {
- final Iterator<Throwable> iter = ex.iterator();
- assertEquals(XPathException.class, iter.next().getClass());
- assertFalse(iter.hasNext());
- }
- }
+ try {
+ deserializer.deserialize(new InputSource(reader));
+ fail("DeserializationException was not thrown");
+ } catch (DeserializationException ex) {
+ final Iterator<Throwable> iter = ex.iterator();
+ assertEquals(XPathException.class,
iter.next().getClass());
+ assertFalse(iter.hasNext());
+ }
+ }
}
Modified:
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodOnObjectTest.java
===================================================================
---
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodOnObjectTest.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodOnObjectTest.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -1,6 +1,6 @@
package net.geant.perfsonar.messaging;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
import java.util.Hashtable;
import java.util.List;
@@ -12,29 +12,32 @@
import org.junit.Before;
import org.junit.Test;
+/**
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class MethodOnObjectTest {
- private final Map<Class<?>, List<Object>> instances = new
Hashtable<Class<?>, List<Object>>();
- private final Command expected = new Command();
- private Method method;
+ final Map<Class<?>, List<Object>> instances = new Hashtable<Class<?>,
List<Object>>();
+ final Command expected = new Command();
+ private Method method;
+
+ @Before
+ public void givenMethodWithObject() throws Exception {
+ expected.setName("object");
+ method = new Method(Command.class.getMethod("setName",
String.class), null, expected);
+ }
- @Before
- public void givenMethodWithObject() throws Exception {
- expected.setName("object");
- method = new Method(Command.class.getMethod("setName",
String.class), null, expected);
- }
+ @After
+ public void tearDown() throws Exception {
+ method = null;
+ }
- @After
- public void tearDown() throws Exception {
- method = null;
- }
-
- @Test
- public void shouldReturnObject() throws Exception {
- assertTrue(expected == method.getInstance(instances));
- }
-
- @Test
- public void shouldBeEmpty() {
- assertTrue(instances.isEmpty());
- }
+ @Test
+ public void shouldReturnObject() throws Exception {
+ assertTrue(expected == method.getInstance(instances));
+ }
+
+ @Test
+ public void shouldBeEmpty() {
+ assertTrue(instances.isEmpty());
+ }
}
Modified:
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodTest.java
===================================================================
---
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodTest.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodTest.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -14,85 +14,92 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-
+
+/**
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class MethodTest {
- private Method eventTypeMethod;
- private Method commandNameMethod;
- private Method currentRouterMethod;
- private Method routerIdMethod;
- private Method routerNameMethod;
+ private Method eventTypeMethod;
+ private Method commandNameMethod;
+ private Method currentRouterMethod;
+ private Method routerIdMethod;
+ private Method routerNameMethod;
+
+ private final Map<Class<?>, List<Object>> instances = new
Hashtable<Class<?>, List<Object>>();
+ private final Map<Class<?>, List<Object>> expected = new
Hashtable<Class<?>, List<Object>>();
- private final Map<Class<?>, List<Object>> instances = new
Hashtable<Class<?>, List<Object>>();
- private final Map<Class<?>, List<Object>> expected = new
Hashtable<Class<?>, List<Object>>();
+ @Before
+ public void givenMethods() throws Throwable {
+ new Command();
+ eventTypeMethod = createCommandMethod("setEventType",
String.class);
+ commandNameMethod = createCommandMethod("setName",
String.class);
+ currentRouterMethod = new
Method(RouterReference.class.getMethod("setCurrentRouter", Router.class),
Router.class.getMethod("getId"));
+ routerIdMethod = new Method(Router.class.getMethod("setId",
String.class), Router.class.getMethod("getId"));
+ routerNameMethod = new
Method(Router.class.getMethod("setName", String.class),
Router.class.getMethod("getId"));
+ whenExecutingMethods(instances);
+ }
- @Before
- public void setUp() throws Throwable {
- new Command();
- eventTypeMethod = createCommandMethod("setEventType", String.class);
- commandNameMethod = createCommandMethod("setName", String.class);
- currentRouterMethod = new
Method(RouterReference.class.getMethod("setCurrentRouter", Router.class),
- Router.class.getMethod("getId"));
- routerIdMethod = new Method(Router.class.getMethod("setId",
String.class), Router.class.getMethod("getId"));
- routerNameMethod = new Method(Router.class.getMethod("setName",
String.class), Router.class.getMethod("getId"));
- // instances.put((Class)Router.class, (List)Arrays.asList(router));
- whenExecutingMethods(instances);
- }
+ private Method createCommandMethod(String methodName, Class<?>...
params) throws NoSuchMethodException {
+ return new Method(Command.class.getMethod(methodName,
params), Command.class.getMethod("getId"));
+ }
- private Method createCommandMethod(String methodName, Class<?>...
params) throws NoSuchMethodException {
- return new Method(Command.class.getMethod(methodName, params),
Command.class.getMethod("getId"));
- }
+ @After
+ public void tearDown() throws Exception {
+ instances.clear();
+ expected.clear();
+ new RouterReference().setCurrentRouter(null);
+ this.eventTypeMethod = null;
+ this.commandNameMethod = null;
+ this.currentRouterMethod = null;
+ this.routerIdMethod = null;
+ this.routerNameMethod = null;
+ }
- @After
- public void tearDown() throws Exception {
- instances.clear();
- expected.clear();
- new RouterReference().setCurrentRouter(null);
- }
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Test
+ public void shouldReturnTwoCommandsAndRouter() throws Throwable {
+ final Router router = createExpectedRouter();
+ expected.put((Class<?>) Router.class, (List)
Arrays.asList(createExpectedRouter()));
+ expected.put((Class<?>) Command.class, (List)
router.getCommands());
+ expected.put((Class<?>) RouterReference.class,
instances.get(RouterReference.class));
+
+ assertEquals(expected, instances);
+ }
+
+ @Test
+ public void shouldSetCurrentRouter() {
+ assertEquals(createExpectedRouter(),
+ RouterReference.getCurrentRouter());
+ }
- @SuppressWarnings({ "unchecked", "rawtypes" })
- @Test
- public void shouldReturnTwoCommandsAndRouter() throws Throwable {
- final Router router = createRouter();
- expected.put((Class<?>) Router.class, (List)
Arrays.asList(createRouter()));
- expected.put((Class<?>) Command.class, (List) router.getCommands());
- expected.put((Class<?>) RouterReference.class,
instances.get(RouterReference.class));
+ private Router createExpectedRouter() {
+ return new Router("router2", "router", null,
+ new Command("test1", null, null, "testing1"),
+ new Command("test2", null, null, "testing2"));
+ }
- assertEquals(expected, instances);
- }
+ private void whenExecutingMethods(
+ final Map<Class<?>, List<Object>> instances) throws
Throwable {
- @Test
- public void shouldSetCurrentRouter() {
- assertEquals(createRouter(),
-
- RouterReference.getCurrentRouter());
- }
-
- private Router createRouter() {
- return new Router("router2", "router", null, new Command("test1",
null, null, "testing1"), new Command("test2",
- null, null, "testing2"));
- }
-
- private void whenExecutingMethods(final Map<Class<?>, List<Object>>
instances) throws Throwable {
-
- // router.setId("router2");
- routerIdMethod.execute("router2", instances);
-
- // router.setName("router");
- routerNameMethod.execute("router", instances);
-
- // RouterReference.setCurrentRouter(getReference("router2"));
- currentRouterMethod.execute("router2", instances);
-
- // command1.setEventType("testing1");
- eventTypeMethod.execute("testing1", instances);
-
- // command1.setName("test1");
- commandNameMethod.execute("test1", instances);
-
- // command2.setName("test2");
- commandNameMethod.execute("test2", instances);
-
- // command2.setEventType("testing2");
- eventTypeMethod.execute("testing2", instances);
- }
+ //router.setId("router2");
+ routerIdMethod.execute("router2", instances);
+
+ //router.setName("router");
+ routerNameMethod.execute("router", instances);
+
+ //RouterReference.setCurrentRouter(getReference("router2"));
+ currentRouterMethod.execute("router2", instances);
+
+ //command1.setEventType("testing1");
+ eventTypeMethod.execute("testing1", instances);
+
+ //command1.setName("test1");
+ commandNameMethod.execute("test1", instances);
+
+ //command2.setName("test2");
+ commandNameMethod.execute("test2", instances);
+
+ //command2.setEventType("testing2");
+ eventTypeMethod.execute("testing2", instances);
+ }
}
Added:
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodThrowsExceptionTest.java
===================================================================
---
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodThrowsExceptionTest.java
(rev 0)
+++
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/MethodThrowsExceptionTest.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -0,0 +1,49 @@
+package net.geant.perfsonar.messaging;
+
+import static org.junit.Assert.*;
+
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
+public class MethodThrowsExceptionTest {
+
+ private Method method;
+ private Map<Class<?>, List<Object>> instances = new
Hashtable<Class<?>, List<Object>>();
+
+ public void throwException(String arg) throws Exception {
+ throw new IllegalArgumentException("test exception.");
+ }
+
+ @Before
+ public void whenMethodThrowsException() throws Throwable {
+ method = new Method(getClass().getMethod("throwException",
String.class));
+ try {
+ method.execute("argument", instances);
+ fail("Exception was not thrown.");
+ } catch (RuntimeException ex) { }
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ method = null;
+ instances = null;
+ }
+
+ @Test
+ public void shouldNotInstantiateObject() throws Throwable {
+ assertEquals(new Hashtable<Class<?>, List<Object>>(),
instances);
+ }
+
+ @Test
+ public void shouldNotIncreaseCounter() {
+ assertEquals(0, method.getCount());
+ }
+}
Modified:
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/StaticMethodTest.java
===================================================================
---
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/StaticMethodTest.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/StaticMethodTest.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -11,32 +11,33 @@
import org.junit.Before;
import org.junit.Test;
+/**
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class StaticMethodTest {
+ final Map<Class<?>, List<Object>> instances = new Hashtable<Class<?>,
List<Object>>();
+ private Method method;
+
+ public static void staticMethod() { }
- private final Map<Class<?>, List<Object>> instances = new
Hashtable<Class<?>, List<Object>>();
- private Method method;
+ @Before
+ public void givenStaticMethod() throws Exception {
+ method = new
Method(StaticMethodTest.class.getMethod("staticMethod"));
+ }
- public static void staticMethod() {
- }
+ @After
+ public void tearDown() throws Exception {
+ method = null;
+ }
- @Before
- public void givenStaticMethod() throws Exception {
- method = new
Method(StaticMethodTest.class.getMethod("staticMethod"));
- }
+ @Test(expected=IllegalStateException.class)
+ public void shouldThrowException() throws Exception {
+ assertNull(method.getInstance(instances));
+ }
+
+ @Test
+ public void shouldBeEmpty() {
+ assertTrue(instances.isEmpty());
+ }
- @After
- public void tearDown() throws Exception {
- method = null;
- }
-
- @Test(expected = IllegalStateException.class)
- public void shouldThrowException() throws Exception {
- assertNull(method.getInstance(instances));
- }
-
- @Test
- public void shouldBeEmpty() {
- assertTrue(instances.isEmpty());
- }
-
}
Modified:
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/XPathDeserializerTest.java
===================================================================
---
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/XPathDeserializerTest.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/messaging/XPathDeserializerTest.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -22,71 +22,85 @@
import org.junit.Test;
import org.xml.sax.InputSource;
+/**
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class XPathDeserializerTest {
- private static final String REQUEST_ID = "1";
+ private static final String REQUEST_ID = "1";
- private Router[] routers;
- private Reader reader;
- private XPathDeserializer deserializer;
+ private Router[] routers;
+ private Reader reader;
+ private XPathDeserializer deserializer;
- @Before
- public void givenMessageAndDeserializationConfigured() throws Exception {
+ @Before
+ public void givenMessageAndDeserializationConfigured() throws
Exception {
- routers = new Router[] {
- new Router("metadata0", "test1",
InetAddress.getAllByName("192.168.1.1")[0], Command.PING_IPV6),
- new Router("metadata1", "test2",
InetAddress.getAllByName("192.168.1.2")[0], Command.SHOW_ENVIRONMENT) };
+ routers = new Router[] {
+ new Router("metadata0", "test1",
+
InetAddress.getAllByName("192.168.1.1")[0],
+ Command.PING_IPV6),
+ new Router("metadata1", "test2",
+
InetAddress.getAllByName("192.168.1.2")[0],
+ Command.SHOW_ENVIRONMENT) };
- reader = NMWG.toMetadataKeyResponse(REQUEST_ID, routers);
+ reader = NMWG.toMetadataKeyResponse(REQUEST_ID, routers);
- declarePrefix("nmwg", "http://ggf.org/ns/nmwg/base/2.0/");
- declarePrefix("nmwgr", "http://ggf.org/ns/nmwg/result/2.0/");
+ declarePrefix("nmwg", "http://ggf.org/ns/nmwg/base/2.0/");
+ declarePrefix("nmwgr", "http://ggf.org/ns/nmwg/result/2.0/");
-
whenXPath("nmwg:message/nmwg:metadata[nmwg:parameters]/@id").then(Router.class).setId(any(String.class));
+ whenXPath("nmwg:message/nmwg:metadata[nmwg:parameters]/@id")
+ .then(Router.class).setId(any(String.class));
-
whenXPath("nmwg:message/nmwg:metadata/nmwg:parameters/nmwg:parameter[@name='url']/@value").then(Router.class)
- .setAddress(any(String.class));
+ whenXPath(
+
"nmwg:message/nmwg:metadata/nmwg:parameters/nmwg:parameter[@name='url']/@value")
+
.then(Router.class).setAddress(any(String.class));
+
+ whenXPath(
+
"nmwg:message/nmwg:metadata[nmwg:parameters]/nmwg:subject/text()")
+
.then(Router.class).setName(any(String.class));
-
whenXPath("nmwg:message/nmwg:metadata[nmwg:parameters]/nmwg:subject/text()").then(Router.class).setName(
- any(String.class));
+ whenXPath(
+
"nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]/@metadataIdRef")
+ .then(RouterReference.class).setCurrentRouter(
+ getReference(Router.class,
identifier(Router.class).getId()));
- whenXPath("nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]/@metadataIdRef").then(
-
RouterReference.class).setCurrentRouter(getReference(Router.class,
identifier(Router.class).getId()));
+ whenXPath(
+
"nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]/nmwgr:datum/nmwg:parameters/nmwg:parameter[@name='command']/@value")
+
.then(Command.class).setName(any(String.class));
- whenXPath(
- "nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]"
- +
"/nmwgr:datum/nmwg:parameters/nmwg:parameter[@name='command']/@value")
- .then(Command.class).setName(any(String.class));
+ whenXPath(
+
"nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]/nmwgr:datum/nmwg:parameters/nmwg:parameter[@name='description']/@value")
+
.then(Command.class).setDescription(any(String.class));
- whenXPath(
- "nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]"
- +
"/nmwgr:datum/nmwg:parameters/nmwg:parameter[@name='description']/@value")
- .then(Command.class).setDescription(any(String.class));
+ whenXPath(
+
"nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]/nmwgr:datum/nmwg:parameters/nmwg:parameter[@name='syntax']/@value")
+
.then(Command.class).setSyntax(any(String.class));
+
+ whenXPath(
+
"nmwg:message/nmwg:data/nmwgr:datum/nmwg:eventType/text()")
+
.then(Command.class).setEventType(any(String.class));
+
+ deserializer = new XPathDeserializer();
+ }
- whenXPath(
- "nmwg:message/nmwg:data[starts-with(@metadataIdRef,
'metadata')]"
- +
"/nmwgr:datum/nmwg:parameters/nmwg:parameter[@name='syntax']/@value")
- .then(Command.class).setSyntax(any(String.class));
+ @After
+ public void clearConfiguration() {
+ XPathDeserializer.clearConfiguration();
+ routers = null;
+ reader = null;
+ }
-
whenXPath("nmwg:message/nmwg:data/nmwgr:datum/nmwg:eventType/text()").then(Command.class).setEventType(
- any(String.class));
-
- deserializer = new XPathDeserializer();
- }
-
- @After
- public void clearConfiguration() {
- XPathDeserializer.clearConfiguration();
- }
-
- @SuppressWarnings("unchecked")
- @Test
- public void shouldReturnRouters() throws Throwable {
- final List<Object> actual = (List<Object>)
deserializer.deserialize(new InputSource(reader));
-
- assertEquals(Arrays.asList(Command.PING_IPV6,
Command.SHOW_ENVIRONMENT),
- deserializer.getObjects(Command.class, actual));
- assertEquals(Arrays.asList(new Router("metadata0", "test1",
InetAddress.getAllByName("192.168.1.1")[0],
- Command.PING_IPV6), new Router("metadata1", "test2",
InetAddress.getAllByName("192.168.1.2")[0],
- Command.SHOW_ENVIRONMENT)),
deserializer.getObjects(Router.class, actual));
- }
+ @SuppressWarnings("unchecked")
+ @Test
+ public void shouldReturnRouters() throws Throwable {
+ final List<Object> actual = (List<Object>)
deserializer.deserialize(new InputSource(reader));
+
+ assertEquals(Arrays.asList(Command.PING_IPV6,
Command.SHOW_ENVIRONMENT),
+ deserializer.getObjects(Command.class, actual));
+
+ assertEquals(Arrays.asList(
+ new Router("metadata0", "test1",
InetAddress.getAllByName("192.168.1.1")[0], Command.PING_IPV6),
+ new Router("metadata1", "test2",
InetAddress.getAllByName("192.168.1.2")[0], Command.SHOW_ENVIRONMENT)),
+ deserializer.getObjects(Router.class, actual));
+ }
}
Modified:
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/templates/TemplateEngineTest.java
===================================================================
---
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/templates/TemplateEngineTest.java
2013-05-08 20:41:45 UTC (rev 6371)
+++
trunk/perfsonar-base/messaging/src/test/java/net/geant/perfsonar/templates/TemplateEngineTest.java
2013-05-12 19:32:41 UTC (rev 6372)
@@ -1,20 +1,23 @@
package net.geant.perfsonar.templates;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
import java.io.StringWriter;
import net.geant.perfsonar.messaging.data.Dummy;
import org.junit.Test;
+/**
+ * @author <a
href="mailto:">Blazej
Pietrzak</a>
+ */
public class TemplateEngineTest {
- @Test
- public void shouldProcessTemplateFromJar() throws Exception {
- final StringWriter out = new StringWriter();
- TemplateEngine.put("name", "John Doe");
- TemplateEngine.evaluate(out, Dummy.class, "Hello.template");
- assertEquals("Hello John Doe!", out.toString());
- }
+ @Test
+ public void shouldProcessTemplateFromJar() throws Exception {
+ final StringWriter out = new StringWriter();
+ TemplateEngine.put("name", "John Doe");
+ TemplateEngine.evaluate(out, Dummy.class, "Hello.template");
+ assertEquals("Hello John Doe!", out.toString());
+ }
}
- [pS-dev] [GEANT/SA2/ps-java-services] r6372 - in trunk/perfsonar-base/messaging/src: main/java/net/geant/perfsonar/messaging test/java/net/geant/perfsonar/messaging test/java/net/geant/perfsonar/templates, svn-noreply, 05/12/2013
Archive powered by MHonArc 2.6.16.