001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.driver.xpath;
017:
018: import org.w3c.dom.Document;
019: import org.xml.sax.SAXException;
020: import scriptella.AbstractTestCase;
021: import scriptella.configuration.StringResource;
022: import scriptella.spi.AbstractConnection;
023: import scriptella.spi.IndexedQueryCallback;
024: import scriptella.spi.MockParametersCallbacks;
025: import scriptella.spi.ParametersCallback;
026: import scriptella.spi.Resource;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.parsers.ParserConfigurationException;
031: import java.io.IOException;
032:
033: /**
034: * Tests for {@link XPathQueryExecutor}.
035: *
036: * @author Fyodor Kupolov
037: * @version 1.0
038: */
039: public class XPathQueryExecutorTest extends AbstractTestCase {
040: private DocumentBuilder documentBuilder;
041:
042: protected void setUp() throws Exception {
043: documentBuilder = DocumentBuilderFactory.newInstance()
044: .newDocumentBuilder();
045: }
046:
047: public void test() throws ParserConfigurationException,
048: IOException, SAXException {
049: Document doc = documentBuilder.parse(getClass()
050: .getResourceAsStream("xml1.xml"));
051: Resource res = new StringResource("/html/body/table/tr");
052: XPathQueryExecutor exec = new XPathQueryExecutor(doc, res,
053: new XPathExpressionCompiler(),
054: new AbstractConnection.StatementCounter());
055: IndexedQueryCallback callback = new IndexedQueryCallback() {
056:
057: protected void processRow(
058: final ParametersCallback parameters,
059: final int rowNumber) {
060: if (rowNumber == 0) {
061: assertEquals("red", parameters
062: .getParameter("bgcolor"));
063: assertEquals("Column1", ((String[]) parameters
064: .getParameter("th"))[0]);
065: assertEquals("Column2", ((String[]) parameters
066: .getParameter("th"))[1]);
067: } else {
068: assertEquals(
069: String.valueOf(rowNumber * 2 - 1),
070: ((String[]) parameters.getParameter("td"))[0]);
071: assertEquals(
072: String.valueOf(rowNumber * 2),
073: ((String[]) parameters.getParameter("td"))[1]);
074: }
075: }
076: };
077: exec.execute(callback, MockParametersCallbacks.NULL);
078: assertEquals(3, callback.getRowsNumber());
079: }
080:
081: public void test2() throws ParserConfigurationException,
082: IOException, SAXException {
083: Document doc = documentBuilder.parse(getClass()
084: .getResourceAsStream("xml2.xml"));
085: Resource res = new StringResource(
086: " /xml/element[@attribute=1] | /xml/element[not(@attribute)]");
087: XPathQueryExecutor exec = new XPathQueryExecutor(doc, res,
088: new XPathExpressionCompiler(),
089: new AbstractConnection.StatementCounter());
090: IndexedQueryCallback callback = new IndexedQueryCallback() {
091:
092: protected void processRow(
093: final ParametersCallback parameters,
094: final int rowNumber) {
095: if (rowNumber == 0) {
096: assertEquals("1", parameters
097: .getParameter("attribute"));
098: } else {
099: assertEquals("", parameters.getParameter("element"));
100: }
101: }
102: };
103: exec.execute(callback, MockParametersCallbacks.NULL);
104: assertEquals(2, callback.getRowsNumber());
105: //Now select element2, also test substitution
106: res = new StringResource(" /xml/$element2 ");
107: exec = new XPathQueryExecutor(doc, res,
108: new XPathExpressionCompiler(),
109: new AbstractConnection.StatementCounter());
110: callback = new IndexedQueryCallback() {
111:
112: protected void processRow(
113: final ParametersCallback parameters,
114: final int rowNumber) {
115: if (rowNumber == 0) {
116: assertEquals("1", parameters
117: .getParameter("attribute"));
118: } else {
119: assertEquals("el2", parameters
120: .getParameter("element2"));
121: }
122: }
123: };
124: exec.execute(callback, MockParametersCallbacks.NAME);
125: assertEquals(2, callback.getRowsNumber());
126: }
127:
128: }
|