001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.MockDatabase;
005: import liquibase.database.sql.AddDefaultValueStatement;
006: import liquibase.database.sql.SqlStatement;
007: import liquibase.test.DatabaseTest;
008: import liquibase.test.DatabaseTestTemplate;
009: import liquibase.util.ISODateFormat;
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: import java.util.Date;
016:
017: public class AddDefaultValueChangeTest extends AbstractChangeTest {
018:
019: @Test
020: public void generateStatement() throws Exception {
021: AddDefaultValueChange change = new AddDefaultValueChange();
022: change.setTableName("TABLE_NAME");
023: change.setColumnName("COLUMN_NAME");
024: change.setDefaultValue("New default value");
025:
026: SqlStatement[] statements = change
027: .generateStatements(new MockDatabase());
028: assertEquals(1, statements.length);
029: AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
030:
031: assertEquals("TABLE_NAME", statement.getTableName());
032: assertEquals("COLUMN_NAME", statement.getColumnName());
033: assertEquals("New default value", statement.getDefaultValue());
034: }
035:
036: @Test
037: public void generateStatements_intDefaultValue() throws Exception {
038: AddDefaultValueChange change = new AddDefaultValueChange();
039: change.setTableName("TABLE_NAME");
040: change.setColumnName("COLUMN_NAME");
041: change.setDefaultValueNumeric("42");
042:
043: SqlStatement[] statements = change
044: .generateStatements(new MockDatabase());
045: assertEquals(1, statements.length);
046: AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
047:
048: assertEquals("TABLE_NAME", statement.getTableName());
049: assertEquals("COLUMN_NAME", statement.getColumnName());
050: assertTrue(statement.getDefaultValue() instanceof Number);
051: assertEquals("42", statement.getDefaultValue().toString());
052: }
053:
054: @Test
055: public void generateStatements_decimalDefaultValue()
056: throws Exception {
057: AddDefaultValueChange change = new AddDefaultValueChange();
058: change.setTableName("TABLE_NAME");
059: change.setColumnName("COLUMN_NAME");
060: change.setDefaultValueNumeric("42.56");
061:
062: SqlStatement[] statements = change
063: .generateStatements(new MockDatabase());
064: assertEquals(1, statements.length);
065: AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
066:
067: assertEquals("TABLE_NAME", statement.getTableName());
068: assertEquals("COLUMN_NAME", statement.getColumnName());
069: assertTrue(statement.getDefaultValue() instanceof Number);
070: assertEquals("42.56", statement.getDefaultValue().toString());
071: }
072:
073: @Test
074: public void generateStatements_booleanDefaultValue_true()
075: throws Exception {
076: AddDefaultValueChange change = new AddDefaultValueChange();
077: change.setTableName("TABLE_NAME");
078: change.setColumnName("COLUMN_NAME");
079: change.setDefaultValueBoolean(Boolean.TRUE);
080:
081: SqlStatement[] statements = change
082: .generateStatements(new MockDatabase());
083: assertEquals(1, statements.length);
084: AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
085:
086: assertEquals("TABLE_NAME", statement.getTableName());
087: assertEquals("COLUMN_NAME", statement.getColumnName());
088: assertTrue(statement.getDefaultValue() instanceof Boolean);
089: assertEquals(Boolean.TRUE, statement.getDefaultValue());
090: }
091:
092: @Test
093: public void generateStatements_booleanDefaultValue_false()
094: throws Exception {
095: AddDefaultValueChange change = new AddDefaultValueChange();
096: change.setTableName("TABLE_NAME");
097: change.setColumnName("COLUMN_NAME");
098: change.setDefaultValueBoolean(Boolean.FALSE);
099:
100: SqlStatement[] statements = change
101: .generateStatements(new MockDatabase());
102: assertEquals(1, statements.length);
103: AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
104:
105: assertEquals("TABLE_NAME", statement.getTableName());
106: assertEquals("COLUMN_NAME", statement.getColumnName());
107: assertTrue(statement.getDefaultValue() instanceof Boolean);
108: assertEquals(Boolean.FALSE, statement.getDefaultValue());
109: }
110:
111: @Test
112: public void generateStatements_dateDefaultValue() throws Exception {
113: new DatabaseTestTemplate()
114: .testOnAllDatabases(new DatabaseTest() {
115:
116: public void performTest(Database database)
117: throws Exception {
118: java.sql.Date date = new java.sql.Date(
119: new Date().getTime());
120:
121: AddDefaultValueChange change = new AddDefaultValueChange();
122: change.setTableName("TABLE_NAME");
123: change.setColumnName("COLUMN_NAME");
124: ISODateFormat dateFormat = new ISODateFormat();
125: change.setDefaultValueDate(dateFormat
126: .format(date));
127:
128: SqlStatement[] statements = change
129: .generateStatements(new MockDatabase());
130: assertEquals(1, statements.length);
131: AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
132:
133: assertEquals("TABLE_NAME", statement
134: .getTableName());
135: assertEquals("COLUMN_NAME", statement
136: .getColumnName());
137: assertTrue(statement.getDefaultValue() instanceof java.sql.Date);
138: assertEquals(date.toString(), statement
139: .getDefaultValue().toString());
140: }
141: });
142: }
143:
144: public void getRefactoringName() throws Exception {
145: assertEquals("Add Default Value", new AddDefaultValueChange()
146: .getChangeName());
147: }
148:
149: @Test
150: public void getConfirmationMessage() throws Exception {
151: AddDefaultValueChange change = new AddDefaultValueChange();
152: change.setSchemaName("SCHEMA_NAME");
153: change.setTableName("TABLE_NAME");
154: change.setColumnName("COLUMN_NAME");
155:
156: assertEquals("Default value added to TABLE_NAME.COLUMN_NAME",
157: change.getConfirmationMessage());
158: }
159:
160: @Test
161: public void createNode() throws Exception {
162: AddDefaultValueChange change = new AddDefaultValueChange();
163: change.setSchemaName("SCHEMA_NAME");
164: change.setTableName("TABLE_NAME");
165: change.setColumnName("COLUMN_NAME");
166: change.setDefaultValue("DEF STRING");
167: change.setDefaultValueNumeric("42");
168: change.setDefaultValueBoolean(true);
169: change.setDefaultValueDate("2007-01-02");
170:
171: Element node = change.createNode(DocumentBuilderFactory
172: .newInstance().newDocumentBuilder().newDocument());
173: assertEquals("addDefaultValue", node.getTagName());
174: assertEquals("SCHEMA_NAME", node.getAttribute("schemaName"));
175: assertEquals("TABLE_NAME", node.getAttribute("tableName"));
176: assertEquals("COLUMN_NAME", node.getAttribute("columnName"));
177: assertEquals("DEF STRING", node.getAttribute("defaultValue"));
178: assertEquals("42", node.getAttribute("defaultValueNumeric"));
179: assertEquals("true", node.getAttribute("defaultValueBoolean"));
180: assertEquals("2007-01-02", node
181: .getAttribute("defaultValueDate"));
182:
183: }
184: }
|