1   /*
2    * Copyright 2005 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;
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   *   &lt;info primitiveTypes="element"&gt;
34   *     &lt;element name="test-class"&gt;
35   *       &lt;attribute name="test-prop-1" property="testPropertyOne"/&gt;
36   *       &lt;attribute name="test-prop-2" property="testPropertyTwo"/&gt;
37   *       &lt;element name="class" property="class"/&gt;
38   *     &lt;/element&gt;
39   *   &lt;/info&gt;
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          // configure bean writer with counting suppression strategy...
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          // test with class without dot-betwixt file...
69          Object bean = new Locale("de", "de"); // just a bean with some properties
70          beanWriter.write(bean);
71  
72          // was the class element written?..
73          assertTrue(buffer.toString().indexOf("<class>" + bean.getClass().getName() + "</class>") > 0);  
74      }
75  
76      public void testHasClassElementWithDotBetwixtFile() throws Exception {
77          // configure bean writer with counting suppression strategy...
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          // test with class without dot-betwixt file...
90          Object bean = new SimpleClass();
91          beanWriter.write(bean);
92  
93          // was the class element written?..
94          assertTrue(buffer.toString().indexOf("<class>" + bean.getClass().getName() + "</class>") > 0);
95      }
96  }