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.strategy;
019:
020: import java.io.StringReader;
021: import java.io.StringWriter;
022: import java.io.Writer;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.commons.betwixt.XMLIntrospector;
027: import org.apache.commons.betwixt.io.BeanReader;
028: import org.apache.commons.betwixt.io.BeanWriter;
029:
030: /**
031: * Tests streaming/destreaming of an <code>Elements</code> bean,
032: * a container for <code>Element</code> instances, using various name mappers
033: * The objective of this is to verify that containers whose names
034: * are plurals of their contents can be written and read back successfully.
035: *
036: * @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
037: */
038: public class TestElementsIO extends TestCase {
039:
040: // private SimpleLog testLog;
041:
042: public TestElementsIO(String name) {
043: super (name);
044: // testLog = new SimpleLog("[TextElementsIO]");
045: // testLog.setLevel(SimpleLog.LOG_LEVEL_TRACE);
046: }
047:
048: public void testCapitalizeNameMapper() throws Exception {
049: // testLog.debug("Testing capitalize name mapper");
050: doTest(new CapitalizeNameMapper(), "capitalize name mapper");
051: }
052:
053: public void testDecapitalizeNameMapper() throws Exception {
054: // testLog.debug("Testing decapitalize name mapper");
055: doTest(new DecapitalizeNameMapper(), "decapitalize name mapper");
056: }
057:
058: public void testDefaultElementMapper() throws Exception {
059: // testLog.debug("Testing default name mapper");
060: doTest(new DefaultNameMapper(), "default name mapper");
061: }
062:
063: public void testHyphenatedNameMapper() throws Exception {
064: // testLog.debug("Testing hyphenated name mapper");
065: doTest(new HyphenatedNameMapper(), "hyphenated name mapper");
066: }
067:
068: private void doTest(NameMapper mapper, String testName)
069: throws Exception {
070: Elements elements = new Elements();
071: elements.addElement(new Element("a"));
072: elements.addElement(new Element("b"));
073: elements.addElement(new Element("c"));
074:
075: StringWriter out = new StringWriter();
076: BeanWriter writer = newBeanWriter(out, mapper);
077: writer.setWriteEmptyElements(true);
078: writer.write(elements);
079: writer.flush();
080:
081: String xmlOut = out.toString();
082:
083: // testLog.debug(xmlOut);
084:
085: StringReader in = new StringReader(xmlOut);
086:
087: // SimpleLog log = new SimpleLog("[TextElementsIO:BeanReader]");
088: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
089:
090: BeanReader reader = new BeanReader();
091: // reader.setLog(log);
092:
093: // log = new SimpleLog("[TextElementsIO:BeanReader]");
094: // log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
095: // BeanCreateRule.setLog(log);
096:
097: reader.setXMLIntrospector(newXMLIntrospector(mapper));
098: reader.registerBeanClass(Elements.class);
099: Elements result = (Elements) reader.parse(in);
100:
101: assertNotNull("Element 'a' is null (" + testName + ")", result
102: .getElement("a"));
103: assertNotNull("Element 'b' is null (" + testName + ")", result
104: .getElement("b"));
105: assertNotNull("Element 'c' is null (" + testName + ")", result
106: .getElement("c"));
107: }
108:
109: private BeanWriter newBeanWriter(Writer writer, NameMapper mapper) {
110: BeanWriter result = new BeanWriter(writer);
111: result.setWriteEmptyElements(true);
112:
113: result.setXMLIntrospector(newXMLIntrospector(mapper));
114: result.setEndOfLine("\n");
115: result.enablePrettyPrint();
116: result.getBindingConfiguration().setMapIDs(false);
117: return result;
118: }
119:
120: private XMLIntrospector newXMLIntrospector(NameMapper mapper) {
121: XMLIntrospector introspector = new XMLIntrospector();
122: introspector.getConfiguration()
123: .setAttributesForPrimitives(true);
124: introspector.getConfiguration().setWrapCollectionsInElement(
125: false);
126: introspector.getConfiguration().setElementNameMapper(mapper);
127: return introspector;
128: }
129: }
|