1 |
|
package liquibase.serializer.core.string; |
2 |
|
|
3 |
|
import liquibase.change.*; |
4 |
|
import liquibase.change.core.*; |
5 |
|
import liquibase.change.custom.CustomChangeWrapper; |
6 |
|
import liquibase.change.custom.CustomSqlChange; |
7 |
|
import liquibase.change.custom.ExampleCustomSqlChange; |
8 |
|
import liquibase.logging.Logger; |
9 |
|
import liquibase.resource.ClassLoaderResourceAccessor; |
10 |
|
import liquibase.resource.ResourceAccessor; |
11 |
|
import liquibase.statement.DatabaseFunction; |
12 |
|
import static org.junit.Assert.*; |
13 |
|
import org.junit.Test; |
14 |
|
|
15 |
|
import java.lang.reflect.Field; |
16 |
|
import java.lang.reflect.ParameterizedType; |
17 |
|
import java.lang.reflect.Type; |
18 |
|
import java.math.BigInteger; |
19 |
|
import java.util.*; |
20 |
|
|
|
|
| 86.3% |
Uncovered Elements: 31 (227) |
Complexity: 48 |
Complexity Density: 0.31 |
|
21 |
|
public class StringChangeLogSerializerTest { |
22 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1
PASS
|
|
23 |
1
|
@Test... |
24 |
|
public void serialized_CustomChange() throws Exception { |
25 |
|
|
26 |
1
|
String expectedString = "customChange:[\n" + |
27 |
|
" className=\"liquibase.change.custom.ExampleCustomSqlChange\"\n" + |
28 |
|
" paramValues={\n" + |
29 |
|
" columnName=\"column_name\",\n" + |
30 |
|
" newValue=\"new_value\",\n" + |
31 |
|
" tableName=\"table_name\"\n" + |
32 |
|
" }\n" + |
33 |
|
"]"; |
34 |
|
|
35 |
1
|
CustomChangeWrapper wrapper = new CustomChangeWrapper(); |
36 |
1
|
wrapper.setResourceAccessor(new ClassLoaderResourceAccessor()); |
37 |
|
|
38 |
|
|
39 |
1
|
wrapper.setClassLoader(getClass().getClassLoader()); |
40 |
1
|
wrapper.setClass("liquibase.change.custom.ExampleCustomSqlChange"); |
41 |
1
|
wrapper.setParam("columnName", "column_name"); |
42 |
1
|
wrapper.setParam("newValue", "new_value"); |
43 |
1
|
wrapper.setParam("tableName", "table_name"); |
44 |
|
|
45 |
1
|
assertEquals(expectedString, new StringChangeLogSerializer().serialize(wrapper)); |
46 |
|
} |
47 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1
PASS
|
|
48 |
1
|
@Test... |
49 |
|
public void serialized_AddColumnChange() { |
50 |
1
|
AddColumnChange change = new AddColumnChange(); |
51 |
|
|
52 |
1
|
assertEquals("addColumn:[\n" + |
53 |
|
" columns=[]\n" + |
54 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
55 |
|
|
56 |
1
|
change.setTableName("TABLE_NAME"); |
57 |
|
|
58 |
1
|
assertEquals("addColumn:[\n" + |
59 |
|
" columns=[]\n" + |
60 |
|
" tableName=\"TABLE_NAME\"\n" + |
61 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
62 |
|
|
63 |
1
|
change.setSchemaName("SCHEMA_NAME"); |
64 |
1
|
assertEquals("addColumn:[\n" + |
65 |
|
" columns=[]\n" + |
66 |
|
" schemaName=\"SCHEMA_NAME\"\n" + |
67 |
|
" tableName=\"TABLE_NAME\"\n" + |
68 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
69 |
|
|
70 |
1
|
ColumnConfig column = new ColumnConfig(); |
71 |
1
|
change.addColumn(column); |
72 |
1
|
column.setName("COLUMN_NAME"); |
73 |
|
|
74 |
1
|
assertEquals("addColumn:[\n" + |
75 |
|
" columns=[\n" + |
76 |
|
" column:[\n" + |
77 |
|
" name=\"COLUMN_NAME\"\n" + |
78 |
|
" ]\n" + |
79 |
|
" ]\n" + |
80 |
|
" schemaName=\"SCHEMA_NAME\"\n" + |
81 |
|
" tableName=\"TABLE_NAME\"\n" + |
82 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
83 |
|
|
84 |
1
|
ColumnConfig column2 = new ColumnConfig(); |
85 |
1
|
change.addColumn(column2); |
86 |
1
|
column2.setName("COLUMN2_NAME"); |
87 |
1
|
column2.setAutoIncrement(true); |
88 |
1
|
column2.setValueNumeric(52); |
89 |
|
|
90 |
1
|
assertEquals("addColumn:[\n" + |
91 |
|
" columns=[\n" + |
92 |
|
" column:[\n" + |
93 |
|
" name=\"COLUMN_NAME\"\n" + |
94 |
|
" ],\n" + |
95 |
|
" column:[\n" + |
96 |
|
" autoIncrement=\"true\"\n" + |
97 |
|
" name=\"COLUMN2_NAME\"\n" + |
98 |
|
" valueNumeric=\"52\"\n" + |
99 |
|
" ]\n" + |
100 |
|
" ]\n" + |
101 |
|
" schemaName=\"SCHEMA_NAME\"\n" + |
102 |
|
" tableName=\"TABLE_NAME\"\n" + |
103 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1
PASS
|
|
106 |
1
|
@Test... |
107 |
|
public void serialized_AddForeignKeyConstraint() { |
108 |
1
|
AddForeignKeyConstraintChange change = new AddForeignKeyConstraintChange(); |
109 |
|
|
110 |
1
|
assertEquals("addForeignKeyConstraint:[]", new StringChangeLogSerializer().serialize(change)); |
111 |
|
|
112 |
1
|
change.setBaseTableName("TABLE_NAME"); |
113 |
1
|
change.setBaseColumnNames("COL1, COL2"); |
114 |
1
|
change.setBaseTableSchemaName("BASE_SCHEM"); |
115 |
1
|
change.setConstraintName("FK_TEST"); |
116 |
1
|
change.setDeferrable(true); |
117 |
1
|
change.setInitiallyDeferred(true); |
118 |
1
|
change.setReferencesUniqueColumn(false); |
119 |
1
|
change.setDeleteCascade(true); |
120 |
1
|
change.setOnDelete("SET NULL"); |
121 |
1
|
change.setOnUpdate("NO ACTION"); |
122 |
1
|
change.setReferencedTableName("REF_TABLE"); |
123 |
1
|
change.setReferencedColumnNames("COLA, COLB"); |
124 |
1
|
change.setReferencedTableSchemaName("REF_SCHEM"); |
125 |
|
|
126 |
1
|
assertEquals("addForeignKeyConstraint:[\n" + |
127 |
|
" baseColumnNames=\"COL1, COL2\"\n" + |
128 |
|
" baseTableName=\"TABLE_NAME\"\n" + |
129 |
|
" baseTableSchemaName=\"BASE_SCHEM\"\n" + |
130 |
|
" constraintName=\"FK_TEST\"\n" + |
131 |
|
" deferrable=\"true\"\n" + |
132 |
|
" initiallyDeferred=\"true\"\n" + |
133 |
|
" onDelete=\"SET NULL\"\n" + |
134 |
|
" onUpdate=\"NO ACTION\"\n" + |
135 |
|
" referencedColumnNames=\"COLA, COLB\"\n" + |
136 |
|
" referencedTableName=\"REF_TABLE\"\n" + |
137 |
|
" referencedTableSchemaName=\"REF_SCHEM\"\n" + |
138 |
|
" referencesUniqueColumn=\"false\"\n" + |
139 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
140 |
|
|
141 |
|
} |
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1
PASS
|
|
142 |
1
|
@Test... |
143 |
|
public void serialized_AddUniqueKeyConstraint() { |
144 |
1
|
AddUniqueConstraintChange change = new AddUniqueConstraintChange(); |
145 |
|
|
146 |
1
|
assertEquals("addUniqueConstraint:[]", new StringChangeLogSerializer().serialize(change)); |
147 |
|
|
148 |
1
|
change.setTableName("TABLE_NAME"); |
149 |
1
|
change.setColumnNames("COL1, COL2"); |
150 |
1
|
change.setSchemaName("BASE_SCHEM"); |
151 |
1
|
change.setConstraintName("FK_TEST"); |
152 |
1
|
change.setDeferrable(true); |
153 |
1
|
change.setInitiallyDeferred(true); |
154 |
1
|
change.setDisabled(true); |
155 |
1
|
change.setTablespace("TABLESPACE1"); |
156 |
|
|
157 |
1
|
assertEquals("addUniqueConstraint:[\n" + |
158 |
|
" columnNames=\"COL1, COL2\"\n" + |
159 |
|
" constraintName=\"FK_TEST\"\n" + |
160 |
|
" deferrable=\"true\"\n" + |
161 |
|
" disabled=\"true\"\n" + |
162 |
|
" initiallyDeferred=\"true\"\n" + |
163 |
|
" schemaName=\"BASE_SCHEM\"\n" + |
164 |
|
" tableName=\"TABLE_NAME\"\n" + |
165 |
|
" tablespace=\"TABLESPACE1\"\n" + |
166 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
167 |
|
|
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
192 |
1
|
@Test... |
193 |
|
public void serialized_SQLFileChange() { |
194 |
1
|
SQLFileChange change = new SQLFileChange(); |
195 |
|
|
196 |
1
|
assertEquals("sqlFile:[\n" + |
197 |
|
" splitStatements=\"true\"\n" + |
198 |
|
" stripComments=\"false\"\n]", new StringChangeLogSerializer().serialize(change)); |
199 |
|
|
200 |
1
|
change.setPath("PATH/TO/File.txt"); |
201 |
|
|
202 |
1
|
assertEquals("sqlFile:[\n" + |
203 |
|
" path=\"PATH/TO/File.txt\"\n" + |
204 |
|
" splitStatements=\"true\"\n" + |
205 |
|
" stripComments=\"false\"\n" + |
206 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
207 |
|
} |
208 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
209 |
1
|
@Test... |
210 |
|
public void serialized_rawSql() { |
211 |
1
|
RawSQLChange change = new RawSQLChange(); |
212 |
|
|
213 |
1
|
assertEquals("sql:[\n" + |
214 |
|
" splitStatements=\"true\"\n" + |
215 |
|
" stripComments=\"false\"\n]", new StringChangeLogSerializer().serialize(change)); |
216 |
|
|
217 |
1
|
change.setSql("some SQL Here"); |
218 |
|
|
219 |
1
|
assertEquals("sql:[\n" + |
220 |
|
" splitStatements=\"true\"\n" + |
221 |
|
" sql=\"some SQL Here\"\n" + |
222 |
|
" stripComments=\"false\"\n" + |
223 |
|
"]", new StringChangeLogSerializer().serialize(change)); |
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1
PASS
|
|
226 |
1
|
@Test... |
227 |
|
public void tryAllChanges() throws Exception { |
228 |
1
|
for (SortedSet<Class<? extends Change>> changeClassSet : ChangeFactory.getInstance().getRegistry().values()) { |
229 |
43
|
Change change = changeClassSet.iterator().next().getConstructor().newInstance(); |
230 |
|
|
231 |
43
|
setFields(change); |
232 |
|
|
233 |
43
|
String string = new StringChangeLogSerializer().serialize(change); |
234 |
|
|
235 |
|
|
236 |
43
|
assertTrue("@ in string. Probably poorly serialzed object reference." + string, string.indexOf("@") < 0); |
237 |
|
} |
238 |
|
} |
239 |
|
|
|
|
| 76.7% |
Uncovered Elements: 27 (116) |
Complexity: 29 |
Complexity Density: 0.47 |
|
240 |
86
|
private void setFields(Object object) throws Exception {... |
241 |
86
|
Class clazz = object.getClass(); |
242 |
86
|
if (clazz.getName().indexOf(".ext.") > 0) { |
243 |
0
|
return; |
244 |
|
} |
245 |
|
|
246 |
86
|
for (Field field : clazz.getDeclaredFields()) { |
247 |
471
|
if (field.getAnnotation(ChangeProperty.class) != null && !field.getAnnotation(ChangeProperty.class).includeInSerialization()) { |
248 |
7
|
continue; |
249 |
|
} |
250 |
464
|
field.setAccessible(true); |
251 |
464
|
if (field.getType().getName().equals("[[Z")) { |
252 |
|
|
253 |
464
|
} else if (field.getName().equals("serialVersionUID")) { |
254 |
|
|
255 |
464
|
} else if (field.getType().equals(Logger.class)) { |
256 |
|
|
257 |
464
|
} else if (field.getType().equals(ResourceAccessor.class)) { |
258 |
|
|
259 |
464
|
} else if (field.getType().equals(ClassLoader.class)) { |
260 |
|
|
261 |
464
|
} else if (field.getType().equals(long.class)) { |
262 |
0
|
field.set(object, createInteger().longValue()); |
263 |
464
|
} else if (field.getType().equals(String.class)) { |
264 |
271
|
field.set(object, createString()); |
265 |
193
|
} else if (field.getType().equals(Number.class)) { |
266 |
20
|
field.set(object, createNumber()); |
267 |
173
|
} else if (field.getType().equals(Integer.class)) { |
268 |
2
|
field.set(object, createInteger()); |
269 |
171
|
} else if (field.getType().equals(BigInteger.class)) { |
270 |
7
|
field.set(object, createBigInteger()); |
271 |
164
|
} else if (field.getType().equals(Date.class)) { |
272 |
20
|
field.set(object, createDate()); |
273 |
144
|
} else if (field.getType().equals(Boolean.class)) { |
274 |
104
|
field.set(object, createBoolean()); |
275 |
40
|
} else if (field.getType().equals(ColumnConfig.class)) { |
276 |
0
|
field.set(object, createColumnConfig()); |
277 |
40
|
} else if (field.getType().equals(DatabaseFunction.class)) { |
278 |
21
|
field.set(object, createDatabaseFunction()); |
279 |
19
|
} else if (field.getType().equals(ConstraintsConfig.class)) { |
280 |
10
|
field.set(object, createConstraintsConfig()); |
281 |
9
|
} else if (field.getType().getName().equals("liquibase.change.custom.CustomChange")) { |
282 |
0
|
field.set(object, createCustomChange()); |
283 |
9
|
} else if (field.getType().equals(Map.class)) { |
284 |
1
|
field.set(object, createMap()); |
285 |
8
|
} else if (Collection.class.isAssignableFrom(field.getType())) { |
286 |
8
|
Type genericType = field.getGenericType(); |
287 |
8
|
if (genericType instanceof ParameterizedType) { |
288 |
8
|
int genericsLength = ((ParameterizedType) genericType).getActualTypeArguments().length; |
289 |
8
|
if (genericsLength == 1) { |
290 |
8
|
Class typeToCreate = (Class) ((ParameterizedType) genericType).getActualTypeArguments()[0]; |
291 |
8
|
Collection collection; |
292 |
8
|
if (field.getType().equals(List.class)) { |
293 |
8
|
collection = new ArrayList(); |
294 |
0
|
} else if (field.getType().equals(SortedSet.class)) { |
295 |
0
|
collection = new TreeSet(); |
296 |
|
} else { |
297 |
0
|
throw new RuntimeException("Unknow collection type: " + field.getType().getName()); |
298 |
|
} |
299 |
8
|
if (typeToCreate.equals(ColumnConfig.class)) { |
300 |
5
|
collection.add(createColumnConfig()); |
301 |
5
|
collection.add(createColumnConfig()); |
302 |
3
|
} else if (typeToCreate.equals(LoadDataColumnConfig.class)) { |
303 |
1
|
collection.add(createLoadDataColumnConfig()); |
304 |
1
|
collection.add(createLoadDataColumnConfig()); |
305 |
2
|
} else if (typeToCreate.equals(String.class)) { |
306 |
2
|
collection.add(createString()); |
307 |
2
|
collection.add(createString()); |
308 |
|
} else { |
309 |
0
|
throw new RuntimeException("Unknown generic type for " + clazz.getName() + "." + field.getName() + ": " + typeToCreate.getName()); |
310 |
|
} |
311 |
8
|
field.set(object, collection); |
312 |
|
} else { |
313 |
0
|
throw new RuntimeException("Found " + genericsLength + " generics for " + clazz.getName() + "." + field.getName()); |
314 |
|
} |
315 |
|
} else { |
316 |
0
|
fail("List not generic"); |
317 |
|
} |
318 |
|
} else { |
319 |
0
|
fail("Unknown field type in " + clazz.getName() + ": " + field.getType().getName()); |
320 |
|
} |
321 |
|
} |
322 |
|
|
323 |
|
} |
324 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
325 |
2
|
private LoadDataColumnConfig createLoadDataColumnConfig() throws Exception {... |
326 |
2
|
LoadDataColumnConfig config = new LoadDataColumnConfig(); |
327 |
2
|
setFields(config); |
328 |
2
|
return config; |
329 |
|
} |
330 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
331 |
281
|
private String createString() {... |
332 |
281
|
return Long.toString(Math.abs(new Random().nextLong()), 36); |
333 |
|
} |
334 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
335 |
20
|
private Number createNumber() {... |
336 |
20
|
return new Random().nextDouble() * 10000; |
337 |
|
} |
338 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
339 |
2
|
private Integer createInteger() {... |
340 |
2
|
return new Random().nextInt(); |
341 |
|
} |
342 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
343 |
7
|
private BigInteger createBigInteger() {... |
344 |
|
|
345 |
7
|
return new BigInteger(20, new Random()); |
346 |
|
} |
347 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
348 |
20
|
private Date createDate() {... |
349 |
20
|
return new Date(new Random().nextLong()); |
350 |
|
} |
351 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
352 |
104
|
private Boolean createBoolean() {... |
353 |
104
|
return true; |
354 |
|
} |
355 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
356 |
1
|
private Map createMap() {... |
357 |
1
|
Map map = new HashMap(); |
358 |
1
|
map.put(createString(), createString()); |
359 |
1
|
map.put(createString(), createString()); |
360 |
1
|
map.put(createString(), createString()); |
361 |
1
|
return map; |
362 |
|
} |
363 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
364 |
10
|
private ColumnConfig createColumnConfig() throws Exception {... |
365 |
10
|
ColumnConfig config = new ColumnConfig(); |
366 |
10
|
setFields(config); |
367 |
10
|
return config; |
368 |
|
} |
369 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
370 |
21
|
private DatabaseFunction createDatabaseFunction() throws Exception {... |
371 |
21
|
DatabaseFunction function = new DatabaseFunction("FUNCTION_HERE"); |
372 |
21
|
setFields(function); |
373 |
21
|
return function; |
374 |
|
} |
375 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
376 |
10
|
private ConstraintsConfig createConstraintsConfig() throws Exception {... |
377 |
10
|
ConstraintsConfig config = new ConstraintsConfig(); |
378 |
10
|
setFields(config); |
379 |
10
|
return config; |
380 |
|
} |
381 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
382 |
0
|
private CustomSqlChange createCustomChange() throws Exception {... |
383 |
0
|
CustomSqlChange config = new ExampleCustomSqlChange(); |
384 |
0
|
setFields(config); |
385 |
0
|
return config; |
386 |
|
} |
387 |
|
} |