001: package liquibase.parser.xml;
002:
003: import liquibase.ChangeSet;
004: import liquibase.DatabaseChangeLog;
005: import liquibase.change.AddColumnChange;
006: import liquibase.change.Change;
007: import liquibase.change.CreateTableChange;
008: import liquibase.database.sql.RawSqlStatement;
009: import liquibase.exception.ChangeLogParseException;
010: import liquibase.preconditions.OrPrecondition;
011: import liquibase.test.JUnitFileOpener;
012: import static org.junit.Assert.*;
013: import org.junit.Test;
014:
015: public class XMLChangeLogParserTest {
016:
017: @Test
018: public void simpleChangeLog() throws Exception {
019: DatabaseChangeLog changeLog = new XMLChangeLogParser().parse(
020: "liquibase/parser/xml/simpleChangeLog.xml",
021: new JUnitFileOpener());
022:
023: assertEquals("liquibase/parser/xml/simpleChangeLog.xml",
024: changeLog.getLogicalFilePath());
025: assertEquals("liquibase/parser/xml/simpleChangeLog.xml",
026: changeLog.getPhysicalFilePath());
027:
028: assertNull(changeLog.getPreconditions());
029: assertEquals(1, changeLog.getChangeSets().size());
030:
031: ChangeSet changeSet = changeLog.getChangeSets().get(0);
032: assertEquals("nvoxland", changeSet.getAuthor());
033: assertEquals("1", changeSet.getId());
034: assertEquals(1, changeSet.getChanges().size());
035: assertEquals("liquibase/parser/xml/simpleChangeLog.xml",
036: changeSet.getFilePath());
037: assertEquals("Some comments go here", changeSet.getComments());
038:
039: Change change = changeSet.getChanges().get(0);
040: assertEquals("createTable", change.getTagName());
041: assertTrue(change instanceof CreateTableChange);
042: }
043:
044: @Test
045: public void multiChangeSetChangeLog() throws Exception {
046: DatabaseChangeLog changeLog = new XMLChangeLogParser().parse(
047: "liquibase/parser/xml/multiChangeSetChangeLog.xml",
048: new JUnitFileOpener());
049:
050: assertEquals(
051: "liquibase/parser/xml/multiChangeSetChangeLog.xml",
052: changeLog.getLogicalFilePath());
053: assertEquals(
054: "liquibase/parser/xml/multiChangeSetChangeLog.xml",
055: changeLog.getPhysicalFilePath());
056:
057: assertNull(changeLog.getPreconditions());
058: assertEquals(3, changeLog.getChangeSets().size());
059:
060: // change 0
061: ChangeSet changeSet = changeLog.getChangeSets().get(0);
062: assertEquals("nvoxland", changeSet.getAuthor());
063: assertEquals("1", changeSet.getId());
064: assertEquals(1, changeSet.getChanges().size());
065: assertEquals(
066: "liquibase/parser/xml/multiChangeSetChangeLog.xml",
067: changeSet.getFilePath());
068: assertNull(changeSet.getComments());
069: assertFalse(changeSet.shouldAlwaysRun());
070: assertFalse(changeSet.shouldRunOnChange());
071:
072: Change change = changeSet.getChanges().get(0);
073: assertEquals("createTable", change.getTagName());
074: assertTrue(change instanceof CreateTableChange);
075:
076: // change 1
077: changeSet = changeLog.getChangeSets().get(1);
078: assertEquals("nvoxland", changeSet.getAuthor());
079: assertEquals("2", changeSet.getId());
080: assertEquals(2, changeSet.getChanges().size());
081: assertEquals(
082: "liquibase/parser/xml/multiChangeSetChangeLog.xml",
083: changeSet.getFilePath());
084: assertEquals("Testing add column", changeSet.getComments());
085: assertTrue(changeSet.shouldAlwaysRun());
086: assertTrue(changeSet.shouldRunOnChange());
087: assertEquals(2, changeSet.getRollBackStatements().length);
088: assertTrue(changeSet.getRollBackStatements()[0] instanceof RawSqlStatement);
089: assertTrue(changeSet.getRollBackStatements()[1] instanceof RawSqlStatement);
090:
091: change = changeSet.getChanges().get(0);
092: assertEquals("addColumn", change.getTagName());
093: assertTrue(change instanceof AddColumnChange);
094:
095: change = changeSet.getChanges().get(1);
096: assertEquals("addColumn", change.getTagName());
097: assertTrue(change instanceof AddColumnChange);
098:
099: // change 2
100: changeSet = changeLog.getChangeSets().get(2);
101: assertEquals("bob", changeSet.getAuthor());
102: assertEquals("3", changeSet.getId());
103: assertEquals(1, changeSet.getChanges().size());
104: assertEquals(
105: "liquibase/parser/xml/multiChangeSetChangeLog.xml",
106: changeSet.getFilePath());
107: assertNull(changeSet.getComments());
108: assertFalse(changeSet.shouldAlwaysRun());
109: assertFalse(changeSet.shouldRunOnChange());
110:
111: change = changeSet.getChanges().get(0);
112: assertEquals("createTable", change.getTagName());
113: assertTrue(change instanceof CreateTableChange);
114: }
115:
116: @Test
117: public void logicalPathChangeLog() throws Exception {
118: DatabaseChangeLog changeLog = new XMLChangeLogParser().parse(
119: "liquibase/parser/xml/logicalPathChangeLog.xml",
120: new JUnitFileOpener());
121:
122: assertEquals(
123: "liquibase/parser-logical/xml/logicalPathChangeLog.xml",
124: changeLog.getLogicalFilePath());
125: assertEquals("liquibase/parser/xml/logicalPathChangeLog.xml",
126: changeLog.getPhysicalFilePath());
127:
128: assertNull(changeLog.getPreconditions());
129: assertEquals(1, changeLog.getChangeSets().size());
130: assertEquals(
131: "liquibase/parser-logical/xml/logicalPathChangeLog.xml",
132: changeLog.getChangeSets().get(0).getFilePath());
133:
134: }
135:
136: @Test
137: public void preconditionsChangeLog() throws Exception {
138: DatabaseChangeLog changeLog = new XMLChangeLogParser().parse(
139: "liquibase/parser/xml/preconditionsChangeLog.xml",
140: new JUnitFileOpener());
141:
142: assertEquals("liquibase/parser/xml/preconditionsChangeLog.xml",
143: changeLog.getLogicalFilePath());
144: assertEquals("liquibase/parser/xml/preconditionsChangeLog.xml",
145: changeLog.getPhysicalFilePath());
146:
147: assertNotNull(changeLog.getPreconditions());
148: assertEquals(2, changeLog.getPreconditions()
149: .getNestedPreconditions().size());
150:
151: assertEquals("runningAs", changeLog.getPreconditions()
152: .getNestedPreconditions().get(0).getTagName());
153:
154: assertEquals("or", changeLog.getPreconditions()
155: .getNestedPreconditions().get(1).getTagName());
156: assertEquals("dbms", ((OrPrecondition) changeLog
157: .getPreconditions().getNestedPreconditions().get(1))
158: .getNestedPreconditions().get(0).getTagName());
159: assertEquals("dbms", ((OrPrecondition) changeLog
160: .getPreconditions().getNestedPreconditions().get(1))
161: .getNestedPreconditions().get(1).getTagName());
162:
163: assertEquals(1, changeLog.getChangeSets().size());
164: }
165:
166: @Test
167: public void testNestedChangeLog() throws Exception {
168: DatabaseChangeLog changeLog = new XMLChangeLogParser().parse(
169: "liquibase/parser/xml/nestedChangeLog.xml",
170: new JUnitFileOpener());
171:
172: assertEquals("liquibase/parser/xml/nestedChangeLog.xml",
173: changeLog.getLogicalFilePath());
174: assertEquals("liquibase/parser/xml/nestedChangeLog.xml",
175: changeLog.getPhysicalFilePath());
176:
177: assertNull(changeLog.getPreconditions());
178: assertEquals(3, changeLog.getChangeSets().size());
179:
180: // change 0
181: ChangeSet changeSet = changeLog.getChangeSets().get(0);
182: assertEquals("nvoxland", changeSet.getAuthor());
183: assertEquals("1", changeSet.getId());
184: assertEquals(1, changeSet.getChanges().size());
185: assertEquals("liquibase/parser/xml/nestedChangeLog.xml",
186: changeSet.getFilePath());
187:
188: Change change = changeSet.getChanges().get(0);
189: assertEquals("createTable", change.getTagName());
190: assertTrue(change instanceof CreateTableChange);
191: assertEquals("employee", ((CreateTableChange) change)
192: .getTableName());
193:
194: // change 1 (from included simple change log)
195: changeSet = changeLog.getChangeSets().get(1);
196: assertEquals("nvoxland", changeSet.getAuthor());
197: assertEquals("1", changeSet.getId());
198: assertEquals(1, changeSet.getChanges().size());
199: assertEquals("liquibase/parser/xml/simpleChangeLog.xml",
200: changeSet.getFilePath());
201:
202: change = changeSet.getChanges().get(0);
203: assertEquals("createTable", change.getTagName());
204: assertTrue(change instanceof CreateTableChange);
205: assertEquals("person", ((CreateTableChange) change)
206: .getTableName());
207:
208: // change 2
209: changeSet = changeLog.getChangeSets().get(2);
210: assertEquals("nvoxland", changeSet.getAuthor());
211: assertEquals("2", changeSet.getId());
212: assertEquals(1, changeSet.getChanges().size());
213: assertEquals("liquibase/parser/xml/nestedChangeLog.xml",
214: changeSet.getFilePath());
215:
216: change = changeSet.getChanges().get(0);
217: assertEquals("addColumn", change.getTagName());
218: assertTrue(change instanceof AddColumnChange);
219: assertEquals("employee", ((AddColumnChange) change)
220: .getTableName());
221: }
222:
223: @Test
224: public void doubleNestedChangeLog() throws Exception {
225: DatabaseChangeLog changeLog = new XMLChangeLogParser().parse(
226: "liquibase/parser/xml/doubleNestedChangeLog.xml",
227: new JUnitFileOpener());
228:
229: assertEquals("liquibase/parser/xml/doubleNestedChangeLog.xml",
230: changeLog.getLogicalFilePath());
231: assertEquals("liquibase/parser/xml/doubleNestedChangeLog.xml",
232: changeLog.getPhysicalFilePath());
233:
234: assertNull(changeLog.getPreconditions());
235: assertEquals(4, changeLog.getChangeSets().size());
236:
237: // change 0
238: ChangeSet changeSet = changeLog.getChangeSets().get(0);
239: assertEquals("nvoxland", changeSet.getAuthor());
240: assertEquals("1", changeSet.getId());
241: assertEquals(1, changeSet.getChanges().size());
242: assertEquals("liquibase/parser/xml/doubleNestedChangeLog.xml",
243: changeSet.getFilePath());
244:
245: Change change = changeSet.getChanges().get(0);
246: assertEquals("createTable", change.getTagName());
247: assertTrue(change instanceof CreateTableChange);
248: assertEquals("partner", ((CreateTableChange) change)
249: .getTableName());
250:
251: // change 1 from nestedChangeLog
252: changeSet = changeLog.getChangeSets().get(1);
253: assertEquals("nvoxland", changeSet.getAuthor());
254: assertEquals("1", changeSet.getId());
255: assertEquals(1, changeSet.getChanges().size());
256: assertEquals("liquibase/parser/xml/nestedChangeLog.xml",
257: changeSet.getFilePath());
258:
259: change = changeSet.getChanges().get(0);
260: assertEquals("createTable", change.getTagName());
261: assertTrue(change instanceof CreateTableChange);
262: assertEquals("employee", ((CreateTableChange) change)
263: .getTableName());
264:
265: // change 2 (from included simple change log)
266: changeSet = changeLog.getChangeSets().get(2);
267: assertEquals("nvoxland", changeSet.getAuthor());
268: assertEquals("1", changeSet.getId());
269: assertEquals(1, changeSet.getChanges().size());
270: assertEquals("liquibase/parser/xml/simpleChangeLog.xml",
271: changeSet.getFilePath());
272:
273: change = changeSet.getChanges().get(0);
274: assertEquals("createTable", change.getTagName());
275: assertTrue(change instanceof CreateTableChange);
276: assertEquals("person", ((CreateTableChange) change)
277: .getTableName());
278:
279: // change 3 from nested Change log
280: changeSet = changeLog.getChangeSets().get(3);
281: assertEquals("nvoxland", changeSet.getAuthor());
282: assertEquals("2", changeSet.getId());
283: assertEquals(1, changeSet.getChanges().size());
284: assertEquals("liquibase/parser/xml/nestedChangeLog.xml",
285: changeSet.getFilePath());
286:
287: change = changeSet.getChanges().get(0);
288: assertEquals("addColumn", change.getTagName());
289: assertTrue(change instanceof AddColumnChange);
290: assertEquals("employee", ((AddColumnChange) change)
291: .getTableName());
292: }
293:
294: @Test
295: public void missingChangeLog() throws Exception {
296: try {
297: DatabaseChangeLog changeLog = new XMLChangeLogParser()
298: .parse("liquibase/parser/xml/missingChangeLog.xml",
299: new JUnitFileOpener());
300: } catch (Exception e) {
301: assertTrue(e instanceof ChangeLogParseException);
302: assertEquals(
303: "liquibase/parser/xml/missingChangeLog.xml does not exist",
304: e.getMessage());
305:
306: }
307: }
308:
309: @Test
310: public void malformedChangeLog() throws Exception {
311: try {
312: DatabaseChangeLog changeLog = new XMLChangeLogParser()
313: .parse(
314: "liquibase/parser/xml/malformedChangeLog.xml",
315: new JUnitFileOpener());
316: } catch (Exception e) {
317: assertTrue(e instanceof ChangeLogParseException);
318: assertTrue(e.getMessage().startsWith("Error parsing line"));
319:
320: }
321: }
322:
323: @Test
324: public void sampleChangeLogs() throws Exception {
325: new XMLChangeLogParser().parse(
326: "changelogs/cache/complete/root.changelog.xml",
327: new JUnitFileOpener());
328: new XMLChangeLogParser().parse(
329: "changelogs/db2/complete/root.changelog.xml",
330: new JUnitFileOpener());
331: new XMLChangeLogParser().parse(
332: "changelogs/derby/complete/root.changelog.xml",
333: new JUnitFileOpener());
334: new XMLChangeLogParser().parse(
335: "changelogs/firebird/complete/root.changelog.xml",
336: new JUnitFileOpener());
337: new XMLChangeLogParser().parse(
338: "changelogs/h2/complete/root.changelog.xml",
339: new JUnitFileOpener());
340: new XMLChangeLogParser().parse(
341: "changelogs/hsqldb/complete/root.changelog.xml",
342: new JUnitFileOpener());
343: new XMLChangeLogParser().parse(
344: "changelogs/maxdb/complete/root.changelog.xml",
345: new JUnitFileOpener());
346: new XMLChangeLogParser().parse(
347: "changelogs/mysql/complete/root.changelog.xml",
348: new JUnitFileOpener());
349: new XMLChangeLogParser().parse(
350: "changelogs/oracle/complete/root.changelog.xml",
351: new JUnitFileOpener());
352: new XMLChangeLogParser().parse(
353: "changelogs/pgsql/complete/root.changelog.xml",
354: new JUnitFileOpener());
355: new XMLChangeLogParser().parse(
356: "changelogs/sybase/complete/root.changelog.xml",
357: new JUnitFileOpener());
358: new XMLChangeLogParser().parse(
359: "changelogs/unsupported/complete/root.changelog.xml",
360: new JUnitFileOpener());
361: }
362: }
|