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.w3c.dom.Element;
020: import scriptella.AbstractTestCase;
021: import scriptella.configuration.MockConnectionEl;
022: import scriptella.configuration.StringResource;
023: import scriptella.spi.AbstractConnection;
024: import scriptella.spi.Connection;
025: import scriptella.spi.ConnectionParameters;
026: import scriptella.spi.IndexedQueryCallback;
027: import scriptella.spi.MockDriverContext;
028: import scriptella.spi.MockParametersCallbacks;
029: import scriptella.spi.ParametersCallback;
030:
031: import javax.xml.parsers.ParserConfigurationException;
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: /**
036: * Tests for {@link scriptella.driver.xpath.XPathConnection}.
037: *
038: * @author Fyodor Kupolov
039: * @version 1.0
040: */
041: public class XPathConnectionPerfTest extends AbstractTestCase {
042: /**
043: * History:
044: * 19.01.2007 - Duron 1.7Mhz - 1125 ms
045: */
046: public void testQuery() {
047: //Create a configuration with non default values
048: Map<String, String> props = new HashMap<String, String>();
049: ConnectionParameters cp = new ConnectionParameters(
050: new MockConnectionEl(props, getClass().getResource(
051: "excel.xml").toString()),
052: MockDriverContext.INSTANCE);
053:
054: Connection con = new Driver().connect(cp);
055: IndexedQueryCallback queryCallback = new IndexedQueryCallback() {
056: protected void processRow(
057: final ParametersCallback parameters,
058: final int rowNumber) {
059: parameters.getParameter("Cell");
060: }
061: };
062: //Quering 200 times.
063: for (int i = 0; i < 200; i++) {
064: con.executeQuery(new StringResource(
065: "/$Workbook/Worksheet/Table/Row"),
066: MockParametersCallbacks.NAME, queryCallback);
067: }
068: assertEquals(600, queryCallback.getRowsNumber());
069: }
070:
071: /**
072: * History:
073: * 19.01.2007 - Duron 1.7Mhz - 1220 ms
074: */
075: public void testQueryLargeDOM() throws ParserConfigurationException {
076: //Create a configuration with non default values
077: Document doc = XPathConnection.DBF.newDocumentBuilder()
078: .newDocument();
079: Element root = doc.createElement("table");
080: doc.appendChild(root);
081: for (int i = 0; i < 4000; i++) {
082: Element row = doc.createElement("row");
083: row.setAttribute("id", String.valueOf(i));
084: root.appendChild(row);
085: }
086: //Quering 200 times.
087: XPathQueryExecutor qe = new XPathQueryExecutor(doc,
088: new StringResource("/table/row[@id mod 2=0]"),
089: new XPathExpressionCompiler(),
090: new AbstractConnection.StatementCounter());
091: for (int i = 0; i < 20; i++) {
092: IndexedQueryCallback queryCallback = new IndexedQueryCallback() {
093: protected void processRow(
094: final ParametersCallback parameters,
095: final int rowNumber) {
096: assertTrue(Integer.parseInt((String) parameters
097: .getParameter("id")) % 2 == 0);
098: }
099: };
100: qe.execute(queryCallback, MockParametersCallbacks.NULL);
101: assertEquals(2000, queryCallback.getRowsNumber());
102: }
103:
104: }
105:
106: }
|