001: /*
002: * $Id: TestDiskDatabase.java,v 1.23 2005/12/20 18:32:47 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.engine;
042:
043: import java.io.BufferedInputStream;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.InputStreamReader;
047: import java.io.Reader;
048: import java.io.StringWriter;
049: import java.math.BigInteger;
050: import java.util.ArrayList;
051: import java.util.Iterator;
052: import java.util.List;
053:
054: import junit.framework.Test;
055: import junit.framework.TestSuite;
056:
057: import org.axiondb.AxionException;
058: import org.axiondb.ColumnIdentifier;
059: import org.axiondb.Database;
060: import org.axiondb.Literal;
061: import org.axiondb.Sequence;
062: import org.axiondb.Table;
063: import org.axiondb.TableIdentifier;
064: import org.axiondb.AbstractDatabaseTest;
065: import org.axiondb.engine.commands.CreateTableCommand;
066: import org.axiondb.engine.commands.InsertCommand;
067: import org.axiondb.types.IntegerType;
068: import org.axiondb.types.LOBType;
069:
070: /**
071: * @version $Revision: 1.23 $ $Date: 2005/12/20 18:32:47 $
072: * @author Chuck Burdick
073: */
074: public class TestDiskDatabase extends AbstractDatabaseTest {
075:
076: //------------------------------------------------------------ Conventional
077:
078: public TestDiskDatabase(String testName) {
079: super (testName);
080: }
081:
082: public static void main(String args[]) {
083: String[] testCaseName = { TestDiskDatabase.class.getName() };
084: junit.textui.TestRunner.main(testCaseName);
085: }
086:
087: public static Test suite() {
088: return new TestSuite(TestDiskDatabase.class);
089: }
090:
091: //--------------------------------------------------------------- Lifecycle
092:
093: public void setUp() throws Exception {
094: _dbs = new ArrayList();
095: super .setUp();
096: }
097:
098: public void tearDown() throws Exception {
099: for (Iterator iter = _dbs.iterator(); iter.hasNext();) {
100: DiskDatabase db = (DiskDatabase) (iter.next());
101: db.shutdown();
102: }
103: super .tearDown();
104: deleteFile(_tempdir);
105: }
106:
107: //-------------------------------------------------------------------- Util
108:
109: private List _dbs = null;
110:
111: private File _tempdir = new File(new File("."), "testmove");
112:
113: protected Database createDatabase(String name) throws Exception {
114: DiskDatabase db = new DiskDatabase(name, getDbdir());
115: _dbs.add(db);
116: return db;
117: }
118:
119: //------------------------------------------------------------------- Tests
120:
121: public void testCreateTableGetTable() throws Exception {
122: super .testCreateTableGetTable();
123: assertTrue("Should have a db dir", getDbdir().exists());
124: File tabledir = new File(getDbdir(), "FOO");
125: File meta = new File(tabledir, "FOO.DATA");
126: assertTrue("Should have table directory", tabledir.exists());
127: assertTrue("Should have a meta file", meta.exists());
128: getDb().shutdown();
129:
130: // Recreate from disk
131: Database another = createDatabase(getDb().getName());
132: Table sysCol = another.getTable("AXION_COLUMNS");
133: assertNotNull("Should have system table of columns", sysCol);
134: boolean found = findStringInTable("ID", sysCol, "COLUMN_NAME");
135: assertTrue("Should find entry for column in system table",
136: found);
137: }
138:
139: public void testDropTable() throws Exception {
140: super .testDropTable();
141: File tabledir = new File(getDbdir(), "FOO1");
142: File meta = new File(tabledir, "FOO1.DATA");
143: assertTrue("Should not have table directory", !tabledir
144: .exists());
145: assertTrue("Should not have a meta file", !meta.exists());
146: }
147:
148: public void testCreateDB() throws Exception {
149: DiskDatabase bogus;
150: try {
151: bogus = new DiskDatabase("junk8998", null);
152: fail("Should fail for null dir");
153: } catch (AxionException e) {
154: // expected
155: }
156:
157: bogus = null;
158: File file = new File(new File("."), "junk8998");
159: file.createNewFile();
160: try {
161: bogus = new DiskDatabase("junk8998", file);
162: fail("Should fail for invalid dir");
163: } catch (AxionException e) {
164: // expected
165: } finally {
166: file.delete();
167: }
168:
169: if (bogus != null) {
170: bogus.shutdown();
171: }
172: }
173:
174: public void testSequence() throws Exception {
175: File seq = new File(getDbdir(), getDb().getName().toUpperCase()
176: + ".SEQ");
177: File ver = new File(getDbdir(), getDb().getName().toUpperCase()
178: + ".VER");
179: assertTrue("Should not have sequence file", !seq.exists());
180: getDb().createSequence(new Sequence("SEQ1", 0));
181: assertTrue("Should have sequence file", seq.exists());
182: assertTrue("Should have version file", ver.exists());
183: assertNotNull("Should get sequence", getDb()
184: .getSequence("SEQ1"));
185: getDb().getSequence("seq1").evaluate();
186: getDb().getSequence("seq1").evaluate();
187: assertEquals("Should have correct value",
188: BigInteger.valueOf(2), getDb().getSequence("SEQ1")
189: .getValue());
190: getDb().shutdown();
191:
192: Database another = createDatabase(getDb().getName());
193: assertNotNull("Should get sequence back", another
194: .getSequence("SEQ1"));
195: assertEquals("Should have correct value",
196: BigInteger.valueOf(2), another.getSequence("SEQ1")
197: .getValue());
198: }
199:
200: public void testMetaFile() throws Exception {
201: CreateTableCommand cmd = new CreateTableCommand("LOB");
202: cmd.addColumn("ID", "integer");
203: cmd.addColumn("TEXT2", "clob");
204: cmd.addColumn("TEXT", "clob");
205: cmd.execute(getDb());
206:
207: File tabledir = new File(getDbdir(), "LOB");
208: File meta = new File(tabledir, "LOB.DATA");
209: assertTrue("Should have table directory", tabledir.exists());
210: assertTrue("Should have a meta file", meta.exists());
211:
212: Reader reader = new InputStreamReader(new BufferedInputStream(
213: new FileInputStream(meta)));
214: StringWriter writer = new StringWriter();
215: int next = -1;
216: while ((next = reader.read()) != -1) {
217: writer.write(next);
218: }
219:
220: String metaText = writer.toString();
221:
222: reader.close();
223: writer.close();
224:
225: List columns = new ArrayList(3);
226: columns.add(new ColumnIdentifier("ID"));
227: columns.add(new ColumnIdentifier("TEXT"));
228: columns.add(new ColumnIdentifier("TEXT2"));
229: for (int i = 0; i < 10; i++) {
230: List values = new ArrayList(3);
231: values.add(new Literal(new Integer(i), new IntegerType()));
232: values.add(new Literal(String.valueOf(i), new LOBType()));
233: values.add(new Literal("***" + String.valueOf(i) + "***",
234: new LOBType()));
235: InsertCommand insertCmd = new InsertCommand(
236: new TableIdentifier("LOB"), columns, values);
237: insertCmd.executeUpdate(getDb());
238: }
239:
240: File textLobDir = new File(new File(tabledir, "LOBS"), "TEXT");
241: assertTrue("Should have lobs directory", textLobDir.exists());
242: assertTrue("getPath() should be relative", !textLobDir
243: .getCanonicalPath().equals(textLobDir.getPath()));
244: assertTrue("getPath() should be relative to '.'", textLobDir
245: .getPath().startsWith("."));
246: assertTrue("Should not find relative lob path: " + metaText,
247: metaText.indexOf(textLobDir.getPath()) == -1);
248: assertTrue("Should not find absolute lob path: " + metaText,
249: metaText.indexOf(textLobDir.getCanonicalPath()) == -1);
250:
251: //The following code demonstrates issue 9 (http://axion.tigris.org/issues/show_bug.cgi?id=9), which should now be fixed
252: textLobDir = new File(new File(tabledir, "LOBS"), "TEXT2");
253: assertTrue("Should have lobs directory", textLobDir.exists());
254: assertTrue("getPath() should be relative", !textLobDir
255: .getCanonicalPath().equals(textLobDir.getPath()));
256: assertTrue("getPath() should be relative to '.'", textLobDir
257: .getPath().startsWith("."));
258: assertTrue("Should not find relative lob path: " + metaText,
259: metaText.indexOf(textLobDir.getPath()) == -1);
260: assertTrue("Should not find absolute lob path: " + metaText,
261: metaText.indexOf(textLobDir.getCanonicalPath()) == -1);
262: }
263:
264: public void testMoveDb() throws Exception {
265: testMetaFile();
266: getDb().shutdown();
267:
268: assertTrue("renaming should succeed", getDbdir().renameTo(
269: _tempdir));
270: assertTrue("Old dir shouldn't exist anymore", !getDbdir()
271: .exists());
272:
273: Database db = new DiskDatabase("temp", _tempdir);
274:
275: List columns = new ArrayList(3);
276: columns.add(new ColumnIdentifier("ID"));
277: columns.add(new ColumnIdentifier("TEXT"));
278: columns.add(new ColumnIdentifier("TEXT2"));
279: for (int i = 30; i < 40; i++) {
280: List values = new ArrayList(3);
281: values.add(new Literal(new Integer(i), new IntegerType()));
282: values.add(new Literal(String.valueOf(i), new LOBType()));
283: values.add(new Literal("***" + String.valueOf(i) + "***",
284: new LOBType()));
285: InsertCommand insertCmd = new InsertCommand(
286: new TableIdentifier("LOB"), columns, values);
287: insertCmd.executeUpdate(db);
288: }
289:
290: db.shutdown();
291:
292: if (getDbdir().exists()) {
293: deleteFile(_tempdir);
294: fail("Old dir still shouldn't exist");
295: }
296:
297: deleteFile(_tempdir);
298: }
299: }
|