Skip to content

Commit

Permalink
Fixed #6375 XmlConfiguration always checks for setter method
Browse files Browse the repository at this point in the history
Fix #6375 by making XmlConfiguration Set handling always check for a matching setter, even if the property attribute is given but not set.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Jun 11, 2021
1 parent 1223bb5 commit 8e9a576
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Expand Up @@ -516,31 +516,38 @@ public void configure(Object obj, XmlParser.Node cfg, int i) throws Exception
private void set(Object obj, XmlParser.Node node) throws Exception
{
String attr = node.getAttribute("name");
String name = "set" + attr.substring(0, 1).toUpperCase(Locale.ENGLISH) + attr.substring(1);
String id = node.getAttribute("id");
String property = node.getAttribute("property");
String propertyValue = null;

Class<?> oClass = nodeClass(node);
if (oClass != null)
obj = null;
else
oClass = obj.getClass();

// Look for a property value
if (property != null)
{
Map<String, String> properties = _configuration.getProperties();
propertyValue = properties.get(property);
// If no property value, then do not set
if (propertyValue == null)
{
// check that there is at least one setter that could have matched
if (Arrays.stream(oClass.getMethods()).noneMatch(m -> m.getName().equals(name)))
throw new NoSuchMethodException(String.format("No '%s' on %s", name, oClass.getName()));
// otherwise it is a noop
return;
}
}

String name = "set" + attr.substring(0, 1).toUpperCase(Locale.ENGLISH) + attr.substring(1);
Object value = value(obj, node);
if (value == null)
value = propertyValue;
Object[] arg = {value};

Class<?> oClass = nodeClass(node);
if (oClass != null)
obj = null;
else
oClass = obj.getClass();

Class<?>[] vClass = {Object.class};
if (value != null)
vClass[0] = value.getClass();
Expand Down
Expand Up @@ -363,6 +363,32 @@ public void testSetWithNullPropertyAndValue() throws Exception
assertNull(configuration.getIdMap().get("test"));
}

@Test
public void testSetWithWrongNameAndProperty() throws Exception
{
XmlConfiguration configuration = asXmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\"><Set name=\"WrongName\" property=\"prop\" id=\"test\"/></Configure>");
configuration.getProperties().put("prop", "This is a property value");
TestConfiguration tc = new TestConfiguration();
tc.setTestString("default");

NoSuchMethodException e = assertThrows(NoSuchMethodException.class, () -> configuration.configure(tc));
assertThat(e.getMessage(), containsString("setWrongName"));
assertEquals("default", tc.getTestString());
}
@Test

public void testSetWithWrongNameAndNullProperty() throws Exception
{
XmlConfiguration configuration = asXmlConfiguration("<Configure class=\"org.eclipse.jetty.xml.TestConfiguration\"><Set name=\"WrongName\" property=\"prop\" id=\"test\"/></Configure>");
configuration.getProperties().remove("prop");
TestConfiguration tc = new TestConfiguration();
tc.setTestString("default");

NoSuchMethodException e = assertThrows(NoSuchMethodException.class, () -> configuration.configure(tc));
assertThat(e.getMessage(), containsString("setWrongName"));
assertEquals("default", tc.getTestString());
}

@Test
public void testMeaningfullSetException() throws Exception
{
Expand Down

0 comments on commit 8e9a576

Please sign in to comment.