001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.algebra.evaluation.iterator;
007:
008: import info.aduna.iteration.CloseableIteratorIteration;
009:
010: import java.util.ArrayList;
011: import java.util.Arrays;
012: import java.util.Collections;
013: import java.util.Comparator;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Set;
017:
018: import junit.framework.TestCase;
019:
020: import org.openrdf.model.Value;
021: import org.openrdf.query.Binding;
022: import org.openrdf.query.BindingSet;
023: import org.openrdf.query.QueryEvaluationException;
024:
025: /**
026: *
027: * @author james
028: *
029: */
030: public class OrderIteratorTest extends TestCase {
031: class IterationStub
032: extends
033: CloseableIteratorIteration<BindingSet, QueryEvaluationException> {
034: int hasNextCount = 0;
035:
036: int nextCount = 0;
037:
038: int removeCount = 0;
039:
040: @Override
041: public void setIterator(Iterator<? extends BindingSet> iter) {
042: super .setIterator(iter);
043: }
044:
045: @Override
046: public boolean hasNext() {
047: hasNextCount++;
048: return super .hasNext();
049: }
050:
051: @Override
052: public BindingSet next() {
053: nextCount++;
054: return super .next();
055: }
056:
057: @Override
058: public void remove() {
059: removeCount++;
060: }
061: }
062:
063: class SizeComparator implements Comparator<BindingSet> {
064: public int compare(BindingSet o1, BindingSet o2) {
065: return Integer.valueOf(o1.size()).compareTo(
066: Integer.valueOf(o2.size()));
067: }
068: }
069:
070: class BindingSetSize implements BindingSet {
071: private int size;
072:
073: public BindingSetSize(int size) {
074: super ();
075: this .size = size;
076: }
077:
078: public Binding getBinding(String bindingName) {
079: throw new UnsupportedOperationException();
080: }
081:
082: public Set<String> getBindingNames() {
083: throw new UnsupportedOperationException();
084: }
085:
086: public Value getValue(String bindingName) {
087: throw new UnsupportedOperationException();
088: }
089:
090: public boolean hasBinding(String bindingName) {
091: throw new UnsupportedOperationException();
092: }
093:
094: public Iterator<Binding> iterator() {
095: throw new UnsupportedOperationException();
096: }
097:
098: public int size() {
099: return size;
100: }
101:
102: @Override
103: public String toString() {
104: return getClass().getSimpleName() + "#" + size;
105: }
106: }
107:
108: private IterationStub iteration;
109:
110: private OrderIterator order;
111:
112: private List<BindingSet> list;
113:
114: private BindingSet b1 = new BindingSetSize(1);
115:
116: private BindingSet b2 = new BindingSetSize(2);
117:
118: private BindingSet b3 = new BindingSetSize(3);
119:
120: private BindingSet b4 = new BindingSetSize(4);
121:
122: private BindingSet b5 = new BindingSetSize(5);
123:
124: private SizeComparator cmp;
125:
126: public void testFirstHasNext() throws Exception {
127: order.hasNext();
128: assertEquals(list.size() + 1, iteration.hasNextCount);
129: assertEquals(list.size(), iteration.nextCount);
130: assertEquals(0, iteration.removeCount);
131: }
132:
133: public void testHasNext() throws Exception {
134: order.hasNext();
135: order.next();
136: order.hasNext();
137: assertEquals(list.size() + 1, iteration.hasNextCount);
138: assertEquals(list.size(), iteration.nextCount);
139: assertEquals(0, iteration.removeCount);
140: }
141:
142: public void testFirstNext() throws Exception {
143: order.next();
144: assertEquals(list.size() + 1, iteration.hasNextCount);
145: assertEquals(list.size(), iteration.nextCount);
146: assertEquals(0, iteration.removeCount);
147: }
148:
149: public void testNext() throws Exception {
150: order.next();
151: order.next();
152: assertEquals(list.size() + 1, iteration.hasNextCount);
153: assertEquals(list.size(), iteration.nextCount);
154: assertEquals(0, iteration.removeCount);
155: }
156:
157: public void testRemove() throws Exception {
158: try {
159: order.remove();
160: fail();
161: } catch (UnsupportedOperationException e) {
162: }
163:
164: }
165:
166: public void testSorting() throws Exception {
167: List<BindingSet> sorted = new ArrayList<BindingSet>(list);
168: Collections.sort(sorted, cmp);
169: for (BindingSet b : sorted) {
170: assertEquals(b, order.next());
171: }
172: assertFalse(order.hasNext());
173: }
174:
175: @Override
176: protected void setUp() throws Exception {
177: list = Arrays.asList(b3, b5, b2, b1, b4, b2);
178: cmp = new SizeComparator();
179: iteration = new IterationStub();
180: iteration.setIterator(list.iterator());
181: order = new OrderIterator(iteration, cmp);
182: }
183:
184: }
|