View Javadoc

1   package liquibase.change.core;
2   
3   import liquibase.change.AbstractChangeTest;
4   import liquibase.database.core.MockDatabase;
5   import liquibase.statement.SqlStatement;
6   import liquibase.statement.DatabaseFunction;
7   import liquibase.statement.core.AddDefaultValueStatement;
8   import static org.junit.Assert.*;
9   import org.junit.Test;
10  
11  public class AddDefaultValueChangeTest extends AbstractChangeTest {
12  
13      @Override
14      @Test
15      public void generateStatement() throws Exception {
16          AddDefaultValueChange change = new AddDefaultValueChange();
17          change.setTableName("TABLE_NAME");
18          change.setColumnName("COLUMN_NAME");
19          change.setDefaultValue("New default value");
20          change.setColumnDataType("VARCHAR(255)");
21  
22          SqlStatement[] statements = change.generateStatements(new MockDatabase());
23          assertEquals(1, statements.length);
24          AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
25  
26  
27          assertEquals("TABLE_NAME", statement.getTableName());
28          assertEquals("COLUMN_NAME", statement.getColumnName());
29          assertEquals("New default value", statement.getDefaultValue());
30          assertEquals("VARCHAR(255)", statement.getColumnDataType());
31      }
32  
33      @Test
34      public void generateStatements_intDefaultValue() throws Exception {
35          AddDefaultValueChange change = new AddDefaultValueChange();
36          change.setTableName("TABLE_NAME");
37          change.setColumnName("COLUMN_NAME");
38          change.setDefaultValueNumeric("42");
39  
40          SqlStatement[] statements = change.generateStatements(new MockDatabase());
41          assertEquals(1, statements.length);
42          AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
43  
44  
45          assertEquals("TABLE_NAME", statement.getTableName());
46          assertEquals("COLUMN_NAME", statement.getColumnName());
47          assertTrue(statement.getDefaultValue() instanceof Number);
48          assertEquals("42", statement.getDefaultValue().toString());
49      }
50  
51      @Test
52      public void generateStatements_decimalDefaultValue() throws Exception {
53          AddDefaultValueChange change = new AddDefaultValueChange();
54          change.setTableName("TABLE_NAME");
55          change.setColumnName("COLUMN_NAME");
56          change.setDefaultValueNumeric("42.56");
57  
58          SqlStatement[] statements = change.generateStatements(new MockDatabase());
59          assertEquals(1, statements.length);
60          AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
61  
62  
63          assertEquals("TABLE_NAME", statement.getTableName());
64          assertEquals("COLUMN_NAME", statement.getColumnName());
65          assertTrue(statement.getDefaultValue() instanceof Number);
66          assertEquals("42.56", statement.getDefaultValue().toString());
67      }
68  
69      @Test
70      public void generateStatements_computedNumeric() throws Exception {
71          AddDefaultValueChange change = new AddDefaultValueChange();
72          change.setTableName("TABLE_NAME");
73          change.setColumnName("COLUMN_NAME");
74          change.setDefaultValueNumeric("Math.random()");
75  
76          SqlStatement[] statements = change.generateStatements(new MockDatabase());
77          assertEquals(1, statements.length);
78          AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
79  
80  
81          assertEquals("TABLE_NAME", statement.getTableName());
82          assertEquals("COLUMN_NAME", statement.getColumnName());
83          assertTrue(statement.getDefaultValue() instanceof DatabaseFunction);
84          assertEquals("Math.random()", statement.getDefaultValue().toString());
85      }
86  
87      @Test
88      public void generateStatements_computedDate() throws Exception {
89          AddDefaultValueChange change = new AddDefaultValueChange();
90          change.setTableName("TABLE_NAME");
91          change.setColumnName("COLUMN_NAME");
92          change.setDefaultValueDate("NOW()");
93  
94          SqlStatement[] statements = change.generateStatements(new MockDatabase());
95          assertEquals(1, statements.length);
96          AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
97  
98  
99          assertEquals("TABLE_NAME", statement.getTableName());
100         assertEquals("COLUMN_NAME", statement.getColumnName());
101         assertTrue(statement.getDefaultValue() instanceof DatabaseFunction);
102         assertEquals("NOW()", statement.getDefaultValue().toString());
103     }
104 
105     @Test
106     public void generateStatements_booleanDefaultValue_true() throws Exception {
107         AddDefaultValueChange change = new AddDefaultValueChange();
108         change.setTableName("TABLE_NAME");
109         change.setColumnName("COLUMN_NAME");
110         change.setDefaultValueBoolean(Boolean.TRUE);
111 
112         SqlStatement[] statements = change.generateStatements(new MockDatabase());
113         assertEquals(1, statements.length);
114         AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
115 
116 
117         assertEquals("TABLE_NAME", statement.getTableName());
118         assertEquals("COLUMN_NAME", statement.getColumnName());
119         assertTrue(statement.getDefaultValue() instanceof Boolean);
120         assertEquals(Boolean.TRUE, statement.getDefaultValue());
121     }
122 
123     @Test
124     public void generateStatements_booleanDefaultValue_false() throws Exception {
125         AddDefaultValueChange change = new AddDefaultValueChange();
126         change.setTableName("TABLE_NAME");
127         change.setColumnName("COLUMN_NAME");
128         change.setDefaultValueBoolean(Boolean.FALSE);
129 
130         SqlStatement[] statements = change.generateStatements(new MockDatabase());
131         assertEquals(1, statements.length);
132         AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
133 
134 
135         assertEquals("TABLE_NAME", statement.getTableName());
136         assertEquals("COLUMN_NAME", statement.getColumnName());
137         assertTrue(statement.getDefaultValue() instanceof Boolean);
138         assertEquals(Boolean.FALSE, statement.getDefaultValue());
139     }
140 
141 //    @Test
142 //    public void generateStatements_dateDefaultValue() throws Exception {
143 //        new DatabaseTestTemplate().testOnAllDatabases(new DatabaseTest() {
144 //
145 //            public void performTest(Database database) throws Exception {
146 //                java.sql.Date date = new java.sql.Date(new Date().getTime());
147 //
148 //                AddDefaultValueChange change = new AddDefaultValueChange();
149 //                change.setTableName("TABLE_NAME");
150 //                change.setColumnName("COLUMN_NAME");
151 //                ISODateFormat dateFormat = new ISODateFormat();
152 //                change.setDefaultValueDate(dateFormat.format(date));
153 //
154 //                SqlStatement[] statements = change.generateStatements(new MockDatabase());
155 //                assertEquals(1, statements.length);
156 //                AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];
157 //
158 //
159 //                assertEquals("TABLE_NAME", statement.getTableName());
160 //                assertEquals("COLUMN_NAME", statement.getColumnName());
161 //                assertTrue(statement.getDefaultValue() instanceof java.sql.Date);
162 //                assertEquals(date.toString(), statement.getDefaultValue().toString());
163 //            }
164 //        });
165 //    }
166 
167     @Override
168     public void getRefactoringName() throws Exception {
169         assertEquals("Add Default Value", new AddDefaultValueChange().getChangeMetaData().getDescription());
170     }
171 
172     @Override
173     @Test
174     public void getConfirmationMessage() throws Exception {
175         AddDefaultValueChange change = new AddDefaultValueChange();
176         change.setSchemaName("SCHEMA_NAME");
177         change.setTableName("TABLE_NAME");
178         change.setColumnName("COLUMN_NAME");
179 
180         assertEquals("Default value added to TABLE_NAME.COLUMN_NAME", change.getConfirmationMessage());
181     }
182 
183     @Test
184     public void getMD5Sum() throws Exception {
185         AddDefaultValueChange change = new AddDefaultValueChange();
186         change.setSchemaName("SCHEMA_NAME");
187         change.setTableName("TABLE_NAME");
188         change.setColumnName("COLUMN_NAME");
189         change.setDefaultValue("DEF STRING");
190         change.setDefaultValueNumeric("42");
191         change.setDefaultValueBoolean(true);
192         change.setDefaultValueDate("2007-01-02");
193 
194         String md5sum1 = change.generateCheckSum().toString();
195 
196         change.setSchemaName("SCHEMA_NAME2");
197         String md5Sum2 = change.generateCheckSum().toString();
198 
199         assertFalse(md5sum1.equals(md5Sum2));
200 
201         change.setSchemaName("SCHEMA_NAME");
202         String md5Sum3 = change.generateCheckSum().toString();
203 
204         assertTrue(md5sum1.equals(md5Sum3));
205 
206     }
207 
208 
209 }