-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ChangeLogParametersTest.java
264 lines (216 loc) · 14.6 KB
/
ChangeLogParametersTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package liquibase.changelog;
import liquibase.ContextExpression;
import liquibase.Contexts;
import liquibase.Labels;
import liquibase.database.core.H2Database;
import liquibase.database.core.MySQLDatabase;
import liquibase.database.core.OracleDatabase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class ChangeLogParametersTest {
@Test
public void setParameterValue_doubleSet() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters();
changeLogParameters.set("doubleSet", "originalValue");
changeLogParameters.set("doubleSet", "newValue");
assertEquals("re-setting a param should not overwrite the value (like how ant works)", "originalValue", changeLogParameters.getValue("doubleSet", null));
}
@Test
public void setParameterValue_doubleSet_withDbms() throws Exception {
final DatabaseChangeLog changelog = new DatabaseChangeLog("com/example/changelog.txt");
ChangeLogParameters h2Params = new ChangeLogParameters(new H2Database());
ChangeLogParameters oracleParams = new ChangeLogParameters(new OracleDatabase());
ChangeLogParameters mysqlParams = new ChangeLogParameters(new MySQLDatabase());
h2Params.set("dbmsProperty", "h2 value", new ContextExpression(), new Labels(), "h2", false, changelog);
h2Params.set("dbmsProperty", "oracle value", new ContextExpression(), new Labels(), "oracle", false, changelog);
oracleParams.set("dbmsProperty", "h2 value", new ContextExpression(), new Labels(), "h2", false, changelog);
oracleParams.set("dbmsProperty", "oracle value", new ContextExpression(), new Labels(), "oracle", false, changelog);
mysqlParams.set("dbmsProperty", "h2 value", new ContextExpression(), new Labels(), "h2", false, changelog);
mysqlParams.set("dbmsProperty", "oracle value", new ContextExpression(), new Labels(), "oracle", false, changelog);
assertEquals("h2 value", h2Params.getValue("dbmsProperty", changelog));
assertEquals("oracle value", oracleParams.getValue("dbmsProperty", changelog));
assertNull(mysqlParams.getValue("dbmsProperty", changelog));
}
@Test
public void getParameterValue_envVariable() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters();
assertEquals(System.getenv("PATH"), changeLogParameters.getValue("PATH", null));
}
@Test
public void getParameterValue_systemProperty() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters();
assertEquals(System.getProperty("user.name"), changeLogParameters.getValue("user.name", null));
}
@Test
public void setParameterValue_doubleSetButSecondWrongDatabase() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
changeLogParameters.set("doubleSet", "originalValue", new ContextExpression(), new Labels(), "baddb", true, null);
changeLogParameters.set("doubleSet", "newValue");
assertEquals("newValue", changeLogParameters.getValue("doubleSet", null));
}
@Test
public void setParameterValue_multiDatabase() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
changeLogParameters.set("doubleSet", "originalValue", new ContextExpression(), new Labels(), "baddb, h2", true, null);
assertEquals("originalValue", changeLogParameters.getValue("doubleSet", null));
}
@Test
public void setParameterValue_rightDBWrongContext() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
changeLogParameters.setContexts(new Contexts("junit"));
changeLogParameters.set("doubleSet", "originalValue", "anotherContext", "anotherLabel", "baddb, h2", true, null);
assertNull(changeLogParameters.getValue("doubleSet", null));
}
@Test
public void setParameterValue_rightDBRightContext() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
changeLogParameters.setContexts(new Contexts("junit"));
changeLogParameters.set("doubleSet", "originalValue", "junit", "junitLabel", "baddb, h2", true, null);
assertEquals("originalValue", changeLogParameters.getValue("doubleSet", null));
}
@Test
/**
* root.xml
* -a.xml
* -b.xml
*
* in a and b we define same prop with key 'aKey'. Expected when b is processed then bValue is taken no matter of Object instances
*/
public void getParameterValue_SamePropertyNonGlobalIn2InnerFiles() {
DatabaseChangeLog inner1 = new DatabaseChangeLog();
inner1.setPhysicalFilePath("a");
DatabaseChangeLog inner2 = new DatabaseChangeLog();
inner2.setPhysicalFilePath("b");
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
changeLogParameters.set("aKey", "aValue", "junit", "junitLabel", "baddb, h2", false, inner1);
changeLogParameters.set("aKey", "bValue", "junit", "junitLabel", "baddb, h2", false, inner2);
DatabaseChangeLog inner2SamePath = new DatabaseChangeLog();
inner2SamePath.setPhysicalFilePath("b");
Object aKey = changeLogParameters.getValue("aKey", inner2SamePath);
assertEquals("bValue", aKey);
}
@Test
/**
* db.changelog-master.xml
* - table_1.xml (table.name=table_1 global=false)
* - - include templates/common_columns_1.xml
* - table_2.xml (table.name=table_2 global=false)
* - - include templates/common_columns_2.xml
* - - - include templates/common_columns_1.xml
*
* For local parameters, return the value of the definition in the closest direct ancestor of the requesting changeSet.
*/
public void getParameterValue_ReturnValueOfLocalParameterDefinedInTheClosestAncestorChangeSet() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
DatabaseChangeLog master = new DatabaseChangeLog("db/db.changelog-master.xml");
DatabaseChangeLog table_1 = new DatabaseChangeLog("db/changelog/table_1.xml");
table_1.setParentChangeLog(master);
changeLogParameters.set("table.name", "table_1", "junit", "junitLabel", "baddb, h2", false, table_1);
DatabaseChangeLog common_columns_1_of_table_1 = new DatabaseChangeLog("db/templates/common_columns_1.xml");
common_columns_1_of_table_1.setParentChangeLog(table_1);
assertEquals("table_1", changeLogParameters.getValue("table.name", common_columns_1_of_table_1));
assertEquals("table_1", changeLogParameters.getValue("table.name", table_1));
DatabaseChangeLog table_2 = new DatabaseChangeLog("db/changelog/table_2.xml");
table_2.setParentChangeLog(master);
changeLogParameters.set("table.name", "table_2", "junit", "junitLabel", "baddb, h2", false, table_2);
DatabaseChangeLog common_columns_2_of_table_2 = new DatabaseChangeLog("db/templates/common_columns_2.xml");
common_columns_2_of_table_2.setParentChangeLog(table_2);
DatabaseChangeLog common_columns_1_of_table_2 = new DatabaseChangeLog("db/templates/common_columns_1.xml");
common_columns_1_of_table_2.setParentChangeLog(common_columns_2_of_table_2);
assertEquals("should return local value of closest ancestor changeSet (here the grand parent)", "table_2", changeLogParameters.getValue("table.name", common_columns_1_of_table_2));
assertEquals("should return local value of closest ancestor changeSet (here the direct parent)", "table_2", changeLogParameters.getValue("table.name", common_columns_2_of_table_2));
assertEquals("should return local value of changeSet", "table_2", changeLogParameters.getValue("table.name", table_2));
}
@Test
/**
* db.changelog-master.xml
* - table_1.xml (table.name=table_1 global=false)
* - - include templates/common_columns_1.xml
* - table_2.xml (table.name=table_2 global=false)
* - - include templates/common_columns_2.xml
* - - - include templates/common_columns_1.xml
*
* Return <code>null</code> if there is a local parameter, but its not defined in an direct ancestor changeSet.
*/
public void getParameterValue_ReturnNullIfLocalParameterNotInAncestorChangeSet() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
DatabaseChangeLog master = new DatabaseChangeLog("db/db.changelog-master.xml");
DatabaseChangeLog table_1 = new DatabaseChangeLog("db/changelog/table_1.xml");
table_1.setParentChangeLog(master);
changeLogParameters.set("table.name", "table_1", "junit", "junitLabel", "baddb, h2", false, table_1);
DatabaseChangeLog common_columns_1_of_table_1 = new DatabaseChangeLog("db/templates/common_columns_1.xml");
common_columns_1_of_table_1.setParentChangeLog(table_1);
assertEquals("table_1", changeLogParameters.getValue("table.name", common_columns_1_of_table_1));
assertEquals("table_1", changeLogParameters.getValue("table.name", table_1));
DatabaseChangeLog table_2 = new DatabaseChangeLog("db/changelog/table_2.xml");
table_2.setParentChangeLog(master);
// programmer forgot to define the property "table.name" in this changeSet
// changeLogParameters.set("table.name", "table_2", "junit", "junitLabel", "baddb, h2", false, table_2);
DatabaseChangeLog common_columns_2_of_table_2 = new DatabaseChangeLog("db/templates/common_columns_2.xml");
common_columns_2_of_table_2.setParentChangeLog(table_2);
DatabaseChangeLog common_columns_1_of_table_2 = new DatabaseChangeLog("db/templates/common_columns_1.xml");
common_columns_1_of_table_2.setParentChangeLog(common_columns_2_of_table_2);
// even though there is a single value for "table.name" it is not being used since it does not belong to an ancestor of the requesting changeSet
assertNull("should return no value since there is no matching global key and no local key belonging to the current changeSet or an ancestor changeSet", changeLogParameters.getValue("table.name", common_columns_1_of_table_2));
assertNull("should return no value since there is no matching global key and no local key belonging to the current changeSet or an ancestor changeSet", changeLogParameters.getValue("table.name", common_columns_2_of_table_2));
assertNull("should return no value since there is no matching global key and no local key belonging to the current changeSet or an ancestor changeSet", changeLogParameters.getValue("table.name", table_2));
}
@Test
/**
* db.changelog-master.xml
* - table_1.xml (table.name=table_1 global=false)
* - - include templates/common_columns_1.xml
* - table_2.xml (table.name=table_2 global=false)
* - - include templates/common_columns_2.xml
* - - - include templates/common_columns_1.xml
*
* Local parameters with the same name are ignore after the first global parameter with that same name.
*/
public void getParameterValue_IgnoreLocalParameterAfterGlobalParameter() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
DatabaseChangeLog master = new DatabaseChangeLog("db/db.changelog-master.xml");
DatabaseChangeLog table_1 = new DatabaseChangeLog("db/changelog/table_1.xml");
table_1.setParentChangeLog(master);
changeLogParameters.set("table.name", "table_1", "junit", "junitLabel", "baddb, h2", true, table_1);
DatabaseChangeLog common_columns_1_of_table_1 = new DatabaseChangeLog("db/templates/common_columns_1.xml");
common_columns_1_of_table_1.setParentChangeLog(table_1);
assertEquals("table_1", changeLogParameters.getValue("table.name", common_columns_1_of_table_1));
assertEquals("table_1", changeLogParameters.getValue("table.name", table_1));
DatabaseChangeLog table_2 = new DatabaseChangeLog("db/changelog/table_2.xml");
table_2.setParentChangeLog(master);
changeLogParameters.set("table.name", "table_2", "junit", "junitLabel", "baddb, h2", false, table_2);
DatabaseChangeLog common_columns_2_of_table_2 = new DatabaseChangeLog("db/templates/common_columns_2.xml");
common_columns_2_of_table_2.setParentChangeLog(table_2);
DatabaseChangeLog common_columns_1_of_table_2 = new DatabaseChangeLog("db/templates/common_columns_1.xml");
common_columns_1_of_table_2.setParentChangeLog(common_columns_2_of_table_2);
// the local parameter value "table_2" is being ignored since there is already a global value defined
assertEquals("should return first global value", "table_1", changeLogParameters.getValue("table.name", common_columns_1_of_table_2));
assertEquals("should return first global value", "table_1", changeLogParameters.getValue("table.name", common_columns_2_of_table_2));
assertEquals("should return first global value", "table_1", changeLogParameters.getValue("table.name", table_2));
}
@Test
/**
* master.xml
* - table_1.xml (table.name=table_1 global=false)
* - - include_of_table_1.xml
* - - - include_of_include_of_table_1.xml
*
* The same parameter defined multiple times on different levels of included files
*/
public void getParameterValue_MultipleLocalParametersInOneHierarchy() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
DatabaseChangeLog master = new DatabaseChangeLog("master.xml");
DatabaseChangeLog table_1 = new DatabaseChangeLog("table_1.xml");
table_1.setParentChangeLog(master);
changeLogParameters.set("aKey", "aValue", "junit", "junitLabel", "baddb, h2", false, table_1);
DatabaseChangeLog include_of_table_1 = new DatabaseChangeLog("include_of_table_1.xml");
include_of_table_1.setParentChangeLog(table_1);
DatabaseChangeLog include_of_include_of_table_1 = new DatabaseChangeLog("include_of_include_of_table_1.xml");
include_of_include_of_table_1.setParentChangeLog(include_of_table_1);
changeLogParameters.set("aKey", "bValue", "junit", "junitLabel", "baddb, h2", false, include_of_include_of_table_1);
assertEquals("should return local value of changeSet", "bValue", changeLogParameters.getValue("aKey", include_of_include_of_table_1));
assertEquals("should return local value of closest ancestor changeSet (here the direct parent)", "aValue", changeLogParameters.getValue("aKey", include_of_table_1));
assertEquals("should return local value of changeSet", "aValue", changeLogParameters.getValue("aKey", table_1));
}
}