001: /*
002: * $Id: TestDiskDatabases.java,v 1.6 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.File;
044: import java.io.FileWriter;
045: import java.io.IOException;
046:
047: import junit.framework.Test;
048: import junit.framework.TestCase;
049: import junit.framework.TestSuite;
050:
051: import org.axiondb.AxionException;
052: import org.axiondb.Database;
053: import org.axiondb.RowIterator;
054: import org.axiondb.Table;
055: import org.axiondb.TableIdentifier;
056: import org.axiondb.engine.tables.BaseFlatfileTable;
057:
058: /**
059: * @version $Revision: 1.6 $ $Date: 2005/12/20 18:32:47 $
060: * @author Ahimanikya Satapathy
061: */
062: public class TestDiskDatabases extends TestCase {
063:
064: //------------------------------------------------------------ Conventional
065:
066: public TestDiskDatabases(String testName) {
067: super (testName);
068: }
069:
070: public static void main(String args[]) {
071: String[] testCaseName = { TestDiskDatabases.class.getName() };
072: junit.textui.TestRunner.main(testCaseName);
073: }
074:
075: public static Test suite() {
076: return new TestSuite(TestDiskDatabases.class);
077: }
078:
079: //--------------------------------------------------------------- Lifecycle
080:
081: private File _dbDir = new File(new File("."), "testdb");
082: protected Database _db = null;
083:
084: public void setUp() throws Exception {
085: getDbdir().mkdirs();
086: }
087:
088: public void tearDown() throws Exception {
089: deleteFile(_dbDir);
090: }
091:
092: protected boolean deleteFile(File file) throws Exception {
093: if (!file.exists()) {
094: return true;
095: }
096: if (file.isDirectory()) {
097: File[] files = file.listFiles();
098: for (int i = 0; i < files.length; i++) {
099: deleteFile(files[i]);
100: }
101: }
102: return file.delete();
103: }
104:
105: protected File getDbdir() {
106: return _dbDir;
107: }
108:
109: //-------------------------------------------------------------------- Util
110:
111: public void testStartUpFile() throws Exception {
112: String eol = System.getProperty("line.separator");
113: String startupFileNameKey = "axiondb.database.testdb.runonstartup";
114: String startupFileName = "startup.sql";
115: File startupFile = new File(getDbdir(), startupFileName);
116: System.setProperty(startupFileNameKey, startupFile
117: .getCanonicalPath());
118:
119: File data = new File(getDbdir(), "FFTest.csv");
120: populateDataFile(data);
121: {
122: // Bad startup script
123: FileWriter out = new FileWriter(startupFile);
124: out
125: .write("create table FFTest (id int, name varchar) "
126: + " organization(loadtype='delimited' filename='FFTest.csv' recorddelimiter='"
127: + BaseFlatfileTable.addEscapeSequence(eol)
128: + "')" + eol);
129: out.close();
130:
131: try {
132: _db = Databases.getOrCreateDatabase("testdb",
133: getDbdir());
134: fail("Expected SQLException");
135: } catch (AxionException e) {
136: // expected
137: }
138: }
139:
140: tearDown();
141: setUp();
142: {
143: // No startup script
144: _db = Databases.getOrCreateDatabase("testdb", getDbdir());
145: assertFalse(_db.hasTable(new TableIdentifier("FFTest")));
146: }
147:
148: Databases.forgetDatabase("testdb");
149: _db.shutdown();
150: tearDown();
151: setUp();
152:
153: populateDataFile(data);
154: {
155: FileWriter out = new FileWriter(startupFile);
156: out
157: .write("create external table FFTest (id int, name varchar(5)) "
158: + " organization(loadtype='delimited' filename='FFTest.csv' recorddelimiter='"
159: + BaseFlatfileTable.addEscapeSequence(eol)
160: + "')" + eol);
161: out.close();
162:
163: _db = Databases.getOrCreateDatabase("testdb", getDbdir());
164: assertTrue(_db.hasTable(new TableIdentifier("FFTEST")));
165: Table table = _db.getTable("FFTEST");
166: RowIterator itr = table.getRowIterator(false);
167:
168: int rowCount = 0;
169: while (itr.hasNext()) {
170: itr.next();
171: rowCount++;
172: }
173: assertEquals("Valid row count should have correct value",
174: 7, rowCount);
175: table.drop();
176: }
177:
178: Databases.forgetDatabase("testdb");
179: _db.shutdown();
180:
181: data.delete();
182: startupFile.delete();
183: System.getProperties().remove(startupFileNameKey);
184: }
185:
186: protected void populateDataFile(File data) throws IOException {
187: FileWriter out = new FileWriter(data);
188: String eol = System.getProperty("line.separator");
189:
190: out.write("1,aa" + eol); // 1
191: out.write("2.00,bbb" + eol); // 2
192: out.write("3.00,ccc" + eol); // 3
193: out.write("4.00,ddd" + eol); // 4
194: out.write("" + eol); // skip
195: out.write("we," + eol); // bad 1
196: out.write("7,dfdf" + eol); // 5
197: out.write("7.0f,ccc" + eol); // bad 3 ?
198: out.write("xx,xx" + eol); // bad 4
199: out.write("5,test" + eol); // 6
200: out.write("2004-10-10,hhhh" + eol); // bad 5
201: out.write("" + eol); // skip
202: out.write("3.00,cccdd" + eol); // 7
203: out.write("" + eol); // skip
204: out.close();
205: }
206:
207: }
|