1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package test.de.smartics.ci.config.utils;
17
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.is;
20
21 import org.jdom.Element;
22 import org.jdom.JDOMException;
23 import org.junit.Test;
24
25 import de.smartics.ci.config.test.JDomTestUtils;
26 import de.smartics.ci.config.utils.JDomUtils;
27 import de.smartics.testdoc.annotations.Uut;
28 import de.smartics.util.lang.NullArgumentException;
29
30
31
32
33 @Uut(type = JDomUtils.class, method = "merge(Element, Element)")
34 public class JDomUtilsMergeTest
35 {
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 private static void assertThatElementsAreEqual(final Element target,
51 final Element expected)
52 {
53 assertThat(JDomTestUtils.toString(target),
54 is(JDomTestUtils.toString(expected)));
55 }
56
57
58
59 @Test(expected = NullArgumentException.class)
60 public void rejectsIfTargetIsNull() throws JDOMException
61 {
62 final Element toBeMerged = new Element("test");
63 JDomUtils.merge(null, toBeMerged);
64 }
65
66 @Test(expected = NullArgumentException.class)
67 public void rejectsIfToBeMergedIsNull() throws JDOMException
68 {
69 final Element target = new Element("test");
70 JDomUtils.merge(target, null);
71 }
72
73 @Test(expected = IllegalArgumentException.class)
74 public void rejectsIfRootElementsDoNotHaveTheSameName() throws JDOMException
75 {
76 final Element target = new Element("test1");
77 final Element toBeMerged = new Element("test2");
78 JDomUtils.merge(target, toBeMerged);
79 }
80
81 @Test
82 public void mergesTwoElementsWithoutContent() throws JDOMException
83 {
84 final Element target = new Element("test");
85 final Element toBeMerged = new Element("test");
86
87 JDomUtils.merge(target, toBeMerged);
88
89 final Element expected = new Element("test");
90
91 assertThatElementsAreEqual(target, expected);
92 }
93
94 @Test
95 public void mergesTwoElementsWithContent() throws JDOMException
96 {
97 final Element target = new Element("test");
98 final Element targetSubElement = new Element("tsub");
99 target.addContent(targetSubElement);
100
101 final Element toBeMerged = new Element("test");
102 final Element subElement = new Element("sub");
103 toBeMerged.addContent(subElement);
104
105 JDomUtils.merge(target, toBeMerged);
106
107 final Element expected = new Element("test");
108 final Element expectedSubElement1 = new Element("tsub");
109 final Element expectedSubElement2 = new Element("sub");
110 expected.addContent(expectedSubElement1);
111 expected.addContent(expectedSubElement2);
112
113 assertThatElementsAreEqual(target, expected);
114 }
115
116 @Test
117 public void mergesTwoElementsWithIdenticalContent() throws JDOMException
118 {
119 final Element target = new Element("test");
120 final Element targetSubElement1 = new Element("sub1");
121 final Element targetSubElement2 = new Element("sub2");
122 final Element targetSubSubElement = new Element("sub3");
123 target.addContent(targetSubElement1);
124 target.addContent(targetSubElement2);
125 targetSubElement1.addContent(targetSubSubElement);
126
127 final Element toBeMerged = new Element("test");
128 final Element subElement1 = new Element("sub1");
129 final Element subElement2 = new Element("sub2");
130 final Element subSubElement = new Element("sub3");
131 toBeMerged.addContent(subElement1);
132 toBeMerged.addContent(subElement2);
133 subElement1.addContent(subSubElement);
134
135 JDomUtils.merge(target, toBeMerged);
136
137 final Element expected = new Element("test");
138 final Element expectedSubElement1 = new Element("sub1");
139 final Element expectedSubElement2 = new Element("sub2");
140 final Element expectedSubSubElement = new Element("sub3");
141 expected.addContent(expectedSubElement1);
142 expected.addContent(expectedSubElement2);
143 expectedSubElement1.addContent(expectedSubSubElement);
144
145 assertThatElementsAreEqual(target, expected);
146 }
147
148 @Test
149 public void recognizesElementDisorders() throws JDOMException
150 {
151 final Element target = new Element("test");
152 final Element targetSubElement1 = new Element("sub1");
153 final Element targetSubElement2 = new Element("sub2");
154 final Element targetSubSubElement = new Element("sub3");
155 target.addContent(targetSubElement1);
156 target.addContent(targetSubElement2);
157 targetSubElement1.addContent(targetSubSubElement);
158
159 final Element toBeMerged = new Element("test");
160 final Element subElement1 = new Element("sub1");
161 final Element subElement2 = new Element("sub2");
162 final Element subSubElement = new Element("sub3");
163 toBeMerged.addContent(subElement2);
164 toBeMerged.addContent(subElement1);
165 subElement1.addContent(subSubElement);
166
167 JDomUtils.merge(target, toBeMerged);
168
169 final Element expected = new Element("test");
170 final Element expectedSubElement1 = new Element("sub1");
171 final Element expectedSubElement2 = new Element("sub2");
172 final Element expectedSubSubElement = new Element("sub3");
173 expected.addContent(expectedSubElement1);
174 expected.addContent(expectedSubElement2);
175 expectedSubElement1.addContent(expectedSubSubElement);
176
177 assertThatElementsAreEqual(target, expected);
178 }
179 }