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