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