460670 - Support multiple names in <Property> elements.

This commit is contained in:
Simone Bordet 2015-02-24 10:54:20 +01:00
parent aac427ab8c
commit 1c00cdc7c9
2 changed files with 86 additions and 21 deletions

View File

@ -35,7 +35,6 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
@ -945,20 +944,45 @@ public class XmlConfiguration
*/
private Object propertyObj(XmlParser.Node node) throws Exception
{
String id = node.getAttribute("id");
String name = node.getAttribute("name");
String idAttr = node.getAttribute("id");
String nameAttr = node.getAttribute("name");
String defaultValue = node.getAttribute("default");
Object prop;
Map<String,String> property_map=_configuration.getProperties();
if (property_map != null && property_map.containsKey(name))
prop = property_map.get(name);
else
prop = defaultValue;
if (id != null)
_configuration.getIdMap().put(id,prop);
if (prop != null)
configure(prop,node,0);
return prop;
Object value = null;
boolean present = false;
Map<String,String> properties = _configuration.getProperties();
if (properties != null && nameAttr != null)
{
String preferredName = null;
String[] names = nameAttr.split(",");
for (String name : names)
{
name = name.trim();
if (name.length() == 0)
continue;
if (preferredName == null)
preferredName = name;
if (properties.containsKey(name))
{
if (!name.equals(preferredName))
LOG.warn("Property '{}' is deprecated, use '{}' instead", name, preferredName);
present = true;
value = properties.get(name);
break;
}
}
}
if (!present)
value = defaultValue;
if (idAttr != null)
_configuration.getIdMap().put(idAttr, value);
if (value != null)
configure(value, node, 0);
return value;
}
/*

View File

@ -18,13 +18,6 @@
package org.eclipse.jetty.xml;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@ -36,6 +29,13 @@ import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class XmlConfigurationTest
{
protected String _configure="org/eclipse/jetty/xml/configure.xml";
@ -779,4 +779,45 @@ public class XmlConfigurationTest
}
}
@Test
public void testWithMultiplePropertyNamesWithNoPropertyThenDefaultIsChosen() throws Exception
{
// No properties
String defolt = "baz";
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
" <Set name=\"first\"><Property name=\"foo,bar\" default=\"" + defolt + "\"/></Set> " +
"</Configure>");
DefaultTestConfiguration config = (DefaultTestConfiguration)xmlConfiguration.configure();
assertEquals(defolt, config.getFirst());
}
@Test
public void testWithMultiplePropertyNamesWithFirstPropertyThenFirstIsChosen() throws Exception
{
String name = "foo";
String value = "foo";
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
" <Set name=\"first\"><Property name=\"" + name + ",bar\" default=\"baz\"/></Set> " +
"</Configure>");
xmlConfiguration.getProperties().put(name, value);
DefaultTestConfiguration config = (DefaultTestConfiguration)xmlConfiguration.configure();
assertEquals(value, config.getFirst());
}
@Test
public void testWithMultiplePropertyNamesWithSecondPropertyThenSecondIsChosen() throws Exception
{
String name = "bar";
String value = "bar";
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
"<Configure class=\"org.eclipse.jetty.xml.DefaultTestConfiguration\">" +
" <Set name=\"first\"><Property name=\"foo," + name + "\" default=\"baz\"/></Set> " +
"</Configure>");
xmlConfiguration.getProperties().put(name, value);
DefaultTestConfiguration config = (DefaultTestConfiguration)xmlConfiguration.configure();
assertEquals(value, config.getFirst());
}
}