1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt;
18
19 import java.io.StringWriter;
20 import java.util.Locale;
21
22 import org.apache.commons.betwixt.io.BeanWriter;
23 import org.apache.commons.betwixt.strategy.PropertySuppressionStrategy;
24
25 /***
26 * This test is the result of a problem I had with outputting a bean's class
27 * property as XML. I had a request for that feature quite a while ago and the
28 * {@link #org.apache.commons.betwixt.strategy.PropertySupressionStretegy}was
29 * added to made this possible. It worked quite well, until I used beans
30 * described in dot-betwixt files that also output the class property like the
31 * following:
32 *
33 * <pre>
34 * <info primitiveTypes="element">
35 * <element name="test-class">
36 * <attribute name="test-prop-1" property="testPropertyOne"/>
37 * <attribute name="test-prop-2" property="testPropertyTwo"/>
38 * <element name="class" property="class"/>
39 * </element>
40 * </info>
41 * </pre>
42 *
43 * So it worked without dot-betwixt files, but the seconds test
44 * {@link #testHasClassElementWithDotBetwixtFile()}would fail. There was a
45 * small block in {@link org.apache.commons.betwixt.digester.ElementRule}that
46 * was marked with ToDo, without that block it works.
47 *
48 * @author Christoph Gaffga, cgaffga@triplemind.com
49 */
50 public class TestClassProperty extends AbstractTestCase {
51
52 public TestClassProperty(String testName) {
53 super(testName);
54 }
55
56 public void testHasClassElementWithoutDotBetwixtFile() throws Exception {
57
58 StringWriter buffer = new StringWriter();
59 BeanWriter beanWriter = new BeanWriter(buffer);
60 beanWriter.getXMLIntrospector().getConfiguration().setPropertySuppressionStrategy(
61 new PropertySuppressionStrategy() {
62
63 public boolean suppressProperty(Class clazz, Class propertyType,
64 String propertyName) {
65 return false;
66 }
67 });
68
69
70 Object bean = new Locale("de", "de");
71 beanWriter.write(bean);
72
73
74 assertTrue(buffer.toString().indexOf("<class>" + bean.getClass().getName() + "</class>") > 0);
75 }
76
77 public void testHasClassElementWithDotBetwixtFile() throws Exception {
78
79 StringWriter buffer = new StringWriter();
80 BeanWriter beanWriter = new BeanWriter(buffer);
81 beanWriter.getXMLIntrospector().getConfiguration().setPropertySuppressionStrategy(
82 new PropertySuppressionStrategy() {
83
84 public boolean suppressProperty(Class clazz, Class propertyType,
85 String propertyName) {
86 return false;
87 }
88 });
89
90
91 Object bean = new SimpleClass();
92 beanWriter.write(bean);
93
94
95 assertTrue(buffer.toString().indexOf("<class>" + bean.getClass().getName() + "</class>") > 0);
96 }
97 }