001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.database.structure.DatabaseSnapshot;
005: import liquibase.database.structure.ForeignKey;
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 AddForeignKeyConstraintStatementTest extends
014: AbstractSqlStatementTest {
015:
016: private static final String FK_NAME = "FK_ADDTEST";
017:
018: private static final String BASE_TABLE_NAME = "AddFKTest";
019: private static final String REF_TABLE_NAME = "AddFKTestRef";
020: private static final String BASE_COLUMN_NAME = "NewCol";
021: private static final String REF_COL_NAME = "id";
022:
023: protected void setupDatabase(Database database) throws Exception {
024: dropAndCreateTable(new CreateTableStatement(null,
025: BASE_TABLE_NAME).addPrimaryKeyColumn("id", "int")
026: .addColumn(BASE_COLUMN_NAME, "int"), database);
027:
028: dropAndCreateTable(new CreateTableStatement(null,
029: REF_TABLE_NAME)
030: .addPrimaryKeyColumn(REF_COL_NAME, "int").addColumn(
031: "existingCol", "int"), database);
032:
033: dropAndCreateTable(new CreateTableStatement(
034: TestContext.ALT_SCHEMA, BASE_TABLE_NAME)
035: .addPrimaryKeyColumn("id", "int").addColumn(
036: BASE_COLUMN_NAME, "int"), database);
037:
038: dropAndCreateTable(new CreateTableStatement(
039: TestContext.ALT_SCHEMA, REF_TABLE_NAME)
040: .addPrimaryKeyColumn(REF_COL_NAME, "int").addColumn(
041: "existingCol", "int"), database);
042: }
043:
044: protected AddForeignKeyConstraintStatement generateTestStatement() {
045: return new AddForeignKeyConstraintStatement(null, null, null,
046: null, null, null, null);
047: }
048:
049: @Test
050: public void execute() throws Exception {
051: new DatabaseTestTemplate()
052: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
053: null, new AddForeignKeyConstraintStatement(
054: FK_NAME, null, BASE_TABLE_NAME,
055: BASE_COLUMN_NAME, null, REF_TABLE_NAME,
056: REF_COL_NAME)) {
057: protected void preExecuteAssert(
058: DatabaseSnapshot snapshot) {
059: assertNull(snapshot.getForeignKey(FK_NAME));
060: }
061:
062: protected void postExecuteAssert(
063: DatabaseSnapshot snapshot) {
064: ForeignKey fkSnapshot = snapshot
065: .getForeignKey(FK_NAME);
066: assertNotNull(fkSnapshot);
067: assertEquals(BASE_TABLE_NAME.toUpperCase(),
068: fkSnapshot.getForeignKeyTable()
069: .getName().toUpperCase());
070: assertEquals(BASE_COLUMN_NAME.toUpperCase(),
071: fkSnapshot.getForeignKeyColumns()
072: .toUpperCase());
073: assertEquals(REF_TABLE_NAME.toUpperCase(),
074: fkSnapshot.getPrimaryKeyTable()
075: .getName().toUpperCase());
076: assertEquals(REF_COL_NAME.toUpperCase(),
077: fkSnapshot.getPrimaryKeyColumns()
078: .toUpperCase());
079: assertFalse(fkSnapshot.isDeferrable());
080: assertFalse(fkSnapshot.isInitiallyDeferred());
081: }
082:
083: });
084: }
085:
086: @Test
087: public void execute_deferrable() throws Exception {
088: new DatabaseTestTemplate()
089: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
090: null, new AddForeignKeyConstraintStatement(
091: FK_NAME, null, BASE_TABLE_NAME,
092: BASE_COLUMN_NAME, null, REF_TABLE_NAME,
093: REF_COL_NAME).setDeferrable(true)
094: .setInitiallyDeferred(true)) {
095: protected boolean expectedException(
096: Database database, JDBCException exception) {
097: return !database
098: .supportsInitiallyDeferrableColumns();
099: }
100:
101: protected void preExecuteAssert(
102: DatabaseSnapshot snapshot) {
103: assertNull(snapshot.getForeignKey(FK_NAME));
104: }
105:
106: protected void postExecuteAssert(
107: DatabaseSnapshot snapshot) {
108: ForeignKey fkSnapshot = snapshot
109: .getForeignKey(FK_NAME);
110: assertNotNull(fkSnapshot);
111: assertEquals(BASE_TABLE_NAME.toUpperCase(),
112: fkSnapshot.getForeignKeyTable()
113: .getName().toUpperCase());
114: assertEquals(BASE_COLUMN_NAME.toUpperCase(),
115: fkSnapshot.getForeignKeyColumns()
116: .toUpperCase());
117: assertEquals(REF_TABLE_NAME.toUpperCase(),
118: fkSnapshot.getPrimaryKeyTable()
119: .getName().toUpperCase());
120: assertEquals(REF_COL_NAME.toUpperCase(),
121: fkSnapshot.getPrimaryKeyColumns()
122: .toUpperCase());
123: assertTrue(fkSnapshot.isDeferrable());
124: assertTrue(fkSnapshot.isInitiallyDeferred());
125: }
126: });
127: }
128:
129: @Test
130: public void execute_deleteCascade() throws Exception {
131: new DatabaseTestTemplate()
132: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
133: null, new AddForeignKeyConstraintStatement(
134: FK_NAME, null, BASE_TABLE_NAME,
135: BASE_COLUMN_NAME, null, REF_TABLE_NAME,
136: REF_COL_NAME).setDeleteCascade(true)) {
137: protected void preExecuteAssert(
138: DatabaseSnapshot snapshot) {
139: assertNull(snapshot.getForeignKey(FK_NAME));
140: }
141:
142: protected void postExecuteAssert(
143: DatabaseSnapshot snapshot) {
144: ForeignKey fkSnapshot = snapshot
145: .getForeignKey(FK_NAME);
146: assertNotNull(fkSnapshot);
147: assertEquals(BASE_TABLE_NAME.toUpperCase(),
148: fkSnapshot.getForeignKeyTable()
149: .getName().toUpperCase());
150: assertEquals(BASE_COLUMN_NAME.toUpperCase(),
151: fkSnapshot.getForeignKeyColumns()
152: .toUpperCase());
153: assertEquals(REF_TABLE_NAME.toUpperCase(),
154: fkSnapshot.getPrimaryKeyTable()
155: .getName().toUpperCase());
156: assertEquals(REF_COL_NAME.toUpperCase(),
157: fkSnapshot.getPrimaryKeyColumns()
158: .toUpperCase());
159: assertFalse(fkSnapshot.isDeferrable());
160: assertFalse(fkSnapshot.isInitiallyDeferred());
161: }
162:
163: });
164: }
165:
166: @Test
167: public void execute_altSchema() throws Exception {
168: new DatabaseTestTemplate()
169: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
170: TestContext.ALT_SCHEMA,
171: new AddForeignKeyConstraintStatement(FK_NAME,
172: TestContext.ALT_SCHEMA,
173: BASE_TABLE_NAME, BASE_COLUMN_NAME,
174: TestContext.ALT_SCHEMA, REF_TABLE_NAME,
175: REF_COL_NAME)) {
176: protected void preExecuteAssert(
177: DatabaseSnapshot snapshot) {
178: assertNull(snapshot.getForeignKey(FK_NAME));
179: }
180:
181: protected void postExecuteAssert(
182: DatabaseSnapshot snapshot) {
183: ForeignKey fkSnapshot = snapshot
184: .getForeignKey(FK_NAME);
185: assertNotNull(fkSnapshot);
186: assertEquals(BASE_TABLE_NAME.toUpperCase(),
187: fkSnapshot.getForeignKeyTable()
188: .getName().toUpperCase());
189: assertEquals(BASE_COLUMN_NAME.toUpperCase(),
190: fkSnapshot.getForeignKeyColumns()
191: .toUpperCase());
192: assertEquals(REF_TABLE_NAME.toUpperCase(),
193: fkSnapshot.getPrimaryKeyTable()
194: .getName().toUpperCase());
195: assertEquals(REF_COL_NAME.toUpperCase(),
196: fkSnapshot.getPrimaryKeyColumns()
197: .toUpperCase());
198: assertFalse(fkSnapshot.isDeferrable());
199: assertFalse(fkSnapshot.isInitiallyDeferred());
200: }
201:
202: });
203: }
204: }
|