001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.PostgresDatabase;
005: import liquibase.database.sql.*;
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: public class AddAutoIncrementChangeTest extends AbstractChangeTest {
015:
016: @Test
017: public void constructor() {
018: AddAutoIncrementChange change = new AddAutoIncrementChange();
019: assertEquals("addAutoIncrement", change.getTagName());
020: assertEquals("Set Column as Auto-Increment", change
021: .getChangeName());
022: }
023:
024: @Test
025: public void generateStatement() throws Exception {
026: new DatabaseTestTemplate()
027: .testOnAllDatabases(new DatabaseTest() {
028: public void performTest(Database database)
029: throws Exception {
030: AddAutoIncrementChange change = new AddAutoIncrementChange();
031: change.setSchemaName("SCHEMA_NAME");
032: change.setTableName("TABLE_NAME");
033: change.setColumnName("COLUMN_NAME");
034: change.setColumnDataType("DATATYPE(255)");
035:
036: SqlStatement[] sqlStatements = change
037: .generateStatements(database);
038: if (database instanceof PostgresDatabase) {
039: assertEquals(3, sqlStatements.length);
040: //todo: improve test as statements are no longer raw statements
041: assertTrue(sqlStatements[0] instanceof CreateSequenceStatement);
042: assertTrue(sqlStatements[1] instanceof SetNullableStatement);
043: assertTrue(sqlStatements[2] instanceof AddDefaultValueStatement);
044: } else {
045: assertEquals(1, sqlStatements.length);
046: assertTrue(sqlStatements[0] instanceof AddAutoIncrementStatement);
047: assertEquals(
048: "SCHEMA_NAME",
049: ((AddAutoIncrementStatement) sqlStatements[0])
050: .getSchemaName());
051: assertEquals(
052: "TABLE_NAME",
053: ((AddAutoIncrementStatement) sqlStatements[0])
054: .getTableName());
055: assertEquals(
056: "COLUMN_NAME",
057: ((AddAutoIncrementStatement) sqlStatements[0])
058: .getColumnName());
059: assertEquals(
060: "DATATYPE(255)",
061: ((AddAutoIncrementStatement) sqlStatements[0])
062: .getColumnDataType());
063: }
064: }
065: });
066: }
067:
068: @Test
069: public void getRefactoringName() throws Exception {
070: assertEquals("Set Column as Auto-Increment",
071: new AddAutoIncrementChange().getChangeName());
072: }
073:
074: @Test
075: public void getConfirmationMessage() throws Exception {
076: AddAutoIncrementChange change = new AddAutoIncrementChange();
077: change.setSchemaName("SCHEMA_NAME");
078: change.setTableName("TABLE_NAME");
079: change.setColumnName("COLUMN_NAME");
080: change.setColumnDataType("DATATYPE(255)");
081:
082: assertEquals("Auto-increment added to TABLE_NAME.COLUMN_NAME",
083: change.getConfirmationMessage());
084: }
085:
086: @Test
087: public void createNode() throws Exception {
088: AddAutoIncrementChange change = new AddAutoIncrementChange();
089: change.setSchemaName("SCHEMA_NAME");
090: change.setTableName("TABLE_NAME");
091: change.setColumnName("COLUMN_NAME");
092: change.setColumnDataType("DATATYPE(255)");
093:
094: Element node = change.createNode(DocumentBuilderFactory
095: .newInstance().newDocumentBuilder().newDocument());
096: assertEquals("addAutoIncrement", node.getTagName());
097: assertEquals("SCHEMA_NAME", node.getAttribute("schemaName"));
098: assertEquals("TABLE_NAME", node.getAttribute("tableName"));
099: assertEquals("COLUMN_NAME", node.getAttribute("columnName"));
100: assertEquals("DATATYPE(255)", node
101: .getAttribute("columnDataType"));
102: }
103: }
|