001: /*
002: * ConstantColumnValuesTest.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.db.importer;
013:
014: import java.sql.Statement;
015: import java.util.ArrayList;
016: import java.util.List;
017: import junit.framework.TestCase;
018: import workbench.TestUtil;
019: import workbench.db.ColumnIdentifier;
020: import workbench.db.WbConnection;
021: import workbench.util.SqlUtil;
022: import workbench.util.ValueConverter;
023:
024: /**
025: *
026: * @author support@sql-workbench.net
027: */
028: public class ConstantColumnValuesTest extends TestCase {
029:
030: public ConstantColumnValuesTest(String testName) {
031: super (testName);
032: }
033:
034: public void testGetStaticValues() {
035: List<ColumnIdentifier> columns = new ArrayList<ColumnIdentifier>();
036: columns.add(new ColumnIdentifier("test_run_id",
037: java.sql.Types.INTEGER));
038: columns.add(new ColumnIdentifier("title",
039: java.sql.Types.VARCHAR));
040: columns.add(new ColumnIdentifier("modified",
041: java.sql.Types.TIMESTAMP));
042: columns.add(new ColumnIdentifier("t2", java.sql.Types.VARCHAR));
043: columns.add(new ColumnIdentifier("t3", java.sql.Types.VARCHAR));
044: columns.add(new ColumnIdentifier("t4", java.sql.Types.VARCHAR));
045: columns
046: .add(new ColumnIdentifier("id",
047: java.sql.Types.TIMESTAMP));
048: try {
049: ConstantColumnValues values = new ConstantColumnValues(
050: "test_run_id=42,title=\"hello, world\",modified=current_timestamp,t2='bla',t3=''bla'',id=${current_timestamp},t4='${ant.var}'",
051: columns);
052: assertEquals(7, values.getColumnCount());
053: assertEquals(new Integer(42), values.getValue(0));
054: assertEquals("hello, world", values.getValue(1));
055: assertEquals(true,
056: values.getValue(2) instanceof java.sql.Timestamp);
057: assertEquals("bla", values.getValue(3));
058: assertEquals("'bla'", values.getValue(4));
059: assertEquals("current_timestamp", values
060: .getFunctionLiteral(5));
061: assertEquals("${ant.var}", values.getValue(6));
062:
063: assertEquals(true, values
064: .removeColumn(new ColumnIdentifier("t2",
065: java.sql.Types.VARCHAR)));
066: assertEquals(false, values
067: .removeColumn(new ColumnIdentifier("kkk",
068: java.sql.Types.VARCHAR)));
069: } catch (Exception ex) {
070: ex.printStackTrace();
071: fail(ex.getMessage());
072: }
073: }
074:
075: public void testInitFromDb() {
076: TestUtil util = new TestUtil("testConstants");
077: WbConnection con = null;
078: String tablename = "constant_test";
079: Statement stmt = null;
080: try {
081: con = util.getConnection("cons_test");
082: stmt = con.createStatement();
083: stmt
084: .executeUpdate("create table constant_test (test_run_id integer, title varchar(20))");
085: ValueConverter converter = new ValueConverter("yyyy-MM-dd",
086: "yyyy-MM-dd HH:mm:ss");
087: ConstantColumnValues values = new ConstantColumnValues(
088: "test_run_id=42,title=\"hello, world\"", con,
089: tablename, converter);
090: assertEquals(2, values.getColumnCount());
091: assertEquals(new Integer(42), values.getValue(0));
092: assertEquals("hello, world", values.getValue(1));
093: } catch (Exception ex) {
094: ex.printStackTrace();
095: fail(ex.getMessage());
096: } finally {
097: SqlUtil.closeStatement(stmt);
098: try {
099: con.disconnect();
100: } catch (Throwable th) {
101: }
102: }
103: }
104:
105: }
|