001: /*
002: * $Id: TestDDL.java,v 1.21 2005/11/03 01:26:57 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 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.math.BigInteger;
044: import java.sql.Connection;
045: import java.sql.DriverManager;
046: import java.sql.ResultSet;
047: import java.sql.SQLException;
048: import java.sql.Statement;
049:
050: import junit.framework.Test;
051: import junit.framework.TestCase;
052: import junit.framework.TestSuite;
053:
054: import org.axiondb.Column;
055: import org.axiondb.Database;
056: import org.axiondb.Sequence;
057: import org.axiondb.Table;
058: import org.axiondb.jdbc.AxionConnection;
059:
060: /**
061: * @version $Revision: 1.21 $ $Date: 2005/11/03 01:26:57 $
062: * @author Rodney Waldhoff
063: */
064: public class TestDDL extends TestCase {
065:
066: private Database _db = null;
067: private Connection _conn = null;
068: private ResultSet _rset = null;
069: private Statement _stmt = null;
070:
071: //------------------------------------------------------------ Conventional
072:
073: public TestDDL(String testName) {
074: super (testName);
075: try {
076: Class.forName("org.axiondb.jdbc.AxionDriver");
077: } catch (Exception e) {
078: throw new RuntimeException(e.toString());
079: }
080: }
081:
082: public static Test suite() {
083: return new TestSuite(TestDDL.class);
084: }
085:
086: //--------------------------------------------------------------- Lifecycle
087:
088: private String _connectString = "jdbc:axiondb:memdb";
089:
090: public void setUp() throws Exception {
091: _conn = DriverManager.getConnection(_connectString);
092: _db = ((AxionConnection) (_conn)).getDatabase();
093: _stmt = _conn.createStatement();
094: }
095:
096: public void tearDown() throws Exception {
097: try {
098: _rset.close();
099: } catch (Exception t) {
100: }
101: try {
102: _stmt.close();
103: } catch (Exception t) {
104: }
105: try {
106: _conn.close();
107: } catch (Exception t) {
108: }
109: _rset = null;
110: _stmt = null;
111: _conn = null;
112: {
113: Connection conn = DriverManager
114: .getConnection(_connectString);
115: Statement stmt = conn.createStatement();
116: stmt.execute("shutdown");
117: stmt.close();
118: conn.close();
119: }
120: }
121:
122: //------------------------------------------------------------------- Tests
123:
124: public void testCreateTable() throws Exception {
125: assertNull(_db.getTable("FOO"));
126: _stmt.execute("create table FOO ( NUM integer )");
127: assertNotNull(_db.getTable("FOO"));
128: assertNull(_db.getTable("BAR"));
129: _stmt
130: .execute("create table BAR ( NUM integer, DESCR varchar(10) )");
131: assertNotNull(_db.getTable("BAR"));
132: }
133:
134: public void testCreateTableIfNotExists() throws Exception {
135: assertNull(_db.getTable("FOO"));
136: _stmt.execute("create table if not exists FOO ( NUM integer )");
137: assertNotNull(_db.getTable("FOO"));
138: _stmt.execute("create table if not exists FOO ( NUM integer )");
139: assertNotNull(_db.getTable("FOO"));
140: }
141:
142: public void testCreateTableWithNoData() throws Exception {
143: assertNull(_db.getTable("FOO"));
144: _stmt.execute("create table FOO ( NUM integer )");
145: assertNotNull(_db.getTable("FOO"));
146: assertNull(_db.getTable("BAR"));
147: _stmt
148: .execute("create table BAR ( NUM integer, DESCR varchar(10) )");
149: assertNotNull(_db.getTable("BAR"));
150: _stmt
151: .execute("create table FOOBAR as select foo.NUM, bar.DESCR from foo , bar WITH NO DATA");
152: }
153:
154: public void testCreateTableWithLiteralDefault() throws Exception {
155: assertNull(_db.getTable("FOO"));
156: _stmt.execute("create table FOO ( NUM int default 1 )");
157: assertNotNull(_db.getTable("FOO"));
158: }
159:
160: public void testCreateTableWithFunctionDefault() throws Exception {
161: assertNull(_db.getTable("FOO"));
162: _stmt
163: .execute("create table FOO ( TS timestamp default now() )");
164: assertNotNull(_db.getTable("FOO"));
165: }
166:
167: public void testCreateTableWithAlwaysGeneratedIdenity()
168: throws Exception {
169: assertNull(_db.getTable("FOO"));
170: _stmt
171: .execute("create table FOO ( NUM int generated always as identity start with 1 increment by 1 maxvalue 1000 minvalue 1 cycle)");
172: assertNotNull(_db.getTable("FOO"));
173: }
174:
175: public void testCreateTableViaDataTypeClassName() throws Exception {
176: assertNull(_db.getTable("FOO"));
177: _stmt
178: .execute("create table FOO ( NUM org.axiondb.types.IntegerType )");
179: assertNotNull(_db.getTable("FOO"));
180: assertNull(_db.getTable("BAR"));
181: _stmt
182: .execute("create table BAR ( NUM org.axiondb.types.IntegerType, "
183: + "DESCR org.axiondb.types.StringType )");
184: assertNotNull(_db.getTable("BAR"));
185: }
186:
187: public void testCantCreateDuplicateTable() throws Exception {
188: assertNull(_db.getTable("FOO"));
189: _stmt.execute("create table FOO ( NUM integer )");
190: assertNotNull(_db.getTable("FOO"));
191: try {
192: _stmt
193: .execute("create table FOO ( NUM integer, DESCR varchar(10) )");
194: fail("Expected SQLException");
195: } catch (SQLException e) {
196: // expected
197: }
198: assertNotNull(_db.getTable("FOO"));
199: }
200:
201: public void testCreateTableWithObjectType() throws Exception {
202: assertNull(_db.getTable("FOO"));
203: _stmt.execute("create table FOO ( X java_object )");
204: assertNotNull(_db.getTable("FOO"));
205: }
206:
207: public void testCantDropNonExistentTable() throws Exception {
208: assertNull(_db.getTable("FOO"));
209: try {
210: _stmt.execute("drop table FOO");
211: fail("Expected SQLException");
212: } catch (SQLException e) {
213: // expected
214: }
215: assertNull(_db.getTable("FOO"));
216: }
217:
218: public void testDropTable() throws Exception {
219: assertNull(_db.getTable("FOO"));
220: _stmt.execute("create table FOO ( NUM integer )");
221: assertNotNull(_db.getTable("FOO"));
222: _stmt.execute("drop table FOO");
223: assertNull(_db.getTable("FOO"));
224: }
225:
226: public void testCreateIndex() throws Exception {
227: assertNull(_db.getTable("FOO"));
228: _stmt
229: .execute("create table FOO ( NUM integer, DESCR varchar(10) )");
230: assertNotNull(_db.getTable("FOO"));
231: _stmt.execute("create index BAR on FOO ( NUM )");
232: Table table = _db.getTable("FOO");
233: Column column = table.getColumn("NUM");
234: assertTrue(null != table.getIndexForColumn(column));
235: }
236:
237: public void testCreateIndexIfNotExists() throws Exception {
238: _stmt.execute("create table FOO ( NUM integer )");
239: assertTrue(!_db.hasIndex("BAR"));
240: _stmt.execute("create index if not exists BAR on FOO ( NUM )");
241: assertTrue(_db.hasIndex("BAR"));
242: _stmt.execute("create index if not exists BAR on FOO ( NUM )");
243: assertTrue(_db.hasIndex("BAR"));
244: }
245:
246: public void testCreateSequenceIfNotExists() throws Exception {
247: assertTrue(!_db.hasSequence("FOO"));
248: _stmt.execute("create sequence if not exists FOO");
249: assertTrue(_db.hasSequence("FOO"));
250: _stmt.execute("create sequence if not exists FOO");
251: assertTrue(_db.hasSequence("FOO"));
252: }
253:
254: public void testCreateAndDropSequence() throws Exception {
255: assertNull("Should not find sequence", _db
256: .getSequence("foo_seq"));
257:
258: _stmt.execute("create sequence FOO_SEQ");
259: assertNotNull("Should find sequence", _db
260: .getSequence("foo_seq"));
261: assertEquals("Should have correct initial value", BigInteger
262: .valueOf(0), _db.getSequence("foo_seq").getValue());
263: _stmt.execute("drop sequence FOO_SEQ");
264: assertNull("Should not find sequence", _db
265: .getSequence("foo_seq"));
266: }
267:
268: public void testCreateAndDropSequenceAsBigInt() throws Exception {
269: assertNull("Should not find sequence", _db
270: .getSequence("foo_seq"));
271:
272: _stmt
273: .execute("create sequence FOO_SEQ as bigint start with 0 increment by 1 "
274: + "maxvalue 10000000000000 minvalue 0 cycle");
275: Sequence seq = _db.getSequence("foo_seq");
276: assertNotNull("Should find sequence", seq);
277: assertEquals("Should have correct initial value", BigInteger
278: .valueOf(0), seq.getValue());
279: assertNull(seq.getCuurentValue());
280: assertEquals("Should have correct next value", new Long(0), seq
281: .evaluate());
282: assertEquals("Should have correct next value", new Long(1), seq
283: .evaluate());
284:
285: _stmt
286: .execute("alter sequence FOO_SEQ restart with 0 increment by -1 "
287: + "maxvalue 1000000000 minvalue 0 cycle");
288:
289: seq = _db.getSequence("foo_seq");
290: assertEquals("Should have correct initial value", BigInteger
291: .valueOf(0), seq.getValue());
292: assertNull(seq.getCuurentValue());
293: assertEquals("Should have correct next value", new Long(0), seq
294: .evaluate());
295: assertEquals("Should have correct next value", new Long(
296: 1000000000), seq.evaluate());
297:
298: _stmt.execute("alter sequence FOO_SEQ no cycle");
299:
300: seq = _db.getSequence("foo_seq");
301: assertEquals("Should have correct initial value", BigInteger
302: .valueOf(999999999), seq.getValue());
303: assertNull(seq.getCuurentValue());
304: assertEquals("Should have correct next value", new Long(
305: 999999999), seq.evaluate());
306: assertEquals("Should have correct next value", new Long(
307: 999999998), seq.evaluate());
308:
309: _stmt.execute("alter sequence FOO_SEQ restart with 100");
310:
311: seq = _db.getSequence("foo_seq");
312: assertEquals("Should have correct initial value", BigInteger
313: .valueOf(100), seq.getValue());
314: assertNull(seq.getCuurentValue());
315: assertEquals("Should have correct next value", new Long(100),
316: seq.evaluate());
317: assertEquals("Should have correct next value", new Long(99),
318: seq.evaluate());
319:
320: _stmt
321: .execute("alter sequence FOO_SEQ restart with 1 increment by -1 "
322: + "maxvalue 1000000000 minvalue 0 NO cycle");
323:
324: seq = _db.getSequence("foo_seq");
325: assertEquals("Should have correct initial value", BigInteger
326: .valueOf(1), seq.getValue());
327: assertNull(seq.getCuurentValue());
328: assertEquals("Should have correct next value", new Long(1), seq
329: .evaluate());
330: try {
331: assertEquals("Should have correct next value", new Long(
332: 1000000000), seq.evaluate());
333: fail("Expected exception");
334: } catch (IllegalStateException ex) {
335: // expected
336: }
337:
338: _stmt.execute("drop sequence FOO_SEQ");
339: assertNull("Should not find sequence", _db
340: .getSequence("foo_seq"));
341:
342: try {
343: _stmt
344: .execute("alter sequence FOO_SEQ restart with 1 increment by -1 "
345: + "maxvalue 1000000000 minvalue 0 NO cycle");
346: fail("Expected exception: sequence does not exist");
347: } catch (Exception e) {
348: // expected
349: }
350: }
351:
352: public void testCreateAndDropSequenceStartWith() throws Exception {
353: assertNull("Should not find sequence", _db
354: .getSequence("foo_seq"));
355: _stmt.execute("create sequence FOO_SEQ start with 23");
356: assertNotNull("Should find sequence", _db
357: .getSequence("foo_seq"));
358: assertEquals("Should have correct initial value", BigInteger
359: .valueOf(23), _db.getSequence("foo_seq").getValue());
360: _stmt.execute("drop sequence FOO_SEQ");
361: assertNull("Should not find sequence", _db
362: .getSequence("foo_seq"));
363: }
364:
365: public void testDropIndex() throws Exception {
366: assertTrue(!_db.hasIndex("FOO_NDX"));
367: _stmt.execute("create table FOO ( id int )");
368: _stmt.execute("create index FOO_NDX on FOO ( id )");
369: assertTrue(_db.hasIndex("FOO_NDX"));
370: _stmt.execute("drop index FOO_NDX");
371: assertTrue(!_db.hasIndex("FOO_NDX"));
372: }
373:
374: public void testDropIndexIfExists() throws Exception {
375: assertTrue(!_db.hasIndex("FOO_NDX"));
376: _stmt.execute("drop index if exists FOO_NDX");
377: assertTrue(!_db.hasIndex("FOO_NDX"));
378: _stmt.execute("create table FOO ( id int )");
379: _stmt.execute("create index FOO_NDX on FOO ( id )");
380: assertTrue(_db.hasIndex("FOO_NDX"));
381: _stmt.execute("drop index if exists FOO_NDX");
382: assertTrue(!_db.hasIndex("FOO_NDX"));
383: _stmt.execute("drop index if exists FOO_NDX");
384: assertTrue(!_db.hasIndex("FOO_NDX"));
385: }
386:
387: public void testDropTableIfExists() throws Exception {
388: assertTrue(!_db.hasTable("FOO"));
389: _stmt.execute("drop table if exists FOO");
390: assertTrue(!_db.hasTable("FOO"));
391: _stmt.execute("create table FOO ( id int )");
392: assertTrue(_db.hasTable("FOO"));
393: _stmt.execute("drop table if exists FOO");
394: assertTrue(!_db.hasTable("FOO"));
395: }
396:
397: public void testDropNonExistentSequence() throws Exception {
398: assertTrue(!_db.hasSequence("FOO"));
399: try {
400: _stmt.execute("drop sequence foo");
401: fail("Expected SQLException");
402: } catch (SQLException e) {
403: // expected
404: }
405: }
406:
407: public void testDropSequenceIfExists() throws Exception {
408: assertTrue(!_db.hasSequence("FOO"));
409: _stmt.execute("drop sequence if exists FOO");
410: assertTrue(!_db.hasSequence("FOO"));
411: _stmt.execute("create sequence FOO");
412: assertTrue(_db.hasSequence("FOO"));
413: _stmt.execute("drop sequence if exists FOO");
414: assertTrue(!_db.hasSequence("FOO"));
415: }
416:
417: }
|