001: /*
002: * $Id: AbstractRowIteratorTest.java,v 1.5 2005/04/22 21:34:17 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-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.List;
044: import java.util.NoSuchElementException;
045:
046: import junit.framework.TestCase;
047:
048: import org.axiondb.Row;
049: import org.axiondb.RowIterator;
050:
051: /**
052: * @version $Revision: 1.5 $ $Date: 2005/04/22 21:34:17 $
053: * @author Rodney Waldhoff
054: * @author Ahimanikya Satapathy
055: */
056: public abstract class AbstractRowIteratorTest extends TestCase {
057:
058: //------------------------------------------------------------ Conventional
059:
060: public AbstractRowIteratorTest(String testName) {
061: super (testName);
062: }
063:
064: //---------------------------------------------------------------- Abstract
065:
066: protected abstract RowIterator makeRowIterator();
067:
068: protected abstract List makeRowList();
069:
070: protected abstract int getSize();
071:
072: //------------------------------------------------------------------- Tests
073:
074: public void testBeforeIterationBegins() throws Exception {
075: RowIterator iterator = makeRowIterator();
076: assertTrue(!iterator.hasCurrent());
077: assertEquals(-1, iterator.currentIndex());
078: assertEquals(0, iterator.nextIndex());
079: assertEquals(-1, iterator.previousIndex());
080: }
081:
082: public void testPeek() throws Exception {
083: RowIterator iterator = makeRowIterator();
084: List list = makeRowList();
085: for (int i = 0; i < list.size(); i++) {
086: assertEquals("" + i, list.get(i), iterator.peekNext());
087: assertEquals(list.get(i), iterator.next());
088: assertEquals(list.get(i), iterator.peekPrevious());
089: }
090: }
091:
092: public void testNextAfterLast() throws Exception {
093: RowIterator iterator = makeRowIterator();
094: iterator.last();
095: try {
096: iterator.next();
097: fail("Expected NoSuchElementException");
098: } catch (NoSuchElementException e) {
099: // expected
100: }
101: try {
102: iterator.next();
103: fail("Expected NoSuchElementException");
104: } catch (NoSuchElementException e) {
105: // expected
106: }
107: }
108:
109: public void testPreviousBeforeFirst() throws Exception {
110: RowIterator iterator = makeRowIterator();
111: iterator.first();
112: try {
113: iterator.previous();
114: fail("Expected NoSuchElementException");
115: } catch (NoSuchElementException e) {
116: // expected
117: }
118: try {
119: iterator.previous();
120: fail("Expected NoSuchElementException");
121: } catch (NoSuchElementException e) {
122: // expected
123: }
124: }
125:
126: public void testReset() throws Exception {
127: RowIterator iterator = makeRowIterator();
128: List list = makeRowList();
129: for (int j = 0; j < 2; j++) {
130: assertTrue(!iterator.hasCurrent());
131: for (int i = 0; i < list.size(); i++) {
132: assertEquals(i, iterator.nextIndex());
133: assertEquals(i - 1, iterator.previousIndex());
134: assertTrue(iterator.hasNext());
135: assertEquals(list.get(i), iterator.next());
136: assertTrue(iterator.hasCurrent());
137: }
138: iterator.reset();
139: }
140: }
141:
142: public void testForwardBackForward() throws Exception {
143: RowIterator iterator = makeRowIterator();
144: List list = makeRowList();
145: for (int i = 0; i < list.size(); i++) {
146: assertEquals(i, iterator.nextIndex());
147: assertEquals(i - 1, iterator.previousIndex());
148: assertTrue(iterator.hasNext());
149: assertEquals(list.get(i), iterator.next());
150:
151: assertEquals(i, iterator.previousIndex());
152: assertEquals(i + 1, iterator.nextIndex());
153: assertTrue(iterator.hasPrevious());
154: assertEquals(list.get(i), iterator.previous());
155:
156: assertEquals(i, iterator.nextIndex());
157: assertEquals(i - 1, iterator.previousIndex());
158: assertTrue(iterator.hasNext());
159: assertEquals(list.get(i), iterator.next());
160: }
161: }
162:
163: public void testWalkForward() throws Exception {
164: RowIterator iterator = makeRowIterator();
165: List list = makeRowList();
166: for (int i = 0; i < list.size(); i++) {
167: assertEquals(i, iterator.nextIndex());
168: assertEquals(i - 1, iterator.previousIndex());
169: assertTrue(iterator.hasNext());
170: assertEquals(list.get(i), iterator.next());
171: }
172: }
173:
174: public void testBackForwardBack() throws Exception {
175: RowIterator iterator = makeRowIterator();
176: List list = makeRowList();
177: while (iterator.hasNext()) {
178: iterator.next();
179: }
180: for (int i = list.size() - 1; i >= 0; i--) {
181: assertEquals(i + 1, iterator.nextIndex());
182: assertEquals(i, iterator.previousIndex());
183: assertTrue(iterator.hasPrevious());
184: assertEquals(list.get(i), iterator.previous());
185:
186: assertEquals(i, iterator.nextIndex());
187: assertEquals(i - 1, iterator.previousIndex());
188: assertTrue(iterator.hasNext());
189: assertEquals(list.get(i), iterator.next());
190:
191: assertEquals(i + 1, iterator.nextIndex());
192: assertEquals(i, iterator.previousIndex());
193: assertTrue(iterator.hasPrevious());
194: assertEquals(list.get(i), iterator.previous());
195: }
196: }
197:
198: public void testWalkBackward() throws Exception {
199: RowIterator iterator = makeRowIterator();
200: List list = makeRowList();
201: while (iterator.hasNext()) {
202: iterator.next();
203: }
204: for (int i = list.size() - 1; i >= 0; i--) {
205: assertEquals(i + 1, iterator.nextIndex());
206: assertEquals(i, iterator.previousIndex());
207: assertTrue(iterator.hasPrevious());
208: assertEquals(list.get(i), iterator.previous());
209: }
210: }
211:
212: public void testFirst() throws Exception {
213: RowIterator iterator = makeRowIterator();
214: List list = makeRowList();
215: if (list.size() > 0) {
216: // walk forward a bit
217: for (int i = 0; i < list.size() / 2; i++) {
218: assertEquals(i, iterator.nextIndex());
219: assertEquals(i - 1, iterator.previousIndex());
220: assertTrue(iterator.hasNext());
221: assertEquals(list.get(i), iterator.next());
222: }
223:
224: // now jump back to the start
225: assertEquals(list.get(0), iterator.first());
226: assertEquals(0, iterator.nextIndex());
227: assertEquals(-1, iterator.previousIndex());
228: assertTrue(iterator.hasNext());
229: assertTrue(!iterator.hasPrevious());
230: assertTrue(iterator.hasNext());
231: assertEquals(list.get(0), iterator.first());
232: } else {
233: try {
234: iterator.first();
235: fail("Expected NoSuchElementException");
236: } catch (NoSuchElementException e) {
237: // expected
238: }
239: }
240: }
241:
242: public void testLast() throws Exception {
243: RowIterator iterator = makeRowIterator();
244: List list = makeRowList();
245: if (list.size() > 0) {
246: // walk forward a bit
247: for (int i = 0; i < list.size() / 2; i++) {
248: assertEquals(i, iterator.nextIndex());
249: assertEquals(i - 1, iterator.previousIndex());
250: assertTrue(iterator.hasNext());
251: assertEquals(list.get(i), iterator.next());
252: }
253:
254: // now jump to the end
255: assertEquals(list.get(list.size() - 1), iterator.last());
256: assertEquals(list.size(), iterator.nextIndex());
257: assertEquals(list.size() - 1, iterator.previousIndex());
258: assertTrue(iterator.hasPrevious());
259: assertTrue(!iterator.hasNext());
260: assertTrue(iterator.hasPrevious());
261:
262: assertEquals(list.get(list.size() - 1), iterator.last());
263: } else {
264: try {
265: iterator.last();
266: fail("Expected NoSuchElementException");
267: } catch (NoSuchElementException e) {
268: // expected
269: }
270: }
271: }
272:
273: public void testCurrent() throws Exception {
274: RowIterator iterator = makeRowIterator();
275: try {
276: iterator.current();
277: fail("Expected NoSuchElementException");
278: } catch (NoSuchElementException e) {
279: // expected
280: }
281: if (iterator.hasNext()) {
282: Row temp = iterator.next();
283: assertEquals(temp, iterator.current());
284: assertEquals(iterator.current(), iterator.current());
285: if (iterator.hasNext()) {
286: temp = iterator.next();
287: assertEquals(temp, iterator.current());
288: assertEquals(iterator.current(), iterator.current());
289: temp = iterator.previous();
290: assertEquals(temp, iterator.current());
291: assertEquals(iterator.current(), iterator.current());
292: }
293: temp = iterator.previous();
294: assertEquals(temp, iterator.current());
295: assertEquals(iterator.current(), iterator.current());
296: }
297: }
298:
299: public void testForwardFirstPreviousForward() throws Exception {
300: RowIterator iterator = makeRowIterator();
301: List list = makeRowList();
302: if (list.size() > 0) {
303: // walk forward
304: for (int i = 0; i < list.size(); i++) {
305: assertEquals(i, iterator.nextIndex());
306: assertEquals(i - 1, iterator.previousIndex());
307: assertTrue(iterator.hasNext());
308: assertEquals(list.get(i), iterator.next());
309: }
310:
311: // jump to first
312: assertEquals(list.get(0), iterator.first());
313: assertEquals(0, iterator.nextIndex());
314: assertEquals(-1, iterator.previousIndex());
315: assertTrue(!iterator.hasPrevious());
316: assertTrue(iterator.hasNext());
317:
318: // walk forward
319: for (int i = 0; i < list.size(); i++) {
320: assertEquals(i, iterator.nextIndex());
321: assertEquals(i - 1, iterator.previousIndex());
322: assertTrue(iterator.hasNext());
323: assertEquals(list.get(i), iterator.next());
324: }
325: }
326: }
327:
328: public void testPeekNextAfterLast() throws Exception {
329: RowIterator iterator = makeRowIterator();
330: iterator.last();
331: try {
332: iterator.peekNext();
333: fail("Expected NoSuchElementException");
334: } catch (NoSuchElementException e) {
335: // expected
336: }
337: }
338:
339: public void testSize() throws Exception {
340: RowIterator iterator = makeRowIterator();
341: assertNotNull(iterator.toString());
342: assertEquals(getSize(), iterator.size());
343: }
344:
345: public void testPeekPreviousBeforeFirst() throws Exception {
346: RowIterator iterator = makeRowIterator();
347: iterator.first();
348: try {
349: iterator.peekPrevious();
350: fail("Expected NoSuchElementException");
351: } catch (NoSuchElementException e) {
352: // expected
353: }
354: }
355:
356: public void testNextAndPrevious() throws Exception {
357: RowIterator rows = makeRowIterator();
358: rows.first();
359: rows.hasPrevious();
360: rows.hasPrevious();
361: rows.hasPrevious();
362: rows.hasNext();
363: rows.hasNext();
364: rows.hasNext();
365:
366: rows.last();
367: rows.hasNext();
368: rows.hasNext();
369: rows.hasNext();
370: rows.hasPrevious();
371: rows.hasPrevious();
372: rows.hasPrevious();
373: }
374:
375: public void testLazyNextprevious() throws Exception {
376: RowIterator rows = makeRowIterator();
377: rows.next(1);
378: assertEquals(1, rows.nextIndex());
379: assertEquals(0, rows.currentIndex());
380:
381: rows.next(1);
382: assertEquals(2, rows.nextIndex());
383: assertEquals(1, rows.currentIndex());
384:
385: rows.previous(1);
386: assertEquals(1, rows.nextIndex());
387: assertEquals(1, rows.currentIndex());
388:
389: rows.previous(1);
390: assertEquals(0, rows.nextIndex());
391: assertEquals(0, rows.currentIndex());
392: }
393:
394: }
|