001: /*
002: * $Id: TestDivideFunction.java,v 1.6 2005/05/02 22:32:02 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.Function;
053: import org.axiondb.RowDecorator;
054: import org.axiondb.engine.rows.SimpleRow;
055: import org.axiondb.types.BigDecimalType;
056: import org.axiondb.types.FloatType;
057: import org.axiondb.types.IntegerType;
058:
059: /**
060: * @version $Revision: 1.6 $ $Date: 2005/05/02 22:32:02 $
061: * @author Rodney Waldhoff
062: */
063: public class TestDivideFunction extends AbstractArithmeticFunctionTest {
064:
065: //------------------------------------------------------------ Conventional
066:
067: public TestDivideFunction(String testName) {
068: super (testName);
069: }
070:
071: public static Test suite() {
072: TestSuite suite = new TestSuite(TestDivideFunction.class);
073: return suite;
074: }
075:
076: //--------------------------------------------------------------- Lifecycle
077:
078: //--------------------------------------------------------------- Framework
079:
080: protected ConcreteFunction makeFunction() {
081: return new DivideFunction();
082: }
083:
084: //------------------------------------------------------------------- Tests
085:
086: public void testMakeNewInstance() {
087: DivideFunction function = new DivideFunction();
088: assertTrue(function.makeNewInstance() instanceof DivideFunction);
089: assertTrue(function.makeNewInstance() != function
090: .makeNewInstance());
091: }
092:
093: public void testInts() throws Exception {
094: Function function = makeFunction();
095: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
096: function.addArgument(sel1);
097: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
098: function.addArgument(sel2);
099:
100: Map map = new HashMap();
101: map.put(sel1, new Integer(0));
102: map.put(sel2, new Integer(1));
103: RowDecorator dec = new RowDecorator(map);
104:
105: dec.setRow(new SimpleRow(new Object[] { new Integer(14),
106: new Integer(2) }));
107: assertEquals(new BigDecimal("7"), function.evaluate(dec));
108: }
109:
110: public void testLargeInts() throws Exception {
111: Function function = makeFunction();
112: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
113: function.addArgument(sel1);
114: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
115: function.addArgument(sel2);
116:
117: Map map = new HashMap();
118: map.put(sel1, new Integer(0));
119: map.put(sel2, new Integer(1));
120: RowDecorator dec = new RowDecorator(map);
121:
122: dec.setRow(new SimpleRow(new Object[] {
123: new Integer(Integer.MAX_VALUE), new Float(0.5) }));
124: assertEquals(new BigDecimal(((Integer.MAX_VALUE)) * 2L),
125: function.evaluate(dec));
126: }
127:
128: public void testIntAndFloat() throws Exception {
129: Function function = makeFunction();
130: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
131: function.addArgument(sel1);
132: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
133: function.addArgument(sel2);
134:
135: Map map = new HashMap();
136: map.put(sel1, new Integer(0));
137: map.put(sel2, new Integer(1));
138: RowDecorator dec = new RowDecorator(map);
139:
140: dec.setRow(new SimpleRow(new Object[] { new Integer(6),
141: new Float(1.5f) }));
142: assertEquals(new BigDecimal("4"), function.evaluate(dec));
143: }
144:
145: public void testBigDecimalAndInt() throws Exception {
146: Function function = makeFunction();
147: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
148: function.addArgument(sel1);
149: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
150: function.addArgument(sel2);
151:
152: Map map = new HashMap();
153: map.put(sel1, new Integer(0));
154: map.put(sel2, new Integer(1));
155: RowDecorator dec = new RowDecorator(map);
156:
157: dec.setRow(new SimpleRow(new Object[] { new BigDecimal("3.00"),
158: new Integer(4) }));
159: assertEquals(new BigDecimal("0.75"), function.evaluate(dec));
160: }
161:
162: public void testZeroDivisorFails() throws Exception {
163: Function function = makeFunction();
164: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
165: sel1.setDataType(new BigDecimalType());
166:
167: function.addArgument(sel1);
168: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
169: sel2.setDataType(new IntegerType());
170: function.addArgument(sel2);
171:
172: Map map = new HashMap();
173: map.put(sel1, new Integer(0));
174: map.put(sel2, new Integer(1));
175: RowDecorator dec = new RowDecorator(map);
176:
177: String msg = "Expected AxionException (22012) - data exception: divide by zero";
178:
179: dec.setRow(new SimpleRow(new Object[] { new BigDecimal("3"),
180: new Integer(0) }));
181: try {
182: function.evaluate(dec);
183: fail(msg);
184: } catch (AxionException expected) {
185: assertEquals(msg, "22012", expected.getSQLState());
186: }
187:
188: sel2.setDataType(new BigDecimalType());
189: dec.setRow(new SimpleRow(new Object[] { new BigDecimal("3"),
190: new BigDecimal("0.0") }));
191: try {
192: function.evaluate(dec);
193: fail(msg);
194: } catch (AxionException expected) {
195: assertEquals(msg, "22012", expected.getSQLState());
196: }
197:
198: sel2.setDataType(new FloatType());
199: dec.setRow(new SimpleRow(new Object[] { new BigDecimal("3"),
200: new Float("0.0") }));
201: try {
202: function.evaluate(dec);
203: fail(msg);
204: } catch (AxionException expected) {
205: assertEquals(msg, "22012", expected.getSQLState());
206: }
207: }
208: }
|