001: /*
002: * $Id: TestCollatingRowIterator.java,v 1.8 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.ArrayList;
044: import java.util.HashMap;
045: import java.util.List;
046: import java.util.NoSuchElementException;
047:
048: import junit.framework.Test;
049: import junit.framework.TestSuite;
050:
051: import org.axiondb.ColumnIdentifier;
052: import org.axiondb.Row;
053: import org.axiondb.RowComparator;
054: import org.axiondb.RowDecorator;
055: import org.axiondb.RowIterator;
056: import org.axiondb.engine.rows.SimpleRow;
057: import org.axiondb.types.IntegerType;
058:
059: /**
060: * @version $Revision: 1.8 $ $Date: 2005/04/22 21:34:17 $
061: * @author Rodney Waldhoff
062: * @author Ahimanikya Satapathy
063: */
064: public class TestCollatingRowIterator extends AbstractRowIteratorTest {
065:
066: //------------------------------------------------------------ Conventional
067:
068: public TestCollatingRowIterator(String testName) {
069: super (testName);
070: }
071:
072: public static Test suite() {
073: TestSuite suite = new TestSuite(TestCollatingRowIterator.class);
074: return suite;
075: }
076:
077: //---------------------------------------------------------- Abstract Impls
078:
079: public RowIterator makeRowIterator() {
080: List list = makeRowList();
081: ArrayList a = new ArrayList();
082: ArrayList b = new ArrayList();
083: a.add(list.get(0));
084: b.add(list.get(1));
085: a.add(list.get(2));
086: b.add(list.get(3));
087: b.add(list.get(4));
088: a.add(list.get(5));
089: b.add(list.get(6));
090: a.add(list.get(7));
091: b.add(list.get(8));
092: a.add(list.get(9));
093:
094: HashMap map = new HashMap();
095: map.put(new ColumnIdentifier("foo"), new Integer(0));
096: RowDecorator decorator = new RowDecorator(map);
097: RowComparator comparator = new RowComparator(
098: new ColumnIdentifier(null, "foo", null,
099: new IntegerType()), decorator);
100: CollatingRowIterator iterator = new CollatingRowIterator(
101: comparator);
102: iterator.addRowIterator(new ListIteratorRowIterator(a
103: .listIterator()));
104: iterator.addRowIterator(new ListIteratorRowIterator(b
105: .listIterator()));
106: return iterator;
107: }
108:
109: public List makeRowList() {
110: return makeRowList(10);
111: }
112:
113: protected int getSize() {
114: return 10;
115: }
116:
117: private List makeRowList(int size) {
118: ArrayList list = new ArrayList();
119: for (int i = 0; i < size; i++) {
120: Row row = new SimpleRow(1);
121: row.set(0, new Integer(i));
122: list.add(row);
123: }
124: return list;
125: }
126:
127: //------------------------------------------------------------------- Tests
128:
129: public void testRemove() throws Exception {
130: RowIterator iterator = makeRowIterator();
131: for (int i = 0; i < 5; i++) {
132: assertTrue(iterator.hasNext());
133: assertNotNull(iterator.next());
134: }
135:
136: for (int i = 5; i < 10; i++) {
137: assertTrue(iterator.hasNext());
138: assertNotNull(iterator.next());
139: iterator.remove();
140: }
141:
142: List list = makeRowList(5);
143: iterator.reset();
144: for (int i = 0; i < list.size(); i++) {
145: assertTrue(iterator.hasNext());
146: assertEquals(list.get(i), iterator.next());
147: }
148: }
149:
150: public void testSet() throws Exception {
151: RowIterator iterator = makeRowIterator();
152: List list = makeRowList();
153: for (int i = 0; i < list.size(); i++) {
154: assertTrue(iterator.hasNext());
155: assertNotNull(iterator.next());
156: Row row = new SimpleRow(1);
157: row.set(0, new Integer(i + 2));
158: iterator.set(row);
159: }
160:
161: iterator.reset();
162: for (int i = 0; i < list.size(); i++) {
163: assertTrue(iterator.hasNext());
164: Row row = iterator.next();
165: assertEquals(new Integer(i + 2), row.get(0));
166: }
167: }
168:
169: public void testNegative() throws Exception {
170: RowIterator iterator = makeRowIterator();
171: try {
172: Row row = new SimpleRow(1);
173: row.set(0, new Integer(12));
174: iterator.set(row);
175: fail("Expected NoSuchElementException");
176: } catch (NoSuchElementException e) {
177: // expected
178: }
179:
180: try {
181: iterator.remove();
182: fail("Expected NoSuchElementException");
183: } catch (NoSuchElementException e) {
184: // expected
185: }
186:
187: iterator.hasNext();
188: try {
189: ((CollatingRowIterator) iterator).addRowIterator(null);
190: fail("Expected IllegalStateException");
191: } catch (IllegalStateException e) {
192: // expected
193: }
194: }
195: }
|