001: package liquibase.change;
002:
003: import liquibase.database.MockDatabase;
004: import liquibase.database.sql.AddForeignKeyConstraintStatement;
005: import liquibase.database.sql.SqlStatement;
006: import static org.junit.Assert.*;
007: import org.junit.Test;
008: import org.w3c.dom.Element;
009:
010: import javax.xml.parsers.DocumentBuilderFactory;
011:
012: public class AddForeignKeyConstraintChangeTest extends
013: AbstractChangeTest {
014:
015: @Test
016: public void generateStatement() throws Exception {
017: AddForeignKeyConstraintChange change = new AddForeignKeyConstraintChange();
018: change.setConstraintName("FK_NAME");
019:
020: change.setBaseTableSchemaName("BASE_SCHEMA_NAME");
021: change.setBaseTableName("BASE_TABLE_NAME");
022: change.setBaseColumnNames("BASE_COL_NAME");
023:
024: change.setReferencedTableSchemaName("REF_SCHEMA_NAME");
025: change.setReferencedTableName("REF_TABLE_NAME");
026: change.setReferencedColumnNames("REF_COL_NAME");
027:
028: change.setDeferrable(true);
029: change.setDeleteCascade(true);
030: change.setInitiallyDeferred(true);
031:
032: SqlStatement[] statements = change
033: .generateStatements(new MockDatabase());
034: assertEquals(1, statements.length);
035: AddForeignKeyConstraintStatement statement = (AddForeignKeyConstraintStatement) statements[0];
036:
037: assertEquals("FK_NAME", statement.getConstraintName());
038:
039: assertEquals("BASE_SCHEMA_NAME", statement
040: .getBaseTableSchemaName());
041: assertEquals("BASE_TABLE_NAME", statement.getBaseTableName());
042: assertEquals("BASE_COL_NAME", statement.getBaseColumnNames());
043:
044: assertEquals("REF_SCHEMA_NAME", statement
045: .getReferencedTableSchemaName());
046: assertEquals("REF_TABLE_NAME", statement
047: .getReferencedTableName());
048: assertEquals("REF_COL_NAME", statement
049: .getReferencedColumnNames());
050:
051: assertEquals(true, statement.isDeferrable());
052: assertEquals(true, statement.isInitiallyDeferred());
053: assertEquals(true, statement.isDeleteCascade());
054: }
055:
056: public void getRefactoringName() throws Exception {
057: assertEquals("Add Foreign Key Constraint",
058: new AddForeignKeyConstraintChange().getChangeName());
059: }
060:
061: @Test
062: public void getConfirmationMessage() throws Exception {
063: AddForeignKeyConstraintChange change = new AddForeignKeyConstraintChange();
064: change.setConstraintName("FK_NAME");
065: change.setBaseTableSchemaName("SCHEMA_NAME");
066: change.setBaseTableName("TABLE_NAME");
067: change.setBaseColumnNames("COL_NAME");
068:
069: assertEquals(
070: "Foreign key contraint added to TABLE_NAME (COL_NAME)",
071: change.getConfirmationMessage());
072: }
073:
074: @Test
075: public void createNode() throws Exception {
076: AddForeignKeyConstraintChange change = new AddForeignKeyConstraintChange();
077: change.setConstraintName("FK_NAME");
078:
079: change.setBaseTableSchemaName("BASE_SCHEMA_NAME");
080: change.setBaseTableName("BASE_TABLE_NAME");
081: change.setBaseColumnNames("BASE_COL_NAME");
082:
083: change.setReferencedTableSchemaName("REF_SCHEMA_NAME");
084: change.setReferencedTableName("REF_TABLE_NAME");
085: change.setReferencedColumnNames("REF_COL_NAME");
086:
087: change.setDeferrable(true);
088: change.setDeleteCascade(true);
089: change.setInitiallyDeferred(true);
090:
091: Element node = change.createNode(DocumentBuilderFactory
092: .newInstance().newDocumentBuilder().newDocument());
093: assertEquals("addForeignKeyConstraint", node.getTagName());
094: assertEquals("FK_NAME", node.getAttribute("constraintName"));
095: assertEquals("BASE_SCHEMA_NAME", node
096: .getAttribute("baseTableSchemaName"));
097: assertEquals("BASE_TABLE_NAME", node
098: .getAttribute("baseTableName"));
099: assertEquals("BASE_COL_NAME", node
100: .getAttribute("baseColumnNames"));
101: assertEquals("REF_SCHEMA_NAME", node
102: .getAttribute("referencedTableSchemaName"));
103: assertEquals("REF_TABLE_NAME", node
104: .getAttribute("referencedTableName"));
105: assertEquals("REF_COL_NAME", node
106: .getAttribute("referencedColumnNames"));
107: assertEquals("true", node.getAttribute("deferrable"));
108: assertEquals("true", node.getAttribute("initiallyDeferred"));
109: assertEquals("true", node.getAttribute("deleteCascade"));
110:
111: }
112: }
|