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