Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #6375 XmlConfiguration always checks for setter method #6398

Merged
merged 4 commits into from Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
gregw marked this conversation as resolved.
Show resolved Hide resolved
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
{
gregw marked this conversation as resolved.
Show resolved Hide resolved
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