001: /*
002: * $Id: TestTableView.java,v 1.13 2005/12/20 18:32:42 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.tables;
042:
043: import java.io.File;
044: import java.util.ArrayList;
045: import java.util.List;
046:
047: import junit.framework.Test;
048: import junit.framework.TestSuite;
049:
050: import org.axiondb.AbstractDbdirTest;
051: import org.axiondb.Column;
052: import org.axiondb.ColumnIdentifier;
053: import org.axiondb.Row;
054: import org.axiondb.RowIterator;
055: import org.axiondb.Table;
056: import org.axiondb.TableIdentifier;
057: import org.axiondb.TransactableTable;
058: import org.axiondb.engine.DiskDatabase;
059: import org.axiondb.engine.TransactableTableImpl;
060: import org.axiondb.engine.commands.CreateViewCommand;
061: import org.axiondb.engine.rows.SimpleRow;
062: import org.axiondb.types.CharacterVaryingType;
063: import org.axiondb.types.IntegerType;
064:
065: /**
066: * @version $Revision: 1.13 $ $Date: 2005/12/20 18:32:42 $
067: * @author Ahimanikya Satapathy
068: */
069: public class TestTableView extends AbstractDbdirTest {
070:
071: //------------------------------------------------------------ Conventional
072:
073: public TestTableView(String testName) {
074: super (testName);
075: }
076:
077: public static Test suite() {
078: TestSuite suite = new TestSuite(TestTableView.class);
079: return suite;
080: }
081:
082: //--------------------------------------------------------------- Lifecycle
083:
084: private DiskDatabase _db = null;
085: protected Table table = null;
086: protected Table view = null;
087:
088: protected Table createTable(String name) throws Exception {
089: Table t = new DiskTable(name, _db);
090: t.addColumn(new Column("ID", new IntegerType()));
091: t.addColumn(new Column("NAME", new CharacterVaryingType(10)));
092: {
093: Row row = new SimpleRow(2);
094: row.set(0, new Integer(1));
095: row.set(1, "one");
096: t.addRow(row);
097: }
098: {
099: Row row = new SimpleRow(2);
100: row.set(0, new Integer(2));
101: row.set(1, "two");
102: t.addRow(row);
103: }
104: return t;
105: }
106:
107: protected Table createView(String name) throws Exception {
108: CreateViewCommand cmd = new CreateViewCommand();
109: cmd.setObjectName(name);
110: cmd.setIfNotExists(true);
111: cmd.setSubQuery("select * from " + getTableName());
112: cmd.execute(_db);
113: return _db.getTable(getViewName());
114: }
115:
116: public void setUp() throws Exception {
117: getDbdir().mkdirs();
118: _db = new DiskDatabase(getDbdir());
119: table = createTable(getTableName());
120: _db.addTable(table);
121: view = createView(getViewName());
122: super .setUp();
123: }
124:
125: public void tearDown() throws Exception {
126: if (table != null) {
127: table.shutdown();
128: table = null;
129: }
130: if (view != null) {
131: view.shutdown();
132: view = null;
133: }
134: _db.shutdown();
135: super .tearDown();
136: }
137:
138: protected Table getView() {
139: return view;
140: }
141:
142: protected Table getTable() {
143: return table;
144: }
145:
146: protected String getTableName() {
147: return "FOO";
148: }
149:
150: protected String getViewName() {
151: return "FOOVIEW";
152: }
153:
154: //------------------------------------------------------------------- Tests
155:
156: public void testGetName() throws Exception {
157: assertEquals(getViewName(), view.getName());
158: }
159:
160: public void testToString() throws Exception {
161: assertNotNull(view.getName());
162: }
163:
164: public void testGetMatchingRowsForNull() throws Exception {
165: RowIterator iter = view.getMatchingRows(null, null, true);
166: assertNotNull(iter);
167: }
168:
169: public void testRowDecorator() throws Exception {
170: assertNotNull(view.makeRowDecorator());
171: assertNotNull(view.makeRowDecorator());
172: assertNotNull(((TableView) view).buildRowDecorator());
173: assertNotNull(((TableView) view)
174: .getColumnIdentifierList(new TableIdentifier("BOGUS")));
175: }
176:
177: public void testGetMatchingRows() throws Exception {
178: List sels = new ArrayList();
179: sels.add(new ColumnIdentifier(new TableIdentifier("FOOVIEW"),
180: "ID"));
181: sels.add(new ColumnIdentifier(new TableIdentifier("FOOVIEW"),
182: "ID"));
183: List vals = new ArrayList();
184: vals.add(new Integer(1));
185: vals.add(new Integer(1));
186:
187: RowIterator iter = view.getMatchingRows(sels, vals, true);
188: assertNotNull(iter);
189: assertTrue(iter.hasNext());
190: Row row = iter.next();
191: assertNotNull(row);
192: assertEquals(new Integer(1), row.get(0));
193: assertEquals("one", row.get(1));
194: assertTrue(!iter.hasNext());
195: }
196:
197: public void testHasColumn() throws Exception {
198: ColumnIdentifier id = new ColumnIdentifier("FOO");
199: assertTrue("Should not have column", !view.hasColumn(id));
200: try {
201: view.getColumnIndex("FOO");
202: fail("Expected AxionException");
203: } catch (Exception e) {
204: // expected
205: }
206:
207: id = new ColumnIdentifier("ID");
208: assertTrue("Should have column", view.hasColumn(id));
209:
210: id.setTableIdentifier(new TableIdentifier(getViewName()));
211: assertTrue("Should have column", view.hasColumn(id));
212:
213: // TODO: This should pass.
214: //id.setTableIdentifier(new TableIdentifier("bogus"));
215: //assertTrue("Should not have column", !view.hasColumn(id));
216: }
217:
218: public void testGetRowIterator() throws Exception {
219: RowIterator iter = view.getRowIterator(true);
220: assertNotNull(iter);
221: assertTrue(iter.hasNext());
222: assertNotNull(iter.next());
223: assertTrue(iter.hasNext());
224: assertNotNull(iter.next());
225: assertTrue(!iter.hasNext());
226: }
227:
228: public void testGetColumnByIndex() throws Exception {
229: assertEquals("ID", view.getColumn(0).getName());
230: assertEquals("NAME", view.getColumn(1).getName());
231: }
232:
233: public void testGetColumnByIndexBadIndex() throws Exception {
234: try {
235: view.getColumn(-1);
236: fail("Expected IndexOutOfBoundsException");
237: } catch (IndexOutOfBoundsException e) {
238: // expected
239: }
240:
241: try {
242: view.getColumn(2);
243: fail("Expected IndexOutOfBoundsException");
244: } catch (IndexOutOfBoundsException e) {
245: // expected
246: }
247: }
248:
249: public void testGetColumnByName() throws Exception {
250: assertTrue(view.getColumn("ID").getDataType() instanceof IntegerType);
251: assertTrue(view.getColumn("NAME").getDataType() instanceof CharacterVaryingType);
252: }
253:
254: public void testGetColumnByNameBadName() throws Exception {
255: assertNull(table.getColumn("FOO"));
256: }
257:
258: public void testViewDrop() throws Exception {
259: File tabledir = new File(getDbdir(), getViewName());
260: File meta = new File(tabledir, getViewName() + ".META");
261: assertTrue("Table directory should exist", tabledir.exists());
262: assertTrue("Meta file should exist", meta.exists());
263:
264: _db.dropTable(getViewName());
265: assertTrue("Meta file should not exist", !meta.exists());
266: assertTrue("Table directory should not exist", !tabledir
267: .exists());
268: }
269:
270: public void testRestartDB() throws Exception {
271: CreateViewCommand cmd = new CreateViewCommand();
272: cmd.setObjectName("ANOTHERVIEW");
273: cmd.setIfNotExists(true);
274: cmd.setSubQuery("select id myid, id+1 myid1, 5 lit from "
275: + getTableName());
276: cmd.execute(_db);
277:
278: table.shutdown();
279: table = null;
280: view.shutdown();
281: view = null;
282:
283: _db.shutdown();
284:
285: _db = new DiskDatabase(getDbdir());
286: assertTrue(_db.hasTable(getViewName()));
287: assertTrue(_db.hasTable(getTableName()));
288: table = _db.getTable(getTableName());
289: view = _db.getTable(getViewName());
290:
291: assertTrue(_db.hasTable("ANOTHERVIEW"));
292:
293: testGetName();
294: testGetRowIterator();
295: }
296:
297: public void testReloadView() throws Exception {
298: view.shutdown();
299: view = null;
300: TableViewFactory factory = new TableViewFactory();
301: view = factory.createTable(_db, getViewName());
302: testGetName();
303: testGetRowIterator();
304: }
305:
306: public void testNotSupported() throws Exception {
307: TableView tv = null;
308: if (view instanceof TransactableTable) {
309: tv = (TableView) ((TransactableTableImpl) view).getTable();
310: } else {
311: tv = (TableView) view;
312: }
313:
314: try {
315: tv.addColumn(new Column("ID", new IntegerType()));
316: fail("Expected UnsupportedOperationException");
317: } catch (UnsupportedOperationException e) {
318: // expected
319: }
320:
321: try {
322: tv.populateIndex(null);
323: fail("Expected UnsupportedOperationException");
324: } catch (UnsupportedOperationException e) {
325: // expected
326: }
327:
328: try {
329: tv.addRow(new SimpleRow(1));
330: fail("Expected UnsupportedOperationException");
331: } catch (UnsupportedOperationException e) {
332: // expected
333: }
334:
335: try {
336: tv.updateRow(null, new SimpleRow(1));
337: fail("Expected UnsupportedOperationException");
338: } catch (UnsupportedOperationException e) {
339: // expected
340: }
341:
342: assertEquals(-1, tv.getNextRowId());
343: tv.freeRowId(0);
344: assertNotNull(tv.toString());
345:
346: try {
347: tv.applyDeletes(null);
348: fail("Expected UnsupportedOperationException");
349: } catch (UnsupportedOperationException e) {
350: // expected
351: }
352:
353: try {
354: tv.applyInserts(null);
355: fail("Expected UnsupportedOperationException");
356: } catch (UnsupportedOperationException e) {
357: // expected
358: }
359:
360: try {
361: tv.applyUpdates(null);
362: fail("Expected UnsupportedOperationException");
363: } catch (UnsupportedOperationException e) {
364: // expected
365: }
366:
367: try {
368: tv.addConstraint(null);
369: fail("Expected UnsupportedOperationException");
370: } catch (UnsupportedOperationException e) {
371: // expected
372: }
373:
374: try {
375: tv.addIndex(null);
376: fail("Expected UnsupportedOperationException");
377: } catch (UnsupportedOperationException e) {
378: // expected
379: }
380:
381: try {
382: tv.removeConstraint(null);
383: fail("Expected UnsupportedOperationException");
384: } catch (UnsupportedOperationException e) {
385: // expected
386: }
387:
388: try {
389: tv.removeIndex(null);
390: fail("Expected UnsupportedOperationException");
391: } catch (UnsupportedOperationException e) {
392: // expected
393: }
394:
395: try {
396: tv.truncate();
397: fail("Expected UnsupportedOperationException");
398: } catch (UnsupportedOperationException e) {
399: // expected
400: }
401:
402: try {
403: tv.getRow(0);
404: fail("Expected UnsupportedOperationException");
405: } catch (UnsupportedOperationException e) {
406: // expected
407: }
408:
409: assertNull(tv.getIndexForColumn(new Column("ID",
410: new IntegerType())));
411: assertFalse(tv.hasIndex(getViewName()));
412: assertFalse(tv.isUniqueConstraintExists(""));
413: assertFalse(tv.isPrimaryKeyConstraintExists(""));
414: tv.remount(null, false);
415:
416: }
417: }
|