1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.creadur.whisker.fromxml;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import junit.framework.TestCase;
27
28 import org.apache.creadur.whisker.model.License;
29 import org.apache.creadur.whisker.model.Organisation;
30 import org.apache.creadur.whisker.model.WithLicense;
31 import org.jdom2.CDATA;
32 import org.jdom2.Element;
33
34
35
36
37 public class JDomBuilderWithLicenseTest extends TestCase {
38
39 private JDomBuilder subject;
40
41 @Override
42 protected void setUp() throws Exception {
43 super.setUp();
44 subject = new JDomBuilder();
45 }
46
47 @Override
48 protected void tearDown() throws Exception {
49 super.tearDown();
50 }
51
52
53 public void testLicenseFromListThrowsMissingIDWhenEmpty() throws Exception {
54 final Map<String, License> licenses = new HashMap<String, License>();
55 try {
56 subject.license(new Element("with-license").setAttribute("id", "id"), licenses);
57 fail("Throw an exception when the ID is missing");
58 } catch (MissingIDException e) {
59
60 }
61 }
62
63 public void testLicenseFromListThrowsMissingIDWhenIDsAreMismatched() throws Exception {
64 final Map<String, License> licenses = new HashMap<String, License>();
65 addLicenseTo(licenses, "noise");
66 try {
67 subject.license(new Element("with-license").setAttribute("id", "id"), licenses);
68 fail("Throw an exception when the ID is missing");
69 } catch (MissingIDException e) {
70
71 }
72 }
73
74 public void testLicenseFromListFindsLicense() throws Exception {
75 final String id = "id";
76
77 final Map<String, License> licenses = new HashMap<String, License>();
78 @SuppressWarnings("unchecked")
79 final License expected = new License(false, "", Collections.EMPTY_LIST, id, "url", "name");
80 expected.storeIn(licenses);
81 addLicenseTo(licenses, "noise");
82
83 final License output = subject.license(new Element("with-license").setAttribute("id", id), licenses);
84 assertNotNull("Expected builder to build", output);
85 assertEquals("Expected license to be found", expected, output);
86 }
87
88
89
90
91
92
93 @SuppressWarnings("unchecked")
94 private License addLicenseTo(final Map<String, License> licenses,
95 final String id) {
96 return new License(false, "", Collections.EMPTY_LIST, id, "noise url", "name").storeIn(licenses);
97 }
98
99 public void testBuildLicenseFromElementWithCopyrightNotice() throws Exception {
100 checkSetCopyrightNotice("Some Copyright Text", "Some Copyright Text");
101 }
102
103 public void testBuildLicenseFromElementWithCopyrightNoticeTrimSpaces() throws Exception {
104 checkSetCopyrightNotice(" Some Copyright Text ", "Some Copyright Text");
105 }
106
107
108
109
110
111 private void checkSetCopyrightNotice(final String copyrightNotice,
112 final String expectCopyrightNotice) {
113 final String id = "an ID";
114 final Map<String, License> licenses = new HashMap<String, License>();
115 final Map<String, Organisation> organisations = new HashMap<String, Organisation>();
116 addLicenseTo(licenses, id);
117 final WithLicense output = subject.withLicense(
118 new Element("with-license")
119 .setAttribute("id", id)
120 .addContent(new Element("copyright-notice").setContent(new CDATA(copyrightNotice)))
121 , licenses, organisations);
122 assertNotNull("Expected builder to build", output);
123 assertEquals("Builder should set copyright notice from xml", expectCopyrightNotice, output.getCopyrightNotice());
124 }
125
126 public void testBuildLicenseFromElementNoCopyrightNoticeNoParameters() throws Exception {
127 checkWithElementJustId("some id");
128 checkWithElementJustId("id");
129 checkWithElementJustId(" some id ");
130 checkWithElementJustId("");
131 }
132
133
134
135
136 private void checkWithElementJustId(final String id) {
137 final Map<String, License> licenses = new HashMap<String, License>();
138 final Map<String, Organisation> organisations = new HashMap<String, Organisation>();
139 final License expected = addLicenseTo(licenses, id);
140 final WithLicense output = subject.withLicense(new Element("with-license").setAttribute("id", id), licenses, organisations);
141 assertNotNull("Expected builder to build", output);
142 assertEquals("Expected builder to find license and set it", expected, output.getLicense());
143 }
144
145
146 public void testBuildLicenseWithParametersIsEmptyWhenNoParameters() throws Exception {
147 final Map<String, String> results = subject.parameters(
148 new Element("with-license")
149 .addContent(new Element("license-parameters")));
150 assertNotNull("Expected builder to build parameters", results);
151 assertTrue("When there are no parameters, map should be empty", results.isEmpty());
152 }
153
154
155 public void testBuildLicenseWithOneParameter() throws Exception {
156 checkBuildLicenseWithParameters(1);
157 }
158
159
160 public void testBuildLicenseWithTwoParameters() throws Exception {
161 checkBuildLicenseWithParameters(2);
162 }
163
164 public void testBuildLicenseWith3Parameters() throws Exception {
165 checkBuildLicenseWithParameters(3);
166 }
167
168 public void testBuildLicenseWith4Parameters() throws Exception {
169 checkBuildLicenseWithParameters(4);
170 }
171
172 public void testBuildLicenseWith7Parameters() throws Exception {
173 checkBuildLicenseWithParameters(7);
174 }
175
176 public void testBuildLicenseWith11Parameters() throws Exception {
177 checkBuildLicenseWithParameters(11);
178 }
179
180 public void testBuildLicenseWith101Parameters() throws Exception {
181 checkBuildLicenseWithParameters(101);
182 }
183
184
185
186
187 private void checkBuildLicenseWithParameters(final int numberOfParameters) {
188 final Element licenseParametersElement = new Element("license-parameters");
189 for (int i=0;i<numberOfParameters; i++) {
190 licenseParametersElement
191 .addContent(new Element("parameter")
192 .addContent(new Element("name").addContent(new CDATA(name(i))))
193 .addContent(new Element("value").addContent(new CDATA(value(i)))));
194 }
195 final Element input = new Element("with-license")
196 .addContent(licenseParametersElement);
197 final Map<String, String> results =
198 subject.parameters(input);
199 assertNotNull("Expected builder to build parameters", results);
200 assertEquals("Expected builder to add one name, value pair per parameter", numberOfParameters, results.size());
201 for (int i=0;i<numberOfParameters;i++) {
202 assertEquals("Value indexed by name", results.get(name(i)), value(i));
203 }
204 }
205
206 public void testWithLicenseBuildWithParameters() throws Exception {
207 for (int i=0;i<128;i++) {
208 checkBuildWithLicenseWithParameters(i);
209 }
210 }
211
212
213
214
215
216 private void checkBuildWithLicenseWithParameters(final int numberOfParameters) {
217 final String id = "some id";
218 final Map<String, License> licenses = new HashMap<String, License>();
219 final Map<String, Organisation> organisations = new HashMap<String, Organisation>();
220 addLicenseTo(licenses, id);
221 final Element licenseParametersElement = new Element("license-parameters");
222 for (int i=0;i<numberOfParameters; i++) {
223 licenseParametersElement
224 .addContent(new Element("parameter")
225 .addContent(new Element("name").addContent(new CDATA(name(i))))
226 .addContent(new Element("value").addContent(new CDATA(value(i)))));
227 }
228 final Element input = new Element("with-license").setAttribute("id", id)
229 .addContent(licenseParametersElement);
230 final Map<String, String> results =
231 subject.withLicense(input, licenses, organisations).getParameters();
232 assertNotNull("Expected builder to build parameters", results);
233 assertEquals("Expected builder to add one name, value pair per parameter", numberOfParameters, results.size());
234 for (int i=0;i<numberOfParameters;i++) {
235 assertEquals("Value indexed by name", results.get(name(i)), value(i));
236 }
237 }
238
239
240
241
242
243
244 private String name(int i) {
245 return "name" + i;
246 }
247
248
249
250
251
252 private String value(int i) {
253 return "value" + i;
254 }
255
256 public void testBuildLicenseWithParametersThrowsExceptionWhenParameterIsDuplicated() throws Exception {
257 try {
258 subject.parameters(
259 new Element("with-license")
260 .addContent(new Element("license-parameters")
261 .addContent(new Element("parameter")
262 .addContent(new Element("name").addContent(new CDATA("A parameter name")))
263 .addContent(new Element("value").addContent(new CDATA("A parameter value"))))
264 .addContent(new Element("parameter")
265 .addContent(new Element("name").addContent(new CDATA("A parameter name")))
266 .addContent(new Element("value").addContent(new CDATA("A parameter value"))))));
267 fail("When element contains duplicate definitions of the same parameter, the build should throw an exception");
268 } catch (DuplicateElementException e) {
269
270 }
271 }
272
273 public void testBuildCollectWithLicenses() throws Exception {
274 for (int i=0; i<256; i++) {
275 checkCollectWithLicenses(i);
276 }
277 }
278
279
280
281
282 private void checkCollectWithLicenses(final int numberOfWithLicenses) {
283 final String id = "an ID";
284 final Map<String, License> licenses = new HashMap<String, License>();
285 final Map<String, Organisation> organisations = new HashMap<String, Organisation>();
286 final Element parent = new Element("within");
287
288 for(int i=0;i<numberOfWithLicenses;i++) {
289 addWithLicense(id + i, licenses, parent);
290 }
291
292 final Collection<WithLicense> results = subject.withLicenses(licenses, organisations, parent);
293 assertNotNull("Builder should build", results);
294 assertEquals("Builder should build one with-license for each child", numberOfWithLicenses, results.size());
295 }
296
297
298
299
300
301
302 private void addWithLicense(final String id,
303 final Map<String, License> licenses, final Element parent) {
304 addLicenseTo(licenses, id);
305 parent.addContent(new Element("with-license").setAttribute("id", id));
306 }
307 }