001: /*
002: * $Id: TestDefrag.java,v 1.10 2005/04/09 02:04:38 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.tools;
042:
043: import java.io.File;
044: import java.sql.Connection;
045: import java.sql.DriverManager;
046: import java.sql.SQLException;
047: import java.sql.Statement;
048:
049: import junit.framework.Test;
050: import junit.framework.TestSuite;
051:
052: import org.apache.commons.collections.Bag;
053: import org.apache.commons.collections.HashBag;
054: import org.axiondb.functional.AbstractFunctionalTest;
055: import org.axiondb.jdbc.AxionConnection;
056:
057: /**
058: * @version $Revision: 1.10 $ $Date: 2005/04/09 02:04:38 $
059: * @author Rodney Waldhoff
060: */
061: public class TestDefrag extends AbstractFunctionalTest {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestDefrag(String testName) {
066: super (testName);
067: }
068:
069: public static Test suite() {
070: return new TestSuite(TestDefrag.class);
071: }
072:
073: //--------------------------------------------------------------- Lifecycle
074:
075: public void setUp() throws Exception {
076: super .setUp();
077: }
078:
079: public void tearDown() throws Exception {
080: super .tearDown();
081: }
082:
083: //-------------------------------------------------------------------------
084:
085: protected String getConnectString() {
086: return "jdbc:axiondb:diskdb:testdb";
087: }
088:
089: protected File getDatabaseDirectory() {
090: return new File("testdb");
091: }
092:
093: //------------------------------------------------------------------- Tests
094:
095: public void testMainWithNoArgs() throws Exception {
096: // could test that we get output to system.out
097: Defrag.main(new String[0]);
098: }
099:
100: public void testMainWithTooManyArgs() throws Exception {
101: // could test that we get output to system.out
102: Defrag.main(new String[2]);
103: }
104:
105: public void testWhenDatabaseIsInUse() throws Exception {
106: // could test that we get output to system.err
107: Defrag.main(new String[] { getDatabaseDirectory()
108: .getCanonicalPath() });
109: }
110:
111: public void testDefragWithNoIndex() throws Exception {
112: dotest(null);
113: }
114:
115: public void testDefragWithArrayIndex() throws Exception {
116: dotest("array");
117: }
118:
119: public void testDefragWithBtreeIndex() throws Exception {
120: dotest("btree");
121: }
122:
123: //------------------------------------------------------------------- Utils
124:
125: private void dotest(String indextype) throws Exception,
126: SQLException {
127: createSchema(indextype);
128: modifyTables();
129: shutdownDatabase();
130: File datafile = new File(
131: new File(getDatabaseDirectory(), "FOO"), "FOO.DATA");
132: long initialsize = datafile.length();
133: Defrag.main(new String[] { getDatabaseDirectory()
134: .getCanonicalPath() });
135: // alternatively, just use:
136: // Defrag.defragDatabase(getDatabaseDirectory().getCanonicalPath());
137: // but we want to excercise the main method in this test
138: long finalsize = datafile.length();
139: assertTrue(finalsize < initialsize);
140: openDatabase();
141: executeQuery();
142: shutdownDatabase();
143: }
144:
145: private void modifyTables() throws SQLException {
146: assertEquals(NUM_ROWS_IN_FOO, _stmt
147: .executeUpdate("update FOO set STR = 'test'"));
148: assertEquals(1, _stmt.executeUpdate("delete FOO where NUM = "
149: + (NUM_ROWS_IN_FOO - 1)));
150: assertEquals(NUM_ROWS_IN_FOO - 1, _stmt
151: .executeUpdate("update FOO set STR = 'xyzzy'"));
152: }
153:
154: private void createSchema(String indextype) throws Exception,
155: SQLException {
156: createTableFoo();
157: if (null != indextype) {
158: _stmt.execute("create unique " + indextype
159: + " index FOO_NUM_NDX on FOO ( NUM )");
160: _stmt.execute("create " + indextype
161: + " index FOO_STR_NDX on FOO ( STR )");
162: }
163: populateTableFoo();
164: }
165:
166: private void executeQuery() throws SQLException {
167: String sql = "select NUM, STR from FOO";
168: _rset = _stmt.executeQuery(sql);
169: assertNotNull("Should have been able to create ResultSet",
170: _rset);
171:
172: // can't assume the order in which rows will be returned
173: // so populate a set and compare 'em
174: Bag expected = new HashBag();
175: Bag found = new HashBag();
176:
177: for (int i = 0; i < (NUM_ROWS_IN_FOO - 1); i++) {
178: assertTrue(
179: "ResultSet should contain more rows [" + i + "]",
180: _rset.next());
181: expected.add(new Integer(i));
182: Integer val = new Integer(_rset.getInt(1));
183: assertTrue("ResultSet shouldn't think value was null",
184: !_rset.wasNull());
185: assertTrue("Shouldn't have seen \"" + val + "\" yet",
186: !found.contains(val));
187: found.add(val);
188: assertEquals("xyzzy", _rset.getString(2));
189: assertTrue(!_rset.wasNull());
190: }
191: assertTrue("ResultSet shouldn't have any more rows", !_rset
192: .next());
193: _rset.close();
194: assertEquals(expected, found);
195: }
196:
197: private void openDatabase() throws SQLException {
198: _conn = (AxionConnection) (DriverManager
199: .getConnection(getConnectString()));
200: _stmt = _conn.createStatement();
201: }
202:
203: private void shutdownDatabase() throws SQLException {
204: try {
205: _stmt.close();
206: } catch (Exception t) {
207: }
208: try {
209: _conn.close();
210: } catch (Exception t) {
211: }
212: _stmt = null;
213: _conn = null;
214: {
215: Connection conn = DriverManager
216: .getConnection(getConnectString());
217: Statement stmt = conn.createStatement();
218: stmt.execute("shutdown");
219: stmt.close();
220: conn.close();
221: }
222: }
223: }
|