001: /*
002: * $Id: TestAxionWithIndex.java,v 1.4 2004/04/16 02:39:47 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 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;
042:
043: import java.sql.Connection;
044: import java.sql.DriverManager;
045: import java.sql.PreparedStatement;
046: import java.sql.ResultSet;
047: import java.sql.SQLException;
048: import java.sql.Statement;
049:
050: import org.axiondb.jdbc.AxionDriver;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055: import junit.textui.TestRunner;
056:
057: /**
058: * @author william.lam
059: * @version $Revision: 1.4 $
060: */
061: public class TestAxionWithIndex extends TestCase {
062:
063: private Connection _connection;
064: private String _tableName = "testTable";
065: private Object[] _keys = new Object[4];
066: private String[] _values = new String[4];
067:
068: public static void main(String[] args) {
069: TestRunner.run(suite());
070: }
071:
072: public static Test suite() {
073: return new TestSuite(TestAxionWithIndex.class);
074: }
075:
076: public TestAxionWithIndex(String testName) {
077: super (testName);
078: }
079:
080: public void setUp() throws Exception {
081: super .setUp();
082:
083: // lets class load the driver
084: //Thread.currentThread().getContextClassLoader().loadClass(AxionDriver.class.getName());
085: getClass().getClassLoader().loadClass(
086: AxionDriver.class.getName());
087:
088: _connection = DriverManager.getConnection("jdbc:axiondb:"
089: + _tableName + ":axiondir");
090: //connection = DriverManager.getConnection("jdbc:axiondb:memdb");
091:
092: // create table
093: createTable(_tableName);
094:
095: for (int i = 0; i < _keys.length; i++) {
096: _keys[i] = createKey(i);
097: _values[i] = String.valueOf(i);
098: }
099: }
100:
101: public void tearDown() throws Exception {
102: dropTable(_tableName);
103:
104: _connection.close();
105: }
106:
107: /**
108: * @param tableName
109: */
110: private void dropTable(String tableName) {
111: String sql;
112: Statement statement = null;
113: try {
114: statement = _connection.createStatement();
115:
116: sql = "drop table " + tableName;
117: statement.execute(sql);
118: } catch (SQLException e) {
119: } finally {
120: if (statement != null) {
121:
122: try {
123: statement.close();
124: } catch (Exception e) {
125: // ignore errors
126: }
127: }
128: }
129: }
130:
131: /**
132: * @param tableName
133: */
134: private void createTable(String tableName) {
135: Statement statement = null;
136: String sql;
137: try {
138: sql = "create table " + tableName
139: + "( key_object java_object, entry java_object )";
140:
141: statement = _connection.createStatement();
142: statement.execute(sql);
143:
144: sql = "create index " + tableName + "_pk on " + tableName
145: + " ( key_object )";
146: /*
147: * Create index for this table
148: *
149: */
150: // THE TEST WILL PASS IF TABLE INDEX IS NOT CREATED
151: statement.execute(sql);
152:
153: } catch (SQLException sqle) {
154: System.err.println("Cannot create TABLE!" + sqle);
155: } finally {
156: if (statement != null) {
157:
158: try {
159: statement.close();
160: } catch (Exception e) {
161: // ignore errors
162: }
163: }
164: }
165: }
166:
167: public Object get(Object key) {
168: PreparedStatement statement = null;
169: ResultSet resultSet = null;
170: Object entry = null;
171: String sql = "Select entry From " + _tableName
172: + " Where key_object = ?";
173:
174: try {
175: statement = _connection.prepareStatement(sql);
176: statement.setObject(1, key);
177: resultSet = statement.executeQuery();
178: if (resultSet.next()) {
179: entry = resultSet.getObject(1);
180: }
181: } catch (Exception e) {
182: e.printStackTrace();
183: }
184: return entry;
185: }
186:
187: public boolean put(Object key, Object entry) {
188: PreparedStatement statement = null;
189: String sql = "Insert Into " + _tableName
190: + " (entry, key_object) Values (?, ?)";
191:
192: try {
193: statement = _connection.prepareStatement(sql);
194: statement.setObject(1, entry);
195: statement.setObject(2, key);
196: assertEquals(1, statement.executeUpdate());
197: return true;
198: } catch (Exception e) {
199: e.printStackTrace();
200: }
201: return false;
202: }
203:
204: public void testRun() {
205: assertNotNull("Connection is null", _connection);
206:
207: assertTrue("Failed putting key 0", put(_keys[0], _values[0]));
208: assertTrue("Failed putting key 1", put(_keys[1], _values[1]));
209: assertTrue("Failed putting key 2", put(_keys[2], _values[2]));
210:
211: assertEquals("Invalid Key 0 value", _values[0], get(_keys[0]));
212: assertEquals("Invalid Key 1 value", _values[1], get(_keys[1]));
213: assertEquals("Invalid Key 2 value", _values[2], get(_keys[2]));
214: }
215:
216: /**
217: * Factory method to create a key instance
218: *
219: * @param i
220: * @return
221: */
222: protected Object createKey(int i) {
223: // THIS TEST WILL PASS IF THE KEY IS A String!
224: // return "key" + i;
225:
226: return new Integer(i);
227: }
228: }
|