From 1aebddfaf7a2ef466a9e857fb525677a09b24440 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 12 Jun 2012 13:12:17 +0200 Subject: [PATCH] 382343 - Jetty XML support for Map is broken. --- .../eclipse/jetty/xml/XmlConfiguration.java | 93 ++++++++++--------- .../eclipse/jetty/xml/TestConfiguration.java | 9 +- .../jetty/xml/XmlConfigurationTest.java | 56 ++++++++--- 3 files changed, 100 insertions(+), 58 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 292294d6d9a..cee3c27cb0a 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 @@ -71,11 +71,10 @@ public class XmlConfiguration private static final Class[] __primitiveHolders = { Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Void.class }; - private static final Integer ZERO = new Integer(0); - + private static final Class[] __supportedCollections = { ArrayList.class,ArrayQueue.class,HashSet.class,Queue.class,List.class,Set.class,Collection.class,}; - + private static final Iterable __factoryLoader; private static final XmlParser __parser = initParser(); @@ -141,9 +140,11 @@ public class XmlConfiguration /* ------------------------------------------------------------ */ /** - * Constructor. Reads the XML configuration file. + * Reads and parses the XML configuration file. * - * @param configuration + * @param configuration the URL of the XML configuration + * @throws IOException if the configuration could not be read + * @throws SAXException if the configuration could not be parsed */ public XmlConfiguration(URL configuration) throws SAXException, IOException { @@ -157,12 +158,12 @@ public class XmlConfiguration /* ------------------------------------------------------------ */ /** - * Constructor. + * Reads and parses the XML configuration string. * - * @param configuration - * String of XML configuration commands excluding the normal XML preamble. The String should start with a " Apply the XML configuration script to the passed object.

- * - * @param obj - * The object to be configured, which must be of a type or super type of the class attribute of the Configure element. - * @exception Exception + * @param obj The object to be configured, which must be of a type or super type + * of the class attribute of the <Configure> element. + * @throws Exception if the configuration fails + * @return the configured object */ public Object configure(Object obj) throws Exception { @@ -283,10 +284,13 @@ public class XmlConfiguration /* ------------------------------------------------------------ */ /** - * Configure an object. If the configuration has an ID, an object is looked up by ID and it's type check. Otherwise a new object is created. + * Applies the XML configuration script. + * If the root element of the configuration has an ID, an object is looked up by ID and its type checked + * against the root element's type. + * Otherwise a new object of the type specified by the root element is created. * * @return The newly created configured object. - * @exception Exception + * @throws Exception if the configuration fails */ public Object configure() throws Exception { @@ -353,12 +357,13 @@ public class XmlConfiguration /* ------------------------------------------------------------ */ /** - * Recursive configuration step. This method applies the remaining Set, Put and Call elements to the current object. + * Recursive configuration routine. + * This method applies the nested Set, Put, Call, etc. elements to the given object. * - * @param obj - * @param cfg - * @param i - * @exception Exception + * @param obj the object to configure + * @param cfg the XML nodes of the configuration + * @param i the index of the XML nodes + * @throws Exception if the configuration fails */ public void configure(Object obj, XmlParser.Node cfg, int i) throws Exception { @@ -576,7 +581,9 @@ public class XmlConfiguration } /** - * @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 + * @param array the array to convert + * @param collectionType the desired collection type + * @return a collection of the desired type if the array can be converted */ private static Collection convertArrayToCollection(Object array, Class collectionType) { @@ -862,7 +869,7 @@ public class XmlConfiguration 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); + al = LazyList.add(al,(v == null && aClass.isPrimitive())?0:v); if (nid != null) _idMap.put(nid,v); } @@ -896,7 +903,7 @@ public class XmlConfiguration XmlParser.Node key = null; XmlParser.Node value = null; - for (Object object : node) + for (Object object : entry) { if (object instanceof String) continue; @@ -932,26 +939,26 @@ public class XmlConfiguration * Get a Property. * * @param node - * @return + * @return * @exception Exception */ private Object propertyObj(XmlParser.Node node) throws Exception { String id = node.getAttribute("id"); String name = node.getAttribute("name"); - String defval = node.getAttribute("default"); - Object prop = null; + String defaultValue = node.getAttribute("default"); + Object prop; if (_propertyMap != null && _propertyMap.containsKey(name)) prop = _propertyMap.get(name); else - prop = defval; + prop = defaultValue; if (id != null) _idMap.put(id,prop); if (prop != null) configure(prop,node,0); return prop; } - + /* ------------------------------------------------------------ */ /* @@ -960,7 +967,7 @@ public class XmlConfiguration */ private Object value(Object obj, XmlParser.Node node) throws Exception { - Object value = null; + Object value; // Get the type String type = node.getAttribute("type"); @@ -989,7 +996,7 @@ public class XmlConfiguration if (type == null || !"String".equals(type)) { // Skip leading white - Object item = null; + Object item; while (first <= last) { item = node.get(first); @@ -1084,7 +1091,7 @@ public class XmlConfiguration throw new InvocationTargetException(e); } } - + for (Class collectionClass : __supportedCollections) { if (isTypeMatchingClass(type,collectionClass)) @@ -1093,12 +1100,11 @@ public class XmlConfiguration throw new IllegalStateException("Unknown type " + type); } - + /* ------------------------------------------------------------ */ private static boolean isTypeMatchingClass(String type, Class classToMatch) { - boolean match = classToMatch.getSimpleName().equalsIgnoreCase(type) || classToMatch.getName().equals(type); - return match; + return classToMatch.getSimpleName().equalsIgnoreCase(type) || classToMatch.getName().equals(type); } /* ------------------------------------------------------------ */ @@ -1134,7 +1140,7 @@ public class XmlConfiguration String defaultValue = node.getAttribute("default"); return System.getProperty(name,defaultValue); } - + if ("Env".equals(tag)) { String name = node.getAttribute("name"); @@ -1167,6 +1173,7 @@ public class XmlConfiguration * * @param args * array of property and xml configuration filenames or {@link Resource}s. + * @throws Exception if the XML configurations cannot be run */ public static void main(final String[] args) throws Exception { 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 e87e71060f2..8c35a8e8e26 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 @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Set; import org.junit.Ignore; @@ -42,6 +43,7 @@ public class TestConfiguration extends HashMap @SuppressWarnings("rawtypes") private Set set; private ConstructorArgTestClass constructorArgTestClass; + public Map map; public void setTest(Object value) { @@ -52,7 +54,7 @@ public class TestConfiguration extends HashMap { testInt=value; } - + public void setPropertyTest(int value) { propValue=value; @@ -141,4 +143,9 @@ public class TestConfiguration extends HashMap { this.constructorArgTestClass = constructorArgTestClass; } + + public void setMap(Map map) + { + this.map = map; + } } 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 79b4038400a..49cb229254b 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 @@ -13,20 +13,24 @@ package org.eclipse.jetty.xml; -import static junit.framework.Assert.assertEquals; -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; - import java.net.URL; import java.util.HashMap; import java.util.Map; +import org.junit.Assert; import org.junit.Test; +import static junit.framework.Assert.assertEquals; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + public class XmlConfigurationTest { protected String _configure="org/eclipse/jetty/xml/configure.xml"; - + private static final String STRING_ARRAY_XML = "String1String2"; private static final String INT_ARRAY_XML = "12"; @@ -37,7 +41,7 @@ public class XmlConfigurationTest XmlConfiguration configuration = new XmlConfiguration(url); configuration.configure(); } - + @Test public void testPassedObject() throws Exception { @@ -53,7 +57,7 @@ public class XmlConfigurationTest assertEquals("Set String","SetValue",tc.testObject); assertEquals("Set Type",2,tc.testInt); - + assertEquals(18080, tc.propValue); assertEquals("Put","PutValue",tc.get("Test")); @@ -76,7 +80,7 @@ public class XmlConfigurationTest assertEquals( "SystemProperty", System.getProperty("user.dir")+"/stuff",tc.get("SystemProperty")); assertEquals( "Env", System.getenv("HOME"),tc.get("Env")); - + assertEquals( "Property", "xxx", tc.get("Property")); @@ -104,12 +108,12 @@ public class XmlConfigurationTest assertEquals("nested config","Call1",tc2.testObject); assertEquals("nested config",4,tc2.testInt); assertEquals( "nested call", "http://www.eclipse.com/",tc2.url.toString()); - + assertEquals("static to field",tc.testField1,77); assertEquals("field to field",tc.testField2,2); assertEquals("literal to static",TestConfiguration.VALUE,42); } - + @Test public void testNewObject() throws Exception { @@ -124,7 +128,7 @@ public class XmlConfigurationTest assertEquals("Set String","SetValue",tc.testObject); assertEquals("Set Type",2,tc.testInt); - + assertEquals(18080, tc.propValue); assertEquals("Put","PutValue",tc.get("Test")); @@ -173,13 +177,13 @@ public class XmlConfigurationTest assertEquals("nested config","Call1",tc2.testObject); assertEquals("nested config",4,tc2.testInt); assertEquals( "nested call", "http://www.eclipse.com/",tc2.url.toString()); - + assertEquals("static to field",71,tc.testField1); assertEquals("field to field",2,tc.testField2); assertEquals("literal to static",42,TestConfiguration.VALUE); } - - + + @Test public void testStringConfiguration() throws Exception { @@ -314,4 +318,28 @@ public class XmlConfigurationTest xmlConfiguration.configure(tc); assertThat("tc.getSet() has two entries as specified in the xml",tc.getSet().size(),is(2)); } + + @Test + public void testMap() throws Exception + { + XmlConfiguration xmlConfiguration = new XmlConfiguration("" + + "" + + " " + + " " + + " " + + " key1" + + " value1" + + " " + + " " + + " key2" + + " value2" + + " " + + " " + + " " + + ""); + TestConfiguration tc = new TestConfiguration(); + Assert.assertNull("tc.map is null as it's not configured yet", tc.map); + xmlConfiguration.configure(tc); + Assert.assertEquals("tc.map is has two entries as specified in the XML", 2, tc.map.size()); + } }