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.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
34
35 @Uut(type = JDomUtils.class, method = "calcXPath(Element, Element)")
36 public class JDomUtilsCalcXPathTest
37 {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 @Test(expected = NullArgumentException.class)
55 public void rejectsIfElementIsNull() throws JDOMException
56 {
57 final Element rootElement = new Element("root");
58 JDomUtils.calcXPath(rootElement, null);
59 }
60
61 @Test
62 public void calcsPathFromDocumentRootIfNullArgumentIsPassed()
63 throws JDOMException
64 {
65 final Element element = new Element("test");
66 final Element elementSubElement = new Element("sub");
67 final Element elementSubSubElement = new Element("subsub");
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 }