001: /*
002: * $Id: TestABSFunction.java,v 1.6 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:
046: import junit.framework.Test;
047: import junit.framework.TestSuite;
048:
049: import org.axiondb.AxionException;
050: import org.axiondb.ColumnIdentifier;
051: import org.axiondb.DataType;
052: import org.axiondb.RowDecorator;
053: import org.axiondb.engine.rows.SimpleRow;
054: import org.axiondb.types.BigDecimalType;
055: import org.axiondb.types.CharacterVaryingType;
056: import org.axiondb.types.FloatType;
057: import org.axiondb.types.IntegerType;
058:
059: /**
060: * @version $Revision: 1.6 $ $Date: 2005/05/10 00:15:20 $
061: * @author Rodney Waldhoff
062: * @author Jonathan Giron
063: */
064: public class TestABSFunction extends BaseFunctionTest {
065:
066: //------------------------------------------------------------ Conventional
067:
068: public TestABSFunction(String testName) {
069: super (testName);
070: }
071:
072: public static Test suite() {
073: TestSuite suite = new TestSuite(TestABSFunction.class);
074: return suite;
075: }
076:
077: public void setUp() throws Exception {
078: super .setUp();
079:
080: _sel = new ColumnIdentifier("arg1");
081:
082: _function = new ABSFunction();
083: _function.addArgument(_sel);
084:
085: _selectableToFieldMap = new HashMap(2);
086: _selectableToFieldMap.put(_sel, new Integer(0));
087:
088: _rowDec = new RowDecorator(_selectableToFieldMap);
089: }
090:
091: public void tearDown() throws Exception {
092: super .tearDown();
093: }
094:
095: //--------------------------------------------------------------- Lifecycle
096:
097: //--------------------------------------------------------------- Framework
098:
099: protected ConcreteFunction makeFunction() {
100: return new ABSFunction();
101: }
102:
103: //------------------------------------------------------------------- Tests
104:
105: public void testMakekNwInstance() {
106: ABSFunction function = new ABSFunction();
107: assertTrue(function.makeNewInstance() instanceof ABSFunction);
108: assertTrue(function.makeNewInstance() != function
109: .makeNewInstance());
110: }
111:
112: public void testFloat() throws Exception {
113: final String[] values = new String[] { "-38.978", "38.978" };
114: final String[] expectedValues = new String[] { "38.978",
115: "38.978" };
116:
117: _sel.setDataType(new FloatType());
118: for (int i = 0; i < values.length; i++) {
119: _rowDec.setRow(new SimpleRow(new Object[] { new Float(
120: values[i]) }));
121: assertEquals(new Float(expectedValues[i]), _function
122: .evaluate(_rowDec));
123: }
124: }
125:
126: public void testInteger() throws Exception {
127: final String[] values = new String[] { "-42", "500" };
128: final String[] expectedValues = new String[] { "42", "500" };
129:
130: _sel.setDataType(new IntegerType());
131: for (int i = 0; i < values.length; i++) {
132: _rowDec.setRow(new SimpleRow(new Object[] { new Integer(
133: values[i]) }));
134: assertEquals(new Integer(expectedValues[i]), _function
135: .evaluate(_rowDec));
136: }
137: }
138:
139: public void testNumeric() throws Exception {
140: final String[] values = new String[] { "-50", "100", "0",
141: "-152.5", "-0.43" };
142: final String[] expectedValues = new String[] { "50.00",
143: "100.00", "0.00", "152.50", "0.43" };
144:
145: _sel.setDataType(new BigDecimalType(5, 2));
146: for (int i = 0; i < values.length; i++) {
147: _rowDec.setRow(new SimpleRow(new Object[] { new BigDecimal(
148: values[i]) }));
149: assertEquals(new BigDecimal(expectedValues[i]), _function
150: .evaluate(_rowDec));
151: }
152: }
153:
154: public void testNullArgument() throws Exception {
155: _sel.setDataType(new IntegerType());
156: _rowDec.setRow(new SimpleRow(new Object[] { null }));
157: assertNull(_function.evaluate(_rowDec));
158: }
159:
160: public void testInvalidArgument() throws Exception {
161: try {
162: _sel.setDataType(new CharacterVaryingType(10));
163: _rowDec.setRow(new SimpleRow(new Object[] { "invalid" }));
164: _function.evaluate(_rowDec);
165: fail("Expected type conversion error");
166: } catch (AxionException e) {
167: // Expected
168: }
169: }
170:
171: public void testIsValid() throws Exception {
172: ABSFunction function = new ABSFunction();
173: assertFalse(function.isValid());
174:
175: function.addArgument(new ColumnIdentifier("arg1"));
176: assertTrue(function.isValid());
177:
178: function.addArgument(new ColumnIdentifier("arg1"));
179: assertFalse(function.isValid());
180: }
181:
182: public void testGetDataType() throws Exception {
183: // Default datatype of ModFunction is BigDecimalType with default precision and scale.
184: // When function is evaluated, its precision changes to match the precision of the
185: // divisor.
186: BigDecimalType defaultType = new BigDecimalType();
187: assertTrue(_function.getDataType() instanceof BigDecimalType);
188: assertEquals(defaultType.getPrecision(), _function
189: .getDataType().getPrecision());
190: assertEquals(defaultType.getScale(), _function.getDataType()
191: .getScale());
192: assertEquals(defaultType.getPrecisionRadix(), _function
193: .getDataType().getPrecisionRadix());
194:
195: _sel.setDataType(new BigDecimalType(5, 0));
196:
197: _rowDec.setRow(new SimpleRow(new Object[] { new BigDecimal(
198: "-10099") }));
199: _function.evaluate(_rowDec);
200:
201: DataType resultType = _function.getDataType();
202: DataType expectedType = _sel.getDataType();
203: assertEquals(expectedType.getPrecision(), resultType
204: .getPrecision());
205: assertEquals(expectedType.getScale(), resultType.getScale());
206: }
207:
208: private ColumnIdentifier _sel;
209: private ABSFunction _function;
210: private HashMap _selectableToFieldMap;
211: private RowDecorator _rowDec;
212: }
|