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 org.apache.creadur.whisker.model.Resource;
22
23 import junit.framework.TestCase;
24 import org.jdom2.Element;
25
26
27
28
29 public class JDomBuilderResourceTest extends TestCase {
30
31 private JDomBuilder subject;
32
33 @Override
34 protected void setUp() throws Exception {
35 super.setUp();
36 subject = new JDomBuilder();
37 }
38
39 @Override
40 protected void tearDown() throws Exception {
41 super.tearDown();
42 }
43
44 public void testBuildResourceWithNameNoticeAndSourceTrimsSpacesBeforeValue() throws Exception {
45 checkResourceBuild(" name", "name",
46 " notice", "notice",
47 " source", "source");
48 }
49
50
51 public void testBuildResourceWithNameNoticeAndSourceTrimsSpaces() throws Exception {
52 checkResourceBuild(" name ", "name",
53 " notice ", "notice", " " +
54 " source ", "source");
55
56 }
57
58
59 public void testBuildResourceWithNameNoticeAndSourceTrimsSpacesAfterValue() throws Exception {
60 checkResourceBuild("name ", "name",
61 "notice ", "notice", " " +
62 "source ", "source");
63 }
64
65
66 public void testBuildResourceWithNameNoticeAndSourceTrimsWithSpacesInValue() throws Exception {
67 checkResourceBuild(" n ame ", "n ame",
68 " not ic e ", "not ic e", " " +
69 "sour ce ", "sour ce");
70 }
71
72 public void testBuildResourceWithNameNoticeAndSourceSpacesInVaule() throws Exception {
73 checkResourceBuildWithoutSurroundingSpace("a name", "a notice", "some source");
74 }
75
76 public void testBuildResourceWithNameNoticeAndSourceLowers() throws Exception {
77 checkResourceBuildWithoutSurroundingSpace("name", "notice", "source");
78 }
79
80 public void testBuildResourceWithNameNoticeAndSourceCaps() throws Exception {
81 checkResourceBuildWithoutSurroundingSpace("NOTICE", "NOTICE", "SOURCE");
82 }
83
84 public void testThrowsIllegalArgumentWhenElementIsNotResource() throws Exception {
85 try {
86 subject.resource(
87 new Element("bogus")
88 .setAttribute("name", "name")
89 .setAttribute("notice", "notice")
90 .setAttribute("source", "source"));
91 fail("Expected IllegalArgument throw when elements is not named 'resource'");
92 } catch (UnexpectedElementException e) {
93
94 } catch (Throwable t) {
95 fail("Expected IllegalArgument throw when elements is not named 'resource' but " + t + " was instead");
96 }
97 }
98
99
100
101
102
103
104 private void checkResourceBuildWithoutSurroundingSpace(
105 final String nameValue, final String noticeValue,
106 final String sourceValue) {
107 final String expectedNameValue = nameValue;
108 final String expectedNoticeValue = noticeValue;
109 final String expectedSourceValue = sourceValue;
110
111 checkResourceBuild(nameValue, expectedNameValue, noticeValue,
112 expectedNoticeValue, sourceValue, expectedSourceValue);
113 }
114
115
116
117
118
119
120
121
122
123 private void checkResourceBuild(final String nameValue,
124 final String expectedNameValue, final String noticeValue,
125 final String expectedNoticeValue, final String sourceValue,
126 final String expectedSourceValue) {
127 final Resource result = subject.resource(
128 new Element("resource")
129 .setAttribute("name", nameValue)
130 .setAttribute("notice", noticeValue)
131 .setAttribute("source", sourceValue));
132 assertNotNull("Expected builder to build", result);
133 assertEquals("Name value set from xml attribute", expectedNameValue, result.getName());
134 assertEquals("Notice value set from xml attribute", expectedNoticeValue, result.getNoticeId());
135 assertEquals("Source value set from xml attribute", expectedSourceValue, result.getSource());
136 }
137 }