Skip to content

Commit d4e2c00

Browse files
committed
start adding some tests
1 parent c61d140 commit d4e2c00

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

src/test/java/net/sourceforge/htmlunit/xpath/XPathTest.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,125 @@ public void simpleSearch() throws Exception {
4848
List<?> hits = XPathHelper.getByXPath(doc, "//element", null, false);
4949
assertEquals(1, hits.size());
5050
}
51+
52+
/**
53+
* @throws Exception in case of problems
54+
*/
55+
@Test
56+
public void pipeSearch() throws Exception {
57+
final String input = "<root><element/><element2/></root>";
58+
59+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
60+
final DocumentBuilder builder = factory.newDocumentBuilder();
61+
Document doc = builder.parse(IOUtils.toInputStream(input, StandardCharsets.UTF_8));
62+
63+
List<?> hits = XPathHelper.getByXPath(doc, "//element | //element2", null, false);
64+
assertEquals(2, hits.size());
65+
}
66+
67+
/**
68+
* @throws Exception in case of problems
69+
*/
70+
@Test
71+
public void mathSearch() throws Exception {
72+
final String input = "<root><p/><p/></root>";
73+
74+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
75+
final DocumentBuilder builder = factory.newDocumentBuilder();
76+
Document doc = builder.parse(IOUtils.toInputStream(input, StandardCharsets.UTF_8));
77+
78+
List<?> hits = XPathHelper.getByXPath(doc, "//p[position()=(1+5-(2*2))div 2]", null, false);
79+
assertEquals(1, hits.size());
80+
}
81+
82+
/**
83+
* @throws Exception in case of problems
84+
*/
85+
@Test
86+
public void gtSearch() throws Exception {
87+
final String input = "<root><p/><p/></root>";
88+
89+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
90+
final DocumentBuilder builder = factory.newDocumentBuilder();
91+
Document doc = builder.parse(IOUtils.toInputStream(input, StandardCharsets.UTF_8));
92+
93+
List<?> hits = XPathHelper.getByXPath(doc, "//p[position()>1]", null, false);
94+
assertEquals(1, hits.size());
95+
}
96+
97+
/**
98+
* @throws Exception in case of problems
99+
*/
100+
@Test
101+
public void gteSearch() throws Exception {
102+
final String input = "<root><p/><p/></root>";
103+
104+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
105+
final DocumentBuilder builder = factory.newDocumentBuilder();
106+
Document doc = builder.parse(IOUtils.toInputStream(input, StandardCharsets.UTF_8));
107+
108+
List<?> hits = XPathHelper.getByXPath(doc, "//p[position()>=1]", null, false);
109+
assertEquals(2, hits.size());
110+
}
111+
112+
113+
/**
114+
* @throws Exception in case of problems
115+
*/
116+
@Test
117+
public void ltSearch() throws Exception {
118+
final String input = "<root><p/><p/></root>";
119+
120+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
121+
final DocumentBuilder builder = factory.newDocumentBuilder();
122+
Document doc = builder.parse(IOUtils.toInputStream(input, StandardCharsets.UTF_8));
123+
124+
List<?> hits = XPathHelper.getByXPath(doc, "//p[position()<2]", null, false);
125+
assertEquals(1, hits.size());
126+
}
127+
128+
/**
129+
* @throws Exception in case of problems
130+
*/
131+
@Test
132+
public void lteSearch() throws Exception {
133+
final String input = "<root><p/><p/></root>";
134+
135+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
136+
final DocumentBuilder builder = factory.newDocumentBuilder();
137+
Document doc = builder.parse(IOUtils.toInputStream(input, StandardCharsets.UTF_8));
138+
139+
List<?> hits = XPathHelper.getByXPath(doc, "//p[position()<=2]", null, false);
140+
assertEquals(2, hits.size());
141+
}
142+
143+
/**
144+
* @throws Exception in case of problems
145+
*/
146+
@Test
147+
public void eqSearch() throws Exception {
148+
final String input = "<root><p/><p/></root>";
149+
150+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
151+
final DocumentBuilder builder = factory.newDocumentBuilder();
152+
Document doc = builder.parse(IOUtils.toInputStream(input, StandardCharsets.UTF_8));
153+
154+
List<?> hits = XPathHelper.getByXPath(doc, "//p[position()=2]", null, false);
155+
assertEquals(1, hits.size());
156+
}
157+
158+
/**
159+
* @throws Exception in case of problems
160+
*/
161+
@Test
162+
public void neqSearch() throws Exception {
163+
final String input = "<root><p/><p/><p/><p/></root>";
164+
165+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
166+
final DocumentBuilder builder = factory.newDocumentBuilder();
167+
Document doc = builder.parse(IOUtils.toInputStream(input, StandardCharsets.UTF_8));
168+
169+
List<?> hits = XPathHelper.getByXPath(doc, "//p[position()!=2]", null, false);
170+
assertEquals(3, hits.size());
171+
}
51172
}

0 commit comments

Comments
 (0)