001: package org.apache.commons.betwixt.scarab;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.FileInputStream;
021: import java.io.StringWriter;
022: import java.io.Writer;
023: import java.util.List;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027: import junit.textui.TestRunner;
028:
029: import org.apache.commons.betwixt.AbstractTestCase;
030: import org.apache.commons.betwixt.XMLIntrospector;
031: import org.apache.commons.betwixt.io.BeanReader;
032: import org.apache.commons.betwixt.io.BeanWriter;
033: import org.apache.commons.betwixt.strategy.HyphenatedNameMapper;
034:
035: /**
036: * Test harness which round trips a Scarab's settings xml file
037: *
038: * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
039: * @version $Id: TestScarabSettings.java 438373 2006-08-30 05:17:21Z bayard $
040: */
041: public class TestScarabSettings extends AbstractTestCase {
042: public static void main(String[] args) {
043: TestRunner.run(suite());
044: }
045:
046: /**
047: * A unit test suite for JUnit
048: */
049: public static Test suite() {
050: return new TestSuite(TestScarabSettings.class);
051: }
052:
053: /**
054: * Constructor for the TestScarabSettings object
055: *
056: * @param testName
057: */
058: public TestScarabSettings(String testName) {
059: super (testName);
060: }
061:
062: /**
063: * Tests we can round trip from the XML -> bean -> XML -> bean. Ideally this
064: * method should test both Project objects are identical
065: */
066: public void testRoundTrip() throws Exception {
067: BeanReader reader = createBeanReader();
068:
069: ScarabSettings ss = (ScarabSettings) reader
070: .parse(new FileInputStream(
071: getTestFile("src/test/org/apache/commons/betwixt/scarab/scarab-settings.xml")));
072:
073: // now lets output it to a buffer
074: StringWriter buffer = new StringWriter();
075: write(ss, buffer);
076:
077: // create a new BeanReader
078: reader = createBeanReader();
079:
080: // now lets try parse the output sing the BeanReader
081: String text = buffer.toString();
082:
083: System.out.println(text);
084:
085: /*
086: ScarabSettings newScarabSettings = (ScarabSettings) reader.parse(new StringReader(text));
087:
088: // managed to parse it again!
089: testScarabSettings(newScarabSettings);
090: */
091: testScarabSettings(ss);
092:
093: // #### should now test the old and new Project instances for equality.
094: }
095:
096: // Implementation methods
097: //-------------------------------------------------------------------------
098:
099: /**
100: * Description of the Method
101: */
102: protected BeanReader createBeanReader() throws Exception {
103: BeanReader reader = new BeanReader();
104: reader.setXMLIntrospector(createXMLIntrospector());
105: reader.registerBeanClass(ScarabSettings.class);
106: return reader;
107: }
108:
109: /**
110: * ### it would be really nice to move this somewhere shareable across Maven
111: * / Turbine projects. Maybe a static helper method - question is what to
112: * call it???
113: */
114: protected XMLIntrospector createXMLIntrospector() {
115: XMLIntrospector introspector = new XMLIntrospector();
116:
117: // set elements for attributes to true
118: introspector.getConfiguration().setAttributesForPrimitives(
119: false);
120:
121: // wrap collections in an XML element
122: //introspector.setWrapCollectionsInElement(true);
123:
124: // turn bean elements into lower case
125: introspector.getConfiguration().setElementNameMapper(
126: new HyphenatedNameMapper());
127:
128: return introspector;
129: }
130:
131: /**
132: * Tests the value of the Project object that has just been parsed
133: */
134: protected void testScarabSettings(ScarabSettings ss)
135: throws Exception {
136: List globalAttributes = ss.getGlobalAttributes();
137: GlobalAttribute ga = (GlobalAttribute) globalAttributes.get(1);
138: assertEquals("Functional area", ga.getName());
139:
140: List globalAttributeOptions = ga.getGlobalAttributeOptions();
141:
142: System.out.println("GlobalAttribute: " + ga);
143: System.out.println("globalAttributeOptions: "
144: + globalAttributeOptions);
145:
146: assertEquals(ga.getCreatedDate().getTimestamp(),
147: "2002-05-31 13:29:27.0");
148:
149: assertEquals(globalAttributeOptions.size(), 2);
150: GlobalAttributeOption gao = (GlobalAttributeOption) globalAttributeOptions
151: .get(0);
152: assertEquals("UI", gao.getChildOption());
153: gao = (GlobalAttributeOption) globalAttributeOptions.get(1);
154: assertEquals("Code", gao.getChildOption());
155:
156: List globalIssueTypes = ss.getGlobalIssueTypes();
157: GlobalIssueType git = (GlobalIssueType) globalIssueTypes.get(0);
158: assertEquals("Defect", git.getName());
159:
160: List modules = ss.getModules();
161: Module m = (Module) modules.get(0);
162: assertEquals("Source", m.getName());
163: }
164:
165: /**
166: * Description of the Method
167: */
168: protected void write(Object bean, Writer out) throws Exception {
169: BeanWriter writer = new BeanWriter(out);
170: writer.setXMLIntrospector(createXMLIntrospector());
171: writer.setEndOfLine("\n");
172: writer.enablePrettyPrint();
173: writer.write(bean);
174: }
175: }
|