1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }