001: /*
002: * $Id: TestNestedLoopJoinedRowIterator.java,v 1.4 2005/04/08 13:40:42 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.ArrayList;
044: import java.util.List;
045: import java.util.NoSuchElementException;
046:
047: import junit.framework.Test;
048: import junit.framework.TestCase;
049: import junit.framework.TestSuite;
050:
051: import org.axiondb.AxionException;
052: import org.axiondb.Row;
053: import org.axiondb.RowIterator;
054: import org.axiondb.engine.rows.JoinedRow;
055: import org.axiondb.engine.rows.SimpleRow;
056:
057: /**
058: * @version $Revision: 1.4 $ $Date: 2005/04/08 13:40:42 $
059: * @author Rodney Waldhoff
060: * @author Amrish Lal
061: */
062: public class TestNestedLoopJoinedRowIterator extends TestCase {
063:
064: //------------------------------------------------------------ Conventional
065:
066: public TestNestedLoopJoinedRowIterator(String testName) {
067: super (testName);
068: }
069:
070: public static Test suite() {
071: TestSuite suite = new TestSuite(
072: TestNestedLoopJoinedRowIterator.class);
073: return suite;
074: }
075:
076: //--------------------------------------------------------------- Lifecycle
077:
078: private ArrayList _listA = null;
079: private ArrayList _listB = null;
080: private ArrayList _listC = null;
081: private NestedLoopJoinedRowIterator _single = null;
082: private NestedLoopJoinedRowIterator _double = null;
083: private NestedLoopJoinedRowIterator _triple = null;
084:
085: public void setUp() throws Exception {
086:
087: _listA = new ArrayList();
088: for (int i = 0; i < 2; i++) {
089: Row row = new SimpleRow(1);
090: row.set(0, new Integer(i));
091: _listA.add(row);
092: }
093: RowIterator A = new ListRowIterator(_listA);
094:
095: _listB = new ArrayList();
096: for (int i = 0; i < 2; i++) {
097: Row row = new SimpleRow(1);
098: row.set(0, new Integer(i));
099: _listB.add(row);
100: }
101:
102: RowIterator B = new ListRowIterator(_listB);
103:
104: _listC = new ArrayList();
105: {
106: Row row = new SimpleRow(1);
107: row.set(0, "A");
108: _listC.add(row);
109: }
110: {
111: Row row = new SimpleRow(1);
112: row.set(0, "B");
113: _listC.add(row);
114: }
115: {
116: Row row = new SimpleRow(1);
117: row.set(0, "C");
118: _listC.add(row);
119: }
120: RowIterator C = new ListRowIterator(_listC);
121:
122: _single = new NestedLoopJoinedRowIterator(A,
123: new SingleRowIterator(new SimpleRow(0)), 0);
124: _double = new NestedLoopJoinedRowIterator(A, B, 1);
125: _triple = new NestedLoopJoinedRowIterator(_double, C, 1);
126:
127: }
128:
129: public void tearDown() {
130: _listA = null;
131: _listB = null;
132: _listC = null;
133: _single = null;
134: _double = null;
135: _triple = null;
136: }
137:
138: //------------------------------------------------------------------- Tests
139:
140: public void testSingle() throws Exception {
141: nextPrevFromStart(_single);
142: _single.last();
143: prevNextFromEnd(_single);
144: }
145:
146: public void testDouble() throws Exception {
147: nextPrevFromStart(_double);
148: _double.last();
149: prevNextFromEnd(_double);
150: }
151:
152: public void testTriple() throws Exception {
153: nextPrevFromStart(_triple);
154: _triple.last();
155: prevNextFromEnd(_triple);
156: }
157:
158: public void testStickyTail() throws Exception {
159: _triple.last();
160: assertTrue(!_triple.hasNext());
161: try {
162: _triple.next();
163: fail("Expected no such element exception");
164: } catch (NoSuchElementException e) {
165: // ignored
166: }
167: assertTrue(!_triple.hasNext());
168: try {
169: _triple.next();
170: fail("Expected no such element exception");
171: } catch (NoSuchElementException e) {
172: // ignored
173: }
174: assertTrue(!_triple.hasNext());
175: }
176:
177: public void testStickyHead() throws Exception {
178: assertTrue(!_triple.hasPrevious());
179: try {
180: _triple.previous();
181: fail("Expected no such element exception");
182: } catch (NoSuchElementException e) {
183: // ignored
184: }
185: assertTrue(!_triple.hasPrevious());
186: try {
187: _triple.previous();
188: fail("Expected no such element exception");
189: } catch (NoSuchElementException e) {
190: // ignored
191: }
192: assertTrue(!_triple.hasPrevious());
193: }
194:
195: public void testWalkForward() throws Exception {
196: assertTrue(_triple.hasNext());
197: assertTrue(!_triple.hasPrevious());
198: for (int i = 0; i < _listA.size(); i++) {
199: for (int j = 0; j < _listB.size(); j++) {
200: for (int k = 0; k < _listC.size(); k++) {
201: assertTrue(_triple.hasNext());
202: Row row = _triple.next();
203: assertNotNull(row);
204: assertEquals(((Row) _listA.get(i)).get(0), row
205: .get(0));
206: assertEquals(((Row) _listB.get(j)).get(0), row
207: .get(1));
208: assertEquals(((Row) _listC.get(k)).get(0), row
209: .get(2));
210: }
211: }
212: }
213: assertTrue(!_triple.hasNext());
214: assertTrue(_triple.hasPrevious());
215: }
216:
217: public void testWalkForwardBackForward() throws Exception {
218: for (int i = 0; i < _listA.size(); i++) {
219: for (int j = 0; j < _listB.size(); j++) {
220: for (int k = 0; k < _listC.size(); k++) {
221: assertTrue(_triple.hasNext());
222: Row row = _triple.next();
223: assertNotNull(row);
224: assertEquals(((Row) _listA.get(i)).get(0), row
225: .get(0));
226: assertEquals(((Row) _listB.get(j)).get(0), row
227: .get(1));
228: assertEquals(((Row) _listC.get(k)).get(0), row
229: .get(2));
230: }
231: }
232: }
233:
234: assertTrue(!_triple.hasNext());
235:
236: _triple.first();
237:
238: for (int i = 0; i < _listA.size(); i++) {
239: for (int j = 0; j < _listB.size(); j++) {
240: for (int k = 0; k < _listC.size(); k++) {
241: assertTrue(_triple.hasNext());
242: Row row = _triple.next();
243: assertNotNull(row);
244: assertEquals(((Row) _listA.get(i)).get(0), row
245: .get(0));
246: assertEquals(((Row) _listB.get(j)).get(0), row
247: .get(1));
248: assertEquals(((Row) _listC.get(k)).get(0), row
249: .get(2));
250: }
251: }
252: }
253:
254: assertTrue(!_triple.hasNext());
255:
256: }
257:
258: public void testWalkBackward() throws Exception {
259: _triple.last();
260: for (int i = _listA.size() - 1; i >= 0; i--) {
261: for (int j = _listB.size() - 1; j >= 0; j--) {
262: for (int k = _listC.size() - 1; k >= 0; k--) {
263: assertTrue(_triple.hasPrevious());
264: Row row = _triple.previous();
265: assertNotNull(row);
266: assertEquals(((Row) _listA.get(i)).get(0), row
267: .get(0));
268: assertEquals(((Row) _listB.get(j)).get(0), row
269: .get(1));
270: assertEquals(((Row) _listC.get(k)).get(0), row
271: .get(2));
272: }
273: }
274: }
275: }
276:
277: public void testBug() throws Exception {
278: Row brow = new SimpleRow(1);
279: brow.set(0, "X");
280: List b = new ArrayList();
281: b.add(brow);
282:
283: List a = new ArrayList();
284: List join = new ArrayList();
285: for (int i = 0; i < 3; i++) {
286: Row arow = new SimpleRow(1);
287: arow.set(0, new Integer(i));
288: a.add(arow);
289:
290: JoinedRow jrow = new JoinedRow();
291: jrow.addRow(arow);
292: jrow.addRow(brow);
293: join.add(jrow);
294: }
295:
296: ListRowIterator expected = new ListRowIterator(join);
297:
298: RowIterator A = new ListRowIterator(a);
299: RowIterator B = new ListRowIterator(b);
300: NestedLoopJoinedRowIterator testing = new NestedLoopJoinedRowIterator(
301: A, B, 1);
302:
303: assertEquals(expected.next(), testing.next());
304: assertEquals(expected.next(), testing.next());
305: assertEquals(expected.previous(), testing.previous());
306: assertEquals(expected.previous(), testing.previous());
307: }
308:
309: public void testBug2() throws Exception {
310: Row brow = new SimpleRow(1);
311: brow.set(0, "X");
312: List b = new ArrayList();
313: b.add(brow);
314:
315: List a = new ArrayList();
316: List join = new ArrayList();
317: for (int i = 0; i < 3; i++) {
318: Row arow = new SimpleRow(1);
319: arow.set(0, new Integer(i));
320: a.add(arow);
321:
322: JoinedRow jrow = new JoinedRow();
323: jrow.addRow(arow);
324: jrow.addRow(brow);
325: join.add(jrow);
326: }
327:
328: ListRowIterator expected = new ListRowIterator(join);
329: RowIterator A = new ListRowIterator(a);
330: RowIterator B = new ListRowIterator(b);
331: NestedLoopJoinedRowIterator testing = new NestedLoopJoinedRowIterator(
332: A, B, 1);
333:
334: assertEquals(expected.next(), testing.next());
335: assertEquals(expected.next(), testing.next());
336: assertEquals(expected.next(), testing.next());
337: assertEquals(expected.previous(), testing.previous());
338: assertEquals(expected.previous(), testing.previous());
339: assertEquals(expected.next(), testing.next());
340: assertEquals(expected.next(), testing.next());
341: }
342:
343: private void nextPrevFromStart(RowIterator iter) throws Exception {
344: assertTrue(iter.hasNext());
345: assertTrue(!iter.hasPrevious());
346:
347: Row a = iter.next();
348: assertNotNull(a);
349: assertEquals(a, iter.current());
350:
351: assertTrue(iter.hasNext());
352: assertTrue(iter.hasPrevious());
353:
354: Row b = iter.previous();
355: assertNotNull(b);
356: assertEquals(b, iter.current());
357: assertEquals(a, b);
358:
359: assertTrue(iter.hasNext());
360: assertTrue(!iter.hasPrevious());
361: }
362:
363: private void prevNextFromEnd(RowIterator iter) throws Exception {
364: assertTrue(!iter.hasNext());
365: assertTrue(iter.hasPrevious());
366:
367: Row a = iter.previous();
368: assertNotNull(a);
369: assertEquals(a, iter.current());
370:
371: assertTrue(iter.hasNext());
372: assertTrue(iter.hasPrevious());
373:
374: Row b = iter.next();
375: assertNotNull(b);
376: assertEquals(b, iter.current());
377: assertEquals(a, b);
378:
379: assertTrue(!iter.hasNext());
380: assertTrue(iter.hasPrevious());
381: }
382:
383: public void testNegative() throws Exception {
384:
385: RowIterator A = new BaseRowIterator() {
386: public Row current() throws NoSuchElementException {
387: return null;
388: }
389:
390: public int currentIndex() throws NoSuchElementException {
391: return 0;
392: }
393:
394: public boolean hasCurrent() {
395: return false;
396: }
397:
398: public boolean hasNext() {
399: return true;
400: }
401:
402: public boolean hasPrevious() {
403: return false;
404: }
405:
406: public Row next() throws NoSuchElementException,
407: AxionException {
408: throw new AxionException("Test bad next");
409: }
410:
411: public int nextIndex() {
412: return 0;
413: }
414:
415: public Row previous() throws NoSuchElementException,
416: AxionException {
417: return null;
418: }
419:
420: public int previousIndex() {
421: return 0;
422: }
423:
424: public void reset() throws AxionException {
425: }
426: };
427:
428: Row brow = new SimpleRow(1);
429: brow.set(0, "X");
430: List b = new ArrayList();
431: b.add(brow);
432: RowIterator B = new ListRowIterator(b);
433:
434: NestedLoopJoinedRowIterator testing = new NestedLoopJoinedRowIterator(
435: A, B, 1);
436:
437: try {
438: testing.next();
439: fail("Excepted Exception for bad next");
440: } catch (Exception e) {
441: // expected
442: }
443:
444: }
445:
446: }
|