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:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023: import junit.textui.TestRunner;
024:
025: import org.apache.commons.beanutils.BasicDynaClass;
026: import org.apache.commons.beanutils.DynaBean;
027: import org.apache.commons.beanutils.DynaClass;
028: import org.apache.commons.beanutils.DynaProperty;
029: import org.apache.commons.betwixt.io.BeanWriter;
030: import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
031:
032: /** Test harness for the DynaBeans support
033: *
034: * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
035: * @version $Revision: 438373 $
036: */
037: public class TestDynaBeanSupport extends AbstractTestCase {
038:
039: public static void main(String[] args) {
040: TestRunner.run(suite());
041: }
042:
043: public static Test suite() {
044: return new TestSuite(TestDynaBeanSupport.class);
045: }
046:
047: public TestDynaBeanSupport(String testName) {
048: super (testName);
049: }
050:
051: public void testIntrospectDynaBean() throws Exception {
052: XMLIntrospector introspector = new XMLIntrospector();
053: introspector.getConfiguration().setAttributesForPrimitives(
054: false);
055: XMLBeanInfo beanInfo = introspector
056: .introspect(createDynasaurClass());
057: ElementDescriptor baseElement = beanInfo.getElementDescriptor();
058: // no attributes
059: assertEquals("Correct number of attributes", 0, baseElement
060: .getAttributeDescriptors().length);
061: ElementDescriptor[] descriptors = baseElement
062: .getElementDescriptors();
063: assertEquals("Correct number of elements", 3,
064: descriptors.length);
065:
066: boolean matchedSpecies = false;
067: boolean matchedIsRaptor = false;
068: boolean matchedPeriod = false;
069:
070: for (int i = 0, size = descriptors.length; i < size; i++) {
071: if ("Species".equals(descriptors[i].getPropertyName())) {
072: matchedSpecies = true;
073: }
074:
075: if ("isRaptor".equals(descriptors[i].getPropertyName())) {
076: matchedIsRaptor = true;
077: }
078:
079: if ("Period".equals(descriptors[i].getPropertyName())) {
080: matchedPeriod = true;
081: }
082: }
083:
084: assertTrue("Species descriptor not found", matchedSpecies);
085: assertTrue("isRaptor descriptor not found", matchedIsRaptor);
086: assertTrue("Period descriptor not found", matchedPeriod);
087: }
088:
089: public void testWriteDynaBean() throws Exception {
090: DynaBean dynasaur = createDynasaurClass().newInstance();
091: dynasaur.set("Species", "Allosaurus");
092: dynasaur.set("isRaptor", Boolean.TRUE);
093: dynasaur.set("Period", "Jurassic");
094:
095: StringWriter out = new StringWriter();
096: out.write("<?xml version='1.0'?>");
097: BeanWriter writer = new BeanWriter(out);
098: writer.getBindingConfiguration().setMapIDs(false);
099: writer.getXMLIntrospector().getConfiguration()
100: .setElementNameMapper(new DecapitalizeNameMapper());
101: writer.write(dynasaur);
102:
103: String xml = "<?xml version='1.0'?><dynasaur><species>Allosaurus</species>"
104: + "<isRaptor>true</isRaptor><period>Jurassic</period></dynasaur>";
105:
106: xmlAssertIsomorphicContent("Test write dyna beans",
107: parseString(xml), parseString(out.getBuffer()
108: .toString()), true);
109: }
110:
111: public void testOverrideWithDotBetwixt() throws Exception {
112: DynaWithDotBetwixt bean = new DynaWithDotBetwixt("Tweedledum",
113: "Tweedledee");
114: StringWriter out = new StringWriter();
115: out.write("<?xml version='1.0'?>");
116: BeanWriter writer = new BeanWriter(out);
117: writer.getBindingConfiguration().setMapIDs(false);
118: writer.getXMLIntrospector().getConfiguration()
119: .setElementNameMapper(new DecapitalizeNameMapper());
120: writer.write("bean", bean);
121:
122: String xml = "<?xml version='1.0'?><bean><ndp>Tweedledum</ndp></bean>";
123: xmlAssertIsomorphicContent(
124: "Test write dyna beans with dt betwixt",
125: parseString(xml), parseString(out.getBuffer()
126: .toString()), true);
127:
128: }
129:
130: private DynaClass createDynasaurClass() throws Exception {
131: DynaClass dynaClass = new BasicDynaClass("Dynasaur", null,
132: new DynaProperty[] {
133: new DynaProperty("Species", String.class),
134: new DynaProperty("isRaptor", Boolean.TYPE),
135: new DynaProperty("Period", String.class), });
136: return (dynaClass);
137: }
138: }
|