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 org.apache.creadur.whisker.model.ByOrganisation;
27 import org.apache.creadur.whisker.model.License;
28 import org.apache.creadur.whisker.model.Organisation;
29 import org.apache.creadur.whisker.model.WithinDirectory;
30
31 import junit.framework.TestCase;
32 import org.jdom2.Element;
33
34
35
36
37 public class JDomBuilderWithDirectoryTest 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 public void testBuildWithinDirectorySetsLicenses() throws Exception {
53 for(int i=0;i<101;i++) {
54 checkWithinLicenses(i);
55 }
56 }
57
58 private void checkWithinLicenses(final int numberOfOrgs) {
59 final Map<String, License> licenses = new HashMap<String, License>();
60 final Map<String, Organisation> organisations = new HashMap<String, Organisation> ();
61 final Element element = withLicense(numberOfOrgs, licenses);
62 final WithinDirectory result = subject.withinDirectory(
63 element.setAttribute("dir", "a name"), licenses, organisations);
64 assertNotNull("Builder should build", result);
65 assertNotNull("Builder should build licenses", result.getLicenses());
66 assertEquals("Builder should set licenses", numberOfOrgs, result.getLicenses().size());
67 }
68
69 @SuppressWarnings("unchecked")
70 private Element withLicense(int numberOfLicenses,
71 final Map<String, License> licenses) {
72 final Element element = new Element("within");
73 for (int i=0;i<numberOfLicenses;i++) {
74 final String idValue = "id" + i;
75 element.addContent(new Element("with-license").setAttribute("id", idValue));
76 new License(false,"", Collections.EMPTY_LIST, idValue, "url" + i, "name" + i).storeIn(licenses);
77 }
78 return element;
79 }
80
81 public void testBuildWithinDirectorySetsPublicDomain() throws Exception {
82 for (int i=0;i<101;i++) {
83 checkWithinPublicDomain(i);
84 }
85 }
86
87 private void checkWithinPublicDomain(final int numberOfOrgs) {
88 final Map<String, License> licenses = new HashMap<String, License>();
89 final Map<String, Organisation> organisations = new HashMap<String, Organisation> ();
90 final Element element = withPublicDomain(numberOfOrgs, organisations);
91 final WithinDirectory result = subject.withinDirectory(
92 element.setAttribute("dir", "a name"), licenses, organisations);
93 assertNotNull("Builder should build", result);
94 assertNotNull("Builder should build public domain", result.getPublicDomain());
95 assertEquals("Builder should set public domain", numberOfOrgs, result.getPublicDomain().size());
96 }
97
98 public void testBuildWithinDirectorySetsDirectoryName() throws Exception {
99 final Map<String, License> licenses = new HashMap<String, License>();
100 final Map<String, Organisation> organisations = new HashMap<String, Organisation> ();
101 final String expected = "a name";
102 final WithinDirectory result = subject.withinDirectory(
103 new Element("within").setAttribute("dir", expected), licenses, organisations);
104 assertNotNull("Builder should build", result);
105 assertEquals("Builder should set name from dir", expected, result.getName());
106 }
107
108 public void testCollectPublicDomainOrgs() throws Exception {
109 for (int i=0;i<256;i++) {
110 checkPublicDomainOrg(i);
111 }
112 }
113
114 private void checkPublicDomainOrg(int numberOfOrgs) {
115 final Map<String, Organisation> organisations = new HashMap<String, Organisation> ();
116 final Element element = withPublicDomain(numberOfOrgs, organisations);
117 final Collection<ByOrganisation> results = subject.publicDomain(
118 organisations,
119 element);
120 assertNotNull("Expected builder to build", results);
121 assertEquals("Expected one organisation per child of public domain",numberOfOrgs, results.size());
122 }
123
124 private Element withPublicDomain(int numberOfOrgs,
125 final Map<String, Organisation> organisations) {
126 final Element publicDomain = new Element("public-domain");
127 for (int i=0;i<numberOfOrgs;i++) {
128 final String idValue = "id" + i;
129 publicDomain.addContent(new Element("by-organisation").setAttribute("id", idValue));
130 new Organisation(idValue, "name" + i, "url" + i).storeIn(organisations);
131 }
132 final Element element = new Element("within").addContent(publicDomain);
133 return element;
134 }
135 }