001: /*
002: * $Id: TestComplexTableView.java,v 1.10 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 junit.framework.Test;
044: import junit.framework.TestCase;
045: import junit.framework.TestSuite;
046:
047: import org.axiondb.AxionException;
048: import org.axiondb.Column;
049: import org.axiondb.ColumnIdentifier;
050: import org.axiondb.Database;
051: import org.axiondb.Row;
052: import org.axiondb.RowIterator;
053: import org.axiondb.Table;
054: import org.axiondb.TableIdentifier;
055: import org.axiondb.engine.MemoryDatabase;
056: import org.axiondb.engine.commands.CreateViewCommand;
057: import org.axiondb.engine.rows.SimpleRow;
058: import org.axiondb.types.CharacterVaryingType;
059: import org.axiondb.types.IntegerType;
060:
061: /**
062: * @version $Revision: 1.10 $ $Date: 2005/12/20 18:32:42 $
063: * @author Ahimanikya Satapathy
064: */
065: public class TestComplexTableView extends TestCase {
066:
067: //------------------------------------------------------------ Conventional
068:
069: public TestComplexTableView(String testName) {
070: super (testName);
071: }
072:
073: public static Test suite() {
074: TestSuite suite = new TestSuite(TestComplexTableView.class);
075: return suite;
076: }
077:
078: //--------------------------------------------------------------- Lifecycle
079:
080: private Database _db = null;
081: protected Table table1 = null;
082: protected Table table2 = null;
083: protected Table table3 = null;
084: protected Table table4 = null;
085: protected Table view = null;
086:
087: protected Table createTable(String name) throws Exception {
088: Table t = new MemoryTable(name);
089: t.addColumn(new Column("ID", new IntegerType()));
090: t.addColumn(new Column("NAME", new CharacterVaryingType(10)));
091: {
092: Row row = new SimpleRow(2);
093: row.set(0, new Integer(1));
094: row.set(1, "one");
095: t.addRow(row);
096: }
097: {
098: Row row = new SimpleRow(2);
099: row.set(0, new Integer(2));
100: row.set(1, "two");
101: t.addRow(row);
102: }
103: return t;
104: }
105:
106: protected Table createView(String name) throws Exception {
107: CreateViewCommand cmd = new CreateViewCommand();
108: cmd.setObjectName(name);
109: cmd.setIfNotExists(true);
110: cmd
111: .setSubQuery("select t1.* from foo1 t1 left outer join foo2 t2 on t1.id = t2.id "
112: + " right outer join foo3 t3 on t2.id = t3.id inner join foo4 t4 on t1.id = t4.id");
113: cmd.execute(_db);
114: return _db.getTable(getViewName());
115: }
116:
117: public void setUp() throws Exception {
118: _db = new MemoryDatabase("testdb");
119: table1 = createTable("FOO1");
120: _db.addTable(table1);
121: table2 = createTable("FOO2");
122: _db.addTable(table2);
123: table3 = createTable("FOO3");
124: _db.addTable(table3);
125: table4 = createTable("FOO4");
126: _db.addTable(table4);
127:
128: view = createView(getViewName());
129: super .setUp();
130: }
131:
132: public void tearDown() throws Exception {
133: table1.shutdown();
134: table1 = null;
135: table2.shutdown();
136: table2 = null;
137: table3.shutdown();
138: table3 = null;
139: table4.shutdown();
140: table4 = null;
141: view.shutdown();
142: view = null;
143: _db.shutdown();
144: super .tearDown();
145: }
146:
147: protected Table getView() {
148: return view;
149: }
150:
151: protected String getViewName() {
152: return "FOOVIEW";
153: }
154:
155: //------------------------------------------------------------------- Tests
156:
157: public void testGetName() throws Exception {
158: assertEquals(getViewName().toUpperCase(), view.getName());
159: }
160:
161: public void testToString() throws Exception {
162: assertNotNull(view.getName());
163: }
164:
165: public void testGetMatchingRowsForNull() throws Exception {
166: RowIterator iter = view.getMatchingRows(null, null, true);
167: assertNotNull(iter);
168: }
169:
170: public void testHasColumn() throws Exception {
171: ColumnIdentifier id = new ColumnIdentifier("FOO");
172: assertTrue("Should not have column", !view.hasColumn(id));
173: try {
174: view.getColumnIndex("FOO");
175: fail("Expected AxionException");
176: } catch (Exception e) {
177: // expected
178: }
179:
180: id = new ColumnIdentifier("ID");
181: assertTrue("Should have column", view.hasColumn(id));
182:
183: id.setTableIdentifier(new TableIdentifier(getViewName()));
184: assertTrue("Should have column", view.hasColumn(id));
185: }
186:
187: public void testGetRowIterator() throws Exception {
188: RowIterator iter = view.getRowIterator(true);
189: assertNotNull(iter);
190: assertTrue(iter.hasNext());
191: assertNotNull(iter.next());
192: assertTrue(iter.hasNext());
193: assertNotNull(iter.next());
194: assertTrue(!iter.hasNext());
195: }
196:
197: public void testGetColumnByIndex() throws Exception {
198: assertEquals("ID", view.getColumn(0).getName());
199: assertEquals("NAME", view.getColumn(1).getName());
200: }
201:
202: public void testGetColumnByIndexBadIndex() throws Exception {
203: try {
204: view.getColumn(-1);
205: fail("Expected IndexOutOfBoundsException");
206: } catch (IndexOutOfBoundsException e) {
207: // expected
208: }
209:
210: try {
211: view.getColumn(2);
212: fail("Expected IndexOutOfBoundsException");
213: } catch (IndexOutOfBoundsException e) {
214: // expected
215: }
216: }
217:
218: public void testGetColumnByName() throws Exception {
219: assertTrue(view.getColumn("ID").getDataType() instanceof IntegerType);
220: assertTrue(view.getColumn("NAME").getDataType() instanceof CharacterVaryingType);
221: }
222:
223: public void testGetColumnByNameBadName() throws Exception {
224: assertNull(table1.getColumn("FOO"));
225: }
226:
227: public void testViewDrop() throws Exception {
228: _db.dropTable(getViewName());
229: try {
230: _db.dropTable(getViewName());
231: fail("Expected Exception");
232: } catch (AxionException e) {
233: // expected
234: }
235: }
236:
237: public void AmbiguousColumnNameTest() throws Exception {
238: CreateViewCommand cmd = new CreateViewCommand();
239: cmd.setObjectName("BADVIEW");
240: cmd
241: .setSubQuery("select * from foo1 t1 left outer join foo2 t2 on t1.id = t2.id "
242: + " right outer join foo3 t3 on t2.id = t3.id inner join t4 on t1.id = t4.id");
243: try {
244: cmd.execute(_db);
245: fail("Expected Exception for AmbiguousColumn");
246: } catch (Exception ex) {
247: // expected
248: }
249: }
250:
251: }
|