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