001: /*
002: * $Id: BaseBooleanBranchFunctionTest.java,v 1.3 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 org.axiondb.AxionException;
047: import org.axiondb.ColumnIdentifier;
048: import org.axiondb.DataType;
049: import org.axiondb.RowDecorator;
050: import org.axiondb.engine.rows.SimpleRow;
051: import org.axiondb.types.IntegerType;
052:
053: /**
054: * @version $Revision: 1.3 $ $Date: 2005/06/18 01:03:44 $
055: * @author Rodney Waldhoff
056: * @author Jonathan Giron
057: */
058: public abstract class BaseBooleanBranchFunctionTest extends
059: BaseFunctionTest {
060:
061: //------------------------------------------------------------ Conventional
062:
063: public BaseBooleanBranchFunctionTest(String testName) {
064: super (testName);
065: }
066:
067: //--------------------------------------------------------------- Lifecycle
068:
069: //--------------------------------------------------------------- Framework
070:
071: protected RowDecorator setupFunctionArguments(int count,
072: ConcreteFunction function) {
073: return setupFunctionArguments(count, function, null);
074: }
075:
076: protected RowDecorator setupFunctionArguments(int count,
077: ConcreteFunction function, DataType type) {
078: Map map = new HashMap();
079: for (int i = 0; i < count; i++) {
080: ColumnIdentifier sel = new ColumnIdentifier(null,
081: "col" + i, null, type);
082: function.addArgument(sel);
083: map.put(sel, new Integer(i));
084: }
085: RowDecorator dec = new RowDecorator(map);
086: return dec;
087: }
088:
089: protected abstract Boolean evaluate(Boolean[] values);
090:
091: protected abstract Object[][] getTruthTable();
092:
093: protected abstract String getExpressionDisplay(Object[] arguments);
094:
095: private static final Boolean[] EMPTY = new Boolean[0];
096: private static final Boolean[] TRUE = new Boolean[] { Boolean.TRUE };
097: private static final Boolean[] FALSE = new Boolean[] { Boolean.FALSE };
098: private static final Boolean[] TRUE_FALSE = new Boolean[] {
099: Boolean.TRUE, Boolean.FALSE };
100: private static final Boolean[] FALSE_TRUE = new Boolean[] {
101: Boolean.FALSE, Boolean.TRUE };
102: private static final Boolean[] TRUE_TRUE = new Boolean[] {
103: Boolean.TRUE, Boolean.TRUE };
104: private static final Boolean[] FALSE_FALSE = new Boolean[] {
105: Boolean.FALSE, Boolean.FALSE };
106: private static final Boolean[] TRUE_TRUE_FALSE = new Boolean[] {
107: Boolean.TRUE, Boolean.TRUE, Boolean.FALSE };
108: private static final Boolean[] TRUE_TRUE_TRUE = new Boolean[] {
109: Boolean.TRUE, Boolean.TRUE, Boolean.TRUE };
110:
111: //------------------------------------------------------------------- Tests
112:
113: public void testEvaluateNoArg() throws Exception {
114: ConcreteFunction function = makeFunction();
115: RowDecorator row = setupFunctionArguments(0, function);
116: row.setRow(new SimpleRow(EMPTY));
117: assertEquals(evaluate(EMPTY), function.evaluate(row));
118: }
119:
120: public void testEvaluateOneArg() throws Exception {
121: ConcreteFunction function = makeFunction();
122: RowDecorator row = setupFunctionArguments(1, function);
123:
124: row.setRow(new SimpleRow(TRUE));
125: assertEquals(evaluate(TRUE), function.evaluate(row));
126:
127: row.setRow(new SimpleRow(FALSE));
128: assertEquals(evaluate(FALSE), function.evaluate(row));
129: }
130:
131: public void testEvaluateNull() throws Exception {
132: ConcreteFunction function = makeFunction();
133: RowDecorator row = setupFunctionArguments(1, function);
134:
135: row.setRow(new SimpleRow(new Object[] { null }));
136: assertNull(function.evaluate(row));
137: }
138:
139: public void testEvaluateTwoArg() throws Exception {
140: ConcreteFunction function = makeFunction();
141: RowDecorator row = setupFunctionArguments(2, function);
142:
143: row.setRow(new SimpleRow(TRUE_TRUE));
144: assertEquals(evaluate(TRUE_TRUE), function.evaluate(row));
145:
146: row.setRow(new SimpleRow(TRUE_FALSE));
147: assertEquals(evaluate(TRUE_FALSE), function.evaluate(row));
148:
149: row.setRow(new SimpleRow(FALSE_FALSE));
150: assertEquals(evaluate(FALSE_FALSE), function.evaluate(row));
151:
152: row.setRow(new SimpleRow(FALSE_TRUE));
153: assertEquals(evaluate(FALSE_TRUE), function.evaluate(row));
154: }
155:
156: public void testEvaluateThreeArg() throws Exception {
157: ConcreteFunction function = makeFunction();
158: RowDecorator row = setupFunctionArguments(3, function);
159:
160: row.setRow(new SimpleRow(TRUE_TRUE_TRUE));
161: assertEquals(evaluate(TRUE_TRUE_TRUE), function.evaluate(row));
162:
163: row.setRow(new SimpleRow(TRUE_TRUE_FALSE));
164: assertEquals(evaluate(TRUE_TRUE_FALSE), function.evaluate(row));
165: }
166:
167: public void testEvaluateBadArgument() throws Exception {
168: ConcreteFunction function = makeFunction();
169: RowDecorator row = setupFunctionArguments(1, function,
170: new IntegerType());
171:
172: row.setRow(new SimpleRow(new Object[] { new Integer(17) }));
173: try {
174: function.evaluate(row);
175: fail("Expected AxionException");
176: } catch (AxionException e) {
177: // expected
178: }
179: }
180:
181: public void testValidity() throws Exception {
182: ConcreteFunction function = makeFunction();
183: assertTrue(function.isValid());
184: function.addArgument(new ColumnIdentifier("foo"));
185: assertTrue(function.isValid());
186: function.addArgument(new ColumnIdentifier("bar"));
187: assertTrue(function.isValid());
188: }
189:
190: public void testTruthTable() throws Exception {
191: ConcreteFunction function = makeFunction();
192: RowDecorator dec = setupFunctionArguments(2, function);
193:
194: Object[][] truthTable = getTruthTable();
195:
196: for (int i = 0; i < truthTable.length; i++) {
197: Object[] assertionRow = truthTable[i];
198: Object[] rowContents = new Object[assertionRow.length - 1];
199: for (int j = 0; j < rowContents.length; j++) {
200: rowContents[j] = assertionRow[j];
201: }
202:
203: Object expected = assertionRow[assertionRow.length - 1];
204:
205: dec.setRow(new SimpleRow(rowContents));
206: assertEquals("Failed test for <"
207: + getExpressionDisplay(rowContents) + ">",
208: expected, function.evaluate(dec));
209: }
210: }
211: }
|