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.10 2004/02/28 13:38:36 yoavs 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          assertEquals("#Component tests", 3, componentTests.size());
89          Componenttest testOne = (Componenttest) componentTests.get(0);
90          assertEquals("Component Test One", "Text", testOne.getCompDescription());
91          Componenttest testTwo = (Componenttest) componentTests.get(1);
92          assertEquals("Component Test Two", "Binding", testTwo.getCompDescription());
93          Componenttest testThree = (Componenttest) componentTests.get(2);
94          assertEquals("Component Test Three", "Paper Cover", testThree.getCompDescription());
95      }
96  
97      /***
98       * Description of the Method
99       */
100     public void write()
101         throws Exception
102     {
103         // Let's try to write the bean
104         StringWriter out = new StringWriter();
105         out.write("<?xml version='1.0'?>");
106         BeanWriter beanWriter = new BeanWriter(out);
107         beanWriter.setXMLIntrospector(createXMLIntrospector());
108         beanWriter.setWriteIDs(false);
109         beanWriter.enablePrettyPrint();
110         
111         beanWriter.write(po);
112         String xml = "<?xml version='1.0'?><content><printingno>555008805581</printingno>"
113                 + "<componenttest><compdescription>Text</compdescription></componenttest>"
114                 + "<componenttest><compdescription>Binding</compdescription></componenttest>"
115                 + "<componenttest><compdescription>Paper Cover</compdescription>"
116                 + "</componenttest></content>";
117                 
118         xmlAssertIsomorphicContent(
119                     parseString(xml),
120                     parseString(out.getBuffer().toString()),
121                     true);
122     }
123 
124     // Implementation methods
125     //-------------------------------------------------------------------------
126 
127     /***
128      * Description of the Method
129      */
130     protected BeanReader createBeanReader(Class beanClass)
131         throws Exception
132     {
133         BeanReader reader = new BeanReader();
134         reader.setXMLIntrospector(createXMLIntrospector());
135         reader.registerBeanClass(beanClass);
136         return reader;
137     }
138 
139     /***
140      * ### it would be really nice to move this somewhere shareable across Maven
141      * / Turbine projects. Maybe a static helper method - question is what to
142      * call it???
143      */
144     protected XMLIntrospector createXMLIntrospector()
145     {
146         XMLIntrospector introspector = new XMLIntrospector();
147 
148         // set elements for attributes to true
149         introspector.setAttributesForPrimitives(false);
150 
151         // wrap collections in an XML element
152         introspector.setWrapCollectionsInElement(false);
153 
154         // turn bean elements first letter into lower case
155         introspector.setElementNameMapper( new DecapitalizeNameMapper() );
156 
157         // Set default plural stemmer.
158         introspector.setPluralStemmer( new DefaultPluralStemmer() );
159 
160         return introspector;
161     }
162 }
163