001: package liquibase.change;
002:
003: import liquibase.database.MockDatabase;
004: import liquibase.database.SybaseDatabase;
005: import liquibase.database.sql.AddColumnStatement;
006: import liquibase.database.sql.SqlStatement;
007: import static org.junit.Assert.*;
008: import org.junit.Test;
009: import org.w3c.dom.Document;
010: import org.w3c.dom.Element;
011: import org.w3c.dom.NodeList;
012:
013: import javax.xml.parsers.DocumentBuilderFactory;
014:
015: /**
016: * Tests for {@link AddColumnChange}
017: */
018: public class AddColumnChangeTest extends AbstractChangeTest {
019:
020: @Test
021: public void getRefactoringName() throws Exception {
022: AddColumnChange refactoring = new AddColumnChange();
023: assertEquals("Add Column", refactoring.getChangeName());
024: }
025:
026: @Test
027: public void generateStatement() throws Exception {
028: AddColumnChange refactoring = new AddColumnChange();
029: refactoring.setSchemaName("SCHEMA");
030: refactoring.setTableName("TAB");
031:
032: ColumnConfig column = new ColumnConfig();
033: column.setName("NEWCOL");
034: column.setType("TYP");
035:
036: ConstraintsConfig constraints = new ConstraintsConfig();
037: constraints.setNullable(Boolean.FALSE);
038:
039: column.setConstraints(constraints);
040:
041: refactoring.addColumn(column);
042:
043: //Add the second column def to the same refactoring
044: column = new ColumnConfig();
045: column.setName("NEWCOL2");
046: column.setType("TYP2");
047: column.setConstraints(new ConstraintsConfig());
048: refactoring.addColumn(column);
049:
050: SqlStatement[] sqlStatements = refactoring
051: .generateStatements(new MockDatabase());
052: assertEquals(2, sqlStatements.length);
053: assertTrue(sqlStatements[0] instanceof AddColumnStatement);
054: assertTrue(sqlStatements[1] instanceof AddColumnStatement);
055:
056: assertEquals("SCHEMA", ((AddColumnStatement) sqlStatements[0])
057: .getSchemaName());
058: assertEquals("TAB", ((AddColumnStatement) sqlStatements[0])
059: .getTableName());
060: assertEquals("NEWCOL", ((AddColumnStatement) sqlStatements[0])
061: .getColumnName());
062: assertEquals("TYP", ((AddColumnStatement) sqlStatements[0])
063: .getColumnType());
064: assertFalse(((AddColumnStatement) sqlStatements[0])
065: .isPrimaryKey());
066: assertFalse(((AddColumnStatement) sqlStatements[0])
067: .isNullable());
068:
069: assertEquals("SCHEMA", ((AddColumnStatement) sqlStatements[1])
070: .getSchemaName());
071: assertEquals("TAB", ((AddColumnStatement) sqlStatements[1])
072: .getTableName());
073: assertEquals("NEWCOL2", ((AddColumnStatement) sqlStatements[1])
074: .getColumnName());
075: assertEquals("TYP2", ((AddColumnStatement) sqlStatements[1])
076: .getColumnType());
077: assertTrue(((AddColumnStatement) sqlStatements[1]).isNullable());
078: }
079:
080: @Test
081: public void generateStatement_nullable() throws Exception {
082: AddColumnChange refactoring = new AddColumnChange();
083: refactoring.setSchemaName("SCHEMA");
084: refactoring.setTableName("TAB");
085: ColumnConfig column = new ColumnConfig();
086: column.setName("NEWCOL");
087: column.setType("TYP");
088:
089: ConstraintsConfig constraints = new ConstraintsConfig();
090: constraints.setNullable(Boolean.TRUE);
091:
092: column.setConstraints(constraints);
093:
094: refactoring.addColumn(column);
095:
096: SqlStatement[] sqlStatements = refactoring
097: .generateStatements(new MockDatabase());
098: assertEquals(1, sqlStatements.length);
099: assertTrue(sqlStatements[0] instanceof AddColumnStatement);
100:
101: assertEquals("SCHEMA", ((AddColumnStatement) sqlStatements[0])
102: .getSchemaName());
103: assertEquals("TAB", ((AddColumnStatement) sqlStatements[0])
104: .getTableName());
105: assertEquals("NEWCOL", ((AddColumnStatement) sqlStatements[0])
106: .getColumnName());
107: assertEquals("TYP", ((AddColumnStatement) sqlStatements[0])
108: .getColumnType());
109: assertFalse(((AddColumnStatement) sqlStatements[0])
110: .isPrimaryKey());
111: assertTrue(((AddColumnStatement) sqlStatements[0]).isNullable());
112: }
113:
114: @Test
115: public void generateStatement_notNull() throws Exception {
116: AddColumnChange refactoring = new AddColumnChange();
117: refactoring.setSchemaName("SCHEMA");
118: refactoring.setTableName("TAB");
119: ColumnConfig column = new ColumnConfig();
120: column.setName("NEWCOL");
121: column.setType("TYP");
122:
123: ConstraintsConfig constraints = new ConstraintsConfig();
124: constraints.setNullable(Boolean.FALSE);
125:
126: column.setConstraints(constraints);
127:
128: refactoring.addColumn(column);
129:
130: SqlStatement[] sqlStatements = refactoring
131: .generateStatements(new MockDatabase());
132: assertEquals(1, sqlStatements.length);
133: assertTrue(sqlStatements[0] instanceof AddColumnStatement);
134:
135: assertEquals("SCHEMA", ((AddColumnStatement) sqlStatements[0])
136: .getSchemaName());
137: assertEquals("TAB", ((AddColumnStatement) sqlStatements[0])
138: .getTableName());
139: assertEquals("NEWCOL", ((AddColumnStatement) sqlStatements[0])
140: .getColumnName());
141: assertEquals("TYP", ((AddColumnStatement) sqlStatements[0])
142: .getColumnType());
143: assertFalse(((AddColumnStatement) sqlStatements[0])
144: .isPrimaryKey());
145: assertFalse(((AddColumnStatement) sqlStatements[0])
146: .isNullable());
147: }
148:
149: @Test
150: public void generateStatement_primaryKey() throws Exception {
151: AddColumnChange refactoring = new AddColumnChange();
152: refactoring.setSchemaName("SCHEMA");
153: refactoring.setTableName("TAB");
154: ColumnConfig column = new ColumnConfig();
155: column.setName("NEWCOL");
156: column.setType("TYP");
157:
158: ConstraintsConfig constraints = new ConstraintsConfig();
159: constraints.setNullable(Boolean.FALSE);
160: constraints.setPrimaryKey(Boolean.TRUE);
161:
162: column.setConstraints(constraints);
163:
164: refactoring.addColumn(column);
165:
166: SqlStatement[] sqlStatements = refactoring
167: .generateStatements(new MockDatabase());
168: assertEquals(2, sqlStatements.length);
169: assertTrue(sqlStatements[0] instanceof AddColumnStatement);
170: assertFalse(((AddColumnStatement) sqlStatements[0])
171: .isPrimaryKey());
172:
173: //todo: check add primary key statements after they have been refactored
174: }
175:
176: @Test
177: public void generateStatement_autoIncrement() throws Exception {
178: AddColumnChange refactoring = new AddColumnChange();
179: refactoring.setSchemaName("SCHEMA");
180: refactoring.setTableName("TAB");
181: ColumnConfig column = new ColumnConfig();
182: column.setName("NEWCOL");
183: column.setType("TYP");
184:
185: ConstraintsConfig constraints = new ConstraintsConfig();
186: constraints.setNullable(Boolean.FALSE);
187: constraints.setPrimaryKey(Boolean.TRUE);
188: column.setAutoIncrement(Boolean.TRUE);
189:
190: column.setConstraints(constraints);
191:
192: refactoring.addColumn(column);
193:
194: SqlStatement[] sqlStatements = refactoring
195: .generateStatements(new MockDatabase());
196: assertEquals(2, sqlStatements.length);
197: assertTrue(sqlStatements[0] instanceof AddColumnStatement);
198: assertFalse(((AddColumnStatement) sqlStatements[0])
199: .isPrimaryKey());
200:
201: //todo: check add primary key and auto-increment statements after they have been refactored
202: }
203:
204: @Test
205: public void getConfirmationMessage() throws Exception {
206: AddColumnChange refactoring = new AddColumnChange();
207: refactoring.setTableName("TAB");
208: ColumnConfig column = new ColumnConfig();
209: column.setName("NEWCOL");
210: column.setType("TYP");
211: refactoring.addColumn(column);
212:
213: assertEquals("Columns NEWCOL(TYP) added to TAB", refactoring
214: .getConfirmationMessage());
215: }
216:
217: @Test
218: public void createNode() throws Exception {
219: Document document = DocumentBuilderFactory.newInstance()
220: .newDocumentBuilder().newDocument();
221:
222: AddColumnChange refactoring = new AddColumnChange();
223: refactoring.setTableName("TAB");
224: ColumnConfig column = new ColumnConfig();
225: column.setName("NEWCOL");
226: column.setType("TYP");
227: refactoring.addColumn(column);
228:
229: Element node = refactoring.createNode(document);
230: assertEquals("addColumn", node.getTagName());
231: assertEquals("TAB", node.getAttribute("tableName"));
232:
233: NodeList columns = node.getElementsByTagName("column");
234: assertEquals(1, columns.getLength());
235: assertEquals("column", ((Element) columns.item(0)).getTagName());
236: assertEquals("NEWCOL", ((Element) columns.item(0))
237: .getAttribute("name"));
238: assertEquals("TYP", ((Element) columns.item(0))
239: .getAttribute("type"));
240:
241: }
242:
243: @Test
244: public void sybaseNull() throws Exception {
245: AddColumnChange refactoring = new AddColumnChange();
246: refactoring.setTableName("TAB");
247: ColumnConfig column = new ColumnConfig();
248: column.setName("NEWCOL");
249: column.setType("TYP");
250: refactoring.addColumn(column);
251:
252: SybaseDatabase db = new SybaseDatabase();
253: assertEquals("ALTER TABLE [TAB] ADD NEWCOL TYP NULL",
254: refactoring.generateStatements(db)[0]
255: .getSqlStatement(db));
256: }
257:
258: @Test
259: public void sybaseNotNull() throws Exception {
260: AddColumnChange refactoring = new AddColumnChange();
261: refactoring.setTableName("TAB");
262: ColumnConfig column = new ColumnConfig();
263: column.setName("NEWCOL");
264: column.setType("TYP");
265: refactoring.addColumn(column);
266:
267: ConstraintsConfig constraints = new ConstraintsConfig();
268: constraints.setPrimaryKey(Boolean.FALSE);
269: constraints.setNullable(Boolean.FALSE);
270:
271: column.setConstraints(constraints);
272:
273: SybaseDatabase database = new SybaseDatabase();
274: assertEquals("ALTER TABLE [TAB] ADD NEWCOL TYP NOT NULL",
275: refactoring.generateStatements(database)[0]
276: .getSqlStatement(database));
277:
278: }
279:
280: @Test
281: public void sybaseConstraintsNull() throws Exception {
282: AddColumnChange refactoring = new AddColumnChange();
283: refactoring.setTableName("TAB");
284: ColumnConfig column = new ColumnConfig();
285: column.setName("NEWCOL");
286: column.setType("TYP");
287: refactoring.addColumn(column);
288:
289: ConstraintsConfig constraints = new ConstraintsConfig();
290: constraints.setPrimaryKey(Boolean.FALSE);
291: constraints.setNullable(Boolean.TRUE);
292:
293: column.setConstraints(constraints);
294:
295: SybaseDatabase database = new SybaseDatabase();
296: assertEquals("ALTER TABLE [TAB] ADD NEWCOL TYP NULL",
297: refactoring.generateStatements(database)[0]
298: .getSqlStatement(database));
299:
300: }
301: }
|