001: /*
002: * $Id: TestNestedLoopJoinedRowIterator_LeftOuterJoinCase1.java,v 1.4 2005/12/22 09:02:30 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.Map;
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.DataType;
052: import org.axiondb.Row;
053: import org.axiondb.RowDecorator;
054: import org.axiondb.RowIterator;
055: import org.axiondb.Selectable;
056: import org.axiondb.VariableContext;
057: import org.axiondb.engine.rows.SimpleRow;
058: import org.axiondb.types.BooleanType;
059:
060: /**
061: * @version $Revision: 1.4 $ $Date: 2005/12/22 09:02:30 $
062: * @author Chris Johnston
063: */
064: public class TestNestedLoopJoinedRowIterator_LeftOuterJoinCase1 extends
065: TestCase {
066:
067: //------------------------------------------------------------ Conventional
068:
069: public TestNestedLoopJoinedRowIterator_LeftOuterJoinCase1(
070: String testName) {
071: super (testName);
072: }
073:
074: public static Test suite() {
075: TestSuite suite = new TestSuite(
076: TestNestedLoopJoinedRowIterator_LeftOuterJoinCase1.class);
077: return suite;
078: }
079:
080: //--------------------------------------------------------------- Lifecycle
081:
082: private ArrayList _listA = null;
083: private ArrayList _listB = null;
084: private NestedLoopJoinedRowIterator _double = null;
085:
086: public void setUp() throws Exception {
087: Selectable where = new Selectable() {
088:
089: public Object evaluate(RowDecorator drow) {
090: Row row = drow.getRow();
091: boolean result = (((Integer) (row.get(0))).intValue() == ((Integer) (row
092: .get(1))).intValue());
093: return (result ? Boolean.TRUE : Boolean.FALSE);
094: }
095:
096: public DataType getDataType() {
097: return (new BooleanType());
098: }
099:
100: public String getName() {
101: return "";
102: }
103:
104: public String getAlias() {
105: return "";
106: }
107:
108: public String getLabel() {
109: return "";
110: }
111:
112: public void setVariableContext(VariableContext ctx) {
113: }
114: };
115:
116: RowDecorator decorator = new RowDecorator((Map) null);
117:
118: _listA = new ArrayList();
119: for (int i = 0; i < 6; i++) {
120: if (i != 3) {
121: Row row = new SimpleRow(1);
122: row.set(0, new Integer(i));
123: _listA.add(row);
124: }
125: }
126: RowIterator left = new ListRowIterator(_listA);
127:
128: _listB = new ArrayList();
129: for (int i = 0; i < 7; i++) {
130: if ((i != 1) && (i != 5)) {
131: Row row = new SimpleRow(1);
132: row.set(0, new Integer(i));
133: _listB.add(row);
134: }
135: }
136: RowIterator right = new ListRowIterator(_listB);
137:
138: _double = new NestedLoopJoinedRowIterator(left, right, 1, true,
139: false);
140: _double.setJoinCondition(where, decorator);
141: }
142:
143: public void tearDown() {
144: _listA = null;
145: _listB = null;
146: _double = null;
147: }
148:
149: //------------------------------------------------------------------- Tests
150:
151: public void testDouble() throws Exception {
152: nextPrevFromStart(_double);
153: _double.last();
154: prevNextFromEnd(_double);
155: }
156:
157: public void testStickyTail() throws Exception {
158: _double.last();
159: assertTrue(!_double.hasNext());
160: try {
161: _double.next();
162: fail("Expected no such element exception");
163: } catch (NoSuchElementException e) {
164: // ignored
165: }
166: assertTrue(!_double.hasNext());
167: try {
168: _double.next();
169: fail("Expected no such element exception");
170: } catch (NoSuchElementException e) {
171: // ignored
172: }
173: assertTrue(!_double.hasNext());
174: }
175:
176: public void testStickyHead() throws Exception {
177: assertTrue(!_double.hasPrevious());
178: try {
179: _double.previous();
180: fail("Expected no such element exception");
181: } catch (NoSuchElementException e) {
182: // ignored
183: }
184: assertTrue(!_double.hasPrevious());
185: try {
186: _double.previous();
187: fail("Expected no such element exception");
188: } catch (NoSuchElementException e) {
189: // ignored
190: }
191: assertTrue(!_double.hasPrevious());
192: }
193:
194: public void testWalkForward() throws Exception {
195: assertTrue(_double.hasNext());
196: assertTrue(!_double.hasPrevious());
197:
198: for (int i = 0; i < _listA.size(); i++) {
199:
200: assertTrue(_double.hasNext());
201: Row row = _double.next();
202:
203: switch (i) {
204: case 0:
205: case 2:
206: case 3:
207: assertEquals(((Row) _listA.get(i)).get(0), row.get(0));
208: //assertEquals(((Row)_listB.get(i)).get(0),row.get(1));
209: assertEquals(row.get(0), row.get(1));
210: break;
211: case 1:
212: case 4:
213: assertEquals(((Row) _listA.get(i)).get(0), row.get(0));
214: assertTrue(!((Row) _listB.get(i)).get(0).equals(
215: row.get(1)));
216: assertTrue(!row.get(0).equals(row.get(1)));
217: assertEquals(null, row.get(1));
218: break;
219: default:
220: assertTrue(false);
221: }
222: }
223: assertTrue(!_double.hasNext());
224: assertTrue(_double.hasPrevious());
225: }
226:
227: public void testWalkForwardBackForward() throws Exception {
228:
229: _double.first();
230: for (int i = 0; i < _listA.size(); i++) {
231:
232: assertTrue(_double.hasNext());
233: Row row = _double.next();
234:
235: switch (i) {
236: case 0:
237: case 2:
238: case 3:
239: assertEquals(((Row) _listA.get(i)).get(0), row.get(0));
240: //assertEquals(((Row)_listB.get(i)).get(0),row.get(1));
241: assertEquals(row.get(0), row.get(1));
242: break;
243: case 1:
244: case 4:
245: assertEquals(((Row) _listA.get(i)).get(0), row.get(0));
246: assertTrue(!((Row) _listB.get(i)).get(0).equals(
247: row.get(1)));
248: assertTrue(!row.get(0).equals(row.get(1)));
249: assertEquals(null, row.get(1));
250: break;
251: default:
252: assertTrue(false);
253: }
254: }
255:
256: assertTrue(!_double.hasNext());
257: _double.first();
258:
259: for (int i = 0; i < _listA.size(); i++) {
260:
261: assertTrue(_double.hasNext());
262: Row row = _double.next();
263:
264: switch (i) {
265: case 0:
266: case 2:
267: case 3:
268: assertEquals(((Row) _listA.get(i)).get(0), row.get(0));
269: //assertEquals(((Row)_listB.get(i)).get(0),row.get(1));
270: assertEquals(row.get(0), row.get(1));
271: break;
272: case 1:
273: case 4:
274: assertEquals(((Row) _listA.get(i)).get(0), row.get(0));
275: assertTrue(!((Row) _listB.get(i)).get(0).equals(
276: row.get(1)));
277: assertTrue(!row.get(0).equals(row.get(1)));
278: assertEquals(null, row.get(1));
279: break;
280: default:
281: assertTrue(false);
282: }
283: }
284:
285: assertTrue(!_double.hasNext());
286:
287: }
288:
289: public void testWalkBackward() throws Exception {
290:
291: _double.last();
292: for (int i = _listA.size() - 1; i >= 0; i--) {
293:
294: assertTrue(_double.hasPrevious());
295: Row row = _double.previous();
296:
297: switch (i) {
298: case 0:
299: case 2:
300: case 3:
301: assertEquals(((Row) _listA.get(i)).get(0), row.get(0));
302: //assertEquals(((Row)_listB.get(i)).get(0),row.get(1));
303: assertEquals(row.get(0), row.get(1));
304: break;
305: case 1:
306: case 4:
307: assertEquals(((Row) _listA.get(i)).get(0), row.get(0));
308: assertTrue(!((Row) _listB.get(i)).get(0).equals(
309: row.get(1)));
310: assertTrue(!row.get(0).equals(row.get(1)));
311: assertEquals(null, row.get(1));
312: break;
313: default:
314: assertTrue(false);
315: }
316: }
317: }
318:
319: private void nextPrevFromStart(RowIterator iter) throws Exception {
320: assertTrue(iter.hasNext());
321: assertTrue(!iter.hasPrevious());
322:
323: Row a = iter.next();
324: assertNotNull(a);
325: assertEquals(a, iter.current());
326:
327: assertTrue(iter.hasNext());
328: assertTrue(iter.hasPrevious());
329:
330: Row b = iter.previous();
331: assertNotNull(b);
332: assertEquals(b, iter.current());
333: assertEquals(a, b);
334:
335: assertTrue(iter.hasNext());
336: assertTrue(!iter.hasPrevious());
337: }
338:
339: private void prevNextFromEnd(RowIterator iter) throws Exception {
340: assertTrue(!iter.hasNext());
341: assertTrue(iter.hasPrevious());
342:
343: Row a = iter.previous();
344: assertNotNull(a);
345: assertEquals(a, iter.current());
346:
347: assertTrue(iter.hasNext());
348: assertTrue(iter.hasPrevious());
349:
350: Row b = iter.next();
351: assertNotNull(b);
352: assertEquals(b, iter.current());
353: assertEquals(a, b);
354:
355: assertTrue(!iter.hasNext());
356: assertTrue(iter.hasPrevious());
357: }
358:
359: }
|