1 package org.apache.commons.betwixt.scarab;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38
39
40
41 public class TestScarabSettings extends AbstractTestCase
42 {
43 public static void main( String[] args )
44 {
45 TestRunner.run( suite() );
46 }
47
48
49
50
51 public static Test suite()
52 {
53 return new TestSuite(TestScarabSettings.class);
54 }
55
56
57
58
59
60
61 public TestScarabSettings(String testName)
62 {
63 super(testName);
64 }
65
66
67
68
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
79 StringWriter buffer = new StringWriter();
80 write(ss, buffer);
81
82
83 reader = createBeanReader();
84
85
86 String text = buffer.toString();
87
88 System.out.println(text);
89
90
91
92
93
94
95
96 testScarabSettings(ss);
97
98
99 }
100
101
102
103
104
105
106
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
119
120
121
122 protected XMLIntrospector createXMLIntrospector()
123 {
124 XMLIntrospector introspector = new XMLIntrospector();
125
126
127 introspector.getConfiguration().setAttributesForPrimitives(false);
128
129
130
131
132
133 introspector.getConfiguration().setElementNameMapper(new HyphenatedNameMapper());
134
135 return introspector;
136 }
137
138
139
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
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