Skip to content
Browse files

- ParamsDefault implementation and test

  • Loading branch information...
1 parent b166bea commit 1ce77d6beea348c6ad66f27d4866b93b8e2b175b @sushantk committed
View
6 ants/src/ants/Const.java
@@ -8,8 +8,10 @@
}
public static class attribute {
- public static final String id = "@id";
- public static final String klass = "@class";
+ public static final String nodeId = "@id";
+ public static final String nodeClass = "@class";
+
+ public static final String name = "name";
}
public static final String comma = ",";
View
111 ants/src/ants/ObjectFactory.java
@@ -10,7 +10,6 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
-import java.util.List;
import java.util.Map.Entry;
import java.util.HashSet;
import java.util.Set;
@@ -27,7 +26,7 @@
import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
-import ants.api.IConfigurable;
+import ants.api.Configurable;
import ants.exception.ObjectConfigureException;
import ants.exception.ObjectCreateException;
import ants.exception.ObjectIncompleteException;
@@ -80,7 +79,7 @@ public ObjectTree parse(String identifier, InputStream json)
*
* @throws ObjectConfigureException
*/
- public IConfigurable configure(ObjectTree tree, String defaultClass,
+ public Configurable configure(ObjectTree tree, String defaultClass,
String tagName, String id, String defaultListItemClass,
String listItemTag) throws ObjectConfigureException {
Stack<String> tagStack = new Stack<String>();
@@ -118,7 +117,7 @@ public IConfigurable configure(ObjectTree tree, String defaultClass,
* Internal method to create and configure the object recursively with the
* given node
*/
- private IConfigurable configure(JsonNode node,
+ private Configurable configure(JsonNode node,
String defaultClass, String tagName, String id,
String defaultListItemClass, String listItemTag,
Stack<String> tagStack)
@@ -127,9 +126,9 @@ private IConfigurable configure(JsonNode node,
IllegalAccessException, InvocationTargetException,
ClassNotFoundException, InstantiationException {
- IConfigurable object = ObjectFactory.create(node, defaultClass,
+ Configurable object = ObjectFactory.create(node, defaultClass,
tagName, id);
- tagStack.push(ObjectFactory.toString(object));
+ tagStack.push(object.toTagString());
logger.debug("Configuring node: {}", tagStack.peek());
Class<?> klass = object.getClass();
@@ -151,7 +150,7 @@ private IConfigurable configure(JsonNode node,
* Internal method to configure the object recursively with the given node's
* children, treating them as child objects
*/
- private void configureWithObjects(IConfigurable object, ObjectNode node,
+ private void configureWithObjects(Configurable object, ObjectNode node,
Stack<String> tagStack) throws ObjectCreateException,
ObjectIncompleteException, SecurityException,
IllegalArgumentException, NoSuchMethodException,
@@ -160,36 +159,47 @@ private void configureWithObjects(IConfigurable object, ObjectNode node,
HashSet<String> setMethods = new HashSet<String>();
Class<?> klass = object.getClass();
+ ConfigurableClass kannotation = klass.getAnnotation(ConfigurableClass.class);
+ boolean expectsValue = (null == kannotation ? false : kannotation.expectsValue());
Iterator<Entry<String, JsonNode>> it = node.getFields();
while (it.hasNext()) {
Entry<String, JsonNode> entry = it.next();
String childTagName = entry.getKey();
+ JsonNode childNode = entry.getValue();
+
+ // attributes
if (childTagName.startsWith("@")) {
- continue; // attributes
- // TODO: set attribute on parent
+ object.setAttribute(childTagName.substring(1), childNode.asText());
+ continue;
}
String setMethodName = Const.set
+ childTagName.substring(0, 1).toUpperCase()
+ childTagName.substring(1);
- Method method = klass.getMethod(setMethodName, IConfigurable.class);
- ConfigurableMethod annotation = method
+ Method method;
+ if(expectsValue) {
+ method = klass.getMethod(setMethodName, String.class);
+ method.invoke(object, childNode.asText());
+ } else {
+ method = klass.getMethod(setMethodName, Configurable.class);
+ ConfigurableMethod mannotation = method
.getAnnotation(ConfigurableMethod.class);
- String defaultClass = "";
- String defaultListItemClass = "";
- String listItemTag = "";
- if(null != annotation) {
- defaultClass = annotation.defaultClass();
- defaultListItemClass = annotation.defaultListItemClass();
- listItemTag = annotation.listItemTag();
+ String defaultClass = "";
+ String defaultListItemClass = "";
+ String listItemTag = "";
+ if(null != mannotation) {
+ defaultClass = mannotation.defaultClass();
+ defaultListItemClass = mannotation.defaultListItemClass();
+ listItemTag = mannotation.listItemTag();
+ }
+
+ Configurable childObject = this.configure(childNode,
+ defaultClass, childTagName, "",
+ defaultListItemClass, listItemTag, tagStack);
+ method.invoke(object, childObject);
}
- IConfigurable childObject = this.configure(entry.getValue(),
- defaultClass, childTagName, "",
- defaultListItemClass, listItemTag, tagStack);
- method.invoke(object, childObject);
-
setMethods.add(setMethodName);
}
@@ -200,7 +210,7 @@ private void configureWithObjects(IConfigurable object, ObjectNode node,
* Internal method to configure the object recursively with the given node's
* children treating them as a list of child objects
*/
- private void configureWithList(IConfigurable object, JsonNode node,
+ private void configureWithList(Configurable object, JsonNode node,
String defaultListItemClass, String listItemTag,
Stack<String> tagStack)
throws ObjectCreateException, ObjectIncompleteException,
@@ -209,18 +219,23 @@ private void configureWithList(IConfigurable object, JsonNode node,
ClassNotFoundException, InstantiationException {
Class<?> klass = object.getClass();
Method method = klass.getMethod(Const.setList, LinkedHashMap.class);
- ConfigurableMethod annotation = method
- .getAnnotation(ConfigurableMethod.class);
- if(listItemTag.isEmpty()) {
- listItemTag = annotation.listItemTag();
+ ConfigurableMethod mannotation = method
+ .getAnnotation(ConfigurableMethod.class);
+ if(null != mannotation) {
+ if(listItemTag.isEmpty()) {
+ listItemTag = mannotation.listItemTag();
+ }
+ if(defaultListItemClass.isEmpty()) {
+ defaultListItemClass = mannotation.defaultListItemClass();
+ }
}
- LinkedHashMap<String, IConfigurable> childList = new LinkedHashMap<String, IConfigurable>();
+ LinkedHashMap<String, Configurable> childList = new LinkedHashMap<String, Configurable>();
if (node.isArray()) {
Iterator<JsonNode> it = node.getElements();
while (it.hasNext()) {
- IConfigurable childObject = this.configure(it.next(),
+ Configurable childObject = this.configure(it.next(),
defaultListItemClass, listItemTag, "", "", "", tagStack);
childList.put(childObject.getId(), childObject);
}
@@ -229,12 +244,15 @@ private void configureWithList(IConfigurable object, JsonNode node,
while (it.hasNext()) {
Entry<String, JsonNode> entry = it.next();
String childId = entry.getKey();
+ JsonNode childNode = entry.getValue();
+
+ // attributes
if (childId.startsWith("@")) {
- continue; // attribute of the parent object
- // TODO: set it on parent
+ object.setAttribute(childId.substring(1), childNode.asText());
+ continue;
}
- IConfigurable childObject = this.configure(entry.getValue(),
+ Configurable childObject = this.configure(childNode,
defaultListItemClass, listItemTag, childId, "", "", tagStack);
childList.put(childObject.getId(), childObject);
}
@@ -247,18 +265,18 @@ private void configureWithList(IConfigurable object, JsonNode node,
* Verifies if all required parameters are set on an object after it is
* configured
*/
- private static void verify(IConfigurable object, Set<String> setMethods)
+ private static void verify(Configurable object, Set<String> setMethods)
throws ObjectIncompleteException {
ArrayList<String> missingParams = new ArrayList<String>();
Class<?> klass = object.getClass();
Method[] methods = klass.getMethods();
for (Method method : methods) {
- ConfigurableMethod annotation = method
+ ConfigurableMethod mannotation = method
.getAnnotation(ConfigurableMethod.class);
- if (null != annotation) {
+ if (null != mannotation) {
String methodName = method.getName();
- boolean required = annotation.required();
+ boolean required = mannotation.required();
if (required && !setMethods.contains(methodName)) {
missingParams.add(methodName.substring(3).toLowerCase());
}
@@ -273,17 +291,17 @@ private static void verify(IConfigurable object, Set<String> setMethods)
}
/**
- * instantiate an IConfigurable object using the class name specified in the
+ * instantiate an Configurable object using the class name specified in the
* JsonNode or the provided default class name.
*/
- private static IConfigurable create(JsonNode node, String defaultClass,
+ private static Configurable create(JsonNode node, String defaultClass,
String tagName, String id) throws ObjectCreateException,
ClassNotFoundException, SecurityException, NoSuchMethodException,
IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException {
String className = defaultClass;
if (node.isObject()) {
- JsonNode classNode = node.get(Const.attribute.klass);
+ JsonNode classNode = node.get(Const.attribute.nodeClass);
if (null != classNode) {
className = classNode.asText();
}
@@ -297,7 +315,7 @@ private static IConfigurable create(JsonNode node, String defaultClass,
}
if (id.isEmpty()) {
- JsonNode idNode = node.get(Const.attribute.id);
+ JsonNode idNode = node.get(Const.attribute.nodeId);
if (null != idNode) {
id = idNode.asText();
}
@@ -309,15 +327,6 @@ private static IConfigurable create(JsonNode node, String defaultClass,
Class<?> klass = Class.forName(className);
Constructor<?> tc = klass.getConstructor(String.class, String.class);
Object t = tc.newInstance(tagName, id);
- return (IConfigurable) t;
- }
-
- /**
- * return a string representation of an IConfigurable object for debugging
- */
- private static String toString(IConfigurable object) {
- return object.getTag() + "<" + object.getClass().getName() + ", "
- + object.getId() + ">";
+ return (Configurable) t;
}
-
}
View
82 ants/src/ants/ParamsDefault.java
@@ -0,0 +1,82 @@
+package ants;
+
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map.Entry;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import ants.annotation.ConfigurableClass;
+import ants.annotation.ConfigurableMethod;
+import ants.api.Configurable;
+import ants.api.Context;
+import ants.api.IParams;
+import ants.api.IString;
+import ants.exception.ObjectEvaluateException;
+
+/**
+ *
+ */
+@ConfigurableClass(expectsList=true)
+public class ParamsDefault extends Configurable
+ implements IParams {
+ static final Logger logger = LoggerFactory.getLogger(ParamsDefault.class);
+
+ LinkedHashMap<String, Configurable> params;
+
+ public ParamsDefault(String tagName, String id) {
+ super(tagName, id);
+ }
+
+ @ConfigurableMethod(required=true, defaultListItemClass="ants.StringDefault", listItemTag="param")
+ public void setList(LinkedHashMap<String, Configurable> params) {
+ this.params = params;
+ }
+
+ @Override
+ public LinkedHashMap<String, SimpleOrComplex> getPairs(Context context,
+ boolean simple) throws ObjectEvaluateException {
+ LinkedHashMap<String, SimpleOrComplex> result = new LinkedHashMap<String, SimpleOrComplex>();
+
+ Iterator<Entry<String, Configurable>> iter = this.params.entrySet().iterator();
+ while(iter.hasNext()) {
+ Entry<String, Configurable> entry = iter.next();
+ Configurable configurable = entry.getValue();
+
+ String name = configurable.getAttribute(Const.attribute.name);
+ if(null == name) {
+ name = entry.getKey();
+ }
+
+ if(configurable instanceof IString) {
+ IString istring = (IString)configurable;
+ String string = istring.getValue(context);
+ if(simple) {
+ result.put(name, new Simple(string));
+ } else {
+ SimpleOrComplex value = result.get(name);
+ if(null == value) {
+ value = new Complex(new LinkedHashMap<String, SimpleOrComplex>());
+ result.put(name, value);
+ }
+ value.getComplex().put(configurable.getId(), new Simple(string));
+ }
+
+ } else if(!simple && (configurable instanceof IParams)) {
+ IParams iparams = (IParams)configurable;
+ result.put(name, new Complex(iparams.getPairs(context, false)));
+ }
+ else {
+ throw new ObjectEvaluateException("Invalid parameter type, string, list or map expected: " + name,
+ context, this);
+ }
+ }
+
+ return result;
+ }
+
+ public String toString() {
+ return this.params.toString();
+ }
+}
View
6 ants/src/ants/StringDefault.java
@@ -3,11 +3,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
import ants.api.Context;
import ants.api.IString;
+@ConfigurableClass(expectsValue=true)
public class StringDefault extends Configurable
implements IString {
static final Logger logger = LoggerFactory.getLogger(StringDefault.class);
@@ -33,4 +35,8 @@ public static String evaluate(Context context, String text) {
if(context.isLogging()) logger.debug("{} - Evaluated {}=>{}", new Object[]{context, text, evaledText});
return evaledText;
}
+
+ public String toString() {
+ return this.value;
+ }
}
View
8 ants/src/ants/StringLiteral.java
@@ -1,10 +1,12 @@
package ants;
+import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
import ants.api.Context;
import ants.api.IString;
+@ConfigurableClass(expectsValue=true)
public class StringLiteral extends Configurable
implements IString {
@@ -22,5 +24,9 @@ public void setValue(String value) {
@Override
public String getValue(Context context) {
return this.value;
- }
+ }
+
+ public String toString() {
+ return this.value;
+ }
}
View
1 ants/src/ants/annotation/ConfigurableClass.java
@@ -6,4 +6,5 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface ConfigurableClass {
boolean expectsList() default false;
+ boolean expectsValue() default false;
}
View
20 ants/src/ants/api/Configurable.java
@@ -1,14 +1,23 @@
package ants.api;
+import java.util.Map;
+import java.util.TreeMap;
-public abstract class Configurable implements IConfigurable {
+public abstract class Configurable {
private String tagName;
private String id;
+ private Map<String, String> attrs;
public Configurable(String tagName, String id) {
this.tagName = tagName;
this.id = id;
+
+ this.attrs = new TreeMap<String, String>();
+ }
+
+ public String getAttribute(String name) {
+ return this.attrs.get(name);
}
public String getTag() {
@@ -18,4 +27,13 @@ public String getTag() {
public String getId() {
return this.id;
}
+
+ public String setAttribute(String name, String value) {
+ return this.attrs.put(name, value);
+ }
+
+ public String toTagString() {
+ return this.getTag() + "<" + this.getClass().getName() + ", "
+ + this.getId() + ">";
+ }
}
View
8 ants/src/ants/api/IConfigurable.java
@@ -1,8 +0,0 @@
-package ants.api;
-
-public interface IConfigurable {
-
- String getTag();
-
- String getId();
-}
View
6 ants/src/ants/api/IList.java
@@ -2,7 +2,9 @@
import java.util.LinkedHashMap;
-public interface IList extends IConfigurable {
+import ants.exception.ObjectEvaluateException;
- LinkedHashMap<String, IConfigurable> getItems(Context context);
+public interface IList {
+
+ LinkedHashMap<String, Configurable> getItems(Context context) throws ObjectEvaluateException;
}
View
62 ants/src/ants/api/IParams.java
@@ -1,9 +1,67 @@
package ants.api;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-public interface IParams extends IConfigurable {
+import ants.exception.ObjectEvaluateException;
- List<Map.Entry<String, Object>> getPairs(Context context);
+public interface IParams {
+
+ public static abstract class SimpleOrComplex {
+ abstract public String getSimple();
+
+ abstract public LinkedHashMap<String, SimpleOrComplex> getComplex();
+ }
+
+ public static class Simple extends SimpleOrComplex {
+ String string;
+
+ public Simple(String string) {
+ this.string = string;
+ }
+
+ @Override
+ public java.lang.String getSimple() {
+ return string;
+ }
+
+ @Override
+ public LinkedHashMap<String, SimpleOrComplex> getComplex() {
+ return null;
+ }
+
+ public String toString() {
+ return this.string;
+ }
+ }
+
+ public static class Complex extends SimpleOrComplex {
+ LinkedHashMap<String, SimpleOrComplex> complex;
+
+ public Complex(LinkedHashMap<String, SimpleOrComplex> complex) {
+ this.complex = complex;
+ }
+
+ @Override
+ public String getSimple() {
+ if(1 != this.complex.size()) {
+ return null;
+ }
+
+ return this.complex.values().iterator().next().getSimple();
+ }
+
+ @Override
+ public LinkedHashMap<String, SimpleOrComplex> getComplex() {
+ return complex;
+ }
+
+ public String toString() {
+ return this.complex.toString();
+ }
+ }
+
+ LinkedHashMap<String, SimpleOrComplex> getPairs(Context context,
+ boolean simple) throws ObjectEvaluateException;
}
View
5 ants/src/ants/api/IString.java
@@ -1,7 +1,8 @@
package ants.api;
+import ants.exception.ObjectEvaluateException;
-public interface IString extends IConfigurable {
+public interface IString {
- String getValue(Context context);
+ String getValue(Context context) throws ObjectEvaluateException;
}
View
69 ants/src/ants/api/ParamsDefault.java
@@ -1,69 +0,0 @@
-package ants.api;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.TreeMap;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import ants.StringDefault;
-
-@SiriObject(hasSetMethods=false)
-public class ParamsDefault extends Configurable
- implements IParams {
-
- static final Logger s_logger = LoggerFactory.getLogger(StringDefault.class);
-
- ArrayList<IConfigurable> m_params;
-
- public ParamsDefault() {
- m_params = new ArrayList<IConfigurable>();
- }
-
- public void addParameter(IConfigurable a_param) {
- m_params.add(a_param);
- }
-
- @Override
- public Map<String, Object> getMap(Context a_context) {
-
- TreeMap<String, Object> values = new TreeMap<String, Object>();
-
- Iterator<IConfigurable> iter = m_params.iterator();
- while(iter.hasNext()) {
- IConfigurable param = iter.next();
- if(param instanceof IString) {
- IString istring = (IString)param;
- String string = istring.get(a_context);
- if(null == string) {
- s_logger.error("{} - Failed to evaluate parameter: {} inside {}",
- new Object[]{a_context, param, this});
- return null;
- }
- values.put(param.getTag(), string);
- } else if(param instanceof IParams) {
- IParams iparams = (IParams)param;
- Map<String, Object> childValues = iparams.getMap(a_context);
- if(null == childValues) {
- s_logger.error("{} - Failed to evaluate parameter: {} inside {}",
- new Object[]{a_context, param, this});
- return null;
- }
- values.put(param.getTag(), childValues);
- }
- else {
- s_logger.error("{} - Invalid parameter type, string or map expected: {} inside {}",
- new Object[]{a_context, param, this});
- return null;
- }
- }
-
- return values;
- }
-
- public String toDebugString() {
- return m_params.toString();
- }
-}
View
21 ants/src/ants/exception/ObjectEvaluateException.java
@@ -0,0 +1,21 @@
+package ants.exception;
+
+import ants.api.Configurable;
+import ants.api.Context;
+
+public class ObjectEvaluateException extends Exception {
+
+ Context context;
+ Configurable object;
+
+ public ObjectEvaluateException(String s, Context context, Configurable object) {
+ super(s);
+
+ this.context = context;
+ this.object = object;
+ }
+
+ public String toContextString() {
+ return this.context + "/" + object.toTagString() + ": " + this.getMessage();
+ }
+}
View
76 ants/test/ants/test/ObjectFactoryTest.java
@@ -1,29 +1,21 @@
package ants.test;
import java.io.InputStream;
-import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
-import java.util.List;
-
-import junit.framework.TestListener;
import org.junit.Test;
-import com.sun.corba.se.spi.orbutil.fsm.Input;
-
-import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
import static org.junit.Assert.*;
import ants.ObjectFactory;
import ants.ObjectTree;
+import ants.ParamsDefault;
import ants.StringDefault;
import ants.annotation.ConfigurableClass;
import ants.annotation.ConfigurableMethod;
import ants.api.Configurable;
-import ants.api.IConfigurable;
-import ants.api.IList;
import ants.exception.ObjectConfigureException;
import ants.exception.ObjectCreateException;
import ants.exception.ObjectIncompleteException;
@@ -67,14 +59,14 @@ public TestList(String tagName, String id) {
super(tagName, id);
}
- LinkedHashMap<String, IConfigurable> children;
+ LinkedHashMap<String, Configurable> children;
- LinkedHashMap<String, IConfigurable> getChildren() {
+ LinkedHashMap<String, Configurable> getChildren() {
return this.children;
}
@ConfigurableMethod(listItemTag="child")
- public void setList(LinkedHashMap<String, IConfigurable> children) {
+ public void setList(LinkedHashMap<String, Configurable> children) {
this.children = children;
}
}
@@ -86,14 +78,14 @@ public TestParams(String tagName, String id) {
super(tagName, id);
}
- LinkedHashMap<String, IConfigurable> children;
+ LinkedHashMap<String, Configurable> children;
- LinkedHashMap<String, IConfigurable> getChildren() {
+ LinkedHashMap<String, Configurable> getChildren() {
return this.children;
}
@ConfigurableMethod(listItemTag="param")
- public void setList(LinkedHashMap<String, IConfigurable> children) {
+ public void setList(LinkedHashMap<String, Configurable> children) {
this.children = children;
}
}
@@ -104,61 +96,61 @@ public TestClass(String tagName, String id) {
super(tagName, id);
}
- IConfigurable requiredChild;
- IConfigurable notRequiredChild;
- IConfigurable childObject;
- IConfigurable childParams;
+ Configurable requiredChild;
+ Configurable notRequiredChild;
+ Configurable childObject;
+ Configurable childParams;
TestList childList;
TestParams childMap;
- IConfigurable getRequiredChild() {
+ Configurable getRequiredChild() {
return this.requiredChild;
}
- IConfigurable getNotRequiredChild() {
+ Configurable getNotRequiredChild() {
return this.notRequiredChild;
}
- IConfigurable getChildObject() {
+ Configurable getChildObject() {
return this.childObject;
}
- IConfigurable getChildParams() {
+ Configurable getChildParams() {
return this.childParams;
}
- LinkedHashMap<String, IConfigurable> getChildList() {
+ LinkedHashMap<String, Configurable> getChildList() {
return this.childList.getChildren();
}
- LinkedHashMap<String, IConfigurable> getChildMap() {
+ LinkedHashMap<String, Configurable> getChildMap() {
return this.childMap.getChildren();
}
@ConfigurableMethod(required=true)
- public void setRequiredChild(IConfigurable child) {
+ public void setRequiredChild(Configurable child) {
this.requiredChild = child;
}
- public void setNotRequiredChild(IConfigurable child) {
+ public void setNotRequiredChild(Configurable child) {
this.notRequiredChild = child;
}
- public void setChildObject(IConfigurable child) {
+ public void setChildObject(Configurable child) {
this.childObject = child;
}
- public void setChildParams(IConfigurable child) {
+ public void setChildParams(Configurable child) {
this.childParams = child;
}
@ConfigurableMethod(defaultClass="ants.test.ObjectFactoryTest$TestList", listItemTag="item")
- public void setChildList(IConfigurable list) {
+ public void setChildList(Configurable list) {
this.childList = (TestList)list;
}
@ConfigurableMethod(defaultClass="ants.test.ObjectFactoryTest$TestParams")
- public void setChildMap(IConfigurable map) {
+ public void setChildMap(Configurable map) {
this.childMap = (TestParams)map;
}
}
@@ -175,13 +167,11 @@ public void testChildren() throws ObjectParseException, ObjectCreateException, O
assertEquals("Tag is populated", "test", testObject.getTag());
assertEquals("Id is populated", "t1", testObject.getId());
assertEquals("Required child should be a string", StringDefault.class, testObject.getRequiredChild().getClass());
- assertEquals("Not required child should be a string", StringDefault.class, testObject.getNotRequiredChild().getClass());
+ assertEquals("Not required child should be a StringDefault", StringDefault.class, testObject.getNotRequiredChild().getClass());
assertEquals("Child object should be of type TestClass", TestClass.class, testObject.getChildObject().getClass());
assertEquals("Child list should be configured", 2, testObject.getChildList().size());
assertEquals("Child map should be configured", 2, testObject.getChildMap().size());
-
- // test params
-
+ assertEquals("Child params should be ParamsDefault", ParamsDefault.class, testObject.getChildParams().getClass());
}
@Test
@@ -207,12 +197,12 @@ public void testParamsArray() throws ObjectParseException, ObjectCreateException
TestList testList = (TestList)factory.configure(tree, TestList.class.getName(), "params", "", "", "child");
- LinkedHashMap<String, IConfigurable> children = testList.getChildren();
+ LinkedHashMap<String, Configurable> children = testList.getChildren();
assertEquals("Params have a list of children", 2, children.size());
- Iterator<IConfigurable> collection = children.values().iterator();
+ Iterator<Configurable> collection = children.values().iterator();
- IConfigurable first = collection.next();
- IConfigurable second = collection.next();
+ Configurable first = collection.next();
+ Configurable second = collection.next();
assertEquals("First children is a string", StringDefault.class, first.getClass());
assertEquals("Child tag is set", "child", first.getTag());
@@ -230,12 +220,12 @@ public void testParamsMap() throws ObjectParseException, ObjectCreateException,
TestParams testParams = (TestParams)factory.configure(tree, TestParams.class.getName(), "params", "", "", "testParam");
- LinkedHashMap<String, IConfigurable> children = testParams.getChildren();
+ LinkedHashMap<String, Configurable> children = testParams.getChildren();
assertEquals("Params have a list of children", 2, children.size());
- Iterator<IConfigurable> collection = children.values().iterator();
+ Iterator<Configurable> collection = children.values().iterator();
- IConfigurable first = collection.next();
- IConfigurable second = collection.next();
+ Configurable first = collection.next();
+ Configurable second = collection.next();
assertEquals("Child tag is set", "testParam", first.getTag());
assertEquals("Child tag is set", "testParam", second.getTag());
View
140 ants/test/ants/test/ParamsDefaultTest.java
@@ -0,0 +1,140 @@
+package ants.test;
+
+import static org.junit.Assert.*;
+
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+
+import org.junit.Test;
+
+import ants.ObjectFactory;
+import ants.ObjectTree;
+import ants.ParamsDefault;
+import ants.api.Context;
+import ants.api.IParams.SimpleOrComplex;
+import ants.api.RequestContext;
+import ants.exception.ObjectConfigureException;
+import ants.exception.ObjectEvaluateException;
+import ants.exception.ObjectParseException;
+
+public class ParamsDefaultTest {
+
+ @Test
+ public void testSimple() throws ObjectParseException, ObjectConfigureException, ObjectEvaluateException {
+ String jsonFile = "data/ParamsDefault/simple.json";
+ InputStream is = this.getClass().getResourceAsStream(jsonFile);
+ ObjectFactory factory = new ObjectFactory();
+ ObjectTree tree = factory.parse(jsonFile, is);
+
+ Context context = new Context(new RequestContext(), "", "");
+ ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
+ LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, true);
+ assertEquals("Simple params size", 2, result.size());
+ assertEquals("Simple params name value", "v1", result.get("n1").getSimple());
+ assertEquals("Simple params name value", "v2", result.get("n2").getSimple());
+ }
+
+ @Test
+ public void testSimpleOrder() throws ObjectParseException, ObjectConfigureException, ObjectEvaluateException {
+ String jsonFile = "data/ParamsDefault/simpleArray.json";
+ InputStream is = this.getClass().getResourceAsStream(jsonFile);
+ ObjectFactory factory = new ObjectFactory();
+ ObjectTree tree = factory.parse(jsonFile, is);
+
+ Context context = new Context(new RequestContext(), "", "");
+ ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
+ LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, true);
+ assertEquals("Simple params size", 3, result.size());
+
+ Iterator<String> niter = result.keySet().iterator();
+ assertEquals("Simple params name 1", "n1", niter.next());
+ assertEquals("Simple params name 2", "n2", niter.next());
+ assertEquals("Simple params name 3", "n3", niter.next());
+
+ Iterator<SimpleOrComplex> viter = result.values().iterator();
+ assertEquals("Simple params value 1", "v1", viter.next().getSimple());
+ assertEquals("Simple params value 2", "v2", viter.next().getSimple());
+ assertEquals("Simple params value 3", "v3", viter.next().getSimple());
+ }
+
+ @Test
+ public void testComplexAsSimple() throws ObjectParseException, ObjectConfigureException {
+ String jsonFile = "data/ParamsDefault/complex.json";
+ InputStream is = this.getClass().getResourceAsStream(jsonFile);
+ ObjectFactory factory = new ObjectFactory();
+ ObjectTree tree = factory.parse(jsonFile, is);
+
+ Context context = new Context(new RequestContext(), "", "");
+ ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
+ boolean failed = false;
+ try {
+ params.getPairs(context, true);
+ } catch(ObjectEvaluateException e) {
+ failed = true;
+ }
+
+ assertTrue("Complex node cannot be parsed as simple", failed);
+ }
+
+ @Test
+ public void testComplex() throws ObjectParseException, ObjectConfigureException, ObjectEvaluateException {
+ String jsonFile = "data/ParamsDefault/complex.json";
+ InputStream is = this.getClass().getResourceAsStream(jsonFile);
+ ObjectFactory factory = new ObjectFactory();
+ ObjectTree tree = factory.parse(jsonFile, is);
+
+ Context context = new Context(new RequestContext(), "", "");
+ ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
+ LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, false);
+ System.out.println(result);
+
+ assertEquals("Complex params size", 2, result.size());
+ assertEquals("Complex as simple value 1", "v1", result.get("n1").getSimple());
+ assertEquals("Complex not as simple", null, result.get("n2").getSimple());
+
+ LinkedHashMap<String, SimpleOrComplex> complex = result.get("n2").getComplex();
+ assertEquals("Nested complex params size", 2, complex.size());
+ assertEquals("Nested complex value 1", "v21", complex.get("n21").getSimple());
+ assertEquals("Nested complex value 2", "v22", complex.get("n22").getSimple());
+ }
+
+ @Test
+ public void testMultiValue() throws ObjectParseException, ObjectConfigureException, ObjectEvaluateException {
+ String jsonFile = "data/ParamsDefault/multivalue.json";
+ InputStream is = this.getClass().getResourceAsStream(jsonFile);
+ ObjectFactory factory = new ObjectFactory();
+ ObjectTree tree = factory.parse(jsonFile, is);
+
+ Context context = new Context(new RequestContext(), "", "");
+ ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
+ LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, false);
+
+ assertEquals("Simple params size", 1, result.size());
+
+ LinkedHashMap<String, SimpleOrComplex> value = result.get("n1").getComplex();
+ assertEquals("Multivalue size", 2, value.size());
+ assertEquals("Multivalue value 1", "v1", value.get("id1").getSimple());
+ assertEquals("Multivalue value 2", "v2", value.get("id2").getSimple());
+ }
+
+ @Test
+ public void testMultiValueArray() throws ObjectParseException, ObjectConfigureException, ObjectEvaluateException {
+ String jsonFile = "data/ParamsDefault/multivalueArray.json";
+ InputStream is = this.getClass().getResourceAsStream(jsonFile);
+ ObjectFactory factory = new ObjectFactory();
+ ObjectTree tree = factory.parse(jsonFile, is);
+
+ Context context = new Context(new RequestContext(), "", "");
+ ParamsDefault params = (ParamsDefault)factory.configure(tree, ParamsDefault.class.getName(), "", "", "", "");
+ LinkedHashMap<String, SimpleOrComplex> result = params.getPairs(context, false);
+
+ assertEquals("Simple params size", 1, result.size());
+
+ LinkedHashMap<String, SimpleOrComplex> value = result.get("n1").getComplex();
+ assertEquals("Multivalue size", 2, value.size());
+ Iterator<SimpleOrComplex> viter = value.values().iterator();
+ assertEquals("Multivalue value 1", "v1", viter.next().getSimple());
+ assertEquals("Multivalue value 2", "v2", viter.next().getSimple());
+ }
+}
View
4 ants/test/ants/test/data/ObjectFactory/childObjectTree.json
@@ -13,5 +13,9 @@
"childMap": {
"i1": "v1",
"i2": "v2"
+ },
+ "childParams": {
+ "i1": "v1",
+ "i2": "v2"
}
}
View
8 ants/test/ants/test/data/ParamsDefault/complex.json
@@ -0,0 +1,8 @@
+{
+ "n1": "v1",
+ "n2": {
+ "@class": "ants.ParamsDefault",
+ "n21": "v21",
+ "n22": "v22"
+ }
+}
View
10 ants/test/ants/test/data/ParamsDefault/multivalue.json
@@ -0,0 +1,10 @@
+{
+ "id1": {
+ "@name": "n1",
+ "value": "v1"
+ },
+ "id2": {
+ "@name": "n1",
+ "value": "v2"
+ }
+}
View
10 ants/test/ants/test/data/ParamsDefault/multivalueArray.json
@@ -0,0 +1,10 @@
+[
+ {
+ "@name": "n1",
+ "value": "v1"
+ },
+ {
+ "@name": "n1",
+ "value": "v2"
+ }
+]
View
7 ants/test/ants/test/data/ParamsDefault/simple.json
@@ -0,0 +1,7 @@
+{
+ "n1": "v1",
+ "id": {
+ "@name": "n2",
+ "value": "v2"
+ }
+}
View
14 ants/test/ants/test/data/ParamsDefault/simpleArray.json
@@ -0,0 +1,14 @@
+[
+ {
+ "@id": "n1",
+ "value": "v1"
+ },
+ {
+ "@name": "n2",
+ "value": "v2"
+ },
+ {
+ "@name": "n3",
+ "value": "v3"
+ }
+]

0 comments on commit 1ce77d6

Please sign in to comment.
Something went wrong with that request. Please try again.