01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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.strategy.ValueSuppressionStrategy;
24:
25: /**
26: * @author <a href='http://jakarta.apache.org/commons'>Jakarta Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
27: */
28: public class TestAttributeSuppression extends AbstractTestCase {
29:
30: public TestAttributeSuppression(String testName) {
31: super (testName);
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()
43: .setAttributesForPrimitives(true);
44:
45: writer.write(bean);
46:
47: String expected = "<?xml version='1.0'?><PersonBean forenames='Corwin'/>";
48:
49: xmlAssertIsomorphicContent(parseString(expected),
50: parseString(out));
51: }
52:
53: public void testCustomStrategy() throws Exception {
54: PersonBean bean = new PersonBean("Zaphod", "Beeblebrox");
55:
56: StringWriter out = new StringWriter();
57: out.write("<?xml version='1.0'?>");
58:
59: BeanWriter writer = new BeanWriter(out);
60: writer.getBindingConfiguration().setMapIDs(false);
61: writer.getXMLIntrospector().getConfiguration()
62: .setAttributesForPrimitives(true);
63: writer.getBindingConfiguration().setValueSuppressionStrategy(
64: new ValueSuppressionStrategy() {
65:
66: public boolean suppressAttribute(
67: AttributeDescriptor attributeDescriptor,
68: String value) {
69: return "Zaphod".equals(value);
70: }
71: });
72: writer.write(bean);
73:
74: String expected = "<?xml version='1.0'?><PersonBean surname='Beeblebrox'/>";
75:
76: xmlAssertIsomorphicContent(parseString(expected),
77: parseString(out));
78: }
79: }
|