1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*
*
* Derby - Class org.apache.derbyTesting.functionTests.tests.lang.XMLConcurrencyTest
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.lang;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
import org.apache.derbyTesting.junit.XML;
/**
* Test that XML operators can be invoked by multiple threads concurrently.
* Regression test case for DERBY-3870.
*/
public class XMLConcurrencyTest extends BaseJDBCTestCase {
/** Create an instance of this test case. */
public XMLConcurrencyTest(String name) {
super(name);
}
/** Create a suite of all test cases in this class. */
public static Test suite() {
// XML operators are engine functionality, so run this test in
// embedded mode only.
if (XML.classpathMeetsXMLReqs()) {
return new CleanDatabaseTestSetup(
TestConfiguration.embeddedSuite(XMLConcurrencyTest.class)) {
protected void decorateSQL(Statement s)
throws SQLException {
createTestTable(s);
}
};
} else {
return new BaseTestSuite("XMLConcurrencyTest - empty");
}
}
/**
* Start four threads that execute queries that use all the XML operators.
* If each thread manages 100 iterations without failing, running
* concurrently with the other threads, the test case passes.
*/
public void testConcurrency() throws Exception {
WorkerThread[] allThreads = new WorkerThread[4];
for (int i = 0; i < allThreads.length; i++) {
allThreads[i] = new WorkerThread(openDefaultConnection(), 100);
allThreads[i].start();
}
for (int i = 0; i < allThreads.length; i++) {
allThreads[i].join();
Throwable t = allThreads[i].throwable;
if (t != null) {
fail("Worker thread failed", t);
}
}
}
/**
* A thread class that does the actual work in the test.
*/
private class WorkerThread extends Thread {
final Connection conn;
final int iterations;
Throwable throwable;
WorkerThread(Connection conn, int iterations) {
this.conn = conn;
this.iterations = iterations;
}
public void run() {
try {
runXMLTest(conn, iterations);
} catch (Throwable t) {
throwable = t;
}
}
}
/**
* <p>
* Create a table with test data. The table contains three columns:
* <p>
*
* <ol>
* <li>
* An ID column used to identify the rows and to give a stable ordering.
* </li>
* <li>
* A VARCHAR column holding the string representation of an XML document.
* </li>
* <li>
* An XML column holding the XML representation of the document in the
* VARCHAR column.
* </li>
* </ol>
*/
private static void createTestTable(Statement s) throws SQLException {
s.executeUpdate("create table t (id int primary key " +
"generated always as identity, vc varchar(100), " +
"x generated always as " +
"(xmlparse(document vc preserve whitespace)))");
PreparedStatement ins = s.getConnection().prepareStatement(
"insert into t(vc) values ?");
String[] docs = {
"<doc><a x='1'>abc</a><b x='2'>def</b></doc>",
"<doc><a x='2'>abc</a><b x='3'>def</b></doc>",
"<doc/>",
"<a/>",
null,
};
for (int i = 0; i < docs.length; i++) {
ins.setString(1, docs[i]);
ins.executeUpdate();
}
ins.close();
}
/**
* Do the work for one of the worker threads. Perform queries that use
* all the XML operators. Repeat the queries the specified number of times.
*
* @param conn the connection on which to execute the queries
* @param iterations the number of times each query should be executed
*/
private static void runXMLTest(Connection conn, int iterations)
throws SQLException {
// Query that tests XMLQUERY and XMLSERIALIZE. Count the number of
// nodes with an attribute named x with a value greater than 1.
PreparedStatement ps1 = conn.prepareStatement(
"select id, xmlserialize(" +
"xmlquery('count(//*[@x>1])' passing by ref x empty on empty) " +
"as varchar(100)) from t order by id");
String[][] expected1 = {
{"1", "1"}, {"2", "2"}, {"3", "0"}, {"4", "0"}, {"5", null}
};
// Query that tests XMLEXISTS. Find all documents containing a "doc"
// node with a nested "a" node whose x attribute is 2.
PreparedStatement ps2 = conn.prepareStatement(
"select id from t where " +
"xmlexists('/doc/a[@x=2]' passing by ref x) " +
"order by id");
String expected2 = "2";
// Query that tests XMLPARSE and XMLSERIALIZE.
PreparedStatement ps3 = conn.prepareStatement(
"select count(*) from t where " +
"xmlserialize(xmlparse(document vc preserve whitespace) " +
"as varchar(100)) = " +
"xmlserialize(x as varchar(100))");
String expected3 = "4";
for (int i = 0; i < iterations; i++) {
JDBC.assertFullResultSet(ps1.executeQuery(), expected1);
JDBC.assertSingleValueResultSet(ps2.executeQuery(), expected2);
JDBC.assertSingleValueResultSet(ps3.executeQuery(), expected3);
}
ps1.close();
ps2.close();
ps3.close();
conn.close();
}
}
|