001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.MockDatabase;
005: import liquibase.database.sql.SetNullableStatement;
006: import liquibase.database.sql.SqlStatement;
007: import liquibase.database.sql.UpdateStatement;
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: /**
017: * Tests for {@link AddNotNullConstraintChange}
018: */
019: public class AddNotNullConstraintChangeTest extends AbstractChangeTest {
020:
021: @Test
022: public void getRefactoringName() throws Exception {
023: assertEquals("Add Not-Null Constraint",
024: new AddNotNullConstraintChange().getChangeName());
025: }
026:
027: @Test
028: public void generateStatement() throws Exception {
029:
030: new DatabaseTestTemplate()
031: .testOnAllDatabases(new DatabaseTest() {
032: public void performTest(Database database)
033: throws Exception {
034: AddNotNullConstraintChange change = new AddNotNullConstraintChange();
035: change.setSchemaName("SCHEMA_NAME");
036: change.setTableName("TABLE_NAME");
037: change.setColumnName("COL_HERE");
038: change.setDefaultNullValue("DEFAULT_VALUE");
039: change.setColumnDataType("varchar(200)");
040:
041: SqlStatement[] sqlStatements = change
042: .generateStatements(new MockDatabase());
043: assertEquals(2, sqlStatements.length);
044: assertTrue(sqlStatements[0] instanceof UpdateStatement);
045: assertTrue(sqlStatements[1] instanceof SetNullableStatement);
046:
047: assertEquals("SCHEMA_NAME",
048: ((UpdateStatement) sqlStatements[0])
049: .getSchemaName());
050: assertEquals("TABLE_NAME",
051: ((UpdateStatement) sqlStatements[0])
052: .getTableName());
053: assertEquals("COL_HERE IS NULL",
054: ((UpdateStatement) sqlStatements[0])
055: .getWhereClause());
056: assertEquals("DEFAULT_VALUE",
057: ((UpdateStatement) sqlStatements[0])
058: .getNewColumnValues().get(
059: "COL_HERE"));
060:
061: assertEquals(
062: "SCHEMA_NAME",
063: ((SetNullableStatement) sqlStatements[1])
064: .getSchemaName());
065: assertEquals(
066: "TABLE_NAME",
067: ((SetNullableStatement) sqlStatements[1])
068: .getTableName());
069: assertEquals(
070: "COL_HERE",
071: ((SetNullableStatement) sqlStatements[1])
072: .getColumnName());
073: assertEquals(
074: "varchar(200)",
075: ((SetNullableStatement) sqlStatements[1])
076: .getColumnDataType());
077: assertFalse(((SetNullableStatement) sqlStatements[1])
078: .isNullable());
079: }
080: });
081: }
082:
083: @Test
084: public void getConfirmationMessage() throws Exception {
085: AddNotNullConstraintChange change = new AddNotNullConstraintChange();
086: change.setTableName("TABLE_NAME");
087: change.setColumnName("COL_HERE");
088:
089: assertEquals(
090: "Null constraint has been added to TABLE_NAME.COL_HERE",
091: change.getConfirmationMessage());
092: }
093:
094: @Test
095: public void createNode() throws Exception {
096: AddNotNullConstraintChange change = new AddNotNullConstraintChange();
097: change.setTableName("TABLE_NAME");
098: change.setColumnName("COL_HERE");
099: change.setDefaultNullValue("DEFAULT_VALUE");
100:
101: Element node = change.createNode(DocumentBuilderFactory
102: .newInstance().newDocumentBuilder().newDocument());
103: assertEquals("addNotNullConstraint", node.getTagName());
104: assertEquals("TABLE_NAME", node.getAttribute("tableName"));
105: assertEquals("COL_HERE", node.getAttribute("columnName"));
106: assertEquals("DEFAULT_VALUE", node
107: .getAttribute("defaultNullValue"));
108: }
109: }
|