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
25 import junit.framework.TestCase;
26
27 import org.apache.creadur.whisker.model.ByOrganisation;
28 import org.apache.creadur.whisker.model.Organisation;
29 import org.apache.creadur.whisker.model.Resource;
30 import org.jdom2.Element;
31
32
33
34
35 public class JDomBuilderByOrganisationTest extends TestCase {
36 private JDomBuilder subject;
37
38 @Override
39 protected void setUp() throws Exception {
40 super.setUp();
41 subject = new JDomBuilder();
42 }
43
44 @Override
45 protected void tearDown() throws Exception {
46 super.tearDown();
47 }
48
49 public void testBuildResourcesFromNoResources() throws Exception {
50 final Collection<Resource> results = subject.collectResources(new Element("by-organisation"));
51 assertNotNull("Expected empty collection to be returned", results);
52 assertEquals("Expected collection to be empty", 0, results.size());
53 }
54
55
56 public void testCollectResourcesNumbered1() throws Exception {
57 checkCollectResourcesNumbered(1);
58 }
59
60 public void testCollectResourcesNumbered2() throws Exception {
61 checkCollectResourcesNumbered(2);
62 }
63
64 public void testCollectResourcesNumbered3() throws Exception {
65 checkCollectResourcesNumbered(3);
66 }
67
68 public void testCollectResourcesNumbered4() throws Exception {
69 checkCollectResourcesNumbered(4);
70 }
71
72 public void testOrganisationByIdThrowsIllegalArgumentWhenOrganisationsEmpty() throws Exception {
73 try {
74 subject.organisation(
75 new Element("by-organisation").setAttribute("id", "some id"),
76 Collections.unmodifiableMap(new HashMap<String, Organisation>()));
77 fail("Expected MissingIDException to be thrown when organisation isn't found in map");
78 } catch (MissingIDException e) {
79
80 } catch (Throwable t) {
81 fail("Expected MissingIDException to be thrown when organisation isn't found in map but instead "
82 + t + " was thrown");
83 }
84 }
85
86 public void testOrganisationByIdThrowsIllegalArgumentWhenOrganisationsMissing() throws Exception {
87 try {
88 final HashMap<String, Organisation> map = new HashMap<String, Organisation>();
89 new Organisation("BOGUS", "name", "url").storeIn(map);
90 subject.organisation(
91 new Element("by-organisation").setAttribute("id", "some id"),
92 Collections.unmodifiableMap(map));
93 fail("Expected MissingIDException to be thrown when organisation isn't found in map");
94 } catch (MissingIDException e) {
95
96 } catch (Throwable t) {
97 fail("Expected MissingIDException to be thrown when organisation isn't found in map but instead "
98 + t + " was thrown");
99 }
100 }
101
102 public void testOrganisationByIdFindsOrganisationsPresent() throws Exception {
103 final String idValue = "some id";
104 final HashMap<String, Organisation> map = new HashMap<String, Organisation>();
105 final Organisation expected = new Organisation(idValue, "name", "url");
106 expected.storeIn(map);
107 assertEquals("Expected organisation with matching ID to be found", expected,
108 subject.organisation(
109 new Element("by-organisation").setAttribute("id", idValue),
110 Collections.unmodifiableMap(map)));
111 }
112
113 public void testByOrganisationBuildsFromOrganisationAndElementWith3ChildResources() throws Exception {
114 checkOrganisationAndElementBuild(3);
115 }
116
117
118 public void testByOrganisationBuildsFromOrganisationAndElementWith5ChildResources() throws Exception {
119 checkOrganisationAndElementBuild(5);
120 }
121
122 public void testByOrganisationBuildsFromOrganisationAndElementWith10ChildResources() throws Exception {
123 checkOrganisationAndElementBuild(10);
124 }
125
126
127
128
129
130 private void checkOrganisationAndElementBuild(
131 final int numberOfChildResources) {
132 final Element byOrganisation = withChildResources(numberOfChildResources);
133 final String idValue = "some id";
134 final Organisation input = new Organisation(idValue, "name", "url");
135 final ByOrganisation result = subject.byOrganisation(byOrganisation, input);
136 assertNotNull("Expected builder to build", result);
137 assertNotNull("Expected resources to be built", result.getResources());
138 assertNotNull("Expected organisation to be set", result.getOrganisation());
139 assertEquals("Expected number of resources built matches children", numberOfChildResources, result.getResources().size());
140 assertEquals("Expected organisation to be set to input", input, result.getOrganisation());
141 }
142
143 public void testByOrganisationBuildsFromMapAndElementWith1ChildResources() throws Exception {
144 checkElementAndMapBuild(1);
145 }
146
147
148 public void testByOrganisationBuildsFromMapAndElementWith5ChildResources() throws Exception {
149 checkElementAndMapBuild(5);
150 }
151
152 public void testByOrganisationBuildsFromMapAndElementWith10ChildResources() throws Exception {
153 checkElementAndMapBuild(10);
154 }
155
156 public void testByOrganisationCollectiveOneChild()throws Exception {
157 checkCollectByOrganisations(1);
158 }
159
160 public void testByOrganisationCollective2Children()throws Exception {
161 checkCollectByOrganisations(2);
162 }
163
164 public void testByOrganisationCollective5Child()throws Exception {
165 checkCollectByOrganisations(5);
166 }
167
168 public void testByOrganisationCollective3Child()throws Exception {
169 checkCollectByOrganisations(3);
170 }
171
172 public void testByOrganisationCollective7Child()throws Exception {
173 checkCollectByOrganisations(7);
174 }
175
176
177
178
179
180 private void checkCollectByOrganisations(final int numberOfChildren) {
181 final Element parent = new Element("with-license");
182 final String idValue = "some id";
183 final HashMap<String, Organisation> map = new HashMap<String, Organisation>();
184 for (int i=0;i<numberOfChildren;i++) {
185 final String nextIdValue = idValue + i;
186 parent.addContent(withChildResources(10, nextIdValue));
187 new Organisation(nextIdValue, "name" + i, "url").storeIn(map);
188 }
189 final Collection<ByOrganisation> results = subject.collectByOrganisations(parent, map);
190 assertNotNull("Builder should build something", results);
191 assertEquals("Expected an by-organisation in collection per child", numberOfChildren, results.size());
192 }
193
194 public void testByOrganisationEmptyCollective()throws Exception {
195 final Element parent = new Element("with-license");
196 final Organisation input = new Organisation("id", "name", "url");
197 final HashMap<String, Organisation> map = new HashMap<String, Organisation>();
198 input.storeIn(map);
199 final Collection<ByOrganisation> results = subject.collectByOrganisations(parent, map);
200 assertNotNull("Builder should build something", results);
201 assertTrue("No children so expected an empty collection", results.isEmpty());
202 }
203
204
205 public void testByOrganisationCollectiveUnmodifiable()throws Exception {
206 final Element parent = new Element("with-license");
207 final Organisation input = new Organisation("id", "name", "url");
208 final HashMap<String, Organisation> map = new HashMap<String, Organisation>();
209 input.storeIn(map);
210 final Collection<ByOrganisation> results = subject.collectByOrganisations(parent, map);
211 assertNotNull("Builder should build something", results);
212 try {
213 results.clear();
214 fail("Expected collection to be unmodifiable");
215 } catch (UnsupportedOperationException e) {
216
217 }
218 }
219
220
221
222
223
224 private void checkElementAndMapBuild(final int numberOfChildResources) {
225 final Element byOrganisation = withChildResources(numberOfChildResources);
226 final String idValue = "some id";
227 final Organisation input = new Organisation(idValue, "name", "url");
228 final HashMap<String, Organisation> map = new HashMap<String, Organisation>();
229 input.storeIn(map);
230 final ByOrganisation result = subject.byOrganisation(byOrganisation, map);
231 assertNotNull("Expected builder to build", result);
232 assertNotNull("Expected resources to be built", result.getResources());
233 assertNotNull("Expected organisation to be set", result.getOrganisation());
234 assertEquals("Expected number of resources built matches children", numberOfChildResources, result.getResources().size());
235 assertEquals("Expected organisation to be set to input", input, result.getOrganisation());
236 }
237
238
239
240
241
242
243 private void checkCollectResourcesNumbered(final int numberOfResources) {
244 final Collection<Resource> results = subject.collectResources(withChildResources(numberOfResources));
245 assertNotNull("Expected empty collection to be returned", results);
246 assertEquals("Expected collection to be empty", numberOfResources, results.size());
247 }
248
249
250
251
252 private Element withChildResources(int children) {
253 return withChildResources(children, "some id");
254 }
255
256
257
258
259
260
261 private Element withChildResources(int children, final String idValue) {
262 final Element result = new Element("by-organisation").setAttribute("id", idValue);
263 for (int i=0;i<children;i++) {
264 result.addContent(new Element("resource").setAttribute("name", "name" + i))
265 .addContent(new Element("whatever"));
266 }
267 return result;
268 }
269 }