1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt.io;
17
18 import java.io.StringWriter;
19
20 import org.apache.commons.betwixt.AbstractTestCase;
21 import org.apache.commons.betwixt.AttributeDescriptor;
22 import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
23
24 /***
25 * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
26 */
27 public class TestAttributeSuppression extends AbstractTestCase {
28
29 public TestAttributeSuppression(String testName) {
30 super(testName);
31 }
32
33
34 public void testEmptyStringSuppression() throws Exception {
35 PersonBean bean = new PersonBean("Corwin", null);
36
37 StringWriter out = new StringWriter();
38 out.write("<?xml version='1.0'?>");
39
40 BeanWriter writer = new BeanWriter(out);
41 writer.getBindingConfiguration().setMapIDs(false);
42 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
43
44 writer.write(bean);
45
46 String expected = "<?xml version='1.0'?><PersonBean forenames='Corwin'/>";
47
48 xmlAssertIsomorphicContent(parseString(expected), parseString(out));
49 }
50
51 public void testCustomStrategy() throws Exception {
52 PersonBean bean = new PersonBean("Zaphod", "Beeblebrox");
53
54 StringWriter out = new StringWriter();
55 out.write("<?xml version='1.0'?>");
56
57 BeanWriter writer = new BeanWriter(out);
58 writer.getBindingConfiguration().setMapIDs(false);
59 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(true);
60 writer.getBindingConfiguration().setValueSuppressionStrategy(new ValueSuppressionStrategy() {
61
62 public boolean suppressAttribute(AttributeDescriptor attributeDescriptor, String value) {
63 return "Zaphod".equals(value);
64 }
65 });
66 writer.write(bean);
67
68 String expected = "<?xml version='1.0'?><PersonBean surname='Beeblebrox'/>";
69
70 xmlAssertIsomorphicContent(parseString(expected), parseString(out));
71 }
72 }