1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.betwixt.strategy;
18  
19  import java.io.StringReader;
20  import java.io.StringWriter;
21  import java.io.Writer;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.commons.betwixt.XMLIntrospector;
26  import org.apache.commons.betwixt.io.BeanReader;
27  import org.apache.commons.betwixt.io.BeanWriter;
28  
29  
30  /*** 
31   * Tests streaming/destreaming of an <code>Elements</code> bean, 
32   * a container for <code>Element</code> instances, using various name mappers
33   * The objective of this is to verify that containers whose names
34   * are plurals of their contents can be written and read back successfully.
35   * 
36   * @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
37   */
38  public class TestElementsIO extends TestCase {
39  
40  //    private SimpleLog testLog;
41  
42      public TestElementsIO(String name) {
43          super(name);
44  //        testLog = new SimpleLog("[TextElementsIO]");
45  //        testLog.setLevel(SimpleLog.LOG_LEVEL_TRACE);
46      }
47  
48      public void testCapitalizeNameMapper() throws Exception {
49  //        testLog.debug("Testing capitalize name mapper");
50          doTest(new CapitalizeNameMapper(), "capitalize name mapper");
51      }
52  
53      public void testDecapitalizeNameMapper() throws Exception {
54  //        testLog.debug("Testing decapitalize name mapper");
55          doTest(new DecapitalizeNameMapper(), "decapitalize name mapper");
56      }
57  
58      public void testDefaultElementMapper() throws Exception {
59  //        testLog.debug("Testing default name mapper");
60          doTest(new DefaultNameMapper(), "default name mapper");
61      }
62  
63      public void testHyphenatedNameMapper() throws Exception {
64  //        testLog.debug("Testing hyphenated name mapper");
65          doTest(new HyphenatedNameMapper(), "hyphenated name mapper");
66      }
67  
68      private void doTest(NameMapper mapper, String testName) throws Exception {
69          Elements elements = new Elements();
70          elements.addElement(new Element("a"));
71          elements.addElement(new Element("b"));
72          elements.addElement(new Element("c"));
73  
74          StringWriter out = new StringWriter();
75          BeanWriter writer = newBeanWriter(out, mapper);
76          writer.setWriteEmptyElements(true);
77          writer.write(elements);
78          writer.flush();
79          
80          String xmlOut = out.toString();
81          
82  //        testLog.debug(xmlOut);
83  
84          StringReader in = new StringReader(xmlOut);
85          
86  //        SimpleLog log = new SimpleLog("[TextElementsIO:BeanReader]");
87  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
88          
89          BeanReader reader = new BeanReader();
90  //        reader.setLog(log);
91  
92  //        log = new SimpleLog("[TextElementsIO:BeanReader]");
93  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
94  //        BeanCreateRule.setLog(log);
95          
96          reader.setXMLIntrospector(newXMLIntrospector(mapper));
97          reader.registerBeanClass(Elements.class);
98          Elements result = (Elements) reader.parse(in);
99  
100         assertNotNull("Element 'a' is null (" + testName + ")", result.getElement("a"));
101         assertNotNull("Element 'b' is null (" + testName + ")", result.getElement("b"));
102         assertNotNull("Element 'c' is null (" + testName + ")", result.getElement("c"));
103     }
104 
105     private BeanWriter newBeanWriter(Writer writer, NameMapper mapper) {
106 //        SimpleLog log = new SimpleLog("[TextElementsIO:BeanWriter]");
107 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
108         
109         BeanWriter result = new BeanWriter(writer);
110         result.setWriteEmptyElements(true);
111 //        result.setLog(log);
112         
113 //        log = new SimpleLog("[TextElementsIO:AbstractBeanWriter]");
114 //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
115 //        result.setAbstractBeanWriterLog(log);
116         
117         result.setXMLIntrospector(newXMLIntrospector(mapper));
118         result.enablePrettyPrint();
119         result.getBindingConfiguration().setMapIDs(false);
120         return result;
121     }
122 
123     private XMLIntrospector newXMLIntrospector(NameMapper mapper) {
124         XMLIntrospector introspector = new XMLIntrospector();
125         introspector.getConfiguration().setAttributesForPrimitives(true);
126         introspector.getConfiguration().setWrapCollectionsInElement(false);
127         introspector.getConfiguration().setElementNameMapper(mapper);
128         return introspector;
129     }
130 }
131