001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.CreateIndexStatement;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.test.DatabaseTest;
007: import liquibase.test.DatabaseTestTemplate;
008: import static org.junit.Assert.*;
009: import org.junit.Test;
010: import org.w3c.dom.Element;
011:
012: import javax.xml.parsers.DocumentBuilderFactory;
013:
014: /**
015: * Tests for {@link CreateIndexChange}
016: */
017: public class CreateIndexChangeTest extends AbstractChangeTest {
018:
019: @Test
020: public void getRefactoringName() throws Exception {
021: assertEquals("Create Index", new CreateIndexChange()
022: .getChangeName());
023: }
024:
025: @Test
026: public void generateStatement() throws Exception {
027: new DatabaseTestTemplate()
028: .testOnAllDatabases(new DatabaseTest() {
029: public void performTest(Database database)
030: throws Exception {
031: CreateIndexChange change = new CreateIndexChange();
032: change.setIndexName("IDX_NAME");
033: change.setSchemaName("SCHEMA_NAME");
034: change.setTableName("TABLE_NAME");
035: change.setTablespace("TABLESPACE_NAME");
036:
037: ColumnConfig column = new ColumnConfig();
038: column.setName("COL_NAME");
039: change.addColumn(column);
040:
041: ColumnConfig column2 = new ColumnConfig();
042: column2.setName("COL2_NAME");
043: change.addColumn(column2);
044:
045: SqlStatement[] sqlStatements = change
046: .generateStatements(database);
047: assertEquals(1, sqlStatements.length);
048: assertTrue(sqlStatements[0] instanceof CreateIndexStatement);
049:
050: assertEquals(
051: "IDX_NAME",
052: ((CreateIndexStatement) sqlStatements[0])
053: .getIndexName());
054: assertEquals(
055: "SCHEMA_NAME",
056: ((CreateIndexStatement) sqlStatements[0])
057: .getTableSchemaName());
058: assertEquals(
059: "TABLE_NAME",
060: ((CreateIndexStatement) sqlStatements[0])
061: .getTableName());
062: assertEquals(
063: "TABLESPACE_NAME",
064: ((CreateIndexStatement) sqlStatements[0])
065: .getTablespace());
066: assertEquals(
067: 2,
068: ((CreateIndexStatement) sqlStatements[0])
069: .getColumns().length);
070: assertEquals(
071: "COL_NAME",
072: ((CreateIndexStatement) sqlStatements[0])
073: .getColumns()[0]);
074: assertEquals(
075: "COL2_NAME",
076: ((CreateIndexStatement) sqlStatements[0])
077: .getColumns()[1]);
078: }
079: });
080: }
081:
082: @Test
083: public void getConfirmationMessage() throws Exception {
084: CreateIndexChange refactoring = new CreateIndexChange();
085: refactoring.setIndexName("IDX_TEST");
086:
087: assertEquals("Index IDX_TEST created", refactoring
088: .getConfirmationMessage());
089: }
090:
091: @Test
092: public void createNode() throws Exception {
093: CreateIndexChange refactoring = new CreateIndexChange();
094: refactoring.setIndexName("IDX_TEST");
095: refactoring.setTableName("TAB_NAME");
096:
097: ColumnConfig column1 = new ColumnConfig();
098: column1.setName("COL1");
099: refactoring.addColumn(column1);
100:
101: ColumnConfig column2 = new ColumnConfig();
102: column2.setName("COL2");
103: refactoring.addColumn(column2);
104:
105: Element element = refactoring.createNode(DocumentBuilderFactory
106: .newInstance().newDocumentBuilder().newDocument());
107: assertEquals("createIndex", element.getTagName());
108: assertEquals("IDX_TEST", element.getAttribute("indexName"));
109: assertEquals("TAB_NAME", element.getAttribute("tableName"));
110:
111: assertEquals(2, element.getChildNodes().getLength());
112: assertEquals("column", ((Element) element.getChildNodes().item(
113: 0)).getTagName());
114: assertEquals("COL1",
115: ((Element) element.getChildNodes().item(0))
116: .getAttribute("name"));
117: assertEquals("column", ((Element) element.getChildNodes().item(
118: 1)).getTagName());
119: assertEquals("COL2",
120: ((Element) element.getChildNodes().item(1))
121: .getAttribute("name"));
122: }
123: }
|