001: package liquibase.change;
002:
003: import liquibase.database.DB2Database;
004: import liquibase.database.Database;
005: import liquibase.database.sql.AddPrimaryKeyStatement;
006: import liquibase.database.sql.ReorganizeTableStatement;
007: import liquibase.database.sql.SqlStatement;
008: import liquibase.test.DatabaseTest;
009: import liquibase.test.DatabaseTestTemplate;
010: import static org.junit.Assert.*;
011: import org.junit.Test;
012: import org.w3c.dom.Element;
013:
014: import javax.xml.parsers.DocumentBuilderFactory;
015:
016: public class AddPrimaryKeyChangeTest extends AbstractChangeTest {
017:
018: @Test
019: public void getRefactoringName() throws Exception {
020: assertEquals("Add Primary Key", new AddPrimaryKeyChange()
021: .getChangeName());
022: }
023:
024: @Test
025: public void generateStatement() throws Exception {
026:
027: new DatabaseTestTemplate()
028: .testOnAllDatabases(new DatabaseTest() {
029: public void performTest(Database database)
030: throws Exception {
031: AddPrimaryKeyChange change = new AddPrimaryKeyChange();
032: change.setSchemaName("SCHEMA_NAME");
033: change.setTableName("TABLE_NAME");
034: change.setColumnNames("COL_HERE");
035: change.setConstraintName("PK_NAME");
036: change.setTablespace("TABLESPACE_NAME");
037:
038: SqlStatement[] sqlStatements = change
039: .generateStatements(database);
040: if (database instanceof DB2Database) {
041: assertEquals(2, sqlStatements.length);
042:
043: assertTrue(sqlStatements[1] instanceof ReorganizeTableStatement);
044: assertEquals(
045: "SCHEMA_NAME",
046: ((ReorganizeTableStatement) sqlStatements[1])
047: .getSchemaName());
048: assertEquals(
049: "TABLE_NAME",
050: ((ReorganizeTableStatement) sqlStatements[1])
051: .getTableName());
052: } else {
053: assertEquals(1, sqlStatements.length);
054: }
055: assertTrue(sqlStatements[0] instanceof AddPrimaryKeyStatement);
056:
057: assertEquals(
058: "SCHEMA_NAME",
059: ((AddPrimaryKeyStatement) sqlStatements[0])
060: .getSchemaName());
061: assertEquals(
062: "TABLE_NAME",
063: ((AddPrimaryKeyStatement) sqlStatements[0])
064: .getTableName());
065: assertEquals(
066: "COL_HERE",
067: ((AddPrimaryKeyStatement) sqlStatements[0])
068: .getColumnNames());
069: assertEquals(
070: "PK_NAME",
071: ((AddPrimaryKeyStatement) sqlStatements[0])
072: .getConstraintName());
073: assertEquals(
074: "TABLESPACE_NAME",
075: ((AddPrimaryKeyStatement) sqlStatements[0])
076: .getTablespace());
077:
078: }
079: });
080: }
081:
082: @Test
083: public void getConfirmationMessage() throws Exception {
084: AddPrimaryKeyChange change = new AddPrimaryKeyChange();
085: change.setTableName("TABLE_NAME");
086: change.setColumnNames("COL_HERE");
087:
088: assertEquals("Primary key added to TABLE_NAME (COL_HERE)",
089: change.getConfirmationMessage());
090: }
091:
092: @Test
093: public void createNode() throws Exception {
094: AddPrimaryKeyChange change = new AddPrimaryKeyChange();
095: change.setSchemaName("SCHEMA_NAME");
096: change.setTableName("TABLE_NAME");
097: change.setColumnNames("COL_HERE");
098: change.setConstraintName("PK_NAME");
099: change.setTablespace("TABLESPACE_NAME");
100:
101: Element node = change.createNode(DocumentBuilderFactory
102: .newInstance().newDocumentBuilder().newDocument());
103: assertEquals("addPrimaryKey", node.getTagName());
104: assertEquals("SCHEMA_NAME", node.getAttribute("schemaName"));
105: assertEquals("TABLE_NAME", node.getAttribute("tableName"));
106: assertEquals("COL_HERE", node.getAttribute("columnNames"));
107: assertEquals("PK_NAME", node.getAttribute("constraintName"));
108: assertEquals("TABLESPACE_NAME", node.getAttribute("tablespace"));
109: }
110: }
|