408945 XML Args ignored without DTD

+ Semi-reverting prior commit as it broke with DTD use
 + Fixing up logic to allow for <Arg> <Set> <Call> with and without DTD
 + Adding testcase that uses default constructor with <Set> calls to
   setup a class
This commit is contained in:
Joakim Erdfelt 2013-06-03 11:31:07 -07:00
parent 8b39cbe5bf
commit 670b6a964c
3 changed files with 132 additions and 27 deletions

View File

@ -742,39 +742,42 @@ public class XmlConfiguration
{
Class<?> oClass = nodeClass(node);
int argIndex = node.size();
Map<String, Object> namedArgMap = new HashMap<>();
List<Object> arguments = new LinkedList<>();
XmlParser.Node child;
// Find the <Arg> elements
for (int i = 0; i < node.size(); i++)
{
Object o = node.get(i);
if (o instanceof String)
continue;
if (!((XmlParser.Node)o).getTag().equals("Arg"))
{
// Skip raw String nodes
continue;
}
child = (XmlParser.Node)o;
if(child.getTag().equals("Arg"))
{
String namedAttribute = child.getAttribute("name");
Object value=value(obj,child);
if (namedAttribute != null)
{
// named arguments
namedArgMap.put(namedAttribute,value);
}
// raw arguments
arguments.add(value);
} else {
// The first non <Arg> child is the start of
// elements that configure the class, such as
// <Set> and <Call> nodes
argIndex = i;
break;
}
}
Map<String, Object> namedArgMap = new HashMap<>();
List<Object> arguments = new LinkedList<>();
for (int i = 0; i < node.size(); i++)
{
Object o = node.get(i);
if (o instanceof String)
{
continue;
}
XmlParser.Node argNode = (XmlParser.Node)o;
String namedAttribute = argNode.getAttribute("name");
Object value=value(obj,(XmlParser.Node)o);
if (namedAttribute != null)
namedArgMap.put(namedAttribute,value);
arguments.add(value);
}
if (LOG.isDebugEnabled())
LOG.debug("XML new " + oClass);

View File

@ -0,0 +1,74 @@
//
// ========================================================================
// Copyright (c) 1995-2013 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;
public class DefaultTestConfiguration
{
private String first;
private String second;
private String third;
DefaultTestConfiguration nested;
public DefaultTestConfiguration()
{
/* default constructor */
}
public String getFirst()
{
return first;
}
public void setFirst(String first)
{
this.first = first;
}
public String getSecond()
{
return second;
}
public void setSecond(String second)
{
this.second = second;
}
public String getThird()
{
return third;
}
public void setThird(String third)
{
this.third = third;
}
public DefaultTestConfiguration getNested()
{
return nested;
}
public void setNested(DefaultTestConfiguration nested)
{
this.nested = nested;
}
}

View File

@ -552,10 +552,10 @@ public class XmlConfigurationTest
" <Arg>arg2</Arg> " +
" <Arg>arg3</Arg> " +
" <Set name=\"nested\"> " +
" <New class=\"org.eclipse.jetty.xml.AnnotatedTestConfiguration\">\n" +
" <Arg>arg1</Arg>\n" +
" <Arg>arg2</Arg>\n" +
" <Arg>arg3</Arg>\n" +
" <New class=\"org.eclipse.jetty.xml.AnnotatedTestConfiguration\">\n" +
" <Arg>arg1</Arg>\n" +
" <Arg>arg2</Arg>\n" +
" <Arg>arg3</Arg>\n" +
" </New>" +
" </Set>" +
"</Configure>").getBytes("ISO-8859-1")));
@ -571,6 +571,34 @@ public class XmlConfigurationTest
Assert.assertEquals("nested third parameter not wired correctly","arg3", atc.getNested().getThird());
}
@Test
public void testSetGetIgnoredMissingDTD() throws Exception
{
XmlConfiguration xmlConfiguration = new XmlConfiguration(new ByteArrayInputStream(("" +
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
" <Set name=\"first\">arg1</Set> " +
" <Set name=\"second\">arg2</Set> " +
" <Set name=\"third\">arg3</Set> " +
" <Set name=\"nested\"> " +
" <New class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">\n" +
" <Set name=\"first\">arg1</Set> " +
" <Set name=\"second\">arg2</Set> " +
" <Set name=\"third\">arg3</Set> " +
" </New>" +
" </Set>" +
"</Configure>").getBytes("ISO-8859-1")));
// XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
DefaultTestConfiguration atc = (DefaultTestConfiguration)xmlConfiguration.configure();
Assert.assertEquals("first parameter not wired correctly","arg1", atc.getFirst());
Assert.assertEquals("second parameter not wired correctly","arg2", atc.getSecond());
Assert.assertEquals("third parameter not wired correctly","arg3", atc.getThird());
Assert.assertEquals("nested first parameter not wired correctly","arg1", atc.getNested().getFirst());
Assert.assertEquals("nested second parameter not wired correctly","arg2", atc.getNested().getSecond());
Assert.assertEquals("nested third parameter not wired correctly","arg3", atc.getNested().getThird());
}
@Test
public void testNestedConstructorNamedInjectionUnorderedMixed() throws Exception
{