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.io;
018:
019: import java.io.StringReader;
020: import java.io.StringWriter;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.commons.betwixt.AbstractTestCase;
025: import org.xml.sax.InputSource;
026:
027: public class TestMixedCollection extends AbstractTestCase {
028: public TestMixedCollection(String name) {
029: super (name);
030: }
031:
032: public void testWithDefaults() throws Exception {
033: toXml(true);
034: }
035:
036: public void testWithoutDefaults() throws Exception {
037: toXml(false);
038: }
039:
040: protected void toXml(boolean addAdders) throws Exception {
041: StringReader configReader = new StringReader(
042: "<?xml version='1.0' ?>"
043: + "<betwixt-config primitiveTypes='attribute'>"
044: + " <class name='org.apache.commons.betwixt.io.TestMixedCollection$ParentBean'>"
045: + " <element name='parentBean'>"
046: + " <element name='childBeans'>"
047: + " <element property='childBeans'/>"
048: + " </element>"
049: + " <addDefaults add-properties='true' guess-names='false' add-adders='"
050: + addAdders
051: + "'/>"
052: + " </element>"
053: + " </class>"
054: + " <class name='org.apache.commons.betwixt.io.TestMixedCollection$ChildBean1'>"
055: + " <element name='childBean1'>"
056: + " <addDefaults/>"
057: + " </element>"
058: + " </class>"
059: + " <class name='org.apache.commons.betwixt.io.TestMixedCollection$ChildBean2'>"
060: + " <element name='childBean2'>"
061: + " <addDefaults/>"
062: + " </element>" + " </class>"
063: + "</betwixt-config>");
064:
065: ParentBean pb = new ParentBean();
066: pb.setStuff("stuff");
067: ChildBean1 cb1 = new ChildBean1();
068: pb.getChildBeans().add(cb1);
069: ChildBean2 cb2 = new ChildBean2();
070: pb.getChildBeans().add(cb2);
071:
072: StringWriter writer = new StringWriter();
073: BeanWriter beanWriter = new BeanWriter(writer);
074: beanWriter.enablePrettyPrint();
075: beanWriter.getXMLIntrospector().register(
076: new InputSource(configReader));
077: beanWriter.writeXmlDeclaration("<?xml version=\"1.0\"?>");
078: beanWriter.write(pb);
079:
080: String expected = "<?xml version='1.0'?>"
081: + "<parentBean stuff='stuff' id='1'>"
082: + " <childBeans>" + " <childBean1/>"
083: + " <childBean2/>" + " </childBeans>"
084: + "</parentBean>";
085:
086: xmlAssertIsomorphic(parseString(expected), parseString(writer));
087: }
088:
089: public static class ParentBean {
090: private List childBeans = new ArrayList();
091:
092: private String stuff = null;
093:
094: public List getChildBeans() {
095: return childBeans;
096: }
097:
098: public void setChildBeans(List childBeans) {
099: this .childBeans = childBeans;
100: }
101:
102: public void addChildBean(ChildBean childBean) {
103: getChildBeans().add(childBean);
104: }
105:
106: public String getStuff() {
107: return stuff;
108: }
109:
110: public void setStuff(String stuff) {
111: this .stuff = stuff;
112: }
113: }
114:
115: public static abstract class ChildBean {
116: }
117:
118: public static class ChildBean1 extends ChildBean {
119: }
120:
121: public static class ChildBean2 extends ChildBean {
122: }
123: }
|