001: /*
002: * $Id: TestDateToCharFunction.java,v 1.7 2005/05/13 00:58:27 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2004 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.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.Literal;
052: import org.axiondb.RowDecorator;
053: import org.axiondb.engine.rows.SimpleRow;
054: import org.axiondb.types.TimestampType;
055:
056: /**
057: * Unit tests for DateToChar function.
058: *
059: * @version $Revision: 1.7 $ $Date: 2005/05/13 00:58:27 $
060: * @author Jonathan Giron
061: */
062: public class TestDateToCharFunction extends BaseFunctionTest {
063:
064: public TestDateToCharFunction(String testName) {
065: super (testName);
066: }
067:
068: public static final void main(String[] args) {
069: junit.textui.TestRunner.run(suite());
070: }
071:
072: public void setUp() throws Exception {
073: super .setUp();
074: TimestampType.setTimeZone("GMT");
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see org.axiondb.functions.BaseFunctionTest#makeFunction()
081: */
082: protected ConcreteFunction makeFunction() {
083: return new DateToCharFunction();
084: }
085:
086: public static Test suite() {
087: TestSuite suite = new TestSuite(TestDateToCharFunction.class);
088: return suite;
089: }
090:
091: public void testValidFormats() throws Exception {
092: DateToCharFunction function = new DateToCharFunction();
093: ColumnIdentifier timestampLbl = new ColumnIdentifier(
094: "timestamp");
095: ColumnIdentifier formatLbl = new ColumnIdentifier("format");
096:
097: function.addArgument(timestampLbl);
098: function.addArgument(formatLbl);
099:
100: HashMap map = new HashMap();
101: map.put(timestampLbl, new Integer(0));
102: map.put(formatLbl, new Integer(1));
103:
104: RowDecorator dec = new RowDecorator(map);
105:
106: // Sample time: 2004-04-01 12:00:00Z
107: TimestampType.setTimeZone("GMT");
108: final Timestamp input = new Timestamp(34 * 365 * 24 * 60 * 60
109: * 1000L + //year
110: 9 * 24 * 60 * 60 * 1000L + // leap years
111: // '72,'76,'80,'84,'88,'92,'96, 2000,
112: // 2004
113: (31 + 28 + 31) * 24 * 60 * 60 * 1000L // April 01
114: );
115:
116: // Test YYYY-MM-DD date only format, with dash separators
117: Literal format = new Literal("yyyy-mm-dd");
118: dec.setRow(new SimpleRow(new Object[] { input, format }));
119:
120: Object returnVal = function.evaluate(dec);
121: assertEquals("Expected valid return for format "
122: + format.toString() + "; ", "2004-04-01", returnVal);
123:
124: // Test US civilian time only format, with colon separators
125: format = new Literal("hh:mi:ss am");
126: dec.setRow(new SimpleRow(new Object[] { input, format }));
127: returnVal = function.evaluate(dec);
128: assertEquals("Expected valid return for format "
129: + format.toString() + "; ", "12:00:00 AM", returnVal);
130:
131: // Test NATO/military time only format, with colon separators and
132: // milliseconds
133: format = new Literal("hh24:mi:ss.ff");
134: dec.setRow(new SimpleRow(new Object[] { input, format }));
135: returnVal = function.evaluate(dec);
136: assertEquals("Expected valid return for format "
137: + format.toString() + "; ", "00:00:00.000", returnVal);
138:
139: // Test compact ISO 8601 format
140: format = new Literal("yyyymmddThh24miss");
141: dec.setRow(new SimpleRow(new Object[] { input, format }));
142: returnVal = function.evaluate(dec);
143: assertEquals("Expected valid return for format "
144: + format.toString() + "; ", "20040401T000000",
145: returnVal);
146:
147: // Test three-letter month abbreviation
148: format = new Literal("dd mon yyyy hh24:mi:ss.ff");
149: dec.setRow(new SimpleRow(new Object[] { input, format }));
150: returnVal = function.evaluate(dec);
151: assertEquals("Expected valid return for format "
152: + format.toString() + "; ", "01 APR 2004 00:00:00.000",
153: returnVal);
154:
155: // Test German-style date format, with dot separators
156: format = new Literal("dd.mm.yyyy");
157: dec.setRow(new SimpleRow(new Object[] { input, format }));
158: returnVal = function.evaluate(dec);
159: assertEquals("Expected valid return for format "
160: + format.toString() + "; ", "01.04.2004", returnVal);
161:
162: // Test US-style date format with slash separators
163: format = new Literal("mm/dd/yyyy");
164: dec.setRow(new SimpleRow(new Object[] { input, format }));
165: returnVal = function.evaluate(dec);
166: assertEquals("Expected valid return for format "
167: + format.toString() + "; ", "04/01/2004", returnVal);
168:
169: // Test pattern with all capital letters
170: format = new Literal("YYYYMMDDTHH24MISS");
171: dec.setRow(new SimpleRow(new Object[] { input, format }));
172: returnVal = function.evaluate(dec);
173: assertEquals("Expected valid return for format "
174: + format.toString() + "; ", "20040401T000000",
175: returnVal);
176: }
177:
178: public void testNullDateExprYieldsNull() throws Exception {
179: DateToCharFunction function = new DateToCharFunction();
180: ColumnIdentifier timestampLbl = new ColumnIdentifier(
181: "timestamp");
182: ColumnIdentifier formatLbl = new ColumnIdentifier("format");
183:
184: function.addArgument(timestampLbl);
185: function.addArgument(formatLbl);
186:
187: HashMap map = new HashMap();
188: map.put(timestampLbl, new Integer(0));
189: map.put(formatLbl, new Integer(1));
190:
191: RowDecorator dec = new RowDecorator(map);
192: Literal format = new Literal("bcdef");
193: dec.setRow(new SimpleRow(new Object[] { null, format }));
194:
195: Object returnVal = function.evaluate(dec);
196: assertNull(
197: "Null value for date-expr input should have returned null",
198: returnVal);
199: }
200:
201: public void testInvalidDateExprThrowsException() throws Exception {
202: DateToCharFunction function = new DateToCharFunction();
203: ColumnIdentifier timestampLbl = new ColumnIdentifier(
204: "timestamp");
205: ColumnIdentifier formatLbl = new ColumnIdentifier("format");
206:
207: function.addArgument(timestampLbl);
208: function.addArgument(formatLbl);
209:
210: HashMap map = new HashMap();
211: map.put(timestampLbl, new Integer(0));
212: map.put(formatLbl, new Integer(1));
213:
214: RowDecorator dec = new RowDecorator(map);
215: Literal format = new Literal("yyyy-mm-dd");
216: dec.setRow(new SimpleRow(new Object[] { "abcdef", format }));
217: try {
218: function.evaluate(dec);
219: fail("Invalid value for date-expr input should have thrown an Exception");
220: } catch (AxionException e) {
221: // Desired effect - ignore.
222: }
223:
224: try {
225: dec.setRow(new SimpleRow(new Object[] {
226: new Object[] { "abcdef" }, format }));
227: function.evaluate(dec);
228: fail("Invalid value for date-expr input should have thrown an Exception");
229: } catch (AxionException e) {
230: // Desired effect - ignore.
231: }
232: }
233:
234: public void testInvalidFormatThrowsException() throws Exception {
235: DateToCharFunction function = new DateToCharFunction();
236: ColumnIdentifier timestampLbl = new ColumnIdentifier(
237: "timestamp");
238: ColumnIdentifier formatLbl = new ColumnIdentifier("format");
239:
240: function.addArgument(timestampLbl);
241: function.addArgument(formatLbl);
242:
243: HashMap map = new HashMap();
244: map.put(timestampLbl, new Integer(0));
245: map.put(formatLbl, new Integer(1));
246:
247: RowDecorator dec = new RowDecorator(map);
248: Literal format = new Literal("bcdef");
249: dec.setRow(new SimpleRow(new Object[] { new Timestamp(0),
250: format }));
251: try {
252: function.evaluate(dec);
253: fail("Invalid value for format input should have thrown an Exception");
254: } catch (AxionException e) {
255: // Desired effect - ignore.
256: }
257: }
258:
259: public void testNullFormatThrowsException() throws Exception {
260: DateToCharFunction function = new DateToCharFunction();
261: ColumnIdentifier timestampLbl = new ColumnIdentifier(
262: "timestamp");
263: ColumnIdentifier formatLbl = new ColumnIdentifier("format");
264:
265: function.addArgument(timestampLbl);
266: function.addArgument(formatLbl);
267:
268: HashMap map = new HashMap();
269: map.put(timestampLbl, new Integer(0));
270: map.put(formatLbl, new Integer(1));
271:
272: RowDecorator dec = new RowDecorator(map);
273: dec.setRow(new SimpleRow(new Object[] {
274: new Timestamp(System.currentTimeMillis()), null }));
275: try {
276: function.evaluate(dec);
277: fail("Null for format input should have thrown an Exception");
278: } catch (AxionException e) {
279: // Desired effect - ignore.
280: }
281: }
282:
283: public void testMakeNewInstance() {
284: DateToCharFunction function = new DateToCharFunction();
285: assertTrue(function.makeNewInstance() instanceof DateToCharFunction);
286: assertTrue(function.makeNewInstance() != function
287: .makeNewInstance());
288: }
289:
290: public void testInvalid() throws Exception {
291: DateToCharFunction function = new DateToCharFunction();
292: assertTrue(!function.isValid());
293: }
294:
295: public void testValid() throws Exception {
296: DateToCharFunction function = new DateToCharFunction();
297: function.addArgument(new ColumnIdentifier("foo"));
298: function.addArgument(new ColumnIdentifier("bar"));
299: assertTrue(function.isValid());
300: }
301:
302: }
|