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