1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.derived;
18
19 import java.io.StringReader;
20 import java.io.StringWriter;
21
22 import org.apache.commons.betwixt.AbstractTestCase;
23 import org.apache.commons.betwixt.io.BeanWriter;
24 import org.apache.commons.betwixt.strategy.PropertySuppressionStrategy;
25 import org.xml.sax.InputSource;
26
27
28
29
30 public class TestWriteClass extends AbstractTestCase {
31
32 public TestWriteClass(String testName) {
33 super(testName);
34 }
35
36 public void testDotBetwixtClass() throws Exception {
37 String customDotBetwixt = "<?xml version='1.0'?><info primitiveTypes='attribute'>" +
38 "<element name='type'>" +
39 "<attribute property='class' name='classname'/>" +
40 "</element>" +
41 "</info>";
42
43 EmployeeBean employeeBean = new EmployeeBean();
44 employeeBean.setAge(32);
45 employeeBean.setName("AN Other");
46 StringWriter out = new StringWriter();
47 BeanWriter writer = new BeanWriter(out);
48 writer.getBindingConfiguration().setMapIDs(false);
49 writer.getXMLIntrospector().register(EmployeeBean.class, new InputSource(new StringReader(customDotBetwixt)));
50 writer.write(employeeBean);
51
52 String expected = "<?xml version='1.0'?><type classname='org.apache.commons.betwixt.derived.EmployeeBean'/>";
53
54 xmlAssertIsomorphicContent("Expected only class name to be mapped", parseString(expected), parseString(out.toString()));
55 }
56
57 public void testPropertySuppressionStrategy() throws Exception {
58
59 BeanWithSecrets bean = new BeanWithSecrets("Surveyor Of The Queen's Pictures", "Queen Elizabeth II",
60 "Sir Anthony Federick Blunt", "Fourth Man", "Soviet Union");
61 StringWriter out = new StringWriter();
62 BeanWriter writer = new BeanWriter(out);
63 writer.getBindingConfiguration().setMapIDs(false);
64 writer.getXMLIntrospector().getConfiguration().setPropertySuppressionStrategy(
65 new PropertySuppressionStrategy() {
66
67 public boolean suppressProperty(Class classContainingThePropety, Class propertyType, String propertyName) {
68 if ("class".equals(propertyName)) {
69 return true;
70 }
71 if (propertyName.startsWith("secret")) {
72 return true;
73 }
74 return false;
75 }
76
77 });
78 writer.write("normal-person", bean);
79
80 String expected = "<?xml version='1.0'?><normal-person>" +
81 "<employer>Queen Elizabeth II</employer>" +
82 "<job>Surveyor Of The Queen's Pictures</job>" +
83 "<name>Sir Anthony Federick Blunt</name>" +
84 "</normal-person>";
85
86 xmlAssertIsomorphicContent("Expected secrets to be supressed", parseString(expected), parseString(out.toString()), true);
87
88 }
89 }