001: package liquibase.change;
002:
003: import liquibase.database.MockDatabase;
004: import liquibase.database.sql.CreateTableStatement;
005: import liquibase.database.sql.ForeignKeyConstraint;
006: import liquibase.database.sql.SqlStatement;
007: import liquibase.database.sql.UniqueConstraint;
008: import liquibase.database.structure.Column;
009: import liquibase.database.structure.DatabaseObject;
010: import liquibase.database.structure.Table;
011: import static org.junit.Assert.*;
012: import org.junit.Before;
013: import org.junit.Test;
014: import org.w3c.dom.Element;
015:
016: import javax.xml.parsers.DocumentBuilderFactory;
017: import java.util.Set;
018:
019: /**
020: * Tests for {@link CreateTableChange}
021: */
022: public class CreateTableChangeTest extends AbstractChangeTest {
023:
024: private CreateTableChange change;
025:
026: @Before
027: public void setUp() throws Exception {
028: change = new CreateTableChange();
029: }
030:
031: @Test
032: public void getRefactoringName() throws Exception {
033: assertEquals("Create Table", change.getChangeName());
034: }
035:
036: @Test
037: public void generateStatement() throws Exception {
038: change.setTableName("TABLE_NAME");
039:
040: ColumnConfig column1 = new ColumnConfig();
041: column1.setName("id");
042: column1.setType("int");
043: ConstraintsConfig column1constraints = new ConstraintsConfig();
044: column1constraints.setPrimaryKey(true);
045: column1constraints.setNullable(false);
046: column1.setConstraints(column1constraints);
047: change.addColumn(column1);
048:
049: ColumnConfig column2 = new ColumnConfig();
050: column2.setName("name");
051: column2.setType("varchar(255)");
052: change.addColumn(column2);
053:
054: ColumnConfig column3 = new ColumnConfig();
055: column3.setName("state_id");
056: ConstraintsConfig column3constraints = new ConstraintsConfig();
057: column3constraints.setNullable(false);
058: column3constraints.setInitiallyDeferred(true);
059: column3constraints.setDeferrable(true);
060: column3constraints.setForeignKeyName("fk_tab_ref");
061: column3constraints.setReferences("state(id)");
062: column3.setConstraints(column3constraints);
063: change.addColumn(column3);
064:
065: ColumnConfig column4 = new ColumnConfig();
066: column4.setName("phone");
067: column4.setType("varchar(255)");
068: column4.setDefaultValue("NOPHONE");
069: change.addColumn(column4);
070:
071: ColumnConfig column5 = new ColumnConfig();
072: column5.setName("phone2");
073: column5.setType("varchar(255)");
074: ConstraintsConfig column5constraints = new ConstraintsConfig();
075: column5constraints.setUnique(true);
076: column5.setConstraints(column5constraints);
077: change.addColumn(column5);
078:
079: SqlStatement[] statements = change
080: .generateStatements(new MockDatabase());
081: assertEquals(1, statements.length);
082: assertTrue(statements[0] instanceof CreateTableStatement);
083: CreateTableStatement statement = (CreateTableStatement) statements[0];
084: assertEquals("TABLE_NAME", statement.getTableName());
085: assertTrue(statement.getColumns().contains("id"));
086: assertTrue(statement.getColumns().contains("state_id"));
087: assertTrue(statement.getColumns().contains("phone"));
088: assertTrue(statement.getColumns().contains("phone2"));
089:
090: assertEquals(1, statement.getPrimaryKeyConstraint()
091: .getColumns().size());
092: assertEquals("id", statement.getPrimaryKeyConstraint()
093: .getColumns().iterator().next());
094:
095: assertEquals(1, statement.getUniqueConstraints().size());
096: UniqueConstraint uniqueConstraint = statement
097: .getUniqueConstraints().iterator().next();
098: assertEquals(1, uniqueConstraint.getColumns().size());
099: assertEquals("phone2", uniqueConstraint.getColumns().iterator()
100: .next());
101:
102: assertEquals(2, statement.getNotNullColumns().size());
103:
104: assertEquals(1, statement.getForeignKeyConstraints().size());
105: ForeignKeyConstraint keyConstraint = statement
106: .getForeignKeyConstraints().iterator().next();
107: assertEquals("fk_tab_ref", keyConstraint.getForeignKeyName());
108: assertEquals("state_id", keyConstraint.getColumn());
109: assertEquals("state(id)", keyConstraint.getReferences());
110: assertTrue(keyConstraint.isDeferrable());
111: assertTrue(keyConstraint.isInitiallyDeferred());
112: }
113:
114: @Test
115: public void getConfirmationMessage() throws Exception {
116: change.setTableName("TAB_NAME");
117: assertEquals("Table TAB_NAME created", change
118: .getConfirmationMessage());
119: }
120:
121: @Test
122: public void createNode() throws Exception {
123: change.setTableName("TABLE_NAME");
124:
125: ColumnConfig column1 = new ColumnConfig();
126: column1.setName("id");
127: column1.setType("int");
128: ConstraintsConfig column1constraints = new ConstraintsConfig();
129: column1constraints.setPrimaryKey(true);
130: column1constraints.setNullable(false);
131: column1.setConstraints(column1constraints);
132: change.addColumn(column1);
133:
134: ColumnConfig column2 = new ColumnConfig();
135: column2.setName("name");
136: column2.setType("varchar(255)");
137: change.addColumn(column2);
138:
139: ColumnConfig column3 = new ColumnConfig();
140: column3.setName("state_id");
141: ConstraintsConfig column3constraints = new ConstraintsConfig();
142: column3constraints.setNullable(false);
143: column3constraints.setInitiallyDeferred(true);
144: column3constraints.setDeferrable(true);
145: column3constraints.setForeignKeyName("fk_tab_ref");
146: column3constraints.setReferences("state(id)");
147: column3.setConstraints(column3constraints);
148: change.addColumn(column3);
149:
150: ColumnConfig column4 = new ColumnConfig();
151: column4.setName("phone");
152: column4.setType("varchar(255)");
153: column4.setDefaultValue("NOPHONE");
154: change.addColumn(column4);
155:
156: ColumnConfig column5 = new ColumnConfig();
157: column5.setName("phone2");
158: column5.setType("varchar(255)");
159: ConstraintsConfig column5constraints = new ConstraintsConfig();
160: column5constraints.setUnique(true);
161: column5.setConstraints(column5constraints);
162: change.addColumn(column5);
163:
164: Element element = change.createNode(DocumentBuilderFactory
165: .newInstance().newDocumentBuilder().newDocument());
166: assertEquals("createTable", element.getTagName());
167: assertEquals(5, element.getChildNodes().getLength());
168:
169: Element columnElement = ((Element) element.getChildNodes()
170: .item(0));
171: assertEquals("column", columnElement.getTagName());
172: assertEquals("id", columnElement.getAttribute("name"));
173: assertEquals("int", columnElement.getAttribute("type"));
174: Element constraintsElement = (Element) columnElement
175: .getChildNodes().item(0);
176: assertEquals("constraints", constraintsElement.getTagName());
177: assertEquals(2, constraintsElement.getAttributes().getLength());
178: assertEquals("true", constraintsElement
179: .getAttribute("primaryKey"));
180: assertEquals("false", constraintsElement
181: .getAttribute("nullable"));
182:
183: columnElement = ((Element) element.getChildNodes().item(1));
184: assertEquals("column", columnElement.getTagName());
185: assertEquals("name", columnElement.getAttribute("name"));
186: assertEquals("varchar(255)", columnElement.getAttribute("type"));
187:
188: columnElement = ((Element) element.getChildNodes().item(2));
189: assertEquals("column", columnElement.getTagName());
190: assertEquals("state_id", columnElement.getAttribute("name"));
191: constraintsElement = (Element) columnElement.getChildNodes()
192: .item(0);
193: assertEquals("constraints", constraintsElement.getTagName());
194: assertEquals(5, constraintsElement.getAttributes().getLength());
195: assertEquals("false", constraintsElement
196: .getAttribute("nullable"));
197: assertEquals("true", constraintsElement
198: .getAttribute("deferrable"));
199: assertEquals("true", constraintsElement
200: .getAttribute("initiallyDeferred"));
201: assertEquals("fk_tab_ref", constraintsElement
202: .getAttribute("foreignKeyName"));
203: assertEquals("state(id)", constraintsElement
204: .getAttribute("references"));
205:
206: columnElement = ((Element) element.getChildNodes().item(3));
207: assertEquals("column", columnElement.getTagName());
208: assertEquals("phone", columnElement.getAttribute("name"));
209: assertEquals("varchar(255)", columnElement.getAttribute("type"));
210:
211: columnElement = ((Element) element.getChildNodes().item(4));
212: assertEquals("column", columnElement.getTagName());
213: assertEquals("phone2", columnElement.getAttribute("name"));
214: assertEquals("varchar(255)", columnElement.getAttribute("type"));
215: constraintsElement = (Element) columnElement.getChildNodes()
216: .item(0);
217: assertEquals("constraints", constraintsElement.getTagName());
218: assertEquals(1, constraintsElement.getAttributes().getLength());
219: assertEquals("true", constraintsElement.getAttribute("unique"));
220: }
221:
222: @Test
223: public void defaultValue_none() throws Exception {
224: CreateTableChange change = new CreateTableChange();
225: ColumnConfig columnConfig = new ColumnConfig();
226: columnConfig.setName("id");
227: change.addColumn(columnConfig);
228:
229: CreateTableStatement statement = (CreateTableStatement) change
230: .generateStatements(new MockDatabase())[0];
231: assertNull(statement.getDefaultValue("id"));
232: }
233:
234: @Test
235: public void defaultValue_string() throws Exception {
236: CreateTableChange change = new CreateTableChange();
237: ColumnConfig columnConfig = new ColumnConfig();
238: columnConfig.setName("id");
239: columnConfig.setDefaultValue("DEFAULTVALUE");
240: change.addColumn(columnConfig);
241:
242: CreateTableStatement statement = (CreateTableStatement) change
243: .generateStatements(new MockDatabase())[0];
244: assertEquals("'DEFAULTVALUE'", statement.getDefaultValue("id"));
245: }
246:
247: @Test
248: public void defaultValue_boolean() throws Exception {
249: CreateTableChange change = new CreateTableChange();
250: ColumnConfig columnConfig = new ColumnConfig();
251: columnConfig.setName("id");
252: columnConfig.setDefaultValueBoolean(Boolean.TRUE);
253: change.addColumn(columnConfig);
254:
255: CreateTableStatement statement = (CreateTableStatement) change
256: .generateStatements(new MockDatabase())[0];
257: assertEquals("'" + new MockDatabase().getTrueBooleanValue()
258: + "'", statement.getDefaultValue("id"));
259: }
260:
261: @Test
262: public void defaultValue_numeric() throws Exception {
263: CreateTableChange change = new CreateTableChange();
264: ColumnConfig columnConfig = new ColumnConfig();
265: columnConfig.setName("id");
266: columnConfig.setDefaultValueNumeric("42");
267: change.addColumn(columnConfig);
268:
269: CreateTableStatement statement = (CreateTableStatement) change
270: .generateStatements(new MockDatabase())[0];
271: assertEquals("42", statement.getDefaultValue("id"));
272: }
273:
274: @Test
275: public void defaultValue_date() throws Exception {
276: CreateTableChange change = new CreateTableChange();
277: ColumnConfig columnConfig = new ColumnConfig();
278: columnConfig.setName("id");
279: columnConfig.setDefaultValueDate("2007-01-02");
280: change.addColumn(columnConfig);
281:
282: CreateTableStatement statement = (CreateTableStatement) change
283: .generateStatements(new MockDatabase())[0];
284: assertEquals("2007-01-02", statement.getDefaultValue("id"));
285: }
286:
287: @Test
288: public void createInverse() {
289: CreateTableChange change = new CreateTableChange();
290: change.setTableName("TestTable");
291:
292: Change[] inverses = change.createInverses();
293: assertEquals(1, inverses.length);
294: assertTrue(inverses[0] instanceof DropTableChange);
295: assertEquals("TestTable", ((DropTableChange) inverses[0])
296: .getTableName());
297: }
298:
299: @Test
300: public void tableSpace_none() throws Exception {
301: CreateTableChange change = new CreateTableChange();
302:
303: CreateTableStatement statement = (CreateTableStatement) change
304: .generateStatements(new MockDatabase())[0];
305: assertNull(statement.getTablespace());
306: }
307:
308: @Test
309: public void tableSpace_set() throws Exception {
310: CreateTableChange change = new CreateTableChange();
311: change.setTablespace("TESTTABLESPACE");
312:
313: CreateTableStatement statement = (CreateTableStatement) change
314: .generateStatements(new MockDatabase())[0];
315: assertEquals("TESTTABLESPACE", statement.getTablespace());
316: }
317:
318: @Test
319: public void getAffectedDatabaseObjects() {
320: CreateTableChange change = new CreateTableChange();
321: change.setTableName("testTable");
322:
323: ColumnConfig column = new ColumnConfig();
324: column.setName("id");
325: change.addColumn(column);
326:
327: column = new ColumnConfig();
328: column.setName("id2");
329: change.addColumn(column);
330:
331: Set<DatabaseObject> affectedDatabaseObjects = change
332: .getAffectedDatabaseObjects();
333: assertEquals(3, affectedDatabaseObjects.size());
334: for (DatabaseObject object : affectedDatabaseObjects) {
335: if (object instanceof Table) {
336: assertEquals("testTable", ((Table) object).getName());
337: } else {
338: assertEquals("testTable", ((Column) object).getTable()
339: .getName());
340: String columnName = ((Column) object).getName();
341: assertTrue(columnName.equals("id")
342: || columnName.equals("id2"));
343: }
344: }
345: }
346:
347: @Test
348: public void foreignKey_deferrable() throws Exception {
349: CreateTableChange change = new CreateTableChange();
350: ColumnConfig columnConfig = new ColumnConfig();
351: columnConfig.setName("id");
352: ConstraintsConfig constraints = new ConstraintsConfig();
353: constraints.setForeignKeyName("fk_test");
354: constraints.setReferences("test(id)");
355: constraints.setDeferrable(true);
356: constraints.setInitiallyDeferred(true);
357: columnConfig.setConstraints(constraints);
358: change.addColumn(columnConfig);
359:
360: CreateTableStatement statement = (CreateTableStatement) change
361: .generateStatements(new MockDatabase())[0];
362: ForeignKeyConstraint keyConstraint = statement
363: .getForeignKeyConstraints().iterator().next();
364: assertTrue(keyConstraint.isDeferrable());
365: assertTrue(keyConstraint.isInitiallyDeferred());
366: }
367:
368: @Test
369: public void foreignKey_notDeferrable() throws Exception {
370: CreateTableChange change = new CreateTableChange();
371: ColumnConfig columnConfig = new ColumnConfig();
372: columnConfig.setName("id");
373: ConstraintsConfig constraints = new ConstraintsConfig();
374: constraints.setForeignKeyName("fk_test");
375: constraints.setReferences("test(id)");
376: constraints.setDeferrable(false);
377: constraints.setInitiallyDeferred(false);
378: columnConfig.setConstraints(constraints);
379: change.addColumn(columnConfig);
380:
381: CreateTableStatement statement = (CreateTableStatement) change
382: .generateStatements(new MockDatabase())[0];
383: ForeignKeyConstraint keyConstraint = statement
384: .getForeignKeyConstraints().iterator().next();
385: assertFalse(keyConstraint.isDeferrable());
386: assertFalse(keyConstraint.isInitiallyDeferred());
387: }
388:
389: @Test
390: public void foreignKey_defaultDeferrable() throws Exception {
391: CreateTableChange change = new CreateTableChange();
392: ColumnConfig columnConfig = new ColumnConfig();
393: columnConfig.setName("id");
394: ConstraintsConfig constraints = new ConstraintsConfig();
395: constraints.setReferences("test(id)");
396: constraints.setForeignKeyName("fk_test");
397: columnConfig.setConstraints(constraints);
398: change.addColumn(columnConfig);
399:
400: CreateTableStatement statement = (CreateTableStatement) change
401: .generateStatements(new MockDatabase())[0];
402: ForeignKeyConstraint keyConstraint = statement
403: .getForeignKeyConstraints().iterator().next();
404: assertFalse(keyConstraint.isDeferrable());
405: assertFalse(keyConstraint.isInitiallyDeferred());
406: }
407: }
|