001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.database.FirebirdDatabase;
005: import liquibase.database.structure.DatabaseSnapshot;
006: import liquibase.database.template.JdbcTemplate;
007: import liquibase.test.DatabaseTestTemplate;
008: import liquibase.test.SqlStatementDatabaseTest;
009: import liquibase.test.TestContext;
010: import static org.junit.Assert.*;
011: import org.junit.Test;
012:
013: public class InsertStatementTest extends AbstractSqlStatementTest {
014: private static final String TABLE_NAME = "InsertTest";
015: private static final String VARCHAR_COL_NAME = "colName_vc";
016: private static final String DATE_COL_NAME = "colName_dt";
017: private static final String BOOLEAN_COL_NAME = "colName_b";
018: private static final String INT_COL_NAME = "colName_i";
019: private static final String FLOAT_COL_NAME = "colName_f";
020:
021: protected void setupDatabase(Database database) throws Exception {
022: dropAndCreateTable(new CreateTableStatement(null, TABLE_NAME)
023: .addColumn(VARCHAR_COL_NAME, "varchar(50)").addColumn(
024: DATE_COL_NAME, "varchar(50)").addColumn(
025: BOOLEAN_COL_NAME, database.getBooleanType())
026: .addColumn(INT_COL_NAME, "int").addColumn(
027: FLOAT_COL_NAME, "float"), database);
028:
029: dropAndCreateTable(
030: new CreateTableStatement(TestContext.ALT_SCHEMA,
031: TABLE_NAME).addColumn(VARCHAR_COL_NAME,
032: "varchar(50)").addColumn(DATE_COL_NAME,
033: "varchar(50)").addColumn(BOOLEAN_COL_NAME,
034: database.getBooleanType()).addColumn(
035: INT_COL_NAME, "int").addColumn(FLOAT_COL_NAME,
036: "float"), database);
037: }
038:
039: protected SqlStatement generateTestStatement() {
040: return new InsertStatement(null, null);
041: }
042:
043: @Test
044: public void execute_defaultSchema() throws Exception {
045: new DatabaseTestTemplate()
046: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
047: null, new InsertStatement(null, TABLE_NAME)
048: .addColumnValue(VARCHAR_COL_NAME,
049: "new value")) {
050:
051: private int oldCount;
052:
053: protected void preExecuteAssert(
054: DatabaseSnapshot snapshot) throws Exception {
055: oldCount = new JdbcTemplate(snapshot
056: .getDatabase())
057: .queryForInt(new RawSqlStatement(
058: "select count(*) from "
059: + TABLE_NAME));
060: }
061:
062: protected void postExecuteAssert(
063: DatabaseSnapshot snapshot) throws Exception {
064: int newCount = new JdbcTemplate(snapshot
065: .getDatabase())
066: .queryForInt(new RawSqlStatement(
067: "select count(*) from "
068: + TABLE_NAME));
069: assertEquals(oldCount + 1, newCount);
070: }
071:
072: });
073: }
074:
075: @Test
076: public void execute_altSchema() throws Exception {
077: new DatabaseTestTemplate()
078: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
079: TestContext.ALT_SCHEMA, new InsertStatement(
080: TestContext.ALT_SCHEMA, TABLE_NAME)
081: .addColumnValue(VARCHAR_COL_NAME,
082: "new value")) {
083:
084: private int oldCount;
085:
086: protected boolean supportsTest(Database database) {
087: return !(database instanceof FirebirdDatabase);
088: }
089:
090: protected void preExecuteAssert(
091: DatabaseSnapshot snapshot) throws Exception {
092: oldCount = new JdbcTemplate(snapshot
093: .getDatabase())
094: .queryForInt(new RawSqlStatement(
095: "select count(*) from "
096: + TestContext.ALT_SCHEMA
097: + "." + TABLE_NAME));
098: }
099:
100: protected void postExecuteAssert(
101: DatabaseSnapshot snapshot) throws Exception {
102: int newCount = new JdbcTemplate(snapshot
103: .getDatabase())
104: .queryForInt(new RawSqlStatement(
105: "select count(*) from "
106: + TestContext.ALT_SCHEMA
107: + "." + TABLE_NAME));
108: assertEquals(oldCount + 1, newCount);
109: }
110:
111: });
112: }
113:
114: @Test
115: public void execute_multiColumns() throws Exception {
116: new DatabaseTestTemplate()
117: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
118: null,
119: new InsertStatement(null, TABLE_NAME)
120: .addColumnValue(VARCHAR_COL_NAME,
121: "new value")
122: .addColumnValue(
123: DATE_COL_NAME,
124: new java.sql.Date(
125: new java.util.Date()
126: .getTime()))
127: .addColumnValue(INT_COL_NAME, 42)
128: .addColumnValue(FLOAT_COL_NAME, 123.456)) {
129:
130: private int oldCount;
131:
132: protected void preExecuteAssert(
133: DatabaseSnapshot snapshot) throws Exception {
134: oldCount = new JdbcTemplate(snapshot
135: .getDatabase())
136: .queryForInt(new RawSqlStatement(
137: "select count(*) from "
138: + TABLE_NAME));
139: }
140:
141: protected void postExecuteAssert(
142: DatabaseSnapshot snapshot) throws Exception {
143: int newCount = new JdbcTemplate(snapshot
144: .getDatabase())
145: .queryForInt(new RawSqlStatement(
146: "select count(*) from "
147: + TABLE_NAME));
148: assertEquals(oldCount + 1, newCount);
149: }
150:
151: });
152: }
153: }
|