001: /*
002: * $Id: TestThreadedSelect.java,v 1.13 2005/04/24 21:35:59 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functional;
042:
043: import java.io.File;
044: import java.sql.Connection;
045: import java.sql.DriverManager;
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.Statement;
049:
050: import junit.framework.Test;
051: import junit.framework.TestCase;
052: import junit.framework.TestSuite;
053:
054: /**
055: *
056: * @version $Revision: 1.13 $ $Date: 2005/04/24 21:35:59 $
057: * @author Chuck Burdick
058: * @author Rodney Waldhoff
059: */
060: public class TestThreadedSelect extends TestCase {
061: private static final int NUMBER_OF_TEST_THREADS = 5;
062: private Connection _conn = null;
063: private ResultSet _rset = null;
064: private Statement _stmt = null;
065:
066: //------------------------------------------------------------ Conventional
067:
068: public TestThreadedSelect(String testName) {
069: super (testName);
070: }
071:
072: public static Test suite() {
073: return new TestSuite(TestThreadedSelect.class);
074: }
075:
076: //--------------------------------------------------------------- Lifecycle
077:
078: public void setUp() throws Exception {
079: Class.forName("org.axiondb.jdbc.AxionDriver");
080: _conn = DriverManager
081: .getConnection("jdbc:axiondb:diskdb:testdb2");
082: _stmt = _conn.createStatement();
083: }
084:
085: public void tearDown() throws Exception {
086: try {
087: if (_rset != null)
088: _rset.close();
089: } catch (Exception t) {
090: }
091: try {
092: if (_stmt != null)
093: _stmt.close();
094: } catch (Exception t) {
095: }
096: try {
097: if (_conn != null)
098: _conn.close();
099: } catch (Exception t) {
100: }
101: _rset = null;
102: _stmt = null;
103: _conn = null;
104:
105: {
106: Connection conn = DriverManager
107: .getConnection("jdbc:axiondb:diskdb:testdb2");
108: Statement stmt = conn.createStatement();
109: stmt.execute("shutdown");
110: stmt.close();
111: conn.close();
112: }
113:
114: deleteFile(new File("testdb2"));
115: }
116:
117: private boolean deleteFile(File file) throws Exception {
118: if (file.exists()) {
119: if (file.isDirectory()) {
120: File[] files = file.listFiles();
121: for (int i = 0; i < files.length; i++) {
122: deleteFile(files[i]);
123: }
124: }
125: return file.delete();
126: }
127: return true;
128: }
129:
130: //------------------------------------------------------------------- Tests
131:
132: public void testWithoutIndex() throws Exception {
133: createTableFoo();
134: populateTableFoo();
135: runTests();
136: }
137:
138: public void testWithIndex() throws Exception {
139: createTableFoo();
140: createIndexOnFoo();
141: populateTableFoo();
142: runTests();
143: }
144:
145: public void testWithoutIndexForFlatFile() throws Exception {
146: createDelimitedTableFoo();
147: populateTableFoo();
148: runTests();
149: }
150:
151: public void testWithIndexForFlatFile() throws Exception {
152: createDelimitedTableFoo();
153: createIndexOnFoo();
154: populateTableFoo();
155: runTests();
156: }
157:
158: //----------------------------------------------------------- Inner Classes
159:
160: private void runTests() throws Exception {
161: TestThread[] tests = new TestThread[NUMBER_OF_TEST_THREADS];
162: Thread[] threads = new Thread[tests.length];
163: for (int i = 0; i < tests.length; i++) {
164: tests[i] = new TestThread();
165: threads[i] = new Thread(tests[i]);
166: }
167: for (int i = 0; i < tests.length; i++) {
168: threads[i].start();
169: }
170: for (int i = 0; i < tests.length; i++) {
171: while (!tests[i].done) {
172: try {
173: Thread.sleep(100L);
174: } catch (Exception e) {
175: }
176: }
177: assertTrue(tests[i].reason, tests[i].success);
178: }
179: }
180:
181: class TestThread implements Runnable {
182: public boolean done = false;
183: public boolean success = false;
184: public String reason = null;
185:
186: public void run() {
187: Connection conn = null;
188: PreparedStatement stmt = null;
189: ResultSet rset = null;
190:
191: try {
192: conn = DriverManager
193: .getConnection("jdbc:axiondb:diskdb:testdb2");
194: stmt = conn
195: .prepareStatement("select NUM, STR from FOO");
196: for (int i = 0; i < 20; i++) {
197: rset = stmt.executeQuery();
198: for (int j = 0; j < 30; j++) {
199: if (!rset.next()) {
200: success = false;
201: reason = "Expected 30 rows, only found "
202: + j + " [" + i + "," + j + "]";
203: break;
204: }
205: if (null == rset.getString(2)) {
206: success = false;
207: reason = "Expected non null column found null at "
208: + j + " [" + i + "," + j + "]";
209: break;
210: }
211: }
212: if (rset.next()) {
213: reason = "Expected no more rows [" + i + "]";
214: success = false;
215: break;
216: }
217: rset.close();
218: }
219: success = true;
220: } catch (Exception e) {
221: reason = e.toString();
222: e.printStackTrace();
223: success = false;
224: } finally {
225: try {
226: rset.close();
227: } catch (Exception t) {
228: }
229: try {
230: stmt.close();
231: } catch (Exception t) {
232: }
233: try {
234: conn.close();
235: } catch (Exception t) {
236: }
237: done = true;
238: }
239: }
240: }
241:
242: //-------------------------------------------------------------------- Util
243:
244: private void createTableFoo() throws Exception {
245: _stmt
246: .execute("create table FOO ( NUM integer, STR varchar2(2), NUMTWO integer )");
247: }
248:
249: private void createDelimitedTableFoo() throws Exception {
250: String eol = System.getProperty("line.separator");
251: _stmt
252: .execute("create external table FOO ( NUM integer, STR varchar2(60), "
253: + " NUMTWO integer ) organization(loadtype='delimited' recorddelimiter='"
254: + eol + "')");
255: }
256:
257: private void createIndexOnFoo() throws Exception {
258: _stmt.execute("create index FOO_NUM_NDX on FOO ( NUM )");
259: }
260:
261: private void populateTableFoo() throws Exception {
262: for (int i = 0; i < 30; i++) {
263: _stmt
264: .execute("insert into FOO ( NUM, STR, NUMTWO ) values ( "
265: + i + ", '" + i + "', " + (i / 2) + ")");
266: }
267: }
268:
269: }
|