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.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://jakarta.apache.org/commons'>Jakarta 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>" + "</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,
49: 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(
55: "Expected only class name to be mapped",
56: parseString(expected), parseString(out.toString()));
57: }
58:
59: public void testPropertySuppressionStrategy() throws Exception {
60:
61: BeanWithSecrets bean = new BeanWithSecrets(
62: "Surveyor Of The Queen's Pictures",
63: "Queen Elizabeth II", "Sir Anthony Federick Blunt",
64: "Fourth Man", "Soviet Union");
65: StringWriter out = new StringWriter();
66: BeanWriter writer = new BeanWriter(out);
67: writer.getBindingConfiguration().setMapIDs(false);
68: writer.getXMLIntrospector().getConfiguration()
69: .setPropertySuppressionStrategy(
70: new PropertySuppressionStrategy() {
71:
72: public boolean suppressProperty(
73: Class classContainingThePropety,
74: Class propertyType,
75: String propertyName) {
76: if ("class".equals(propertyName)) {
77: return true;
78: }
79: if (propertyName.startsWith("secret")) {
80: return true;
81: }
82: return false;
83: }
84:
85: });
86: writer.write("normal-person", bean);
87:
88: String expected = "<?xml version='1.0'?><normal-person>"
89: + "<employer>Queen Elizabeth II</employer>"
90: + "<job>Surveyor Of The Queen's Pictures</job>"
91: + "<name>Sir Anthony Federick Blunt</name>"
92: + "</normal-person>";
93:
94: xmlAssertIsomorphicContent("Expected secrets to be supressed",
95: parseString(expected), parseString(out.toString()),
96: true);
97:
98: }
99: }
|