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.util;
007:
008: import info.aduna.iteration.CloseableIteration;
009:
010: import java.util.Arrays;
011: import java.util.Iterator;
012:
013: import junit.framework.TestCase;
014:
015: import org.openrdf.model.Value;
016: import org.openrdf.query.BindingSet;
017: import org.openrdf.query.QueryEvaluationException;
018: import org.openrdf.query.algebra.Order;
019: import org.openrdf.query.algebra.OrderElem;
020: import org.openrdf.query.algebra.TupleExpr;
021: import org.openrdf.query.algebra.ValueExpr;
022: import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
023: import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException;
024:
025: /**
026: *
027: * @author james
028: *
029: */
030: public class OrderComparatorTest extends TestCase {
031: class EvaluationStrategyStub implements EvaluationStrategy {
032: public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(
033: TupleExpr expr, BindingSet bindings)
034: throws QueryEvaluationException {
035: throw new UnsupportedOperationException();
036: }
037:
038: public Value evaluate(ValueExpr expr, BindingSet bindings)
039: throws ValueExprEvaluationException,
040: QueryEvaluationException {
041: return null;
042: }
043:
044: public boolean isTrue(ValueExpr expr, BindingSet bindings)
045: throws ValueExprEvaluationException,
046: QueryEvaluationException {
047: throw new UnsupportedOperationException();
048: }
049: }
050:
051: class ComparatorStub extends ValueComparator {
052: Iterator<Integer> iter;
053:
054: public void setIterator(Iterator<Integer> iter) {
055: this .iter = iter;
056: }
057:
058: @Override
059: public int compare(Value o1, Value o2) {
060: return iter.next();
061: }
062: }
063:
064: private EvaluationStrategyStub strategy = new EvaluationStrategyStub();
065:
066: private Order order = new Order();
067:
068: private OrderElem asc = new OrderElem();
069:
070: private OrderElem desc = new OrderElem();
071:
072: private ComparatorStub cmp = new ComparatorStub();
073:
074: private int ZERO = 0;
075:
076: private int POS = 378;
077:
078: private int NEG = -7349;
079:
080: public void testEquals() throws Exception {
081: order.addElement(asc);
082: cmp.setIterator(Arrays.asList(ZERO).iterator());
083: OrderComparator sud = new OrderComparator(strategy, order, cmp);
084: assertTrue(sud.compare(null, null) == 0);
085: }
086:
087: public void testZero() throws Exception {
088: order.addElement(asc);
089: order.addElement(asc);
090: cmp.setIterator(Arrays.asList(ZERO, POS).iterator());
091: OrderComparator sud = new OrderComparator(strategy, order, cmp);
092: assertTrue(sud.compare(null, null) > 0);
093: }
094:
095: public void testTerm() throws Exception {
096: order.addElement(asc);
097: order.addElement(asc);
098: cmp.setIterator(Arrays.asList(POS, NEG).iterator());
099: OrderComparator sud = new OrderComparator(strategy, order, cmp);
100: assertTrue(sud.compare(null, null) > 0);
101: }
102:
103: public void testAscLessThan() throws Exception {
104: order.addElement(asc);
105: cmp.setIterator(Arrays.asList(NEG).iterator());
106: OrderComparator sud = new OrderComparator(strategy, order, cmp);
107: assertTrue(sud.compare(null, null) < 0);
108: }
109:
110: public void testAscGreaterThan() throws Exception {
111: order.addElement(asc);
112: cmp.setIterator(Arrays.asList(POS).iterator());
113: OrderComparator sud = new OrderComparator(strategy, order, cmp);
114: assertTrue(sud.compare(null, null) > 0);
115: }
116:
117: public void testDescLessThan() throws Exception {
118: order.addElement(desc);
119: cmp.setIterator(Arrays.asList(NEG).iterator());
120: OrderComparator sud = new OrderComparator(strategy, order, cmp);
121: assertTrue(sud.compare(null, null) > 0);
122: }
123:
124: public void testDescGreaterThan() throws Exception {
125: order.addElement(desc);
126: cmp.setIterator(Arrays.asList(POS).iterator());
127: OrderComparator sud = new OrderComparator(strategy, order, cmp);
128: assertTrue(sud.compare(null, null) < 0);
129: }
130:
131: @Override
132: protected void setUp() throws Exception {
133: asc.setAscending(true);
134: desc.setAscending(false);
135: }
136: }
|