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.equalTo;
20  import static org.hamcrest.Matchers.is;
21  
22  import org.jdom.Element;
23  import org.jdom.JDOMException;
24  import org.junit.Test;
25  import org.junit.experimental.categories.Category;
26  
27  import de.smartics.ci.config.utils.JDomUtils;
28  import de.smartics.testdoc.annotations.Uut;
29  import de.smartics.testdoc.categories.Technical;
30  import de.smartics.util.lang.NullArgumentException;
31  
32  /**
33   * Tests {@link JDomUtils#calcXPath(Element, Element)}.
34   */
35  @Uut(type = JDomUtils.class, method = "calcXPath(Element, Element)")
36  public class JDomUtilsCalcXPathTest
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    // ****************************** Inner Classes *****************************
45  
46    // ********************************* Methods ********************************
47  
48    // --- prepare --------------------------------------------------------------
49  
50    // --- helper ---------------------------------------------------------------
51  
52    // --- tests ----------------------------------------------------------------
53  
54    @Test(expected = NullArgumentException.class)
55    public void rejectsIfElementIsNull() throws JDOMException
56    {
57      final Element rootElement = new Element("root"); // NOPMD
58      JDomUtils.calcXPath(rootElement, null);
59    }
60  
61    @Test
62    public void calcsPathFromDocumentRootIfNullArgumentIsPassed()
63      throws JDOMException
64    {
65      final Element element = new Element("test"); // NOPMD
66      final Element elementSubElement = new Element("sub"); // NOPMD
67      final Element elementSubSubElement = new Element("subsub"); // NOPMD
68      element.addContent(elementSubElement);
69      elementSubElement.addContent(elementSubSubElement);
70  
71      final String path = JDomUtils.calcXPath(null, elementSubSubElement);
72      assertThat(path, is(equalTo("/test/sub/subsub")));
73    }
74  
75    @Test
76    public void calcsPathRelativeToGivenRootElement()
77      throws JDOMException
78    {
79      final Element element = new Element("test");
80      final Element elementSubElement = new Element("sub");
81      final Element elementSubSubElement = new Element("subsub");
82      element.addContent(elementSubElement);
83      elementSubElement.addContent(elementSubSubElement);
84  
85      final String path = JDomUtils.calcXPath(element, elementSubSubElement);
86      assertThat(path, is(equalTo("sub/subsub")));
87    }
88  
89    @Test
90    @Category(Technical.class)
91    public void rootElementIsNotRequiredToBeADirectChildOfTheDocumentsRoot()
92      throws JDOMException
93    {
94      final Element element = new Element("test");
95      final Element elementSubElement = new Element("sub");
96      final Element elementSubSubElement = new Element("subsub");
97      element.addContent(elementSubElement);
98      elementSubElement.addContent(elementSubSubElement);
99  
100     final String path = JDomUtils.calcXPath(elementSubElement, elementSubSubElement);
101     assertThat(path, is(equalTo("subsub")));
102   }
103 }