diff options
4 files changed, 375 insertions, 75 deletions
diff --git a/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java b/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java index af8aec2b66..2d70e70d23 100644 --- a/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java +++ b/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java @@ -16,6 +16,7 @@ package org.eclipse.jetty.xml; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; +import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -25,15 +26,14 @@ import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; -import java.security.AccessControlException; import java.security.AccessController; import java.security.PrivilegedAction; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -46,6 +46,7 @@ import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.resource.Resource; +import org.eclipse.jetty.xml.XmlParser.Node; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -71,6 +72,9 @@ public class XmlConfiguration private static final Integer ZERO = new Integer(0); private static final Iterable<?> __factoryLoader; + + private static final XmlParser __parser = initParser(); + static { Iterable<?> loader=null; @@ -93,46 +97,41 @@ public class XmlConfiguration } /* ------------------------------------------------------------ */ - private static XmlParser __parser; private URL _url; - private XmlParser.Node _config; private String _dtd; private ConfigurationProcessor _processor; private final Map<String, Object> _idMap = new HashMap<String, Object>(); private final Map<String, String> _propertyMap = new HashMap<String, String>(); /* ------------------------------------------------------------ */ - private synchronized static void initParser() throws IOException + private synchronized static XmlParser initParser() { - if (__parser != null) - return; - - __parser = new XmlParser(); + XmlParser parser = new XmlParser(); try { URL config60 = Loader.getResource(XmlConfiguration.class,"org/eclipse/jetty/xml/configure_6_0.dtd",true); URL config76 = Loader.getResource(XmlConfiguration.class,"org/eclipse/jetty/xml/configure_7_6.dtd",true); - __parser.redirectEntity("configure.dtd",config76); - __parser.redirectEntity("configure_1_0.dtd",config60); - __parser.redirectEntity("configure_1_1.dtd",config60); - __parser.redirectEntity("configure_1_2.dtd",config60); - __parser.redirectEntity("configure_1_3.dtd",config60); - __parser.redirectEntity("configure_6_0.dtd",config60); - __parser.redirectEntity("configure_7_6.dtd",config76); - - - __parser.redirectEntity("http://jetty.mortbay.org/configure.dtd",config76); - __parser.redirectEntity("http://jetty.eclipse.org/configure.dtd",config76); - __parser.redirectEntity("http://www.eclipse.org/jetty/configure.dtd",config76); - - __parser.redirectEntity("-//Mort Bay Consulting//DTD Configure//EN",config76); - __parser.redirectEntity("-//Jetty//Configure//EN",config76); + parser.redirectEntity("configure.dtd",config76); + parser.redirectEntity("configure_1_0.dtd",config60); + parser.redirectEntity("configure_1_1.dtd",config60); + parser.redirectEntity("configure_1_2.dtd",config60); + parser.redirectEntity("configure_1_3.dtd",config60); + parser.redirectEntity("configure_6_0.dtd",config60); + parser.redirectEntity("configure_7_6.dtd",config76); + + parser.redirectEntity("http://jetty.mortbay.org/configure.dtd",config76); + parser.redirectEntity("http://jetty.eclipse.org/configure.dtd",config76); + parser.redirectEntity("http://www.eclipse.org/jetty/configure.dtd",config76); + + parser.redirectEntity("-//Mort Bay Consulting//DTD Configure//EN",config76); + parser.redirectEntity("-//Jetty//Configure//EN",config76); } catch (ClassNotFoundException e) { LOG.warn(e.toString()); LOG.debug(e); } + return parser; } /* ------------------------------------------------------------ */ @@ -143,7 +142,6 @@ public class XmlConfiguration */ public XmlConfiguration(URL configuration) throws SAXException, IOException { - initParser(); synchronized (__parser) { _url=configuration; @@ -163,7 +161,6 @@ public class XmlConfiguration */ public XmlConfiguration(String configuration) throws SAXException, IOException { - initParser(); configuration = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<!DOCTYPE Configure PUBLIC \"-//Mort Bay Consulting//DTD Configure 1.2//EN\" \"http://jetty.eclipse.org/configure_1_2.dtd\">" + configuration; InputSource source = new InputSource(new StringReader(configuration)); @@ -185,7 +182,6 @@ public class XmlConfiguration */ public XmlConfiguration(InputStream configuration) throws SAXException, IOException { - initParser(); InputSource source = new InputSource(configuration); synchronized (__parser) { @@ -197,7 +193,6 @@ public class XmlConfiguration /* ------------------------------------------------------------ */ private void setConfig(XmlParser.Node config) { - _config=config; if ("Configure".equals(config.getTag())) { _processor=new JettyXmlConfiguration(); @@ -228,7 +223,7 @@ public class XmlConfiguration { throw new IllegalArgumentException("Unknown XML tag:"+config.getTag()); } - _processor.init(_url,_config,_idMap, _propertyMap); + _processor.init(_url,config,_idMap, _propertyMap); } @@ -242,6 +237,7 @@ public class XmlConfiguration /** * @deprecated use {@link #getIdMap()}.put(...) */ + @Deprecated public void setIdMap(Map<String, Object> map) { _idMap.clear(); @@ -252,6 +248,7 @@ public class XmlConfiguration /** * @deprecated use {@link #getProperties()}.putAll(...) */ + @Deprecated public void setProperties(Map<String, String> map) { _propertyMap.clear(); @@ -311,7 +308,7 @@ public class XmlConfiguration public Object configure(Object obj) throws Exception { // Check the class of the object - Class<?> oClass = (Class<?>)nodeClass(_config); + Class<?> oClass = nodeClass(_config); if (oClass != null && !oClass.isInstance(obj)) { String loaders = (oClass.getClassLoader()==obj.getClass().getClassLoader())?"":"Object Class and type Class are from different loaders."; @@ -324,7 +321,7 @@ public class XmlConfiguration /* ------------------------------------------------------------ */ public Object configure() throws Exception { - Class<?> oClass = (Class<?>)nodeClass(_config); + Class<?> oClass = nodeClass(_config); String id = _config.getAttribute("id"); Object obj = id == null?null:_idMap.get(id); @@ -340,7 +337,7 @@ public class XmlConfiguration } /* ------------------------------------------------------------ */ - private Class<?> nodeClass(XmlParser.Node node) throws ClassNotFoundException + private static Class<?> nodeClass(XmlParser.Node node) throws ClassNotFoundException { String className = node.getAttribute("class"); if (className == null) @@ -389,7 +386,7 @@ public class XmlConfiguration else if ("Ref".equals(tag)) refObj(obj,node); else if ("Property".equals(tag)) - propertyObj(obj,node); + propertyObj(node); else throw new IllegalStateException("Unknown tag: " + tag); } @@ -417,13 +414,13 @@ public class XmlConfiguration Object[] arg = { value }; - Class oClass = nodeClass(node); + Class<?> oClass = nodeClass(node); if (oClass != null) obj = null; else oClass = obj.getClass(); - Class[] vClass = + Class<?>[] vClass = { Object.class }; if (value != null) vClass[0] = value.getClass(); @@ -455,7 +452,7 @@ public class XmlConfiguration try { Field type = vClass[0].getField("TYPE"); - vClass[0] = (Class)type.get(null); + vClass[0] = (Class<?>)type.get(null); Method set = oClass.getMethod(name,vClass); set.invoke(obj,arg); return; @@ -518,26 +515,24 @@ public class XmlConfiguration LOG.ignore(e); } - // Can we convert to a collection - if (paramTypes[0].isAssignableFrom(Collection.class) && value.getClass().isArray()) + Class<?> parameterClass = paramTypes[0]; + Collection<?> collection = tryToConvertObjectToCollection(parameterClass,value); + if (collection != null) { try { - if (paramTypes[0].isAssignableFrom(Set.class)) - sets[s].invoke(obj,new Object[] - { new HashSet<Object>(Arrays.asList((Object[])value)) }); - else - sets[s].invoke(obj,new Object[] - { Arrays.asList((Object[])value) }); + sets[s].invoke(obj,collection); return; } - catch (IllegalArgumentException e) + catch (IllegalAccessException e) { LOG.ignore(e); } - catch (IllegalAccessException e) + catch (IllegalArgumentException e) { - LOG.ignore(e); + // it's ok to throw here as we wouldn't be here if parameterClass is not a Collection and this is the only place we evaluate + // collections + throw new IllegalArgumentException("Not a supported Collection type."); } } } @@ -548,7 +543,7 @@ public class XmlConfiguration { try { - Class sClass = set.getParameterTypes()[0]; + Class<?> sClass = set.getParameterTypes()[0]; if (sClass.isPrimitive()) { for (int t = 0; t < __primitives.length; t++) @@ -560,7 +555,7 @@ public class XmlConfiguration } } } - Constructor cons = sClass.getConstructor(vClass); + Constructor<?> cons = sClass.getConstructor(vClass); arg[0] = cons.newInstance(arg); set.invoke(obj,arg); return; @@ -583,6 +578,31 @@ public class XmlConfiguration throw new NoSuchMethodException(oClass + "." + name + "(" + vClass[0] + ")"); } + /** + * @return a collection if compareValueToClass is a Set or List. null if that's not the case or value can't be converted to a Collection + */ + private static Collection<?> tryToConvertObjectToCollection(Class<?> compareValueToClass, Object value) + { + Collection<?> collection = null; + if (value.getClass().isArray()) + { + if (HashSet.class.isAssignableFrom(compareValueToClass) || Set.class.isAssignableFrom(compareValueToClass)) + collection = new HashSet<Object>(convertArrayToList(value)); + else if (ArrayList.class.isAssignableFrom(compareValueToClass) || List.class.isAssignableFrom(compareValueToClass)) + collection = convertArrayToList(value); + } + return collection; + } + + private static List<Object> convertArrayToList(Object value) + { + int length = Array.getLength(value); + List<Object> list = new ArrayList<Object>(length); + for (int i = 0; i < length; i++) + list.add(Array.get(value,i)); + return list; + } + /* ------------------------------------------------------------ */ /* * Call a put method. @@ -593,6 +613,7 @@ public class XmlConfiguration { if (!(obj instanceof Map)) throw new IllegalArgumentException("Object for put is not a Map: " + obj); + @SuppressWarnings("unchecked") Map<Object, Object> map = (Map<Object, Object>)obj; String name = node.getAttribute("name"); @@ -610,7 +631,7 @@ public class XmlConfiguration */ private Object get(Object obj, XmlParser.Node node) throws Exception { - Class oClass = nodeClass(node); + Class<?> oClass = nodeClass(node); if (oClass != null) obj = null; else @@ -657,7 +678,7 @@ public class XmlConfiguration private Object call(Object obj, XmlParser.Node node) throws Exception { String id = node.getAttribute("id"); - Class oClass = nodeClass(node); + Class<?> oClass = nodeClass(node); if (oClass != null) obj = null; else if (obj != null) @@ -718,7 +739,7 @@ public class XmlConfiguration */ private Object newObj(Object obj, XmlParser.Node node) throws Exception { - Class oClass = nodeClass(node); + Class<?> oClass = nodeClass(node); String id = node.getAttribute("id"); int size = 0; int argi = node.size(); @@ -748,7 +769,7 @@ public class XmlConfiguration LOG.debug("XML new " + oClass); // Lets just try all constructors for now - Constructor[] constructors = oClass.getConstructors(); + Constructor<?>[] constructors = oClass.getConstructors(); for (int c = 0; constructors != null && c < constructors.length; c++) { if (constructors[c].getParameterTypes().length != size) @@ -809,7 +830,7 @@ public class XmlConfiguration { // Get the type - Class aClass = java.lang.Object.class; + Class<?> aClass = java.lang.Object.class; String type = node.getAttribute("type"); final String id = node.getAttribute("id"); if (type != null) @@ -830,10 +851,9 @@ public class XmlConfiguration Object al = null; - Iterator iter = node.iterator("Item"); - while (iter.hasNext()) + for (Object nodeObject : node) { - XmlParser.Node item = (XmlParser.Node)iter.next(); + XmlParser.Node item = (Node)nodeObject; String nid = item.getAttribute("id"); Object v = value(obj,item); al = LazyList.add(al,(v == null && aClass.isPrimitive())?ZERO:v); @@ -859,9 +879,8 @@ public class XmlConfiguration if (id != null) _idMap.put(id,map); - for (int i = 0; i < node.size(); i++) + for (Object o : node) { - Object o = node.get(i); if (o instanceof String) continue; XmlParser.Node entry = (XmlParser.Node)o; @@ -871,12 +890,11 @@ public class XmlConfiguration XmlParser.Node key = null; XmlParser.Node value = null; - for (int j = 0; j < entry.size(); j++) + for (Object object : node) { - o = entry.get(j); - if (o instanceof String) + if (object instanceof String) continue; - XmlParser.Node item = (XmlParser.Node)o; + XmlParser.Node item = (XmlParser.Node)object; if (!item.getTag().equals("Item")) throw new IllegalStateException("Not an Item"); if (key == null) @@ -907,9 +925,11 @@ public class XmlConfiguration /* * Get a Property. * - * @param obj @param node @return @exception Exception + * @param node + * @return + * @exception Exception */ - private Object propertyObj(Object obj, XmlParser.Node node) throws Exception + private Object propertyObj(XmlParser.Node node) throws Exception { String id = node.getAttribute("id"); String name = node.getAttribute("name"); @@ -1019,19 +1039,19 @@ public class XmlConfiguration // Try to type the object if (type == null) { - if (value != null && value instanceof String) + if (value instanceof String) return ((String)value).trim(); return value; } - if ("String".equals(type) || "java.lang.String".equals(type)) + if (isTypeMatchingClass(type,String.class)) return value.toString(); Class<?> pClass = TypeUtil.fromName(type); if (pClass != null) return TypeUtil.valueOf(pClass,value.toString()); - if ("URL".equals(type) || "java.net.URL".equals(type)) + if (isTypeMatchingClass(type,URL.class)) { if (value instanceof URL) return value; @@ -1045,7 +1065,7 @@ public class XmlConfiguration } } - if ("InetAddress".equals(type) || "java.net.InetAddress".equals(type)) + if (isTypeMatchingClass(type,InetAddress.class)) { if (value instanceof InetAddress) return value; @@ -1059,9 +1079,32 @@ public class XmlConfiguration } } + if (isTypeMatchingClass(type,List.class)) + { + if (value.getClass().isArray()) + return convertArrayToList(value); + throw new IllegalStateException("Can't convert \"" + value + "\" to " + type + ". Only Array elements are supported"); + } + + if (isTypeMatchingClass(type,Set.class)) + { + if (value.getClass().isArray()) + { + Collection<?> set = tryToConvertObjectToCollection(Set.class,value); + if (set != null) + return set; + } + throw new IllegalStateException("Can't convert \"" + value + "\" to " + type + ". Only Array elements are supported"); + } + throw new IllegalStateException("Unknown type " + type); } + private static boolean isTypeMatchingClass(String type, Class<?> classToMatch) + { + return classToMatch.getSimpleName().equals(type) || classToMatch.getName().equals(type); + } + /* ------------------------------------------------------------ */ /* * Get the value of a single element. @param obj @param item @return @exception Exception @@ -1087,7 +1130,7 @@ public class XmlConfiguration if ("Map".equals(tag)) return newMap(obj,node); if ("Property".equals(tag)) - return propertyObj(obj,node); + return propertyObj(node); if ("SystemProperty".equals(tag)) { @@ -1129,7 +1172,6 @@ public class XmlConfiguration * @param args * array of property and xml configuration filenames or {@link Resource}s. */ - @SuppressWarnings("unchecked") public static void main(final String[] args) throws Exception { @@ -1198,7 +1240,7 @@ public class XmlConfiguration { props.put(key.toString(),String.valueOf(properties.get(key))); } - configuration.setProperties(props); + configuration.getProperties().putAll(props); } obj[i] = configuration.configure(); last = configuration; diff --git a/jetty-xml/src/test/java/org/eclipse/jetty/xml/ConstructorArgTestClass.java b/jetty-xml/src/test/java/org/eclipse/jetty/xml/ConstructorArgTestClass.java new file mode 100644 index 0000000000..59d144837b --- /dev/null +++ b/jetty-xml/src/test/java/org/eclipse/jetty/xml/ConstructorArgTestClass.java @@ -0,0 +1,77 @@ +// ======================================================================== +// Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + +package org.eclipse.jetty.xml; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +/* ------------------------------------------------------------ */ +/** + */ +public class ConstructorArgTestClass +{ + @SuppressWarnings("rawtypes") + private List list; + + @SuppressWarnings("rawtypes") + private ArrayList arrayList; + + @SuppressWarnings("rawtypes") + private Set set; + + @SuppressWarnings("rawtypes") + public ConstructorArgTestClass(LinkedList list) + { + // not supported yet + } + + @SuppressWarnings("rawtypes") + public ConstructorArgTestClass(ArrayList arrayList, List list) + { + this.arrayList = arrayList; + this.list = list; + } + + @SuppressWarnings("rawtypes") + public ConstructorArgTestClass(ArrayList list) + { + this.list = list; + } + + @SuppressWarnings("rawtypes") + public ConstructorArgTestClass(Set set) + { + this.set = set; + } + + @SuppressWarnings("rawtypes") + public List getList() + { + return list; + } + + @SuppressWarnings("rawtypes") + public ArrayList getArrayList() + { + return arrayList; + } + + @SuppressWarnings("rawtypes") + public Set getSet() + { + return set; + } +} diff --git a/jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java b/jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java index 9c8e55ee23..e87e71060f 100644 --- a/jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java +++ b/jetty-xml/src/test/java/org/eclipse/jetty/xml/TestConfiguration.java @@ -14,7 +14,11 @@ package org.eclipse.jetty.xml; import java.net.URL; +import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; import org.junit.Ignore; @@ -33,6 +37,11 @@ public class TestConfiguration extends HashMap<String,Object> public int testField1; public int testField2; public int propValue; + @SuppressWarnings("rawtypes") + private List list; + @SuppressWarnings("rawtypes") + private Set set; + private ConstructorArgTestClass constructorArgTestClass; public void setTest(Object value) { @@ -87,4 +96,49 @@ public class TestConfiguration extends HashMap<String,Object> { this.ia=ia; } + + @SuppressWarnings("rawtypes") + public List getList() + { + if (constructorArgTestClass != null) + return constructorArgTestClass.getList(); + return list; + } + + @SuppressWarnings("rawtypes") + public void setList(List list) + { + this.list = list; + } + + @SuppressWarnings("rawtypes") + public void setLinkedList(LinkedList list) + { + this.list = list; + } + + @SuppressWarnings("rawtypes") + public void setArrayList(ArrayList list) + { + this.list = list; + } + + @SuppressWarnings("rawtypes") + public Set getSet() + { + if (constructorArgTestClass != null) + return constructorArgTestClass.getSet(); + return set; + } + + @SuppressWarnings("rawtypes") + public void setSet(Set set) + { + this.set = set; + } + + public void setConstructorArgTestClass(ConstructorArgTestClass constructorArgTestClass) + { + this.constructorArgTestClass = constructorArgTestClass; + } } diff --git a/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java b/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java index 16965d4201..1a7f72fb6a 100644 --- a/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java +++ b/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java @@ -14,7 +14,8 @@ package org.eclipse.jetty.xml; import static junit.framework.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; import java.net.URL; import java.util.HashMap; @@ -25,13 +26,16 @@ import org.junit.Test; public class XmlConfigurationTest { protected String _configure="org/eclipse/jetty/xml/configure.xml"; + + private static final String STRING_ARRAY_XML = "<Array type=\"String\"><Item type=\"String\">String1</Item><Item type=\"String\">String2</Item></Array>"; + private static final String INT_ARRAY_XML = "<Array type=\"int\"><Item type=\"int\">1</Item><Item type=\"int\">2</Item></Array>"; @Test public void testMortBay() throws Exception { URL url = XmlConfigurationTest.class.getClassLoader().getResource("org/eclipse/jetty/xml/mortbay.xml"); XmlConfiguration configuration = new XmlConfiguration(url); - Object o=configuration.configure(); + configuration.configure(); } @Test @@ -185,6 +189,129 @@ public class XmlConfigurationTest configuration.configure(tc); assertEquals("Set String 3","SetValue",tc.testObject); assertEquals("Set Type 3",2,tc.testInt); + } + + @Test + public void testListConstructorArg() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\">" + + "<Set name=\"constructorArgTestClass\"><New class=\"org.eclipse.jetty.xml.ConstructorArgTestClass\"><Arg type=\"List\">" + + STRING_ARRAY_XML + "</Arg></New></Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getList() returns null as it's not configured yet",tc.getList(),is(nullValue())); + xmlConfiguration.configure(tc); + assertThat("tc.getList() returns not null",tc.getList(),not(nullValue())); + assertThat("tc.getList() has two entries as specified in the xml",tc.getList().size(),is(2)); + } + + @Test + public void testTwoArgumentListConstructorArg() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\">" + + "<Set name=\"constructorArgTestClass\"><New class=\"org.eclipse.jetty.xml.ConstructorArgTestClass\">" + + "<Arg type=\"List\">" + STRING_ARRAY_XML + "</Arg>" + + "<Arg type=\"List\">" + STRING_ARRAY_XML + "</Arg>" + + "</New></Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getList() returns null as it's not configured yet",tc.getList(),is(nullValue())); + xmlConfiguration.configure(tc); + assertThat("tc.getList() returns not null",tc.getList(),not(nullValue())); + assertThat("tc.getList() has two entries as specified in the xml",tc.getList().size(),is(2)); + } + + @Test(expected = IllegalStateException.class) + public void testListNotContainingArray() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\">" + + "<New class=\"org.eclipse.jetty.xml.ConstructorArgTestClass\"><Arg type=\"List\">Some String</Arg></New></Configure>"); + TestConfiguration tc = new TestConfiguration(); + xmlConfiguration.configure(tc); + } + + @Test + public void testSetConstructorArg() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\">" + + "<Set name=\"constructorArgTestClass\"><New class=\"org.eclipse.jetty.xml.ConstructorArgTestClass\"><Arg type=\"Set\">" + + STRING_ARRAY_XML + "</Arg></New></Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getList() returns null as it's not configured yet",tc.getSet(),is(nullValue())); + xmlConfiguration.configure(tc); + assertThat("tc.getList() returns not null",tc.getSet(),not(nullValue())); + assertThat("tc.getList() has two entries as specified in the xml",tc.getSet().size(),is(2)); + } + + @Test(expected = IllegalStateException.class) + public void testSetNotContainingArray() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\">" + + "<New class=\"org.eclipse.jetty.xml.ConstructorArgTestClass\"><Arg type=\"Set\">Some String</Arg></New></Configure>"); + TestConfiguration tc = new TestConfiguration(); + xmlConfiguration.configure(tc); + } + + @Test + public void testListSetterWithStringArray() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\"><Set name=\"List\">" + + STRING_ARRAY_XML + "</Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getList() returns null as it's not configured yet",tc.getList(),is(nullValue())); + xmlConfiguration.configure(tc); + assertThat("tc.getList() has two entries as specified in the xml",tc.getList().size(),is(2)); + } + + @Test + public void testListSetterWithPrimitiveArray() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\"><Set name=\"List\">" + + INT_ARRAY_XML + "</Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getList() returns null as it's not configured yet",tc.getList(),is(nullValue())); + xmlConfiguration.configure(tc); + assertThat("tc.getList() has two entries as specified in the xml",tc.getList().size(),is(2)); + } + + @Test(expected=IllegalArgumentException.class) + public void testNotSupportedLinkedListSetter() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\"><Set name=\"LinkedList\">" + + INT_ARRAY_XML + "</Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getSet() returns null as it's not configured yet",tc.getList(),is(nullValue())); + xmlConfiguration.configure(tc); + } + @Test + public void testArrayListSetter() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\"><Set name=\"ArrayList\">" + + INT_ARRAY_XML + "</Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getSet() returns null as it's not configured yet",tc.getList(),is(nullValue())); + xmlConfiguration.configure(tc); + assertThat("tc.getSet() has two entries as specified in the xml",tc.getList().size(),is(2)); + } + + @Test + public void testSetSetter() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\"><Set name=\"Set\">" + + STRING_ARRAY_XML + "</Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getSet() returns null as it's not configured yet",tc.getSet(),is(nullValue())); + xmlConfiguration.configure(tc); + assertThat("tc.getSet() has two entries as specified in the xml",tc.getSet().size(),is(2)); + } + + @Test + public void testSetSetterWithPrimitiveArray() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\"><Set name=\"Set\">" + + INT_ARRAY_XML + "</Set></Configure>"); + TestConfiguration tc = new TestConfiguration(); + assertThat("tc.getSet() returns null as it's not configured yet",tc.getSet(),is(nullValue())); + xmlConfiguration.configure(tc); + assertThat("tc.getSet() has two entries as specified in the xml",tc.getSet().size(),is(2)); } } |