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.ArrayList;
22 import java.util.Calendar;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Map;
28
29 import junit.framework.TestCase;
30
31 import org.apache.creadur.whisker.model.License;
32 import org.apache.creadur.whisker.model.Organisation;
33 import org.apache.creadur.whisker.model.WithinDirectory;
34 import org.jdom2.CDATA;
35 import org.jdom2.Document;
36 import org.jdom2.Element;
37
38
39
40
41 public class JDomBuilderWorkTest extends TestCase {
42
43 private JDomBuilder subject;
44
45 @Override
46 protected void setUp() throws Exception {
47 super.setUp();
48 subject = new JDomBuilder();
49 }
50
51 @Override
52 protected void tearDown() throws Exception {
53 super.tearDown();
54 }
55
56
57 public void testMapNoticesIsEmptyWhenDocumentHasNoNotices() throws Exception {
58 final Map<String, String> results = subject.mapNotices(new Document().setRootElement(new Element("manifest")));
59 assertNotNull("Builder should build something", results);
60 assertTrue("Should be empty when no licenses present", results.isEmpty());
61 }
62
63 public void testMapNoticesExpectedToBeImmutable() throws Exception {
64 final Map<String, String> results =
65 subject.mapNotices(new Document().setRootElement(new Element("manifest")));
66 assertNotNull("Expected builder to build something", results);
67 try {
68 results.put("whatever", "next");
69 fail("Expected map to be immutable");
70 } catch (UnsupportedOperationException e) {
71
72 }
73 }
74
75
76 public void testMapNoticesFindsNoticesDefinedInDocument() throws Exception {
77 for (int i=0; i<256; i++) {
78 checkMapNoticesWith(i);
79 }
80 }
81
82
83
84
85 private void checkMapNoticesWith(final int numberOfLicenses) {
86 final String baseId = "ID";
87 final String baseText = "Rhubarb Rhubarb";
88 final Document in = new Document();
89 final Element licenses = new Element("notices");
90 in.setRootElement(new Element("manifest").addContent(licenses));
91 for (int i=0;i<numberOfLicenses;i++) {
92 licenses.addContent(
93 new Element("notice").setAttribute("id", combine(baseId, i)).addContent(new CDATA(combine(baseText, i))));
94 }
95 final Map<String, String> results = subject.mapNotices(in);
96 assertEquals("One license in map for each in the document", numberOfLicenses, results.size());
97 for (int i=0;i<numberOfLicenses;i++) {
98 final String next = results.get(combine(baseId, i));
99 assertNotNull("Expected organisation to be stored", next);
100 assertEquals("Expected correct organisation to be stored", next, combine(baseText, i));
101 }
102 }
103
104
105 public void testMapLicensesIsEmptyWhenDocumentHasNoLicenses() throws Exception {
106 final Map<String, License> results = subject.mapLicenses(new Document().setRootElement(new Element("manifest")));
107 assertNotNull("Builder should build something", results);
108 assertTrue("Should be empty when no licenses present", results.isEmpty());
109 }
110
111 public void testMapLicensesExpectedToBeImmutable() throws Exception {
112 final Map<String, License> results =
113 subject.mapLicenses(new Document().setRootElement(new Element("manifest")));
114 assertNotNull("Expected builder to build something", results);
115 try {
116 results.put("whatever", new License(true, "", new ArrayList<String>(), "", "", ""));
117 fail("Expected map to be immutable");
118 } catch (UnsupportedOperationException e) {
119
120 }
121 }
122
123 public void testMapLicensesFindsLicensesDefinedInDocument() throws Exception {
124 for (int i=0; i<256; i++) {
125 checkMapLicensesWith(i);
126 }
127 }
128
129
130
131
132 private void checkMapLicensesWith(final int numberOfLicenses) {
133 final String baseId = "ID";
134 final String baseName = "Name";
135 final Document in = new Document();
136 final Element licenses = new Element("licenses");
137 in.setRootElement(new Element("manifest").addContent(licenses));
138 for (int i=0;i<numberOfLicenses;i++) {
139 licenses.addContent(
140 new Element("license").setAttribute("id", combine(baseId, i)).setAttribute("name", combine(baseName, i)));
141 }
142 final Map<String, License> results = subject.mapLicenses(in);
143 assertEquals("One license in map for each in the document", numberOfLicenses, results.size());
144 for (int i=0;i<numberOfLicenses;i++) {
145 final License next = results.get(combine(baseId, i));
146 assertNotNull("Expected organisation to be stored", next);
147 assertEquals("Expected correct organisation to be stored", next.getName(), combine(baseName, i));
148 }
149 }
150
151
152 public void testMapOrganisationsIsEmptyWhenDocumentHasNoOrganisations() throws Exception {
153 final Map<String, Organisation> results =
154 subject.mapOrganisations(new Document().setRootElement(new Element("manifest")));
155 assertNotNull("Expected builder to build something", results);
156 assertTrue("Expected builder to build empty map when document has no documents", results.isEmpty());
157 }
158
159 public void testMapOrganisationsExpectedToBeImmutable() throws Exception {
160 final Map<String, Organisation> results =
161 subject.mapOrganisations(new Document().setRootElement(new Element("manifest")));
162 assertNotNull("Expected builder to build something", results);
163 try {
164 results.put("whatever", new Organisation("", "", ""));
165 fail("Expected map to be immutable");
166 } catch (UnsupportedOperationException e) {
167
168 }
169 }
170
171 public void testMapOrganisationsFindOrganisationDefinedInDocument() throws Exception {
172 for (int i=1;i<256;i++) {
173 checkMapOrganisationsWith(i);
174 };
175 }
176
177
178
179
180 private void checkMapOrganisationsWith(final int numberOfOrganisations) {
181 final String baseId = "ID";
182 final String baseName = "Name";
183 final Document in = new Document();
184 final Element organisations = new Element("organisations");
185 in.setRootElement(new Element("manifest").addContent(organisations));
186 for (int i=0;i<numberOfOrganisations;i++) {
187 organisations.addContent(
188 new Element("organisation").setAttribute("id", combine(baseId, i)).setAttribute("name", combine(baseName, i)));
189 }
190 final Map<String, Organisation> results = subject.mapOrganisations(in);
191 assertEquals("One organisation in map for each in the document", numberOfOrganisations, results.size());
192 for (int i=0;i<numberOfOrganisations;i++) {
193 final Organisation next = results.get(combine(baseId, i));
194 assertNotNull("Expected organisation to be stored", next);
195 assertEquals("Expected correct organisation to be stored", next.getName(), combine(baseName, i));
196 }
197 }
198
199
200
201
202
203
204 private String combine(String base, int i) {
205 return base + i;
206 }
207
208
209
210
211
212
213 @SuppressWarnings("unchecked")
214 private License addLicenseTo(final Map<String, License> licenses,
215 final String id) {
216 return new License(false, "", Collections.EMPTY_LIST, id, "noise url", "name").storeIn(licenses);
217 }
218
219 public void testPrimaryLicense() throws Exception {
220 final String id = "The primary ID";
221 final Map<String, License> licenses = new HashMap<String, License> ();
222 @SuppressWarnings("unchecked")
223 final License expected = new License(false, "", Collections.EMPTY_LIST, id, "url", "name");
224 expected.storeIn(licenses);
225 addLicenseTo(licenses, "noise");
226 final License result = subject.primaryLicense(new Document().setRootElement(new Element("manifest").
227 addContent(new Element("primary-license").setAttribute("id", id))), licenses);
228 assertNotNull("Builder should find primary license", result);
229 assertEquals("Builder should find primary licenser", expected, result);
230 }
231
232 public void testNoPrimaryCopyright() throws Exception {
233 final String primaryCopyrightNotice = subject.primaryCopyrightNotice(new Document().setRootElement(new Element("manifest").
234 addContent(new Element("primary-license").setAttribute("id", "The primary ID"))));
235 assertNull("Builder should only set primary copyright when present", primaryCopyrightNotice);
236 }
237
238 public void testPrimaryCopyright() throws Exception {
239 final String copyrightNoticeSet = "Some Copyright Claim";
240 final String result = subject.primaryCopyrightNotice(new Document().setRootElement(new Element("manifest").
241 addContent(
242 new Element("primary-license").setAttribute("id", "The primary ID")
243 .addContent(
244 new Element("copyright-notice").addContent(copyrightNoticeSet)))));
245 assertEquals("Builder should set primary copyright notice", result, copyrightNoticeSet);
246 }
247
248 public void testBuildPrimaryCopyright() throws Exception {
249 final String copyrightNoticeSet = "Some Copyright Claim";
250 final String result = subject.build(new Document().setRootElement(
251 new Element("manifest")
252 .addContent(
253 new Element("licenses").addContent(
254 new Element("license").setAttribute("id", "The primary ID")))
255 .addContent(
256 new Element("primary-license").setAttribute("id", "The primary ID")
257 .addContent(
258 new Element("copyright-notice").addContent(copyrightNoticeSet))))
259 ).getPrimaryCopyrightNotice();
260 assertEquals("Builder should set primary copyright notice", result, copyrightNoticeSet);
261 }
262
263 public void testThrowsMissingIDExceptionWhenPrimaryLicenseMissing() throws Exception {
264 final String id = "The primary ID";
265 final Map<String, License> licenses = new HashMap<String, License> ();
266 addLicenseTo(licenses, "noise");
267 try {
268 subject.primaryLicense(new Document().setRootElement(new Element("manifest").
269 addContent(new Element("primary-license").setAttribute("id", id))), licenses);
270 fail();
271 } catch (MissingIDException e) {
272
273 }
274 }
275
276 public void testPrimaryNoticeFindsNoticeText() throws Exception {
277 final String noticeText = "Some sort of notice";
278 final String result = subject.primaryNotice(new Document().setRootElement(
279 new Element("manifest").addContent(
280 new Element("primary-notice").addContent(new CDATA(noticeText)))));
281 assertEquals("Expected builder to find the text of the primary notice element", noticeText, result);
282 }
283
284 public void testPrimaryNoticeIsNullWhenThereIsNoNoticeText() throws Exception {
285 final String result = subject.primaryNotice(new Document().setRootElement(
286 new Element("manifest")));
287 assertNull("When there is no primary notice, expect null", result);
288 }
289
290 public void testPrimaryNoticeSubstitutesYearInNoticeText() throws Exception {
291 final String noticeBaseText = "Some sort of notice";
292 final String result = subject.primaryNotice(new Document().setRootElement(
293 new Element("manifest").addContent(
294 new Element("primary-notice").addContent(new CDATA(noticeBaseText + "${year}")))));
295 assertEquals("Expected builder to find the text of the primary notice element",
296 noticeBaseText + Calendar.getInstance().get(Calendar.YEAR), result);
297 }
298
299 public void testFindPrimaryOrganisationIdReturnsNullWhenOrganisationUnset() throws Exception {
300 final String result = subject.primaryOrganisationId(new Document().setRootElement(new Element("manifest")));
301 assertNull("Primary organisation is optional, and null should be returned when unset", result);
302 }
303
304 public void testFindPrimaryOrganisationIdWhenSet() throws Exception {
305 final String idValue = "An ID value";
306 final String result = subject.primaryOrganisationId(
307 new Document().setRootElement(
308 new Element("manifest").addContent(
309 new Element("primary-organisation").setAttribute("id", idValue))));
310 assertEquals("When set, builder should find value", idValue, result);
311 }
312
313 public void testCollectContentsReturneEmptyWhenDocumentHasNoContents() throws Exception {
314 final Collection<WithinDirectory> results = subject.collectContents(
315 new Document().setRootElement(new Element("manifest")), new HashMap<String, License>(),
316 new HashMap<String, Organisation>());
317 assertNotNull("Builder should build something", results);
318 assertTrue("Collection should be empty when there are no contents", results.isEmpty());
319 }
320
321
322 public void testCollectDirectoriesDefinedInDocument() throws Exception {
323 for (int i=1;i<256;i++) {
324 checkCollectDirectoriesWith(i);
325 };
326 }
327
328
329
330
331 private void checkCollectDirectoriesWith(final int numberOfDirectories) {
332 final String baseDir = "/dir/path";
333 final Document in = new Document();
334 final Element rootElement = new Element("manifest");
335 in.setRootElement(rootElement);
336 for (int i=0;i<numberOfDirectories;i++) {
337 rootElement.addContent(
338 new Element("within").setAttribute("dir", combine(baseDir, i)));
339 }
340 final Collection<WithinDirectory> results = subject.collectContents(in, new HashMap<String, License>(),
341 new HashMap<String, Organisation>());
342 assertEquals("One organisation in map for each in the document", numberOfDirectories, results.size());
343 final Collection<String> dirNames = new HashSet<String>();
344 for (final WithinDirectory within:results) {
345 dirNames.add(within.getName());
346 }
347 for (int i=0;i<numberOfDirectories;i++) {
348 assertTrue("", dirNames.contains(combine(baseDir,i)));
349 }
350 }
351
352 public void testCollectDirectoriesThrowsDuplicateElementExceptionWhenDirAttributeDuplicated() throws Exception {
353 final String dir = "duplicate/path";
354 try {
355 subject.collectContents(
356 new Document().setRootElement(
357 new Element("manifest").addContent(
358 new Element("within").setAttribute("dir", dir)).addContent(
359 new Element("within").setAttribute("dir", dir))),
360 new HashMap<String, License>(),
361 new HashMap<String, Organisation>());
362 fail("Collect should throw a DuplicateElementException when dir names are not unique");
363 } catch (DuplicateElementException e) {
364
365 }
366 }
367 }