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