001: /*
002: * $Id: TestRebindableIndexedRowIterator.java,v 1.3 2005/04/22 21:34:17 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 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.rowiterators;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: import org.axiondb.AxionException;
050: import org.axiondb.BindVariable;
051: import org.axiondb.Column;
052: import org.axiondb.ColumnIdentifier;
053: import org.axiondb.Index;
054: import org.axiondb.Row;
055: import org.axiondb.RowIterator;
056: import org.axiondb.Table;
057: import org.axiondb.engine.indexes.ObjectArrayIndex;
058: import org.axiondb.engine.rows.LazyRow;
059: import org.axiondb.engine.rows.SimpleRow;
060: import org.axiondb.engine.tables.MemoryTable;
061: import org.axiondb.event.RowInsertedEvent;
062: import org.axiondb.functions.ConcreteFunction;
063: import org.axiondb.functions.EqualFunction;
064: import org.axiondb.types.StringType;
065:
066: /**
067: * @version $Revision: 1.3 $ $Date: 2005/04/22 21:34:17 $
068: * @author Ahimanikya Satapathy
069: */
070: public class TestRebindableIndexedRowIterator extends
071: AbstractRowIteratorTest {
072:
073: //------------------------------------------------------------ Conventional
074:
075: public TestRebindableIndexedRowIterator(String testName) {
076: super (testName);
077: }
078:
079: public static Test suite() {
080: TestSuite suite = new TestSuite(
081: TestRebindableIndexedRowIterator.class);
082: return suite;
083: }
084:
085: private Table _table;
086: private Index _index;
087: private ConcreteFunction _where;
088: private BindVariable _bindVar;
089:
090: protected void setUp() throws Exception {
091: Column a = new Column("a", StringType.instance());
092: _table = new MemoryTable("X");
093: _table.addColumn(a);
094: _table.addColumn(new Column("b", StringType.instance()));
095: _index = new ObjectArrayIndex("Y", a, false);
096: {
097: SimpleRow row = new SimpleRow(2);
098: row.set(0, "A");
099: row.set(1, "A");
100: _table.addRow(row);
101: _index.rowInserted(new RowInsertedEvent(_table, null, row));
102: }
103: {
104: SimpleRow row = new SimpleRow(2);
105: row.set(0, "A");
106: row.set(1, "B");
107: _table.addRow(row);
108: _index.rowInserted(new RowInsertedEvent(_table, null, row));
109: }
110: {
111: SimpleRow row = new SimpleRow(2);
112: row.set(0, "B");
113: row.set(1, "A");
114: _table.addRow(row);
115: _index.rowInserted(new RowInsertedEvent(_table, null, row));
116: }
117: {
118: SimpleRow row = new SimpleRow(2);
119: row.set(0, "B");
120: row.set(1, "B");
121: _table.addRow(row);
122: _index.rowInserted(new RowInsertedEvent(_table, null, row));
123: }
124:
125: _where = new EqualFunction();
126: _where.addArgument(new ColumnIdentifier("b"));
127: _bindVar = new BindVariable();
128: _where.addArgument(_bindVar);
129: }
130:
131: protected RowIterator makeRowIterator() {
132: try {
133: _bindVar.setValue("A");
134: RebindableIndexedRowIterator iter = new RebindableIndexedRowIterator(
135: _index, _table, _where, _bindVar);
136: return iter;
137: } catch (AxionException e) {
138: return null;
139: }
140: }
141:
142: protected int getSize() {
143: return 2;
144: }
145:
146: protected List makeRowList() {
147: List list = new ArrayList();
148: {
149: SimpleRow row = new SimpleRow(2);
150: row.set(0, "A");
151: row.set(1, "B");
152: list.add(row);
153: }
154: {
155: SimpleRow row = new SimpleRow(2);
156: row.set(0, "A");
157: row.set(1, "A");
158: list.add(row);
159: }
160: return list;
161: }
162:
163: //------------------------------------------------------------------- Tests
164:
165: public void test() throws Exception {
166: RowIterator iter = makeRowIterator();
167: assertTrue(iter.hasNext());
168: Row row = iter.next();
169: assertNotNull(row);
170: }
171:
172: public void testAddUnsupported() throws Exception {
173: RowIterator rows = makeRowIterator();
174: SimpleRow row = new SimpleRow(2);
175: row.set(0, "A");
176: row.set(1, "A");
177: try {
178: rows.add(row);
179: fail("Expected UnsupportedOperationException");
180: } catch (UnsupportedOperationException e) {
181: // expected
182: }
183: }
184:
185: public void testSetRemove() throws Exception {
186: RowIterator rows = makeRowIterator();
187: rows.next();
188: int nextIndex = rows.nextIndex();
189:
190: SimpleRow row = new SimpleRow(2);
191: row.set(0, "A");
192: row.set(1, "B");
193: rows.set(row);
194:
195: Row expected = new LazyRow(_table, row.getIdentifier());
196: assertEquals(expected, rows.current());
197: rows.remove();
198: assertEquals(nextIndex - 1, rows.nextIndex());
199:
200: try {
201: rows.set(row);
202: fail("Expected IllegalStateException");
203: } catch (IllegalStateException e) {
204:
205: }
206:
207: try {
208: rows.remove();
209: fail("Expected IllegalStateException");
210: } catch (IllegalStateException e) {
211:
212: }
213: }
214:
215: }
|