1 package liquibase.parser.core.xml;
2
3 import liquibase.change.Change;
4 import liquibase.change.core.AddColumnChange;
5 import liquibase.change.core.CreateTableChange;
6 import liquibase.change.core.RawSQLChange;
7 import liquibase.change.custom.CustomChangeWrapper;
8 import liquibase.change.custom.ExampleCustomSqlChange;
9 import liquibase.changelog.ChangeSet;
10 import liquibase.changelog.DatabaseChangeLog;
11 import liquibase.changelog.ChangeLogParameters;
12 import liquibase.exception.ChangeLogParseException;
13 import liquibase.precondition.core.OrPrecondition;
14 import liquibase.precondition.core.PreconditionContainer;
15 import liquibase.test.JUnitResourceAccessor;
16 import static org.junit.Assert.*;
17
18 import org.junit.Test;
19
20 public class XMLChangeLogSAXParserTest {
21
22 @Test
23 public void simpleChangeLog() throws Exception {
24 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse("liquibase/parser/core/xml/simpleChangeLog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
25
26 assertEquals("liquibase/parser/core/xml/simpleChangeLog.xml", changeLog.getLogicalFilePath());
27 assertEquals("liquibase/parser/core/xml/simpleChangeLog.xml", changeLog.getPhysicalFilePath());
28
29 assertEquals(0, changeLog.getPreconditions().getNestedPreconditions().size());
30 assertEquals(1, changeLog.getChangeSets().size());
31
32 ChangeSet changeSet = changeLog.getChangeSets().get(0);
33 assertEquals("nvoxland", changeSet.getAuthor());
34 assertEquals("1", changeSet.getId());
35 assertEquals(1, changeSet.getChanges().size());
36 assertEquals("liquibase/parser/core/xml/simpleChangeLog.xml", changeSet.getFilePath());
37 assertEquals("Some comments go here", changeSet.getComments());
38
39 Change change = changeSet.getChanges().get(0);
40 assertEquals("createTable", change.getChangeMetaData().getName());
41 assertTrue(change instanceof CreateTableChange);
42 }
43
44 @Test
45 public void multiChangeSetChangeLog() throws Exception {
46 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse("liquibase/parser/core/xml/multiChangeSetChangeLog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
47
48 assertEquals("liquibase/parser/core/xml/multiChangeSetChangeLog.xml", changeLog.getLogicalFilePath());
49 assertEquals("liquibase/parser/core/xml/multiChangeSetChangeLog.xml", changeLog.getPhysicalFilePath());
50
51 assertEquals(0, changeLog.getPreconditions().getNestedPreconditions().size());
52 assertEquals(4, changeLog.getChangeSets().size());
53
54
55 ChangeSet changeSet = changeLog.getChangeSets().get(0);
56 assertEquals("nvoxland", changeSet.getAuthor());
57 assertEquals("1", changeSet.getId());
58 assertEquals(1, changeSet.getChanges().size());
59 assertEquals("liquibase/parser/core/xml/multiChangeSetChangeLog.xml", changeSet.getFilePath());
60 assertNull(changeSet.getComments());
61 assertFalse(changeSet.shouldAlwaysRun());
62 assertFalse(changeSet.shouldRunOnChange());
63
64 Change change = changeSet.getChanges().get(0);
65 assertEquals("createTable", change.getChangeMetaData().getName());
66 assertTrue(change instanceof CreateTableChange);
67
68
69 changeSet = changeLog.getChangeSets().get(1);
70 assertEquals("nvoxland", changeSet.getAuthor());
71 assertEquals("2", changeSet.getId());
72 assertEquals(2, changeSet.getChanges().size());
73 assertEquals("liquibase/parser/core/xml/multiChangeSetChangeLog.xml", changeSet.getFilePath());
74 assertEquals("Testing add column", changeSet.getComments());
75 assertTrue(changeSet.shouldAlwaysRun());
76 assertTrue(changeSet.shouldRunOnChange());
77 assertEquals(2, changeSet.getRollBackChanges().length);
78 assertTrue(changeSet.getRollBackChanges()[0] instanceof RawSQLChange);
79 assertTrue(changeSet.getRollBackChanges()[1] instanceof RawSQLChange);
80
81 change = changeSet.getChanges().get(0);
82 assertEquals("addColumn", change.getChangeMetaData().getName());
83 assertTrue(change instanceof AddColumnChange);
84
85 change = changeSet.getChanges().get(1);
86 assertEquals("addColumn", change.getChangeMetaData().getName());
87 assertTrue(change instanceof AddColumnChange);
88
89
90 changeSet = changeLog.getChangeSets().get(2);
91 assertEquals("bob", changeSet.getAuthor());
92 assertEquals("3", changeSet.getId());
93 assertEquals(1, changeSet.getChanges().size());
94 assertEquals("liquibase/parser/core/xml/multiChangeSetChangeLog.xml", changeSet.getFilePath());
95 assertNull(changeSet.getComments());
96 assertFalse(changeSet.shouldAlwaysRun());
97 assertFalse(changeSet.shouldRunOnChange());
98
99 change = changeSet.getChanges().get(0);
100 assertEquals("createTable", change.getChangeMetaData().getName());
101 assertTrue(change instanceof CreateTableChange);
102
103
104
105 changeSet = changeLog.getChangeSets().get(3);
106 assertEquals(1, changeSet.getChanges().size());
107
108 change = changeSet.getChanges().get(0);
109 assertTrue(change instanceof CustomChangeWrapper);
110 CustomChangeWrapper wrapper = (CustomChangeWrapper) change;
111 wrapper.generateStatements(null);
112 assertTrue(wrapper.getCustomChange() instanceof ExampleCustomSqlChange);
113 ExampleCustomSqlChange exChg = (ExampleCustomSqlChange) wrapper.getCustomChange();
114 assertEquals("table", exChg.getTableName());
115 assertEquals("column", exChg.getColumnName());
116
117 }
118
119 @Test
120 public void logicalPathChangeLog() throws Exception {
121 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse("liquibase/parser/core/xml/logicalPathChangeLog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
122
123 assertEquals("liquibase/parser-logical/xml/logicalPathChangeLog.xml", changeLog.getLogicalFilePath());
124 assertEquals("liquibase/parser/core/xml/logicalPathChangeLog.xml", changeLog.getPhysicalFilePath());
125
126 assertEquals(0, changeLog.getPreconditions().getNestedPreconditions().size());
127 assertEquals(1, changeLog.getChangeSets().size());
128 assertEquals("liquibase/parser-logical/xml/logicalPathChangeLog.xml", changeLog.getChangeSets().get(0).getFilePath());
129
130 }
131
132 @Test
133 public void preconditionsChangeLog() throws Exception {
134 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse("liquibase/parser/core/xml/preconditionsChangeLog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
135
136 assertEquals("liquibase/parser/core/xml/preconditionsChangeLog.xml", changeLog.getLogicalFilePath());
137 assertEquals("liquibase/parser/core/xml/preconditionsChangeLog.xml", changeLog.getPhysicalFilePath());
138
139 assertNotNull(changeLog.getPreconditions());
140 assertEquals(2, changeLog.getPreconditions().getNestedPreconditions().size());
141
142 assertEquals("runningAs", changeLog.getPreconditions().getNestedPreconditions().get(0).getName());
143
144 assertEquals("or", changeLog.getPreconditions().getNestedPreconditions().get(1).getName());
145 assertEquals("dbms", ((OrPrecondition) changeLog.getPreconditions().getNestedPreconditions().get(1)).getNestedPreconditions().get(0).getName());
146 assertEquals("dbms", ((OrPrecondition) changeLog.getPreconditions().getNestedPreconditions().get(1)).getNestedPreconditions().get(1).getName());
147
148 assertEquals(1, changeLog.getChangeSets().size());
149 }
150
151 @Test
152 public void testNestedChangeLog() throws Exception {
153 final String nestedFileName = "liquibase/parser/core/xml/nestedChangeLog.xml";
154 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse("liquibase/parser/core/xml/nestedChangeLog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
155 nestedFileAssertions(changeLog, nestedFileName);
156
157 }
158
159 @Test
160 public void nestedRelativeChangeLog() throws Exception {
161 final String nestedFileName = "liquibase/parser/core/xml/nestedRelativeChangeLog.xml";
162 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse(nestedFileName, new ChangeLogParameters(), new JUnitResourceAccessor());
163 nestedFileAssertions(changeLog, nestedFileName);
164
165 }
166
167 private void nestedFileAssertions(DatabaseChangeLog changeLog, String nestedFileName) {
168 assertEquals(nestedFileName, changeLog.getLogicalFilePath());
169 assertEquals(nestedFileName, changeLog.getPhysicalFilePath());
170
171 assertEquals(1, changeLog.getPreconditions().getNestedPreconditions().size());
172 assertEquals(0, ((PreconditionContainer) changeLog.getPreconditions().getNestedPreconditions().get(0)).getNestedPreconditions().size());
173 assertEquals(3, changeLog.getChangeSets().size());
174
175
176 ChangeSet changeSet = changeLog.getChangeSets().get(0);
177 assertEquals("nvoxland", changeSet.getAuthor());
178 assertEquals("1", changeSet.getId());
179 assertEquals(1, changeSet.getChanges().size());
180 assertEquals(nestedFileName, changeSet.getFilePath());
181
182
183 Change change = changeSet.getChanges().get(0);
184 assertEquals("createTable", change.getChangeMetaData().getName());
185 assertTrue(change instanceof CreateTableChange);
186 assertEquals("employee", ((CreateTableChange) change).getTableName());
187
188
189 changeSet = changeLog.getChangeSets().get(1);
190 assertEquals("nvoxland", changeSet.getAuthor());
191 assertEquals("1", changeSet.getId());
192 assertEquals(1, changeSet.getChanges().size());
193 assertEquals("liquibase/parser/core/xml/simpleChangeLog.xml", changeSet.getFilePath());
194
195 change = changeSet.getChanges().get(0);
196 assertEquals("createTable", change.getChangeMetaData().getName());
197 assertTrue(change instanceof CreateTableChange);
198 assertEquals("person", ((CreateTableChange) change).getTableName());
199
200
201 changeSet = changeLog.getChangeSets().get(2);
202 assertEquals("nvoxland", changeSet.getAuthor());
203 assertEquals("2", changeSet.getId());
204 assertEquals(1, changeSet.getChanges().size());
205 assertEquals(nestedFileName, changeSet.getFilePath());
206
207 change = changeSet.getChanges().get(0);
208 assertEquals("addColumn", change.getChangeMetaData().getName());
209 assertTrue(change instanceof AddColumnChange);
210 assertEquals("employee", ((AddColumnChange) change).getTableName());
211 }
212
213
214 @Test
215 public void doubleNestedChangeLog() throws Exception {
216 final String doubleNestedFileName = "liquibase/parser/core/xml/doubleNestedChangeLog.xml";
217 final String nestedFileName = "liquibase/parser/core/xml/nestedChangeLog.xml";
218 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse(doubleNestedFileName, new ChangeLogParameters(), new JUnitResourceAccessor());
219
220 doubleNestedFileAssertions(doubleNestedFileName, nestedFileName,
221 changeLog);
222 }
223
224 @Test
225 public void doubleNestedRelativeChangeLog() throws Exception {
226 final String doubleNestedFileName = "liquibase/parser/core/xml/doubleNestedRelativeChangeLog.xml";
227 final String nestedFileName = "liquibase/parser/core/xml/nestedRelativeChangeLog.xml";
228 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse(doubleNestedFileName, new ChangeLogParameters(), new JUnitResourceAccessor());
229
230 doubleNestedFileAssertions(doubleNestedFileName, nestedFileName,
231 changeLog);
232 }
233
234 private void doubleNestedFileAssertions(final String doubleNestedFileName,
235 final String nestedFileName, DatabaseChangeLog changeLog) {
236 assertEquals(doubleNestedFileName, changeLog.getLogicalFilePath());
237 assertEquals(doubleNestedFileName, changeLog.getPhysicalFilePath());
238
239 assertEquals(1, changeLog.getPreconditions().getNestedPreconditions().size());
240 PreconditionContainer nested = (PreconditionContainer) changeLog.getPreconditions().getNestedPreconditions().get(0);
241 assertEquals(1, nested.getNestedPreconditions().size());
242 assertEquals(0, ((PreconditionContainer) nested.getNestedPreconditions().get(0)).getNestedPreconditions().size());
243 assertEquals(4, changeLog.getChangeSets().size());
244
245
246 ChangeSet changeSet = changeLog.getChangeSets().get(0);
247 assertEquals("nvoxland", changeSet.getAuthor());
248 assertEquals("1", changeSet.getId());
249 assertEquals(1, changeSet.getChanges().size());
250 assertEquals(doubleNestedFileName, changeSet.getFilePath());
251
252 Change change = changeSet.getChanges().get(0);
253 assertEquals("createTable", change.getChangeMetaData().getName());
254 assertTrue(change instanceof CreateTableChange);
255 assertEquals("partner", ((CreateTableChange) change).getTableName());
256
257
258 changeSet = changeLog.getChangeSets().get(1);
259 assertEquals("nvoxland", changeSet.getAuthor());
260 assertEquals("1", changeSet.getId());
261 assertEquals(1, changeSet.getChanges().size());
262 assertEquals(nestedFileName, changeSet.getFilePath());
263
264 change = changeSet.getChanges().get(0);
265 assertEquals("createTable", change.getChangeMetaData().getName());
266 assertTrue(change instanceof CreateTableChange);
267 assertEquals("employee", ((CreateTableChange) change).getTableName());
268
269
270 changeSet = changeLog.getChangeSets().get(2);
271 assertEquals("nvoxland", changeSet.getAuthor());
272 assertEquals("1", changeSet.getId());
273 assertEquals(1, changeSet.getChanges().size());
274 assertEquals("liquibase/parser/core/xml/simpleChangeLog.xml", changeSet.getFilePath());
275
276 change = changeSet.getChanges().get(0);
277 assertEquals("createTable", change.getChangeMetaData().getName());
278 assertTrue(change instanceof CreateTableChange);
279 assertEquals("person", ((CreateTableChange) change).getTableName());
280
281
282 changeSet = changeLog.getChangeSets().get(3);
283 assertEquals("nvoxland", changeSet.getAuthor());
284 assertEquals("2", changeSet.getId());
285 assertEquals(1, changeSet.getChanges().size());
286 assertEquals(nestedFileName, changeSet.getFilePath());
287
288 change = changeSet.getChanges().get(0);
289 assertEquals("addColumn", change.getChangeMetaData().getName());
290 assertTrue(change instanceof AddColumnChange);
291 assertEquals("employee", ((AddColumnChange) change).getTableName());
292 }
293
294 @Test
295 public void missingChangeLog() throws Exception {
296 try {
297 @SuppressWarnings("unused")
298 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse("liquibase/changelog/parser/xml/missingChangeLog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
299 } catch (Exception e) {
300 assertTrue(e instanceof ChangeLogParseException);
301 assertEquals("liquibase/changelog/parser/xml/missingChangeLog.xml does not exist", e.getMessage());
302
303 }
304 }
305
306 @Test
307 public void malformedChangeLog() throws Exception {
308 try {
309 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse("liquibase/parser/core/xml/malformedChangeLog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
310 } catch (Exception e) {
311 assertTrue(e instanceof ChangeLogParseException);
312 assertTrue(e.getMessage().startsWith("Error parsing line"));
313
314 }
315 }
316
317 @Test
318 public void otherNamespaceAttributesChangeLog() throws Exception {
319 DatabaseChangeLog changeLog = new XMLChangeLogSAXParser().parse("liquibase/parser/core/xml/simpleChangeLog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
320
321 assertEquals("liquibase/parser/core/xml/simpleChangeLog.xml", changeLog.getLogicalFilePath());
322 assertEquals("liquibase/parser/core/xml/simpleChangeLog.xml", changeLog.getPhysicalFilePath());
323
324 assertEquals(0, changeLog.getPreconditions().getNestedPreconditions().size());
325 assertEquals(1, changeLog.getChangeSets().size());
326
327 ChangeSet changeSet = changeLog.getChangeSets().get(0);
328 assertEquals("nvoxland", changeSet.getAuthor());
329 assertEquals("1", changeSet.getId());
330 assertEquals(1, changeSet.getChanges().size());
331 assertEquals("liquibase/parser/core/xml/simpleChangeLog.xml", changeSet.getFilePath());
332 assertEquals("Some comments go here", changeSet.getComments());
333
334 Change change = changeSet.getChanges().get(0);
335 assertEquals("createTable", change.getChangeMetaData().getName());
336 assertTrue(change instanceof CreateTableChange);
337 }
338
339 }