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.License;
22
23 import junit.framework.TestCase;
24 import org.jdom2.CDATA;
25 import org.jdom2.Element;
26
27
28
29
30 public class JDomBuilderLicenseTest extends TestCase {
31
32 private JDomBuilder subject;
33
34 @Override
35 protected void setUp() throws Exception {
36 super.setUp();
37 subject = new JDomBuilder();
38 }
39
40 @Override
41 protected void tearDown() throws Exception {
42 super.tearDown();
43 }
44
45 public void testLicenseWithNameUrl() throws Exception {
46 checkSetNameUrlId("a name", "some url", "some id");
47 checkSetNameUrlId("name", "url", "id");
48 checkSetNameUrlId("NAME", "URL", "ID");
49 checkSetNameUrlId("a name", " some url ", " some id ");
50 }
51
52
53
54
55
56
57 private void checkSetNameUrlId(final String nameValue,
58 final String urlValue, final String idValue) {
59 final License result = subject.license(new Element("license")
60 .setAttribute("name", nameValue)
61 .setAttribute("url", urlValue)
62 .setAttribute("id", idValue));
63 assertNotNull("Builder should build", result);
64 assertEquals("Set id", idValue, result.getId());
65 assertEquals("Set url", urlValue, result.getURL());
66 assertEquals("Set name", nameValue, result.getName());
67 }
68
69 public void testLicenseRequiresSourceTrue() throws Exception {
70 assertTrue("Expected required source to be set", licenseWithSource("yes").isSourceRequired());
71 }
72
73 public void testLicenseRequiresSourceTrueAllCaps() throws Exception {
74 assertTrue("Expected required source to be set even when yes is in all caps case. This is against the schema.",
75 licenseWithSource("YES").isSourceRequired());
76 }
77
78 public void testLicenseRequiresSourceTrueMixedCase() throws Exception {
79 assertTrue("Expected required source to be set even when yes is in mixed case. This is against the schema.",
80 licenseWithSource("Yes").isSourceRequired());
81 }
82
83 public void testLicenseRequiresSourceFalse() throws Exception {
84 assertFalse("Expected required source to be set", licenseWithSource("no").isSourceRequired());
85 }
86
87 public void testLicenseRequiresSourceDefault() throws Exception {
88 final License result = subject.license(new Element("license")
89 .setAttribute("name", "name Value")
90 .setAttribute("url", "urlValue")
91 .setAttribute("id", "idValue"));
92 assertNotNull("Builder should build", result);
93 assertFalse("Expected to default to false", result.isSourceRequired());
94 }
95
96
97
98
99
100 private License licenseWithSource(final String requireSourceValue) {
101 final License result = subject.license(new Element("license")
102 .setAttribute("name", "name Value")
103 .setAttribute("url", "urlValue")
104 .setAttribute("id", "idValue")
105 .setAttribute("requires-source", requireSourceValue));
106 assertNotNull("Builder should build", result);
107 return result;
108 }
109
110 public void testLicenseNoParameters() throws Exception {
111 final License result = subject.license(new Element("license")
112 .setAttribute("name", "name Value")
113 .setAttribute("url", "urlValue")
114 .setAttribute("id", "idValue")
115 .setContent(new Element("template")));
116 assertNotNull("Builder should build", result);
117 assertNotNull("Builder should always set parameters even when empty", result.getExpectedParameters());
118 assertTrue("No parameters in template so collection should be empty", result.getExpectedParameters().isEmpty());
119 }
120
121 public void testLicenseOneParameter() throws Exception {
122 final String parameterName = "whatever";
123 final License result = subject.license(new Element("license")
124 .setAttribute("name", "name Value")
125 .setAttribute("url", "urlValue")
126 .setAttribute("id", "idValue")
127 .setContent(new Element("template").setContent(new Element("parameter-name").setContent(new CDATA(parameterName)))));
128 assertNotNull("Builder should build", result);
129 assertNotNull("Builder should always set parameters even when empty", result.getExpectedParameters());
130 assertEquals("One parameters in template", 1, result.getExpectedParameters().size());
131 assertEquals("Parameter name should be set from xml", parameterName, result.getExpectedParameters().iterator().next());
132 }
133
134 public void testLicenseTwoParameters() throws Exception {
135 final String parameterName = "whatever";
136 final License result = subject.license(new Element("license")
137 .setAttribute("name", "name Value")
138 .setAttribute("url", "urlValue")
139 .setAttribute("id", "idValue")
140 .setContent(new Element("template")
141 .addContent(new Element("parameter-name").setContent(new CDATA(parameterName)))
142 .addContent(new Element("parameter-name").setContent(new CDATA(parameterName + "2")))
143 ));
144 assertNotNull("Builder should build", result);
145 assertNotNull("Builder should always set parameters even when empty", result.getExpectedParameters());
146 assertEquals("One parameters in template", 2, result.getExpectedParameters().size());
147 }
148
149
150 public void testLicenseBaseText() throws Exception {
151 final String text = "Some text";
152 final License result = subject.license(new Element("license")
153 .setAttribute("name", "name Value")
154 .setAttribute("url", "urlValue")
155 .setAttribute("id", "idValue")
156 .setContent(new Element("text").setContent(new CDATA(text))));
157 assertNotNull("Builder should build", result);
158 assertEquals("Expected base text to be set", text, result.getText());
159 }
160
161 }