1 package org.apache.commons.betwixt.scarab;
2
3 /*
4 * ====================================================================
5 *
6 * The Apache Software License, Version 1.1
7 *
8 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
9 * reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
21 * distribution.
22 *
23 * 3. The end-user documentation included with the redistribution, if
24 * any, must include the following acknowlegement:
25 * "This product includes software developed by the
26 * Apache Software Foundation (http://www.apache.org/)."
27 * Alternately, this acknowlegement may appear in the software itself,
28 * if and wherever such third-party acknowlegements normally appear.
29 *
30 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
31 * Foundation" must not be used to endorse or promote products derived
32 * from this software without prior written permission. For written
33 * permission, please contact apache@apache.org.
34 *
35 * 5. Products derived from this software may not be called "Apache"
36 * nor may "Apache" appear in their names without prior written
37 * permission of the Apache Group.
38 *
39 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This software consists of voluntary contributions made by many
54 * individuals on behalf of the Apache Software Foundation. For more
55 * information on the Apache Software Foundation, please see
56 * <http://www.apache.org/>.
57 *
58 */
59
60 import java.io.FileInputStream;
61 import java.io.StringWriter;
62 import java.io.Writer;
63 import java.util.List;
64
65 import junit.framework.Test;
66 import junit.framework.TestSuite;
67 import junit.textui.TestRunner;
68
69 import org.apache.commons.betwixt.AbstractTestCase;
70 import org.apache.commons.betwixt.XMLIntrospector;
71 import org.apache.commons.betwixt.io.BeanReader;
72 import org.apache.commons.betwixt.io.BeanWriter;
73 import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
74
75 /***
76 * Test harness which round trips a Scarab's settings xml file
77 *
78 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
79 * @version $Id: TestScarabSettings.java,v 1.7 2002/06/05 07:57:07 jstrachan Exp $
80 */
81 public class TestScarabSettings extends AbstractTestCase
82 {
83 public static void main( String[] args )
84 {
85 TestRunner.run( suite() );
86 }
87
88 /***
89 * A unit test suite for JUnit
90 */
91 public static Test suite()
92 {
93 return new TestSuite(TestScarabSettings.class);
94 }
95
96 /***
97 * Constructor for the TestScarabSettings object
98 *
99 * @param testName
100 */
101 public TestScarabSettings(String testName)
102 {
103 super(testName);
104 }
105
106 /***
107 * Tests we can round trip from the XML -> bean -> XML -> bean. Ideally this
108 * method should test both Project objects are identical
109 */
110 public void testRoundTrip()
111 throws Exception
112 {
113 BeanReader reader = createBeanReader();
114
115 ScarabSettings ss = (ScarabSettings) reader.parse(
116 new FileInputStream(getTestFile("src/test/org/apache/commons/betwixt/scarab/scarab-settings.xml")));
117
118 // now lets output it to a buffer
119 StringWriter buffer = new StringWriter();
120 write(ss, buffer);
121
122 // create a new BeanReader
123 reader = createBeanReader();
124
125 // now lets try parse the output sing the BeanReader
126 String text = buffer.toString();
127
128 System.out.println(text);
129
130 /*
131 ScarabSettings newScarabSettings = (ScarabSettings) reader.parse(new StringReader(text));
132
133 // managed to parse it again!
134 testScarabSettings(newScarabSettings);
135 */
136 testScarabSettings(ss);
137
138 // #### should now test the old and new Project instances for equality.
139 }
140
141
142 // Implementation methods
143 //-------------------------------------------------------------------------
144
145 /***
146 * Description of the Method
147 */
148 protected BeanReader createBeanReader()
149 throws Exception
150 {
151 BeanReader reader = new BeanReader();
152 reader.setXMLIntrospector(createXMLIntrospector());
153 reader.registerBeanClass(ScarabSettings.class);
154 return reader;
155 }
156
157 /***
158 * ### it would be really nice to move this somewhere shareable across Maven
159 * / Turbine projects. Maybe a static helper method - question is what to
160 * call it???
161 */
162 protected XMLIntrospector createXMLIntrospector()
163 {
164 XMLIntrospector introspector = new XMLIntrospector();
165
166 // set elements for attributes to true
167 introspector.setAttributesForPrimitives(false);
168
169 // wrap collections in an XML element
170 //introspector.setWrapCollectionsInElement(true);
171
172 // turn bean elements into lower case
173 introspector.setElementNameMapper(new HyphenatedNameMapper());
174
175 return introspector;
176 }
177
178 /***
179 * Tests the value of the Project object that has just been parsed
180 */
181 protected void testScarabSettings(ScarabSettings ss)
182 throws Exception
183 {
184 List globalAttributes = ss.getGlobalAttributes();
185 GlobalAttribute ga = (GlobalAttribute) globalAttributes.get(1);
186 assertEquals("Functional area", ga.getName());
187
188 List globalAttributeOptions = ga.getGlobalAttributeOptions();
189
190 System.out.println( "GlobalAttribute: " + ga);
191 System.out.println( "globalAttributeOptions: " + globalAttributeOptions);
192
193 assertEquals(ga.getCreatedDate().getTimestamp(), "2002-05-31 13:29:27.0");
194
195 assertEquals(globalAttributeOptions.size(), 2);
196 GlobalAttributeOption gao = (GlobalAttributeOption) globalAttributeOptions.get(0);
197 assertEquals("UI", gao.getChildOption());
198 gao = (GlobalAttributeOption) globalAttributeOptions.get(1);
199 assertEquals("Code", gao.getChildOption());
200
201 List globalIssueTypes = ss.getGlobalIssueTypes();
202 GlobalIssueType git = (GlobalIssueType) globalIssueTypes.get(0);
203 assertEquals("Defect", git.getName());
204
205 List modules = ss.getModules();
206 Module m = (Module) modules.get(0);
207 assertEquals("Source", m.getName());
208 }
209
210 /***
211 * Description of the Method
212 */
213 protected void write(Object bean, Writer out)
214 throws Exception
215 {
216 BeanWriter writer = new BeanWriter(out);
217 writer.setXMLIntrospector(createXMLIntrospector());
218 writer.enablePrettyPrint();
219 writer.write(bean);
220 }
221 }
222
This page was automatically generated by Maven