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