View Javadoc

1   package liquibase.change.core;
2   
3   import liquibase.change.AbstractChangeTest;
4   import liquibase.change.Change;
5   import liquibase.change.ColumnConfig;
6   import liquibase.change.ConstraintsConfig;
7   import liquibase.database.core.MockDatabase;
8   import liquibase.database.typeconversion.core.DefaultTypeConverter;
9   import liquibase.statement.ForeignKeyConstraint;
10  import liquibase.statement.SqlStatement;
11  import liquibase.statement.UniqueConstraint;
12  import liquibase.statement.core.CreateTableStatement;
13  import static org.junit.Assert.*;
14  import org.junit.Before;
15  import org.junit.Test;
16  
17  /**
18   * Tests for {@link CreateTableChange}
19   */
20  public class CreateTableChangeTest extends AbstractChangeTest {
21  
22      private CreateTableChange change;
23  
24      @Before
25      public void setUp() throws Exception {
26          change = new CreateTableChange();
27      }
28  
29      @Override
30      @Test
31      public void getRefactoringName() throws Exception {
32          assertEquals("Create Table", change.getChangeMetaData().getDescription());
33      }
34  
35      @Override
36      @Test
37      public void generateStatement() throws Exception {
38          change.setTableName("TABLE_NAME");
39  
40          ColumnConfig column1 = new ColumnConfig();
41          column1.setName("id");
42          column1.setType("int");
43          ConstraintsConfig column1constraints = new ConstraintsConfig();
44          column1constraints.setPrimaryKey(true);
45          column1constraints.setNullable(false);
46          column1.setConstraints(column1constraints);
47          change.addColumn(column1);
48  
49          ColumnConfig column2 = new ColumnConfig();
50          column2.setName("name");
51          column2.setType("varchar(255)");
52          change.addColumn(column2);
53  
54          ColumnConfig column3 = new ColumnConfig();
55          column3.setName("state_id");
56          column3.setType("int");
57          ConstraintsConfig column3constraints = new ConstraintsConfig();
58          column3constraints.setNullable(false);
59          column3constraints.setInitiallyDeferred(true);
60          column3constraints.setDeferrable(true);
61          column3constraints.setForeignKeyName("fk_tab_ref");
62          column3constraints.setReferences("state(id)");
63          column3.setConstraints(column3constraints);
64          change.addColumn(column3);
65  
66          ColumnConfig column4 = new ColumnConfig();
67          column4.setName("phone");
68          column4.setType("varchar(255)");
69          column4.setDefaultValue("NOPHONE");
70          change.addColumn(column4);
71  
72          ColumnConfig column5 = new ColumnConfig();
73          column5.setName("phone2");
74          column5.setType("varchar(255)");
75          ConstraintsConfig column5constraints = new ConstraintsConfig();
76          column5constraints.setUnique(true);
77          column5.setConstraints(column5constraints);
78          change.addColumn(column5);
79  
80  
81          SqlStatement[] statements = change.generateStatements(new MockDatabase());
82          assertEquals(1, statements.length);
83          assertTrue(statements[0] instanceof CreateTableStatement);
84          CreateTableStatement statement = (CreateTableStatement) statements[0];
85          assertEquals("TABLE_NAME", statement.getTableName());
86          assertTrue(statement.getColumns().contains("id"));
87          assertTrue(statement.getColumns().contains("state_id"));
88          assertTrue(statement.getColumns().contains("phone"));
89          assertTrue(statement.getColumns().contains("phone2"));
90  
91          assertEquals(1, statement.getPrimaryKeyConstraint().getColumns().size());
92          assertEquals("id", statement.getPrimaryKeyConstraint().getColumns().iterator().next());
93  
94          assertEquals(1, statement.getUniqueConstraints().size());
95          UniqueConstraint uniqueConstraint = statement.getUniqueConstraints().iterator().next();
96          assertEquals(1, uniqueConstraint.getColumns().size());
97          assertEquals("phone2", uniqueConstraint.getColumns().iterator().next());
98  
99          assertEquals(2, statement.getNotNullColumns().size());
100 
101         assertEquals(1, statement.getForeignKeyConstraints().size());
102         ForeignKeyConstraint keyConstraint = statement.getForeignKeyConstraints().iterator().next();
103         assertEquals("fk_tab_ref", keyConstraint.getForeignKeyName());
104         assertEquals("state_id", keyConstraint.getColumn());
105         assertEquals("state(id)", keyConstraint.getReferences());
106         assertTrue(keyConstraint.isDeferrable());
107         assertTrue(keyConstraint.isInitiallyDeferred());
108     }
109 
110     @Override
111     @Test
112     public void getConfirmationMessage() throws Exception {
113         change.setTableName("TAB_NAME");
114         assertEquals("Table TAB_NAME created", change.getConfirmationMessage());
115     }
116 
117     @Test
118     public void defaultValue_none() throws Exception {
119         CreateTableChange change = new CreateTableChange();
120         ColumnConfig columnConfig = new ColumnConfig();
121         columnConfig.setName("id");
122         columnConfig.setType("int");
123         change.addColumn(columnConfig);
124 
125         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
126         assertNull(statement.getDefaultValue("id"));
127     }
128     
129     @Test
130     public void defaultValue_string() throws Exception {
131         CreateTableChange change = new CreateTableChange();
132         ColumnConfig columnConfig = new ColumnConfig();
133         columnConfig.setName("id");
134         columnConfig.setType("int");
135         columnConfig.setDefaultValue("DEFAULTVALUE");
136         change.addColumn(columnConfig);
137 
138         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
139         assertEquals("DEFAULTVALUE", statement.getDefaultValue("id"));
140     }
141 
142     @Test
143     public void defaultValue_boolean() throws Exception {
144         CreateTableChange change = new CreateTableChange();
145         ColumnConfig columnConfig = new ColumnConfig();
146         columnConfig.setName("id");
147         columnConfig.setType("boolean");
148         columnConfig.setDefaultValueBoolean(Boolean.TRUE);
149         change.addColumn(columnConfig);
150 
151         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
152         assertEquals(true, statement.getDefaultValue("id"));
153     }
154 
155     @Test
156     public void defaultValue_numeric() throws Exception {
157         CreateTableChange change = new CreateTableChange();
158         ColumnConfig columnConfig = new ColumnConfig();
159         columnConfig.setName("id");
160         columnConfig.setType("int");
161         columnConfig.setDefaultValueNumeric("42");
162         change.addColumn(columnConfig);
163 
164         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
165         assertEquals(42L, statement.getDefaultValue("id"));
166     }
167 
168     @Test
169     public void defaultValue_date() throws Exception {
170         CreateTableChange change = new CreateTableChange();
171         ColumnConfig columnConfig = new ColumnConfig();
172         columnConfig.setName("id");
173         columnConfig.setType("date");
174         columnConfig.setDefaultValueDate("2007-01-02");
175         change.addColumn(columnConfig);
176 
177         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
178         assertEquals(columnConfig.getDefaultValueDate(), statement.getDefaultValue("id"));
179     }
180 
181     @Test
182     public void createInverse() {
183         CreateTableChange change = new CreateTableChange();
184         change.setTableName("TestTable");
185 
186         Change[] inverses = change.createInverses();
187         assertEquals(1, inverses.length);
188         assertTrue(inverses[0] instanceof DropTableChange);
189         assertEquals("TestTable", ((DropTableChange) inverses[0]).getTableName());
190     }
191 
192     @Test
193     public void tableSpace_none() throws Exception {
194         CreateTableChange change = new CreateTableChange();
195 
196         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
197         assertNull(statement.getTablespace());
198     }
199 
200     @Test
201     public void tableSpace_set() throws Exception {
202         CreateTableChange change = new CreateTableChange();
203         change.setTablespace("TESTTABLESPACE");
204 
205         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
206         assertEquals("TESTTABLESPACE", statement.getTablespace());
207     }
208 
209     @Test
210     public void foreignKey_deferrable() throws Exception {
211         CreateTableChange change = new CreateTableChange();
212         ColumnConfig columnConfig = new ColumnConfig();
213         columnConfig.setName("id");
214         columnConfig.setType("int");
215         ConstraintsConfig constraints = new ConstraintsConfig();
216         constraints.setForeignKeyName("fk_test");
217         constraints.setReferences("test(id)");
218         constraints.setDeferrable(true);
219         constraints.setInitiallyDeferred(true);
220         columnConfig.setConstraints(constraints);
221         change.addColumn(columnConfig);
222 
223         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
224         ForeignKeyConstraint keyConstraint = statement.getForeignKeyConstraints().iterator().next();
225         assertTrue(keyConstraint.isDeferrable());
226         assertTrue(keyConstraint.isInitiallyDeferred());
227     }
228 
229     @Test
230     public void foreignKey_notDeferrable() throws Exception {
231         CreateTableChange change = new CreateTableChange();
232         ColumnConfig columnConfig = new ColumnConfig();
233         columnConfig.setName("id");
234         columnConfig.setType("int");
235         ConstraintsConfig constraints = new ConstraintsConfig();
236         constraints.setForeignKeyName("fk_test");
237         constraints.setReferences("test(id)");
238         constraints.setDeferrable(false);
239         constraints.setInitiallyDeferred(false);
240         columnConfig.setConstraints(constraints);
241         change.addColumn(columnConfig);
242 
243         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
244         ForeignKeyConstraint keyConstraint = statement.getForeignKeyConstraints().iterator().next();
245         assertFalse(keyConstraint.isDeferrable());
246         assertFalse(keyConstraint.isInitiallyDeferred());
247     }
248 
249     @Test
250     public void foreignKey_defaultDeferrable() throws Exception {
251         CreateTableChange change = new CreateTableChange();
252         ColumnConfig columnConfig = new ColumnConfig();
253         columnConfig.setName("id");
254         columnConfig.setType("int");
255         ConstraintsConfig constraints = new ConstraintsConfig();
256         constraints.setReferences("test(id)");
257         constraints.setForeignKeyName("fk_test");
258         columnConfig.setConstraints(constraints);
259         change.addColumn(columnConfig);
260 
261         CreateTableStatement statement = (CreateTableStatement) change.generateStatements(new MockDatabase())[0];
262         ForeignKeyConstraint keyConstraint = statement.getForeignKeyConstraints().iterator().next();
263         assertFalse(keyConstraint.isDeferrable());
264         assertFalse(keyConstraint.isInitiallyDeferred());
265     }
266 }