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