001: /*
002: * $Id: TestCastAsFunction.java,v 1.12 2005/12/20 18:32:27 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2004-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.sql.Timestamp;
044: import java.text.SimpleDateFormat;
045: import java.util.HashMap;
046: import java.util.TimeZone;
047:
048: import junit.framework.Test;
049: import junit.framework.TestSuite;
050:
051: import org.axiondb.AxionException;
052: import org.axiondb.ColumnIdentifier;
053: import org.axiondb.Literal;
054: import org.axiondb.RowDecorator;
055: import org.axiondb.engine.rows.SimpleRow;
056: import org.axiondb.types.BooleanType;
057: import org.axiondb.types.CharacterType;
058: import org.axiondb.types.CharacterVaryingType;
059: import org.axiondb.types.FloatType;
060: import org.axiondb.types.IntegerType;
061: import org.axiondb.types.BigIntType;
062: import org.axiondb.types.TimestampType;
063:
064: /**
065: * @author Ritesh Adval
066: * @author Ahimanikya Satapathy
067: * @version $Revision: 1.12 $ $Date: 2005/12/20 18:32:27 $
068: */
069: public class TestCastAsFunction extends BaseFunctionTest {
070: private static final SimpleDateFormat ISO_DATE_FORMAT = new SimpleDateFormat(
071: "yyyy-MM-dd HH:mm:ss.SSS", TimestampType.LOCALE);
072:
073: public TestCastAsFunction(String testName) {
074: super (testName);
075: }
076:
077: public void setUp() throws Exception {
078: super .setUp();
079: TimestampType.setTimeZone("GMT");
080: TimeZone tZone = TimeZone.getTimeZone("GMT");
081: if (tZone != null) {
082: ISO_DATE_FORMAT.setTimeZone(tZone);
083: }
084: }
085:
086: /**
087: * @see org.axiondb.functions.BaseFunctionTest#makeFunction()
088: */
089: protected ConcreteFunction makeFunction() {
090: return new CastAsFunction();
091: }
092:
093: public static Test suite() {
094: TestSuite suite = new TestSuite(TestCastAsFunction.class);
095: return suite;
096: }
097:
098: public void testMakeNewInstance() {
099: assertNotNull((new CastAsFunction()).makeNewInstance());
100: }
101:
102: public void testIsValid() {
103: ConcreteFunction function = makeFunction();
104: assertFalse(function.isValid());
105: function.addArgument(new ColumnIdentifier("COLUMN"));
106: assertFalse(function.isValid());
107: function.addArgument(new Literal("DATATYPE",
108: new TimestampType()));
109: assertTrue(function.isValid());
110: function.addArgument(new ColumnIdentifier("COLUMN"));
111: assertFalse(function.isValid());
112: }
113:
114: public void testCastStringAsDate() throws Exception {
115: CastAsFunction function = new CastAsFunction();
116: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
117: Literal dataType = new Literal("DATATYPE", new TimestampType());
118:
119: function.addArgument(column);
120: function.addArgument(dataType);
121:
122: HashMap map = new HashMap();
123: map.put(column, new Integer(0));
124: map.put(dataType, new Integer(1));
125: RowDecorator dec = new RowDecorator(map);
126:
127: //Nov 30 1973 GMT
128: Timestamp result = new Timestamp(3 * 365 * 24 * 60 * 60
129: * 1000L
130: + //year
131: 1 * 24 * 60 * 60 * 1000L
132: + // leap years '72
133: (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
134: * 24 * 60 * 60 * 1000L // Nov
135: // 30
136: );
137:
138: String toConvert = ISO_DATE_FORMAT.format(result);
139: dec.setRow(new SimpleRow(
140: new Object[] { toConvert, "timestamp" }));
141:
142: assertEquals(result, function.evaluate(dec));
143: }
144:
145: public void testCastStringAsDateFailure() throws Exception {
146: CastAsFunction function = new CastAsFunction();
147: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
148: Literal dataType = new Literal("DATATYPE", new TimestampType());
149:
150: function.addArgument(column);
151: function.addArgument(dataType);
152:
153: HashMap map = new HashMap();
154: map.put(column, new Integer(0));
155: map.put(dataType, new Integer(1));
156: RowDecorator dec = new RowDecorator(map);
157:
158: //Dec 12 1973 GMT
159: Timestamp date = new Timestamp(
160: 3 * 365 * 24
161: * 60
162: * 60
163: * 1000L
164: + //year
165: 1
166: * 24
167: * 60
168: * 60
169: * 1000L
170: + // leap years '72
171: (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30
172: + 31 + 30 + 12) * 24 * 60 * 60 * 1000L // Dec
173: // 12
174: );
175:
176: //Dec 12 1973 00:00:01 GMT
177: Timestamp result = new Timestamp(
178: 3 * 365 * 24
179: * 60
180: * 60
181: * 1000L
182: + //year
183: 1
184: * 24
185: * 60
186: * 60
187: * 1000L
188: + // leap years '72
189: (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30
190: + 31 + 30 + 12) * 24 * 60 * 60 * 1000L + // Dec
191: // 12
192: 1 * 1000L);
193:
194: dec.setRow(new SimpleRow(new Object[] { date.toString(),
195: "timestamp" }));
196: assertNotSame(result, function.evaluate(dec));
197: }
198:
199: public void testCastBigIntAsDate() throws Exception {
200: CastAsFunction function = new CastAsFunction();
201: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
202: Literal dataType = new Literal("DATATYPE", new TimestampType());
203:
204: function.addArgument(column);
205: function.addArgument(dataType);
206:
207: HashMap map = new HashMap();
208: map.put(column, new Integer(0));
209: map.put(dataType, new Integer(1));
210: RowDecorator dec = new RowDecorator(map);
211:
212: //Dec 31 1999 11:43:54.334 GMT
213: Timestamp result = new Timestamp(
214: 29 * 365 * 24
215: * 60
216: * 60
217: * 1000L
218: + //year
219: 7
220: * 24
221: * 60
222: * 60
223: * 1000L
224: + // leap years '72,'76,'80,'84,'88,'92,'96
225: (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30
226: + 31 + 30 + 31) * 24 * 60 * 60 * 1000L + // Dec
227: // 31
228: 11 * 60 * 60 * 1000L + //Hours
229: 43 * 60 * 1000L + //minutes
230: 54 * 1000L + //seconds
231: 334 //millisecond
232: );
233:
234: dec.setRow(new SimpleRow(new Object[] {
235: new Long(result.getTime()), "timestamp" }));
236:
237: assertEquals(result, function.evaluate(dec));
238: }
239:
240: public void testCastDateAsBigInt() throws Exception {
241: CastAsFunction function = new CastAsFunction();
242: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
243: Literal dataType = new Literal("DATATYPE", new BigIntType());
244:
245: function.addArgument(column);
246: function.addArgument(dataType);
247:
248: HashMap map = new HashMap();
249: map.put(column, new Integer(0));
250: map.put(dataType, new Integer(1));
251: RowDecorator dec = new RowDecorator(map);
252:
253: //Dec 31 1999 11:43:54.334 GMT
254: Timestamp date = new Timestamp(
255: 29 * 365 * 24
256: * 60
257: * 60
258: * 1000L
259: + //year
260: 7
261: * 24
262: * 60
263: * 60
264: * 1000L
265: + // leap years '72,'76,'80,'84,'88,'92,'96
266: (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30
267: + 31 + 30 + 31) * 24 * 60 * 60 * 1000L + // Dec
268: // 31
269: 11 * 60 * 60 * 1000L + //Hours
270: 43 * 60 * 1000L + //minutes
271: 54 * 1000L + //seconds
272: 334 //millisecond
273: );
274:
275: dec.setRow(new SimpleRow(new Object[] { date, "bigint" }));
276: Long result = new Long(date.getTime());
277:
278: assertEquals(result, function.evaluate(dec));
279: }
280:
281: public void testCastDateAsString() throws Exception {
282: CastAsFunction function = new CastAsFunction();
283: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
284: Literal dataType = new Literal("DATATYPE",
285: new CharacterVaryingType(21));
286:
287: function.addArgument(column);
288: function.addArgument(dataType);
289:
290: HashMap map = new HashMap();
291: map.put(column, new Integer(0));
292: map.put(dataType, new Integer(1));
293: RowDecorator dec = new RowDecorator(map);
294:
295: //Nov 11 1973 12:11:23 GMT
296: Timestamp date = new Timestamp(3 * 365 * 24 * 60 * 60 * 1000L
297: + //year
298: 1 * 24 * 60 * 60 * 1000L
299: + // leap years '72
300: (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 11)
301: * 24 * 60 * 60 * 1000L + // Nov
302: // 31
303: 12 * 60 * 60 * 1000L + //Hours
304: 11 * 60 * 1000L + //minutes
305: 23 * 1000L //seconds
306: );
307:
308: dec.setRow(new SimpleRow(new Object[] { date, "varchar" }));
309: String result = date.toString();
310: assertEquals(result, function.evaluate(dec));
311: assertTrue(function.getDataType() instanceof CharacterVaryingType);
312: }
313:
314: public void testCastFloatAsInteger() throws Exception {
315: CastAsFunction function = new CastAsFunction();
316: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
317: Literal dataType = new Literal("DATATYPE", new IntegerType());
318:
319: function.addArgument(column);
320: function.addArgument(dataType);
321:
322: HashMap map = new HashMap();
323: map.put(column, new Integer(0));
324: map.put(dataType, new Integer(1));
325: RowDecorator dec = new RowDecorator(map);
326:
327: dec.setRow(new SimpleRow(new Object[] { new Float(125.7),
328: "integer" }));
329: Integer result = new Integer(125);
330: assertEquals(result, function.evaluate(dec));
331: assertTrue(function.getDataType() instanceof IntegerType);
332: }
333:
334: public void testCastIntegerAsFloat() throws Exception {
335: CastAsFunction function = new CastAsFunction();
336: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
337: Literal dataType = new Literal("DATATYPE", new FloatType());
338:
339: function.addArgument(column);
340: function.addArgument(dataType);
341:
342: HashMap map = new HashMap();
343: map.put(column, new Integer(0));
344: map.put(dataType, new Integer(1));
345: RowDecorator dec = new RowDecorator(map);
346:
347: dec.setRow(new SimpleRow(new Object[] { new Integer(125),
348: "float" }));
349: Float result = new Float(125.0);
350: assertEquals(result, function.evaluate(dec));
351: }
352:
353: public void testInvalidCast() throws Exception {
354: CastAsFunction function = new CastAsFunction();
355: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
356: Literal dataType = new Literal("DATATYPE", new FloatType());
357:
358: function.addArgument(column);
359: function.addArgument(dataType);
360:
361: HashMap map = new HashMap();
362: map.put(column, new Integer(0));
363: map.put(dataType, new Integer(1));
364: RowDecorator dec = new RowDecorator(map);
365:
366: dec.setRow(new SimpleRow(new Object[] { null, "float" }));
367: assertNull(function.evaluate(dec));
368:
369: try {
370: dec
371: .setRow(new SimpleRow(new Object[] { "string",
372: "float" }));
373: function.evaluate(dec);
374: fail("Expected Conversion exception");
375: } catch (AxionException e) {
376: // expected
377: }
378: }
379:
380: public void testCastNull() throws Exception {
381: CastAsFunction function = new CastAsFunction();
382: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
383: Literal dataType = new Literal("DATATYPE", new IntegerType());
384:
385: function.addArgument(column);
386: function.addArgument(dataType);
387:
388: HashMap map = new HashMap();
389: map.put(column, new Integer(0));
390: map.put(dataType, new Integer(1));
391: RowDecorator dec = new RowDecorator(map);
392:
393: dec.setRow(new SimpleRow(new Object[] { null, "integer" }));
394: assertNull(function.evaluate(dec));
395: assertTrue(function.getDataType() instanceof IntegerType);
396: }
397:
398: public void testCastBooleanToCharWithTruncation() throws Exception {
399: final int length = 5;
400:
401: CastAsFunction function = new CastAsFunction();
402: ColumnIdentifier column = new ColumnIdentifier("COLUMN");
403: Literal literal = new Literal(Boolean.FALSE, new BooleanType());
404: Literal dataType = new Literal("DATATYPE", new CharacterType(
405: length));
406:
407: function.addArgument(literal);
408: function.addArgument(dataType);
409:
410: HashMap map = new HashMap();
411: map.put(column, new Integer(0));
412: map.put(dataType, new Integer(1));
413: RowDecorator dec = new RowDecorator(map);
414:
415: dec.setRow(new SimpleRow(new Object[] { literal, "boolean" }));
416: assertEquals("false", function.evaluate(dec));
417: assertTrue(function.getDataType() instanceof CharacterType);
418: }
419:
420: public void testCastCharLiteralToCharWithTruncation()
421: throws Exception {
422: final String literalStr = "1234567890";
423: final int downcastLength = 5;
424:
425: CastAsFunction function = new CastAsFunction();
426: ColumnIdentifier column = new ColumnIdentifier("column");
427: Literal literal = new Literal(literalStr, new CharacterType(
428: literalStr.length()));
429: Literal dataType = new Literal("datatype", new CharacterType(
430: downcastLength));
431:
432: function.addArgument(literal);
433: function.addArgument(dataType);
434:
435: HashMap map = new HashMap();
436: map.put(column, new Integer(0));
437: map.put(dataType, new Integer(1));
438: RowDecorator dec = new RowDecorator(map);
439:
440: dec
441: .setRow(new SimpleRow(new Object[] { literalStr,
442: "varchar" }));
443: assertEquals(literalStr.substring(0, downcastLength), function
444: .evaluate(dec));
445: assertTrue(function.getDataType() instanceof CharacterType);
446: }
447: }
|