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.alt;
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  import org.apache.commons.betwixt.strategy.CapitalizeNameMapper;
30  import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
31  import org.apache.commons.betwixt.strategy.DefaultNameMapper;
32  import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
33  import org.apache.commons.betwixt.strategy.NameMapper;
34  
35  
36  /** 
37   * Tests streaming/destreaming of an <code>Elements</code> bean, 
38   * a container for <code>Element</code> instances, using various name mappers
39   * The objective of this is to verify that containers whose names
40   * are plurals of their contents can be written and read back successfully.
41   * 
42   * @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
43   */
44  public class TestElementsAlt extends TestCase {
45  
46  //    private SimpleLog testLog;
47  
48      public TestElementsAlt(String name) {
49          super(name);
50  //        testLog = new SimpleLog("[TestElementsAlt]");
51  //        testLog.setLevel(SimpleLog.LOG_LEVEL_TRACE);
52      }
53  
54      public void testCapitalizeNameMapper() throws Exception {
55  //        testLog.debug("Testing capitalize name mapper");
56          doTest(new CapitalizeNameMapper(), "capitalize name mapper");
57      }
58  
59      public void testDecapitalizeNameMapper() throws Exception {
60  //        testLog.debug("Testing decapitalize name mapper");
61          doTest(new DecapitalizeNameMapper(), "decapitalize name mapper");
62      }
63  
64      public void testDefaultElementMapper() throws Exception {
65  //        testLog.debug("Testing default name mapper");
66          doTest(new DefaultNameMapper(), "default name mapper");
67      }
68  
69      public void testHyphenatedNameMapper() throws Exception {
70  //        testLog.debug("Testing hyphenated name mapper");
71          doTest(new HyphenatedNameMapper(), "hyphenated name mapper");
72      }
73  
74      private void doTest(NameMapper mapper, String testName) throws Exception {
75          Elements elements = new Elements();
76          elements.addElement(new Element("a"));
77          elements.addElement(new Element("b"));
78          elements.addElement(new Element("c"));
79  
80          StringWriter out = new StringWriter();
81          BeanWriter writer = newBeanWriter(out, mapper);
82          writer.setWriteEmptyElements( true );
83          writer.write(elements);
84          writer.flush();
85          
86          String xmlOut = out.toString();
87          
88  //        testLog.debug(xmlOut);
89  
90          StringReader in = new StringReader(xmlOut);
91          
92  //        SimpleLog log = new SimpleLog("[TestElementsAlt:BeanReader]");
93  //        log.setLevel(SimpleLog.LOG_LEVEL_TRACE);
94          
95          BeanReader reader = new BeanReader();
96  //        reader.setLog(log);
97  
98  //        log = new SimpleLog("[TestElementsAlt:BeanReader]");
99  //        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.getElement("a"));
107         assertNotNull("Element 'b' is null (" + testName + ")", result.getElement("b"));
108         assertNotNull("Element 'c' is null (" + testName + ")", result.getElement("c"));
109     }
110 
111     private BeanWriter newBeanWriter(Writer writer, NameMapper mapper) {        
112         BeanWriter result = new BeanWriter(writer);
113         result.setWriteEmptyElements( true );
114         
115         result.setXMLIntrospector(newXMLIntrospector(mapper));
116         result.setEndOfLine("\n");
117         result.enablePrettyPrint();
118         result.getBindingConfiguration().setMapIDs(false);
119         return result;
120     }
121 
122     private XMLIntrospector newXMLIntrospector(NameMapper mapper) {
123         XMLIntrospector introspector = new XMLIntrospector();
124         introspector.getConfiguration().setAttributesForPrimitives(true);
125         introspector.getConfiguration().setWrapCollectionsInElement(false);
126         introspector.getConfiguration().setElementNameMapper(mapper);
127         return introspector;
128     }
129 }
130