001: /*
002: * $Id: TestModFunction.java,v 1.5 2005/05/10 00:15:20 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2003 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.math.BigDecimal;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: import junit.framework.Test;
048: import junit.framework.TestSuite;
049:
050: import org.axiondb.AxionException;
051: import org.axiondb.ColumnIdentifier;
052: import org.axiondb.DataType;
053: import org.axiondb.RowDecorator;
054: import org.axiondb.engine.rows.SimpleRow;
055: import org.axiondb.types.BigDecimalType;
056: import org.axiondb.types.CharacterType;
057: import org.axiondb.types.FloatType;
058: import org.axiondb.types.IntegerType;
059:
060: /**
061: * @version $Revision: 1.5 $ $Date: 2005/05/10 00:15:20 $
062: * @author Rodney Waldhoff
063: */
064: public class TestModFunction extends BaseFunctionTest {
065:
066: //------------------------------------------------------------ Conventional
067:
068: public TestModFunction(String testName) {
069: super (testName);
070: }
071:
072: public static Test suite() {
073: TestSuite suite = new TestSuite(TestModFunction.class);
074: return suite;
075: }
076:
077: //--------------------------------------------------------------- Lifecycle
078:
079: public void setUp() throws Exception {
080: super .setUp();
081:
082: _sel1 = new ColumnIdentifier("arg1");
083: _sel2 = new ColumnIdentifier("arg2");
084:
085: _function = new ModFunction();
086: _function.addArgument(_sel1);
087: _function.addArgument(_sel2);
088:
089: _selectableToFieldMap = new HashMap(2);
090: _selectableToFieldMap.put(_sel1, new Integer(0));
091: _selectableToFieldMap.put(_sel2, new Integer(1));
092:
093: _rowDec = new RowDecorator(_selectableToFieldMap);
094: }
095:
096: public void tearDown() throws Exception {
097: super .tearDown();
098:
099: _function = null;
100: _sel1 = null;
101: _sel2 = null;
102: _selectableToFieldMap = null;
103: _rowDec = null;
104: }
105:
106: //--------------------------------------------------------------- Framework
107:
108: protected ConcreteFunction makeFunction() {
109: return new ModFunction();
110: }
111:
112: //------------------------------------------------------------------- Tests
113:
114: public void testMkNwInstance() {
115: ModFunction function = new ModFunction();
116: assertTrue(function.makeNewInstance() instanceof ModFunction);
117: assertTrue(function.makeNewInstance() != function
118: .makeNewInstance());
119: }
120:
121: public void testEvalNulls() throws Exception {
122: _sel1.setDataType(new IntegerType());
123: _sel2.setDataType(new IntegerType());
124:
125: _rowDec.setRow(new SimpleRow(new Object[] { null,
126: new Integer(2) }));
127: assertNull(_function.evaluate(_rowDec));
128:
129: _rowDec.setRow(new SimpleRow(new Object[] { new Integer(5),
130: null }));
131: assertNull(_function.evaluate(_rowDec));
132: }
133:
134: public void testEvalIntegers() throws Exception {
135: _sel1.setDataType(new IntegerType());
136: _sel2.setDataType(new IntegerType());
137:
138: _rowDec.setRow(new SimpleRow(new Object[] { new Integer(2),
139: new Integer(5) }));
140: assertEquals(new BigDecimal("2"), _function.evaluate(_rowDec));
141: }
142:
143: public void testEvalInvalidTypes() throws Exception {
144: _sel1.setDataType(new CharacterType());
145: _sel2.setDataType(new IntegerType());
146:
147: try {
148: _rowDec.setRow(new SimpleRow(new Object[] { "string",
149: new Integer(2) }));
150: _function.evaluate(_rowDec);
151: fail("Expected conversion error");
152: } catch (AxionException e) {
153: // expected
154: }
155:
156: _sel1.setDataType(new IntegerType());
157: _sel2.setDataType(new CharacterType());
158:
159: try {
160: _rowDec.setRow(new SimpleRow(new Object[] { new Integer(5),
161: "string" }));
162: _function.evaluate(_rowDec);
163: fail("Expected conversion error");
164: } catch (AxionException e) {
165: // expected
166: }
167: }
168:
169: public void testEvalNumerics() throws Exception {
170: _sel1.setDataType(new BigDecimalType(5, 0));
171: _sel2.setDataType(new BigDecimalType(3, 0));
172:
173: _rowDec.setRow(new SimpleRow(new Object[] {
174: new BigDecimal("10099"), new BigDecimal("100") }));
175: assertEquals(new BigDecimal("99"), _function.evaluate(_rowDec));
176:
177: _sel1.setDataType(new BigDecimalType(8, 0));
178: _sel2.setDataType(new BigDecimalType(4, 0));
179:
180: _rowDec.setRow(new SimpleRow(new Object[] {
181: new BigDecimal("12345678"), new BigDecimal("1234") }));
182: assertEquals(new BigDecimal("742"), _function.evaluate(_rowDec));
183:
184: _sel1.setDataType(new BigDecimalType(8, 0));
185: _sel2.setDataType(new BigDecimalType(7, 0));
186:
187: _rowDec
188: .setRow(new SimpleRow(new Object[] {
189: new BigDecimal("25000000"),
190: new BigDecimal("1234567") }));
191: assertEquals(new BigDecimal("308660"), _function
192: .evaluate(_rowDec));
193:
194: _sel1.setDataType(new BigDecimalType(5, 0));
195: _sel2.setDataType(new BigDecimalType(4, 0));
196:
197: // Tests whether ISO rules for MOD are implemented correctly - sign of result should be
198: // same as that of the dividend.
199: _rowDec.setRow(new SimpleRow(new Object[] {
200: new BigDecimal("26000"), new BigDecimal("2500") }));
201: assertEquals(new BigDecimal("1000"), _function
202: .evaluate(_rowDec));
203:
204: _rowDec.setRow(new SimpleRow(new Object[] {
205: new BigDecimal("-26000"), new BigDecimal("2500") }));
206: assertEquals(new BigDecimal("-1000"), _function
207: .evaluate(_rowDec));
208:
209: _rowDec.setRow(new SimpleRow(new Object[] {
210: new BigDecimal("26000"), new BigDecimal("-2500") }));
211: assertEquals(new BigDecimal("1000"), _function
212: .evaluate(_rowDec));
213: }
214:
215: public void testZeroDivisor() throws Exception {
216: _rowDec.setRow(new SimpleRow(new Object[] { new Integer(5),
217: new Integer(0) }));
218:
219: final String msg = "Expected AxionException (22012) - data exception: division by zero";
220: try {
221: _function.evaluate(_rowDec);
222: fail(msg);
223: } catch (AxionException expected) {
224: assertEquals(msg, "22012", expected.getSQLState());
225: }
226: }
227:
228: public void testIsValid() throws Exception {
229: ModFunction function = new ModFunction();
230: assertFalse(function.isValid());
231:
232: function.addArgument(new ColumnIdentifier("arg1"));
233: assertFalse(function.isValid());
234:
235: function.addArgument(new ColumnIdentifier("arg2"));
236: assertTrue(function.isValid());
237:
238: function.addArgument(new ColumnIdentifier("arg3"));
239: assertFalse(function.isValid());
240: }
241:
242: public void testGetDataType() throws Exception {
243: // Default datatype of ModFunction is BigDecimalType with default precision and scale.
244: // When function is evaluated, its precision changes to match the precision of the
245: // divisor.
246: BigDecimalType defaultType = new BigDecimalType();
247: assertTrue(_function.getDataType() instanceof BigDecimalType);
248: assertEquals(defaultType.getPrecision(), _function
249: .getDataType().getPrecision());
250: assertEquals(defaultType.getScale(), _function.getDataType()
251: .getScale());
252: assertEquals(defaultType.getPrecisionRadix(), _function
253: .getDataType().getPrecisionRadix());
254:
255: _sel1.setDataType(new BigDecimalType(5, 0));
256: _sel2.setDataType(new BigDecimalType(3, 0));
257:
258: _rowDec.setRow(new SimpleRow(new Object[] {
259: new BigDecimal("10099"), new BigDecimal("100") }));
260: _function.evaluate(_rowDec);
261:
262: DataType resultType = _function.getDataType();
263: DataType expectedType = _sel2.getDataType();
264: assertEquals(expectedType.getPrecision(), resultType
265: .getPrecision());
266: assertEquals(expectedType.getScale(), resultType.getScale());
267:
268: _sel2.setDataType(new FloatType());
269: _rowDec.setRow(new SimpleRow(new Object[] {
270: new BigDecimal("10099"), new Float("100.0") }));
271: _function.evaluate(_rowDec);
272:
273: resultType = _function.getDataType();
274: expectedType = new BigDecimalType(_sel2.getDataType()
275: .getPrecision(), 0);
276: assertEquals(expectedType.getPrecision(), resultType
277: .getPrecision());
278: assertEquals(expectedType.getScale(), resultType.getScale());
279: }
280:
281: private ModFunction _function;
282: private ColumnIdentifier _sel1;
283: private ColumnIdentifier _sel2;
284: private Map _selectableToFieldMap;
285: private RowDecorator _rowDec;
286: }
|