001: /*
002: * SqlLiteralFormatterTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.storage;
013:
014: import java.sql.Types;
015: import java.util.Calendar;
016: import workbench.db.ColumnIdentifier;
017:
018: /**
019: *
020: * @author support@sql-workbench.net
021: */
022: public class SqlLiteralFormatterTest extends junit.framework.TestCase {
023:
024: public SqlLiteralFormatterTest(String testName) {
025: super (testName);
026: }
027:
028: public void testGetJdbcLiteral() {
029: try {
030: SqlLiteralFormatter f = new SqlLiteralFormatter();
031: f.setProduct(SqlLiteralFormatter.JDBC_DATE_LITERAL_TYPE);
032:
033: Calendar c = Calendar.getInstance();
034: c.clear();
035: c.set(Calendar.YEAR, 2002);
036: c.set(Calendar.MONTH, 3); // April
037: c.set(Calendar.DAY_OF_MONTH, 2);
038: c.set(Calendar.HOUR_OF_DAY, 14);
039: c.set(Calendar.MINUTE, 15);
040: c.set(Calendar.SECOND, 16);
041:
042: java.sql.Time tm = new java.sql.Time(c.getTime().getTime());
043: ColumnIdentifier timecol = new ColumnIdentifier("TIME_COL",
044: Types.TIME);
045: ColumnData data = new ColumnData(tm, timecol);
046: CharSequence literal = f.getDefaultLiteral(data);
047: assertEquals("JDBC time incorrect", "{t '14:15:16'}",
048: literal);
049:
050: java.sql.Date dt = new java.sql.Date(c.getTime().getTime());
051: ColumnIdentifier datecol = new ColumnIdentifier("DATE_COL",
052: Types.DATE);
053: data = new ColumnData(dt, datecol);
054: literal = f.getDefaultLiteral(data);
055: assertEquals("JDBC date incorrect", "{d '2002-04-02'}",
056: literal);
057:
058: java.sql.Timestamp ts = new java.sql.Timestamp(c.getTime()
059: .getTime());
060: ColumnIdentifier tscol = new ColumnIdentifier("TS_COL",
061: Types.TIMESTAMP);
062: data = new ColumnData(ts, tscol);
063: literal = f.getDefaultLiteral(data);
064: assertEquals("JDBC timestamp incorrect",
065: "{ts '2002-04-02 14:15:16.000'}", literal);
066: } catch (Exception e) {
067: e.printStackTrace();
068: fail(e.getMessage());
069: }
070: }
071:
072: public void testGetAnsiLiteral() {
073: try {
074: SqlLiteralFormatter f = new SqlLiteralFormatter();
075: f.setProduct(SqlLiteralFormatter.ANSI_DATE_LITERAL_TYPE);
076:
077: Calendar c = Calendar.getInstance();
078: c.clear();
079: c.set(Calendar.YEAR, 2002);
080: c.set(Calendar.MONTH, 3); // April
081: c.set(Calendar.DAY_OF_MONTH, 2);
082: c.set(Calendar.HOUR_OF_DAY, 14);
083: c.set(Calendar.MINUTE, 15);
084: c.set(Calendar.SECOND, 16);
085:
086: java.sql.Time tm = new java.sql.Time(c.getTime().getTime());
087: ColumnIdentifier timecol = new ColumnIdentifier("TIME_COL",
088: Types.TIME);
089: ColumnData data = new ColumnData(tm, timecol);
090: CharSequence literal = f.getDefaultLiteral(data);
091: assertEquals("ANSI time incorrect", "TIME '14:15:16'",
092: literal);
093:
094: java.sql.Date dt = new java.sql.Date(c.getTime().getTime());
095: ColumnIdentifier datecol = new ColumnIdentifier("DATE_COL",
096: Types.DATE);
097: data = new ColumnData(dt, datecol);
098: literal = f.getDefaultLiteral(data);
099: assertEquals("ANSI date incorrect", "DATE '2002-04-02'",
100: literal);
101:
102: java.sql.Timestamp ts = new java.sql.Timestamp(c.getTime()
103: .getTime());
104: ColumnIdentifier tscol = new ColumnIdentifier("TS_COL",
105: Types.TIMESTAMP);
106: data = new ColumnData(ts, tscol);
107: literal = f.getDefaultLiteral(data);
108: assertEquals("ANSI timestamp incorrect",
109: "TIMESTAMP '2002-04-02 14:15:16'", literal);
110: } catch (Exception e) {
111: e.printStackTrace();
112: fail(e.getMessage());
113: }
114: }
115:
116: public void testGetOracleLiteral() {
117: try {
118: SqlLiteralFormatter f = new SqlLiteralFormatter();
119: f.setProduct("oracle");
120:
121: Calendar c = Calendar.getInstance();
122: c.clear();
123: c.set(Calendar.YEAR, 2002);
124: c.set(Calendar.MONTH, 3); // April
125: c.set(Calendar.DAY_OF_MONTH, 2);
126: c.set(Calendar.HOUR_OF_DAY, 14);
127: c.set(Calendar.MINUTE, 15);
128: c.set(Calendar.SECOND, 16);
129:
130: java.sql.Date dt = new java.sql.Date(c.getTime().getTime());
131: ColumnIdentifier datecol = new ColumnIdentifier("DATE_COL",
132: Types.DATE);
133: ColumnData data = new ColumnData(dt, datecol);
134: CharSequence literal = f.getDefaultLiteral(data);
135: assertEquals("Oracle date incorrect",
136: "to_date('2002-04-02', 'YYYY-MM-DD')", literal);
137:
138: java.sql.Timestamp ts = new java.sql.Timestamp(c.getTime()
139: .getTime());
140: ColumnIdentifier tscol = new ColumnIdentifier("TS_COL",
141: Types.TIMESTAMP);
142: data = new ColumnData(ts, tscol);
143: literal = f.getDefaultLiteral(data);
144: assertEquals(
145: "Oracle timestamp incorrect",
146: "to_date('2002-04-02 14:15:16', 'YYYY-MM-DD HH24:MI:SS')",
147: literal);
148: } catch (Exception e) {
149: e.printStackTrace();
150: fail(e.getMessage());
151: }
152: }
153:
154: }
|