offline building of site deploy to improve performance.
[utils] / support / general / src / test / java / org / wamblee / xml / XPathExpressionTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
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 org.wamblee.xml;
17
18 import java.io.IOException;
19 import java.net.URISyntaxException;
20
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26
27 import static junit.framework.TestCase.*;
28 import static org.wamblee.xml.XMLDocument.*;
29 import static org.wamblee.xml.XPathContext.*;
30 import static org.wamblee.xml.SimpleNamespaceContext.*;
31
32 public class XPathExpressionTest {
33
34     private XPathContext context; 
35     private XMLDocument doc; 
36     
37     @Before
38     public void setUp() throws XMLException, URISyntaxException, IOException { 
39         context = xpathcontext(namespaces().addPrefix("n", "http://example.com/1").addPrefix("m", "http://example.com/2"));
40         doc = xmldocument(getClass().getResource("xpathexample.xml").toURI());
41     }
42     
43     @Test(expected = XMLException.class)
44     public void testInvalidXpath() throws XMLException { 
45         context.createExpression("a[");
46     }
47     
48     @Test
49     public void testStringVal() throws XMLException { 
50         String value = context.createExpression("/n:root/m:x").stringEval(doc);
51         assertEquals("hallo", value.trim());
52     }
53     
54     @Test
55     public void testBooleanVal() throws XMLException { 
56         assertTrue(context.createExpression("/n:root/m:x").booleanEval(doc));        
57         assertFalse(context.createExpression("unknownelement").booleanEval(doc));
58     }
59     
60     @Test
61     public void testNummberVal() throws XMLException { 
62         assertEquals(1.0d, context.createExpression("count(/n:root/m:x)").numberEval(doc));
63         assertEquals(11.0d, context.createExpression("//n:elem").numberEval(doc));
64     }
65     
66     @Test
67     public void testNodeVal() throws XMLException { 
68         Node node = context.createExpression("/n:root/n:elem").nodeEval(doc);
69         assertTrue(node instanceof Element); 
70         assertEquals("11", node.getTextContent().trim());
71         assertEquals("elem", node.getLocalName());
72         assertEquals("http://example.com/1", node.getNamespaceURI());
73     }
74     
75     @Test
76     public void testNodeSetVal() throws XMLException { 
77         NodeList nodelist = context.createExpression("/n:root/n:elem").nodelistEval(doc);
78         assertEquals(2, nodelist.getLength());
79         assertEquals("11", nodelist.item(0).getTextContent().trim());
80         assertEquals("22", nodelist.item(1).getTextContent().trim());
81     }
82 }