View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Tests {@link JDomUtils#merge(Element, Element)}.
32   */
33  @Uut(type = JDomUtils.class, method = "merge(Element, Element)")
34  public class JDomUtilsMergeTest
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    // --- members --------------------------------------------------------------
41  
42    // ****************************** Inner Classes *****************************
43  
44    // ********************************* Methods ********************************
45  
46    // --- prepare --------------------------------------------------------------
47  
48    // --- helper ---------------------------------------------------------------
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    // --- tests ----------------------------------------------------------------
58  
59    @Test(expected = NullArgumentException.class)
60    public void rejectsIfTargetIsNull() throws JDOMException
61    {
62      final Element toBeMerged = new Element("test"); // NOPMD
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"); // NOPMD
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");// NOPMD
121     final Element targetSubElement2 = new Element("sub2"); // NOPMD
122     final Element targetSubSubElement = new Element("sub3"); // NOPMD
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");// NOPMD
153     final Element targetSubElement2 = new Element("sub2"); // NOPMD
154     final Element targetSubSubElement = new Element("sub3"); // NOPMD
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 }