1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.io;
18
19 import java.io.StringWriter;
20
21 import org.apache.commons.betwixt.AbstractTestCase;
22 import org.apache.commons.betwixt.AttributeDescriptor;
23 import org.apache.commons.betwixt.ElementDescriptor;
24 import org.apache.commons.betwixt.strategy.ValueSuppressionStrategy;
25
26
27
28
29
30
31
32 public class TestSuppressElement extends AbstractTestCase {
33
34 public TestSuppressElement(String testName) {
35 super(testName);
36 }
37
38 public void testSuppressNothing() throws Exception {
39 PersonBean angLee = new NullPersonBean("Ang","Lee");
40 MovieBean movie = new MovieBean("Crouching Tiger, Hidden Dragon", 2000, angLee);
41 movie.addActor(new NullPersonBean("Yun-Fat", "Chow"));
42 movie.addActor(new PersonBean("Michelle", "Yeoh"));
43 movie.addActor(new PersonBean("Ziyi", "Zhang"));
44
45 StringWriter out = new StringWriter();
46 out.write("<?xml version='1.0'?>");
47 BeanWriter writer = new BeanWriter(out);
48 writer.getBindingConfiguration().setMapIDs(false);
49 writer.write(movie);
50
51 String expected = "<?xml version='1.0'?>" +
52 "<movie>" +
53 " <name>Crouching Tiger, Hidden Dragon</name>" +
54 " <year>2000</year>" +
55 " <director>" +
56 " <forenames>Ang</forenames>" +
57 " <surname>Lee</surname>" +
58 " </director>" +
59 " <actors>" +
60 " <actor>" +
61 " <forenames>Yun-Fat</forenames>" +
62 " <surname>Chow</surname>" +
63 " </actor>" +
64 " <actor>" +
65 " <forenames>Michelle</forenames>" +
66 " <surname>Yeoh</surname>" +
67 " </actor>" +
68 " <actor>" +
69 " <forenames>Ziyi</forenames>" +
70 " <surname>Zhang</surname>" +
71 " </actor>" +
72 " </actors>" +
73 "</movie>";
74
75 xmlAssertIsomorphicContent(parseString(expected), parseString(out));
76 }
77
78
79 public void testSuppressType() throws Exception {
80 PersonBean angLee = new NullPersonBean("Ang","Lee");
81 MovieBean movie = new MovieBean("Crouching Tiger, Hidden Dragon", 2000, angLee);
82 movie.addActor(new NullPersonBean("Yun-Fat", "Chow"));
83 movie.addActor(new PersonBean("Michelle", "Yeoh"));
84 movie.addActor(new PersonBean("Ziyi", "Zhang"));
85
86 StringWriter out = new StringWriter();
87 out.write("<?xml version='1.0'?>");
88 BeanWriter writer = new BeanWriter(out);
89 writer.getBindingConfiguration().setMapIDs(false);
90 writer.getBindingConfiguration().setValueSuppressionStrategy(new ValueSuppressionStrategy() {
91
92 public boolean suppressAttribute(AttributeDescriptor attributeDescriptor, String value) {
93 return DEFAULT.suppressAttribute(attributeDescriptor, value);
94 }
95
96 public boolean suppressElement(ElementDescriptor element, String namespaceUrl, String localName, String qualifiedName, Object value) {
97
98 boolean result = false;
99 if (value instanceof NullPersonBean) {
100 result = true;
101 }
102 return result;
103 }
104 });
105 writer.write(movie);
106
107 String expected = "<?xml version='1.0'?>" +
108 "<movie>" +
109 " <name>Crouching Tiger, Hidden Dragon</name>" +
110 " <year>2000</year>" +
111 " <actors>" +
112 " <actor>" +
113 " <forenames>Michelle</forenames>" +
114 " <surname>Yeoh</surname>" +
115 " </actor>" +
116 " <actor>" +
117 " <forenames>Ziyi</forenames>" +
118 " <surname>Zhang</surname>" +
119 " </actor>" +
120 " </actors>" +
121 "</movie>";
122
123 xmlAssertIsomorphicContent(parseString(expected), parseString(out));
124 }
125
126
127 public void testSuppressElementName() throws Exception {
128 PersonBean angLee = new NullPersonBean("Ang","Lee");
129 MovieBean movie = new MovieBean("Crouching Tiger, Hidden Dragon", 2000, angLee);
130 movie.addActor(new NullPersonBean("Yun-Fat", "Chow"));
131 movie.addActor(new PersonBean("Michelle", "Yeoh"));
132 movie.addActor(new PersonBean("Ziyi", "Zhang"));
133
134 StringWriter out = new StringWriter();
135 out.write("<?xml version='1.0'?>");
136 BeanWriter writer = new BeanWriter(out);
137 writer.getBindingConfiguration().setMapIDs(false);
138 writer.getBindingConfiguration().setValueSuppressionStrategy(new ValueSuppressionStrategy() {
139
140 public boolean suppressAttribute(AttributeDescriptor attributeDescriptor, String value) {
141 return DEFAULT.suppressAttribute(attributeDescriptor, value);
142 }
143
144 public boolean suppressElement(ElementDescriptor element, String namespaceUrl, String localName, String qualifiedName, Object value) {
145
146 boolean result = false;
147 if ("year".equals(element.getQualifiedName())){
148 result = true;
149 }
150 return result;
151 }
152 });
153 writer.write(movie);
154
155 String expected = "<?xml version='1.0'?>" +
156 "<movie>" +
157 " <name>Crouching Tiger, Hidden Dragon</name>" +
158 " <director>" +
159 " <forenames>Ang</forenames>" +
160 " <surname>Lee</surname>" +
161 " </director>" +
162 " <actors>" +
163 " <actor>" +
164 " <forenames>Yun-Fat</forenames>" +
165 " <surname>Chow</surname>" +
166 " </actor>" +
167 " <actor>" +
168 " <forenames>Michelle</forenames>" +
169 " <surname>Yeoh</surname>" +
170 " </actor>" +
171 " <actor>" +
172 " <forenames>Ziyi</forenames>" +
173 " <surname>Zhang</surname>" +
174 " </actor>" +
175 " </actors>" +
176 "</movie>";
177
178 xmlAssertIsomorphicContent(parseString(expected), parseString(out));
179 }
180
181 public void testSuppressName() throws Exception {
182 PersonBean angLee = new NullPersonBean("Ang","Lee");
183 MovieBean movie = new MovieBean("Crouching Tiger, Hidden Dragon", 2000, angLee);
184 movie.addActor(new NullPersonBean("Yun-Fat", "Chow"));
185 movie.addActor(new PersonBean("Michelle", "Yeoh"));
186 movie.addActor(new PersonBean("Ziyi", "Zhang"));
187
188 StringWriter out = new StringWriter();
189 out.write("<?xml version='1.0'?>");
190 BeanWriter writer = new BeanWriter(out);
191 writer.getBindingConfiguration().setMapIDs(false);
192 writer.getBindingConfiguration().setValueSuppressionStrategy(new ValueSuppressionStrategy() {
193
194 public boolean suppressAttribute(AttributeDescriptor attributeDescriptor, String value) {
195 return DEFAULT.suppressAttribute(attributeDescriptor, value);
196 }
197
198 public boolean suppressElement(ElementDescriptor element, String namespaceUrl, String localName, String qualifiedName, Object value) {
199
200 boolean result = false;
201 if ("actor".equals(qualifiedName)){
202 result = true;
203 }
204 return result;
205 }
206 });
207 writer.write(movie);
208
209 String expected = "<?xml version='1.0'?>" +
210 "<movie>" +
211 " <name>Crouching Tiger, Hidden Dragon</name>" +
212 " <year>2000</year>" +
213 " <director>" +
214 " <forenames>Ang</forenames>" +
215 " <surname>Lee</surname>" +
216 " </director>" +
217 " <actors>" +
218 " </actors>" +
219 "</movie>";
220
221 xmlAssertIsomorphicContent(parseString(expected), parseString(out));
222 }
223 }