001: /*
002: * $Id: AbstractComparisonFunctionTest.java,v 1.4 2005/06/18 01:03:44 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2003-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.functions;
042:
043: import java.util.HashMap;
044: import java.util.Map;
045:
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: import org.axiondb.ColumnIdentifier;
050: import org.axiondb.Function;
051: import org.axiondb.RowDecorator;
052: import org.axiondb.engine.rows.SimpleRow;
053:
054: /**
055: * @version $Revision: 1.4 $ $Date: 2005/06/18 01:03:44 $
056: * @author Rodney Waldhoff
057: * @author Jonathan Giron
058: */
059: public abstract class AbstractComparisonFunctionTest extends
060: BaseFunctionTest {
061:
062: //------------------------------------------------------------ Conventional
063:
064: public AbstractComparisonFunctionTest(String testName) {
065: super (testName);
066: }
067:
068: public static Test suite() {
069: TestSuite suite = new TestSuite(
070: AbstractComparisonFunctionTest.class);
071: return suite;
072: }
073:
074: //--------------------------------------------------------------- Lifecycle
075:
076: //--------------------------------------------------------------- Framework
077: protected abstract void checkResult(Object left, Object right,
078: int leftIndex, int rightIndex, boolean result);
079:
080: //------------------------------------------------------------------- Util
081:
082: private void setupFunctionArguments(Function function) {
083: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
084: function.addArgument(sel1);
085: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
086: function.addArgument(sel2);
087: }
088:
089: private RowDecorator setupRowDecorator() {
090: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
091: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
092:
093: Map map = new HashMap();
094: map.put(sel1, new Integer(0));
095: map.put(sel2, new Integer(1));
096: RowDecorator dec = new RowDecorator(map);
097: return dec;
098: }
099:
100: //------------------------------------------------------------------- Tests
101:
102: public final void testEvaluateInts() throws Exception {
103: Function function = makeFunction();
104: setupFunctionArguments(function);
105: RowDecorator dec = setupRowDecorator();
106: Integer[] values = new Integer[] {
107: new Integer(Integer.MIN_VALUE), new Integer(-7),
108: new Integer(-1), new Integer(0), new Integer(1),
109: new Integer(7), new Integer(Integer.MAX_VALUE) };
110: for (int i = 0; i < values.length; i++) {
111: for (int j = 0; j < values.length; j++) {
112: dec.setRow(new SimpleRow(new Object[] { values[i],
113: values[j] }));
114: checkResult(values[i], values[j], i, j,
115: ((Boolean) (function.evaluate(dec)))
116: .booleanValue());
117: }
118: }
119: }
120:
121: public final void testFlip() throws Exception {
122: ComparisonFunction function = (ComparisonFunction) makeFunction();
123: setupFunctionArguments(function);
124: RowDecorator straight = setupRowDecorator();
125:
126: ComparisonFunction flip = function.flip();
127: assertNotNull(flip);
128: RowDecorator flipped = setupRowDecorator();
129:
130: Integer[] values = new Integer[] {
131: new Integer(Integer.MIN_VALUE), new Integer(-7),
132: new Integer(-1), new Integer(0), new Integer(1),
133: new Integer(7), new Integer(Integer.MAX_VALUE) };
134: for (int i = 0; i < values.length; i++) {
135: for (int j = 0; j < values.length; j++) {
136: straight.setRow(new SimpleRow(new Object[] { values[i],
137: values[j] }));
138: flipped.setRow(new SimpleRow(new Object[] { values[j],
139: values[i] }));
140: assertEquals(function.evaluate(straight), flip
141: .evaluate(flipped));
142: }
143: }
144: }
145:
146: public void testValidity() throws Exception {
147: ConcreteFunction function = makeFunction();
148: assertTrue(!function.isValid());
149: function.addArgument(new ColumnIdentifier("foo"));
150: assertTrue(!function.isValid());
151: function.addArgument(new ColumnIdentifier("bar"));
152: assertTrue(function.isValid());
153: function.addArgument(new ColumnIdentifier("xyz"));
154: assertTrue(!function.isValid());
155: }
156:
157: /**
158: * Tests enforcement of ISO/IEC 9075-2:2003 Section 8.2, <comparison predicate>
159: * General Rule 1(a), for values XV and YV represented by value expressions X and Y,
160: * respectively: "If either XV or YV is the null value, then X <comp op> Y is Unknown
161: * (i.e., NULL)."
162: *
163: * @throws Exception if unexpected exception occurs during test evaluation.
164: */
165: public void testNullArguments() throws Exception {
166: ConcreteFunction function = makeFunction();
167: setupFunctionArguments(function);
168:
169: RowDecorator dec = setupRowDecorator();
170: dec
171: .setRow(new SimpleRow(new Object[] { null,
172: new Integer(0) }));
173: assertNull(function.evaluate(dec));
174:
175: dec
176: .setRow(new SimpleRow(new Object[] { new Integer(0),
177: null }));
178: assertNull(function.evaluate(dec));
179:
180: dec.setRow(new SimpleRow(new Object[] { null, null }));
181: assertNull(function.evaluate(dec));
182: }
183: }
|