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