result.setSecret("bar");
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Constrain a single field of the result
Call c = new Call(null, null, null);
c.setConstraints(Arrays.asList(new String[] { "secret" }));
c.setResult(result);
MarshalUtils.marshalResult(c, out);
SAXReader xmlReader = new SAXReader();
Document doc = xmlReader.read(new StringReader(new String(out
.toByteArray())));
Widget widget = (Widget) ParserUtils.unmarshalResult(doc
.getRootElement());
// value field should equal "foo"
assert "foo".equals(widget.getValue());
// secret field should be null
assert widget.getSecret() == null;
// Now extend our object graph a little further
result.setChild(new Widget());
result.getChild().setValue("foo");
result.getChild().setSecret("bar");
// Reset our output stream so we can re-use it
out.reset();
// Now we're going to constrain result.child's secret field
c.setConstraints(Arrays.asList(new String[] { "child.secret" }));
MarshalUtils.marshalResult(c, out);
doc = xmlReader.read(new StringReader(new String(out.toByteArray())));
widget = (Widget) ParserUtils.unmarshalResult(doc.getRootElement());
assert "foo".equals(widget.getValue());
assert "bar".equals(widget.getSecret());
assert "foo".equals(widget.getChild().getValue());
assert widget.getChild().getSecret() == null;
// Add a map to our result
result.setWidgetMap(new HashMap<String, Widget>());
Widget val = new Widget();
val.setValue("foo");
val.setSecret("bar");
result.getWidgetMap().put("foo", val);
// Reset our output stream again
out.reset();
// Constrain the "secret" field of the widgetMap map's values (sounds
// confusing, I know...)
c.setConstraints(Arrays.asList(new String[] { "widgetMap[value].secret" }));
MarshalUtils.marshalResult(c, out);
doc = xmlReader.read(new StringReader(new String(out.toByteArray())));
widget = (Widget) ParserUtils.unmarshalResult(doc.getRootElement());
val = widget.getWidgetMap().get("foo");
assert val != null;
assert "foo".equals(val.getValue());
assert val.getSecret() == null;
// Reset our output stream
out.reset();
// Add a list to our result
result.setWidgetList(new ArrayList<Widget>());
Widget item = new Widget();
item.setValue("foo");
item.setSecret("bar");
result.getWidgetList().add(item);
// Constraint the "secret" field of widgetList
c.setConstraints(Arrays.asList(new String[] { "widgetList.secret" }));
MarshalUtils.marshalResult(c, out);
doc = xmlReader.read(new StringReader(new String(out.toByteArray())));
widget = (Widget) ParserUtils.unmarshalResult(doc.getRootElement());
item = widget.getWidgetList().get(0);
assert item != null;
assert "foo".equals(item.getValue());
assert item.getSecret() == null;
// Reset our output stream
out.reset();
// Now constrain all secrets
c.setConstraints(Arrays.asList(new String[] { "[" + Widget.class.getName() + "].secret" }));
MarshalUtils.marshalResult(c, out);
doc = xmlReader.read(new StringReader(new String(out.toByteArray())));
widget = (Widget) ParserUtils.unmarshalResult(doc.getRootElement());