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:
018: package org.apache.commons.betwixt;
019:
020: import java.io.StringReader;
021: import java.io.StringWriter;
022: import java.util.Iterator;
023:
024: import org.apache.commons.betwixt.expression.IteratorExpression;
025: import org.apache.commons.betwixt.io.BeanReader;
026: import org.apache.commons.betwixt.io.BeanWriter;
027: import org.apache.commons.betwixt.strategy.CapitalizeNameMapper;
028:
029: /**
030: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
031: * @version $Revision: 438373 $
032: */
033: public class TestCollectives extends AbstractTestCase {
034:
035: private IntrospectionConfiguration categoriesIntrospectionConfiguration = new IntrospectionConfiguration();
036: private BindingConfiguration noIDsBindingConfiguration = new BindingConfiguration();
037:
038: public TestCollectives(String testName) {
039: super (testName);
040:
041: CapitalizeNameMapper capitalizeNameMapper = new CapitalizeNameMapper();
042: categoriesIntrospectionConfiguration
043: .setAttributesForPrimitives(false);
044: categoriesIntrospectionConfiguration
045: .setElementNameMapper(capitalizeNameMapper);
046: categoriesIntrospectionConfiguration
047: .setAttributeNameMapper(capitalizeNameMapper);
048: categoriesIntrospectionConfiguration
049: .setWrapCollectionsInElement(false);
050:
051: noIDsBindingConfiguration.setMapIDs(false);
052: }
053:
054: public void testWriteCategories() throws Exception {
055: StringWriter out = new StringWriter();
056: out.write("<?xml version='1.0'?>");
057: BeanWriter writer = new BeanWriter(out);
058: writer.getXMLIntrospector().setConfiguration(
059: categoriesIntrospectionConfiguration);
060: writer.setBindingConfiguration(noIDsBindingConfiguration);
061:
062: Categories categories = new Categories();
063: categories.addCategory(new Category("Runs"));
064: categories.addCategory(new Category("Innings"));
065: categories.addCategory(new Category("Dismissals"));
066: categories.addCategory(new Category("High Score"));
067: categories.addCategory(new Category("Average"));
068:
069: writer.write(categories);
070:
071: String xml = out.getBuffer().toString();
072: String expected = "<?xml version='1.0'?><Categories>"
073: + "<Category><Name>Runs</Name></Category>"
074: + "<Category><Name>Innings</Name></Category>"
075: + "<Category><Name>Dismissals</Name></Category>"
076: + "<Category><Name>High Score</Name></Category>"
077: + "<Category><Name>Average</Name></Category>"
078: + "</Categories>";
079:
080: xmlAssertIsomorphicContent(parseString(expected),
081: parseString(xml));
082: }
083:
084: public void testReadCategories() throws Exception {
085: BeanReader beanReader = new BeanReader();
086: beanReader.getXMLIntrospector().setConfiguration(
087: categoriesIntrospectionConfiguration);
088: beanReader.setBindingConfiguration(noIDsBindingConfiguration);
089: beanReader.registerBeanClass(Categories.class);
090:
091: String xml = "<?xml version='1.0'?><Categories>"
092: + "<Category><Name>Runs</Name></Category>"
093: + "<Category><Name>Innings</Name></Category>"
094: + "<Category><Name>Dismissals</Name></Category>"
095: + "<Category><Name>High Score</Name></Category>"
096: + "<Category><Name>Average</Name></Category>"
097: + "</Categories>";
098:
099: StringReader in = new StringReader(xml);
100:
101: Categories bean = (Categories) beanReader.parse(in);
102:
103: assertEquals("5 categories", 5, bean.size());
104:
105: Iterator it = bean.getCategories();
106: assertEquals("Runs category", new Category("Runs"), it.next());
107: assertEquals("Runs category", new Category("Innings"), it
108: .next());
109: assertEquals("Runs category", new Category("Dismissals"), it
110: .next());
111: assertEquals("Runs category", new Category("High Score"), it
112: .next());
113: assertEquals("Runs category", new Category("Average"), it
114: .next());
115:
116: }
117:
118: public void testIntrospectListExtension() throws Exception {
119: XMLIntrospector xmlIntrospector = new XMLIntrospector();
120: XMLBeanInfo beanInfo = xmlIntrospector
121: .introspect(ArrayListExtender.class);
122:
123: ElementDescriptor elementDescriptor = beanInfo
124: .getElementDescriptor();
125: ElementDescriptor[] childDescriptors = elementDescriptor
126: .getElementDescriptors();
127: assertEquals(2, childDescriptors.length);
128: assertEquals("another", childDescriptors[0].getPropertyName());
129: assertTrue(childDescriptors[1].getContextExpression() instanceof IteratorExpression);
130: }
131:
132: public void testWriteListExtension() throws Exception {
133: ArrayListExtender bean = new ArrayListExtender("Whatever");
134: bean.add(new Long(11));
135: bean.add(new Long(12));
136: bean.add(new Long(13));
137:
138: StringWriter out = new StringWriter();
139: out.write("<?xml version='1.0'?>");
140:
141: BeanWriter writer = new BeanWriter(out);
142: writer.getBindingConfiguration().setMapIDs(false);
143: writer.write(bean);
144:
145: String expected = "<?xml version='1.0'?><ArrayListExtender><another>Whatever</another>"
146: + "<Long>11</Long><Long>12</Long><Long>13</Long></ArrayListExtender>";
147:
148: xmlAssertIsomorphicContent(parseString(expected),
149: parseString(out));
150: }
151:
152: public void testReadListExtension() throws Exception {
153: String xml = "<?xml version='1.0'?><ArrayListExtender><another>Whatever</another>"
154: + "<Long>11</Long><Long>12</Long><Long>13</Long></ArrayListExtender>";
155:
156: StringReader in = new StringReader(xml);
157:
158: BeanReader reader = new BeanReader();
159: reader.getBindingConfiguration().setMapIDs(false);
160:
161: reader.registerBeanClass(ArrayListExtender.class);
162: ArrayListExtender bean = (ArrayListExtender) reader.parse(in);
163:
164: assertEquals("Whatever", bean.getAnother());
165: }
166: }
|