1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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.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   * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
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  }