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.test.DatabaseTest;
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 SetNullableStatementTest extends AbstractSqlStatementTest {
014:
015: private static final String NULLABLE_TABLE_NAME = "DropNotNullTest";
016: private static final String NOTNULL_TABLE_NAME = "AddNotNullTest";
017: private static final String COLUMN_NAME = "testCol";
018:
019: protected void setupDatabase(Database database) throws Exception {
020: dropAndCreateTable(new CreateTableStatement(null,
021: NOTNULL_TABLE_NAME).addColumn("id", "int").addColumn(
022: COLUMN_NAME, "varchar(50)"), database);
023:
024: dropAndCreateTable(new CreateTableStatement(null,
025: NULLABLE_TABLE_NAME).addColumn("id", "int").addColumn(
026: COLUMN_NAME, "varchar(50)", new NotNullConstraint()),
027: database);
028:
029: dropAndCreateTable(new CreateTableStatement(
030: TestContext.ALT_SCHEMA, NOTNULL_TABLE_NAME).addColumn(
031: "id", "int").addColumn(COLUMN_NAME, "varchar(50)"),
032: database);
033:
034: dropAndCreateTable(new CreateTableStatement(
035: TestContext.ALT_SCHEMA, NULLABLE_TABLE_NAME).addColumn(
036: "id", "int").addColumn(COLUMN_NAME, "varchar(50)",
037: new NotNullConstraint()), database);
038: }
039:
040: protected SetNullableStatement generateTestStatement() {
041: return new SetNullableStatement(null, null, null, null, true);
042: }
043:
044: @Test
045: public void supportsDatabase() throws Exception {
046: new DatabaseTestTemplate()
047: .testOnAllDatabases(new DatabaseTest() {
048:
049: public void performTest(Database database)
050: throws Exception {
051: SetNullableStatement statement = generateTestStatement();
052:
053: if (database instanceof FirebirdDatabase) {
054: assertFalse(statement
055: .supportsDatabase(database));
056: } else {
057: assertTrue(statement
058: .supportsDatabase(database));
059: }
060: }
061: });
062: }
063:
064: @Test
065: public void execute_nowNotNullNoSchema() throws Exception {
066: new DatabaseTestTemplate()
067: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
068: null, new SetNullableStatement(null,
069: NOTNULL_TABLE_NAME, COLUMN_NAME,
070: "varchar(50)", false)) {
071:
072: protected void preExecuteAssert(
073: DatabaseSnapshot snapshot) {
074: assertTrue(snapshot
075: .getTable(NOTNULL_TABLE_NAME)
076: .getColumn(COLUMN_NAME).isNullable());
077: }
078:
079: protected void postExecuteAssert(
080: DatabaseSnapshot snapshot) {
081: assertFalse(snapshot.getTable(
082: NOTNULL_TABLE_NAME).getColumn(
083: COLUMN_NAME).isNullable());
084: }
085: });
086: }
087:
088: @Test
089: public void execute_withSchema() throws Exception {
090: new DatabaseTestTemplate()
091: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
092: TestContext.ALT_SCHEMA,
093: new SetNullableStatement(
094: TestContext.ALT_SCHEMA,
095: NOTNULL_TABLE_NAME, COLUMN_NAME,
096: "varchar(50)", false)) {
097:
098: protected void preExecuteAssert(
099: DatabaseSnapshot snapshot) {
100: assertTrue(snapshot
101: .getTable(NOTNULL_TABLE_NAME)
102: .getColumn(COLUMN_NAME).isNullable());
103: }
104:
105: protected void postExecuteAssert(
106: DatabaseSnapshot snapshot) {
107: assertFalse(snapshot.getTable(
108: NOTNULL_TABLE_NAME).getColumn(
109: COLUMN_NAME).isNullable());
110: }
111: });
112: }
113:
114: @Test
115: public void execute_nowNullNoSchema() throws Exception {
116: new DatabaseTestTemplate()
117: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
118: null, new SetNullableStatement(null,
119: NULLABLE_TABLE_NAME, COLUMN_NAME,
120: "varchar(50)", true)) {
121:
122: protected void preExecuteAssert(
123: DatabaseSnapshot snapshot) {
124: assertFalse(snapshot.getTable(
125: NULLABLE_TABLE_NAME).getColumn(
126: COLUMN_NAME).isNullable());
127: }
128:
129: protected void postExecuteAssert(
130: DatabaseSnapshot snapshot) {
131: assertTrue(snapshot.getTable(
132: NULLABLE_TABLE_NAME).getColumn(
133: COLUMN_NAME).isNullable());
134: }
135: });
136: }
137:
138: @Test
139: public void execute_nowNullableWithSchema() throws Exception {
140: new DatabaseTestTemplate()
141: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
142: TestContext.ALT_SCHEMA,
143: new SetNullableStatement(
144: TestContext.ALT_SCHEMA,
145: NULLABLE_TABLE_NAME, COLUMN_NAME,
146: "varchar(50)", true)) {
147:
148: protected void preExecuteAssert(
149: DatabaseSnapshot snapshot) {
150: assertFalse(snapshot.getTable(
151: NULLABLE_TABLE_NAME).getColumn(
152: COLUMN_NAME).isNullable());
153: }
154:
155: protected void postExecuteAssert(
156: DatabaseSnapshot snapshot) {
157: assertTrue(snapshot.getTable(
158: NULLABLE_TABLE_NAME).getColumn(
159: COLUMN_NAME).isNullable());
160: }
161: });
162: }
163: }
|