1   package org.apache.commons.betwixt.scarab;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */ 
19  
20  import java.io.FileInputStream;
21  import java.io.StringWriter;
22  import java.io.Writer;
23  import java.util.List;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  import junit.textui.TestRunner;
28  
29  import org.apache.commons.betwixt.AbstractTestCase;
30  import org.apache.commons.betwixt.XMLIntrospector;
31  import org.apache.commons.betwixt.io.BeanReader;
32  import org.apache.commons.betwixt.io.BeanWriter;
33  import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
34  
35  /**
36   * Test harness which round trips a Scarab's settings xml file
37   *
38   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
39   * @version $Id: TestScarabSettings.java 438373 2006-08-30 05:17:21Z bayard $
40   */
41  public class TestScarabSettings extends AbstractTestCase
42  {
43      public static void main( String[] args )
44      {
45          TestRunner.run( suite() );
46      }
47  
48      /**
49       * A unit test suite for JUnit
50       */
51      public static Test suite()
52      {
53          return new TestSuite(TestScarabSettings.class);
54      }
55  
56      /**
57       * Constructor for the TestScarabSettings object
58       *
59       * @param testName
60       */
61      public TestScarabSettings(String testName)
62      {
63          super(testName);
64      }
65  
66      /**
67       * Tests we can round trip from the XML -> bean -> XML -> bean. Ideally this
68       * method should test both Project objects are identical
69       */
70      public void testRoundTrip()
71          throws Exception
72      {
73          BeanReader reader = createBeanReader();
74  
75          ScarabSettings ss = (ScarabSettings) reader.parse(
76              new FileInputStream(getTestFile("src/test/org/apache/commons/betwixt/scarab/scarab-settings.xml")));
77  
78          // now lets output it to a buffer
79          StringWriter buffer = new StringWriter();
80          write(ss, buffer);
81  
82          // create a new BeanReader
83          reader = createBeanReader();
84  
85          // now lets try parse the output sing the BeanReader
86          String text = buffer.toString();
87  
88          System.out.println(text);
89  
90          /*
91          ScarabSettings newScarabSettings = (ScarabSettings) reader.parse(new StringReader(text));
92  
93          // managed to parse it again!
94          testScarabSettings(newScarabSettings);
95          */
96          testScarabSettings(ss);
97  
98          // #### should now test the old and new Project instances for equality.
99      }
100 
101 
102     // Implementation methods
103     //-------------------------------------------------------------------------
104 
105     /**
106      * Description of the Method
107      */
108     protected BeanReader createBeanReader()
109         throws Exception
110     {
111         BeanReader reader = new BeanReader();
112         reader.setXMLIntrospector(createXMLIntrospector());
113         reader.registerBeanClass(ScarabSettings.class);
114         return reader;
115     }
116 
117     /**
118      * ### it would be really nice to move this somewhere shareable across Maven
119      * / Turbine projects. Maybe a static helper method - question is what to
120      * call it???
121      */
122     protected XMLIntrospector createXMLIntrospector()
123     {
124         XMLIntrospector introspector = new XMLIntrospector();
125 
126         // set elements for attributes to true
127         introspector.getConfiguration().setAttributesForPrimitives(false);
128 
129         // wrap collections in an XML element
130         //introspector.setWrapCollectionsInElement(true);
131 
132         // turn bean elements into lower case
133         introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
134 
135         return introspector;
136     }
137 
138     /**
139      * Tests the value of the Project object that has just been parsed
140      */
141     protected void testScarabSettings(ScarabSettings ss)
142         throws Exception
143     {
144         List globalAttributes = ss.getGlobalAttributes();
145         GlobalAttribute ga = (GlobalAttribute) globalAttributes.get(1);
146         assertEquals("Functional area", ga.getName());
147 
148         List globalAttributeOptions = ga.getGlobalAttributeOptions();
149         
150         System.out.println( "GlobalAttribute: " + ga);
151         System.out.println( "globalAttributeOptions: " + globalAttributeOptions);
152 
153         assertEquals(ga.getCreatedDate().getTimestamp(), "2002-05-31 13:29:27.0");
154         
155         assertEquals(globalAttributeOptions.size(), 2);
156         GlobalAttributeOption gao = (GlobalAttributeOption) globalAttributeOptions.get(0);
157         assertEquals("UI", gao.getChildOption());        
158         gao = (GlobalAttributeOption) globalAttributeOptions.get(1);
159         assertEquals("Code", gao.getChildOption());        
160 
161         List globalIssueTypes = ss.getGlobalIssueTypes();
162         GlobalIssueType git = (GlobalIssueType) globalIssueTypes.get(0);
163         assertEquals("Defect", git.getName());
164 
165         List modules = ss.getModules();
166         Module m = (Module) modules.get(0);
167         assertEquals("Source", m.getName());
168     }
169 
170     /**
171      * Description of the Method
172      */
173     protected void write(Object bean, Writer out)
174         throws Exception
175     {
176         BeanWriter writer = new BeanWriter(out);
177         writer.setXMLIntrospector(createXMLIntrospector());
178         writer.setEndOfLine("\n");
179         writer.enablePrettyPrint();
180         writer.write(bean);
181     }
182 }
183