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.nowrap;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.StringWriter;
23  import java.util.List;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.commons.betwixt.AbstractTestCase;
29  import org.apache.commons.betwixt.XMLIntrospector;
30  import org.apache.commons.betwixt.io.BeanReader;
31  import org.apache.commons.betwixt.io.BeanWriter;
32  import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
33  import org.apache.commons.betwixt.strategy.DefaultPluralStemmer;
34  
35  /**
36   * Test harness for the base PO object
37   *
38   * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
39   * @version $Id: TestNoWrap.java 438373 2006-08-30 05:17:21Z bayard $
40   */
41  public class TestNoWrap
42      extends AbstractTestCase
43  {
44      private POTest po;
45  
46      /**
47       * A unit test suite for JUnit
48       */
49      public static Test suite()
50      {
51          return new TestSuite(TestNoWrap.class);
52      }
53  
54      /**
55       * Constructor for the TestScarabSettings object
56       *
57       * @param testName
58       */
59      public TestNoWrap(String testName)
60      {
61          super(testName);
62      }
63  
64      /**
65       * Description of the Method
66       */
67      public void testRoundTrip()
68          throws Exception
69      {
70          load();
71          write();
72      }
73  
74      /**
75       * Description of the Method
76       */
77      public void load()
78          throws Exception
79      {
80          String xmlLocation = getTestFile("src/test/org/apache/commons/betwixt/nowrap/po_add_test.xml");
81  
82          FileInputStream in = new FileInputStream(new File(xmlLocation));
83  
84          // create a new BeanReader
85          BeanReader reader = createBeanReader(POTest.class);
86          po = (POTest) reader.parse(in);
87          assertEquals("PO Printing No", "555008805581", po.getPrintingNumber());
88          List componentTests = po.getComponenttests();
89          
90          assertEquals("#Component tests", 3, componentTests.size());
91          Componenttest testOne = (Componenttest) componentTests.get(0);
92          assertEquals("Component Test One", "Text", testOne.getCompDescription());
93          Componenttest testTwo = (Componenttest) componentTests.get(1);
94          assertEquals("Component Test Two", "Binding", testTwo.getCompDescription());
95          Componenttest testThree = (Componenttest) componentTests.get(2);
96          assertEquals("Component Test Three", "Paper Cover", testThree.getCompDescription());
97      }
98  
99      /**
100      * Description of the Method
101      */
102     public void write()
103         throws Exception
104     {
105         // Let's try to write the bean
106         StringWriter out = new StringWriter();
107         out.write("<?xml version='1.0'?>");
108         BeanWriter beanWriter = new BeanWriter(out);
109         beanWriter.setXMLIntrospector(createXMLIntrospector());
110         beanWriter.getBindingConfiguration().setMapIDs(false);
111         beanWriter.setEndOfLine("\n");
112         beanWriter.enablePrettyPrint();
113         
114         beanWriter.write(po);
115         String xml = "<?xml version='1.0'?><content><printingno>555008805581</printingno>"
116                 + "<componenttest><compdescription>Text</compdescription></componenttest>"
117                 + "<componenttest><compdescription>Binding</compdescription></componenttest>"
118                 + "<componenttest><compdescription>Paper Cover</compdescription>"
119                 + "</componenttest></content>";
120                 
121         xmlAssertIsomorphicContent(
122                     parseString(xml),
123                     parseString(out.getBuffer().toString()),
124                     true);
125     }
126 
127     // Implementation methods
128     //-------------------------------------------------------------------------
129 
130     /**
131      * Description of the Method
132      */
133     protected BeanReader createBeanReader(Class beanClass)
134         throws Exception
135     {
136         BeanReader reader = new BeanReader();
137         reader.setXMLIntrospector(createXMLIntrospector());
138         reader.registerBeanClass(beanClass);
139         return reader;
140     }
141 
142     /**
143      * ### it would be really nice to move this somewhere shareable across Maven
144      * / Turbine projects. Maybe a static helper method - question is what to
145      * call it???
146      */
147     protected XMLIntrospector createXMLIntrospector()
148     {
149         XMLIntrospector introspector = new XMLIntrospector();
150 
151         // set elements for attributes to true
152         introspector.getConfiguration().setAttributesForPrimitives(false);
153 
154         // wrap collections in an XML element
155         introspector.getConfiguration().setWrapCollectionsInElement(false);
156 
157         // turn bean elements first letter into lower case
158         introspector.getConfiguration().setElementNameMapper( new DecapitalizeNameMapper() );
159 
160         // Set default plural stemmer.
161         introspector.getConfiguration().setPluralStemmer( new DefaultPluralStemmer() );
162 
163         return introspector;
164     }
165 }
166