001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt;
018:
019: import java.io.StringWriter;
020: import java.util.Locale;
021:
022: import org.apache.commons.betwixt.io.BeanWriter;
023: import org.apache.commons.betwixt.strategy.PropertySuppressionStrategy;
024:
025: /**
026: * This test is the result of a problem I had with outputting a bean's class
027: * property as XML. I had a request for that feature quite a while ago and the
028: * {@link #org.apache.commons.betwixt.strategy.PropertySupressionStretegy}was
029: * added to made this possible. It worked quite well, until I used beans
030: * described in dot-betwixt files that also output the class property like the
031: * following:
032: *
033: * <pre>
034: * <info primitiveTypes="element">
035: * <element name="test-class">
036: * <attribute name="test-prop-1" property="testPropertyOne"/>
037: * <attribute name="test-prop-2" property="testPropertyTwo"/>
038: * <element name="class" property="class"/>
039: * </element>
040: * </info>
041: * </pre>
042: *
043: * So it worked without dot-betwixt files, but the seconds test
044: * {@link #testHasClassElementWithDotBetwixtFile()}would fail. There was a
045: * small block in {@link org.apache.commons.betwixt.digester.ElementRule}that
046: * was marked with ToDo, without that block it works.
047: *
048: * @author Christoph Gaffga, cgaffga@triplemind.com
049: */
050: public class TestClassProperty extends AbstractTestCase {
051:
052: public TestClassProperty(String testName) {
053: super (testName);
054: }
055:
056: public void testHasClassElementWithoutDotBetwixtFile()
057: throws Exception {
058: // configure bean writer with counting suppression strategy...
059: StringWriter buffer = new StringWriter();
060: BeanWriter beanWriter = new BeanWriter(buffer);
061: beanWriter.getXMLIntrospector().getConfiguration()
062: .setPropertySuppressionStrategy(
063: new PropertySuppressionStrategy() {
064:
065: public boolean suppressProperty(
066: Class clazz, Class propertyType,
067: String propertyName) {
068: return false;
069: }
070: });
071:
072: // test with class without dot-betwixt file...
073: Object bean = new Locale("de", "de"); // just a bean with some properties
074: beanWriter.write(bean);
075:
076: // was the class element written?..
077: assertTrue(buffer.toString().indexOf(
078: "<class>" + bean.getClass().getName() + "</class>") > 0);
079: }
080:
081: public void testHasClassElementWithDotBetwixtFile()
082: throws Exception {
083: // configure bean writer with counting suppression strategy...
084: StringWriter buffer = new StringWriter();
085: BeanWriter beanWriter = new BeanWriter(buffer);
086: beanWriter.getXMLIntrospector().getConfiguration()
087: .setPropertySuppressionStrategy(
088: new PropertySuppressionStrategy() {
089:
090: public boolean suppressProperty(
091: Class clazz, Class propertyType,
092: String propertyName) {
093: return false;
094: }
095: });
096:
097: // test with class without dot-betwixt file...
098: Object bean = new SimpleClass();
099: beanWriter.write(bean);
100:
101: // was the class element written?..
102: assertTrue(buffer.toString().indexOf(
103: "<class>" + bean.getClass().getName() + "</class>") > 0);
104: }
105: }
|