KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > XmlElementTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.configuration;
17
18 import org.w3c.dom.Element JavaDoc;
19 import org.xml.sax.InputSource JavaDoc;
20 import scriptella.AbstractTestCase;
21 import scriptella.expression.PropertiesSubstitutor;
22 import scriptella.spi.MockParametersCallbacks;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.net.URL JavaDoc;
29
30 /**
31  * Tests for {@link XmlElement}.
32  *
33  * @author Fyodor Kupolov
34  * @version 1.0
35  */

36 public class XmlElementTest extends AbstractTestCase {
37     private static final DocumentBuilder JavaDoc BUILDER;
38
39     static {
40         try {
41             BUILDER = DocumentBuilderFactory.newInstance().newDocumentBuilder();
42         } catch (ParserConfigurationException JavaDoc e) {
43             throw new IllegalStateException JavaDoc(e.getMessage(), e);
44         }
45     }
46
47     public void testGetXPath() {
48         String JavaDoc xml = "<etl>\n" +
49                 " <query connection-id=\"db1\">\n" +
50                 " query1" +
51                 " <script connection-id=\"db2\">\n" +
52                 " SCRIPT1" +
53                 " </script>\n" +
54                 " </query>\n" +
55                 " <query connection-id=\"db3\">\n" +
56                 " query2" +
57                 " <script connection-id=\"db4\">\n" +
58                 " script2" +
59                 " </script>\n" +
60                 " </query>\n" +
61                 "\n" +
62                 "</etl>";
63         XmlElement root = asElement(xml);
64         //selecting second query, first script
65
XmlElement el = root.getChildren("query").get(1).getChild("script");
66         assertEquals("/etl/query[2]/script[1]", el.getXPath());
67
68     }
69
70     static XmlElement asElement(String JavaDoc xml) {
71         try {
72             Element JavaDoc el = BUILDER.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml))).getDocumentElement();
73             return new XmlElement(el, new URL JavaDoc("file:/test"), new PropertiesSubstitutor(MockParametersCallbacks.NULL));
74         } catch (Exception JavaDoc e) {
75             throw new IllegalStateException JavaDoc("Unable to create XML element", e);
76         }
77     }
78
79 }
80
Popular Tags