001: package liquibase.database.sql;
002:
003: import liquibase.database.*;
004: import liquibase.database.structure.Column;
005: import liquibase.database.structure.DatabaseSnapshot;
006: import liquibase.exception.JDBCException;
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 AddColumnStatementTest extends AbstractSqlStatementTest {
014:
015: private static final String TABLE_NAME = "AddColTest";
016: private static final String NEW_COLUMN_NAME = "NewCol";
017:
018: protected void setupDatabase(Database database) throws Exception {
019: dropAndCreateTable(new CreateTableStatement(null, TABLE_NAME)
020: .addColumn("existingCol", "int"), database);
021: dropAndCreateTable(new CreateTableStatement(
022: TestContext.ALT_SCHEMA, TABLE_NAME).addColumn(
023: "existingCol", "int"), database);
024: }
025:
026: protected AddColumnStatement generateTestStatement() {
027: return new AddColumnStatement(null, null, null, null, null);
028: }
029:
030: @Test
031: public void execute_stringDefault() throws Exception {
032: new DatabaseTestTemplate()
033: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
034: null, new AddColumnStatement(null, TABLE_NAME,
035: NEW_COLUMN_NAME, "varchar(50)",
036: "new default")) {
037: protected void preExecuteAssert(
038: DatabaseSnapshot snapshot) {
039: assertNull(snapshot.getTable(TABLE_NAME)
040: .getColumn(NEW_COLUMN_NAME));
041: }
042:
043: protected void postExecuteAssert(
044: DatabaseSnapshot snapshot) {
045: Column columnSnapshot = snapshot.getTable(
046: TABLE_NAME).getColumn(NEW_COLUMN_NAME);
047: assertNotNull(columnSnapshot);
048: assertEquals(NEW_COLUMN_NAME.toUpperCase(),
049: columnSnapshot.getName().toUpperCase());
050: assertEquals("varchar".toUpperCase(),
051: columnSnapshot.getTypeName()
052: .toUpperCase());
053: assertEquals(50, columnSnapshot.getColumnSize());
054: assertEquals("new default", columnSnapshot
055: .getDefaultValue());
056:
057: assertEquals(true, columnSnapshot.isNullable());
058: }
059: });
060: }
061:
062: @Test
063: public void execute_intDefault() throws Exception {
064: new DatabaseTestTemplate()
065: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
066: null, new AddColumnStatement(null, TABLE_NAME,
067: NEW_COLUMN_NAME, "int", 42)) {
068: protected void preExecuteAssert(
069: DatabaseSnapshot snapshot) {
070: assertNull(snapshot.getTable(TABLE_NAME)
071: .getColumn(NEW_COLUMN_NAME));
072: }
073:
074: protected void postExecuteAssert(
075: DatabaseSnapshot snapshot) {
076: Column columnSnapshot = snapshot.getTable(
077: TABLE_NAME).getColumn(NEW_COLUMN_NAME);
078: assertNotNull(columnSnapshot);
079: assertEquals(NEW_COLUMN_NAME.toUpperCase(),
080: columnSnapshot.getName().toUpperCase());
081: if (snapshot.getDatabase() instanceof OracleDatabase) {
082: assertEquals("NUMBER", columnSnapshot
083: .getTypeName().toUpperCase());
084: } else {
085: assertTrue(columnSnapshot.getTypeName()
086: .toUpperCase().startsWith("INT"));
087: }
088: assertEquals(42, ((Number) columnSnapshot
089: .getDefaultValue()).intValue());
090:
091: assertEquals(true, columnSnapshot.isNullable());
092: }
093:
094: }
095:
096: );
097: }
098:
099: @Test
100: public void execute_floatDefault() throws Exception {
101: new DatabaseTestTemplate()
102: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
103: null, new AddColumnStatement(null, TABLE_NAME,
104: NEW_COLUMN_NAME, "float", 42.5)) {
105: protected void preExecuteAssert(
106: DatabaseSnapshot snapshot) {
107: assertNull(snapshot.getTable(TABLE_NAME)
108: .getColumn(NEW_COLUMN_NAME));
109: }
110:
111: protected void postExecuteAssert(
112: DatabaseSnapshot snapshot) {
113: Column columnSnapshot = snapshot.getTable(
114: TABLE_NAME).getColumn(NEW_COLUMN_NAME);
115: assertNotNull(columnSnapshot);
116: assertEquals(NEW_COLUMN_NAME.toUpperCase(),
117: columnSnapshot.getName().toUpperCase());
118: assertEquals(42.5, ((Number) columnSnapshot
119: .getDefaultValue()).doubleValue());
120:
121: assertEquals(true, columnSnapshot.isNullable());
122: }
123: });
124: }
125:
126: @Test
127: public void execute_notNull() throws Exception {
128: new DatabaseTestTemplate()
129: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
130: null, new AddColumnStatement(null, TABLE_NAME,
131: NEW_COLUMN_NAME, "int", 42,
132: new NotNullConstraint())) {
133: protected void preExecuteAssert(
134: DatabaseSnapshot snapshot) {
135: assertNull(snapshot.getTable(TABLE_NAME)
136: .getColumn(NEW_COLUMN_NAME));
137: }
138:
139: protected void postExecuteAssert(
140: DatabaseSnapshot snapshot) {
141: Column columnSnapshot = snapshot.getTable(
142: TABLE_NAME).getColumn(NEW_COLUMN_NAME);
143: assertNotNull(columnSnapshot);
144: assertEquals(false, columnSnapshot.isNullable());
145: }
146: }
147:
148: );
149: }
150:
151: @Test
152: public void execute_primaryKey_nonAutoIncrement() throws Exception {
153: new DatabaseTestTemplate()
154: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
155: null, new AddColumnStatement(null, TABLE_NAME,
156: NEW_COLUMN_NAME, "int", null,
157: new PrimaryKeyConstraint())) {
158:
159: protected boolean expectedException(
160: Database database, JDBCException exception) {
161: return (database instanceof DB2Database
162: || database instanceof DerbyDatabase
163: || database instanceof H2Database || database instanceof CacheDatabase);
164: }
165:
166: protected void preExecuteAssert(
167: DatabaseSnapshot snapshot) {
168: assertNull(snapshot.getTable(TABLE_NAME)
169: .getColumn(NEW_COLUMN_NAME));
170: }
171:
172: protected void postExecuteAssert(
173: DatabaseSnapshot snapshot) {
174: Column columnSnapshot = snapshot.getTable(
175: TABLE_NAME).getColumn(NEW_COLUMN_NAME);
176: assertNotNull(columnSnapshot);
177: assertEquals(false, columnSnapshot.isNullable());
178: assertTrue(columnSnapshot.isPrimaryKey());
179: assertEquals(false, columnSnapshot
180: .isAutoIncrement());
181: }
182: });
183: }
184:
185: @Test
186: public void execute_altSchema() throws Exception {
187: new DatabaseTestTemplate()
188: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
189: TestContext.ALT_SCHEMA, new AddColumnStatement(
190: TestContext.ALT_SCHEMA, TABLE_NAME,
191: NEW_COLUMN_NAME, "varchar(50)",
192: "new default")) {
193: protected void preExecuteAssert(
194: DatabaseSnapshot snapshot) {
195: assertNull(snapshot.getTable(TABLE_NAME)
196: .getColumn(NEW_COLUMN_NAME));
197: }
198:
199: protected void postExecuteAssert(
200: DatabaseSnapshot snapshot) {
201: Column columnSnapshot = snapshot.getTable(
202: TABLE_NAME).getColumn(NEW_COLUMN_NAME);
203: assertNotNull(columnSnapshot);
204: assertEquals(NEW_COLUMN_NAME.toUpperCase(),
205: columnSnapshot.getName().toUpperCase());
206: assertEquals("new default", columnSnapshot
207: .getDefaultValue());
208:
209: assertEquals(true, columnSnapshot.isNullable());
210: }
211:
212: });
213: }
214:
215: @Test
216: public void execute_primaryKeyAutoIncrement() throws Exception {
217: new DatabaseTestTemplate()
218: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
219: null, new AddColumnStatement(null, TABLE_NAME,
220: NEW_COLUMN_NAME, "int", null,
221: new PrimaryKeyConstraint(),
222: new AutoIncrementConstraint())) {
223:
224: protected boolean expectedException(
225: Database database, JDBCException exception) {
226: return (database instanceof DB2Database
227: || database instanceof DerbyDatabase
228: || database instanceof H2Database
229: || database instanceof CacheDatabase || !database
230: .supportsAutoIncrement());
231: }
232:
233: protected void preExecuteAssert(
234: DatabaseSnapshot snapshot) {
235: assertNull(snapshot.getTable(TABLE_NAME)
236: .getColumn(NEW_COLUMN_NAME));
237: }
238:
239: protected void postExecuteAssert(
240: DatabaseSnapshot snapshot) {
241: Column columnSnapshot = snapshot.getTable(
242: TABLE_NAME).getColumn(NEW_COLUMN_NAME);
243: assertNotNull(columnSnapshot);
244: assertEquals(false, columnSnapshot.isNullable());
245: assertTrue(columnSnapshot.isPrimaryKey());
246: assertEquals(true, columnSnapshot
247: .isAutoIncrement());
248: }
249: });
250: }
251:
252: }
|