Skip to content

Commit

Permalink
-Add handling for incorrectly setup mongeez.xml file to fix NPE that …
Browse files Browse the repository at this point in the history
…occurs when the changeFiles block is not present, but there is valid XML

-Add handling for empty mongoChangeLog blocks to prevent NPE
  • Loading branch information
echrist committed Oct 15, 2013
1 parent 6bf91e0 commit 49b22a5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/main/java/org/mongeez/reader/FilesetXMLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ public List<Resource> getFiles(Resource file) {
digester.addSetNext("changeFiles/file", "add");

ChangeFileSet changeFileSet = (ChangeFileSet) digester.parse(file.getInputStream());
logger.info("Num of changefiles " + changeFileSet.getChangeFiles().size());

for (ChangeFile changeFile : changeFileSet.getChangeFiles()) {
files.add(file.createRelative(changeFile.getPath()));
}
if (changeFileSet != null) {
logger.info("Num of changefiles " + changeFileSet.getChangeFiles().size());

for (ChangeFile changeFile : changeFileSet.getChangeFiles()) {
files.add(file.createRelative(changeFile.getPath()));
}
}
else {
logger.error("The file {} doesn't seem to contain a changeFiles declaration. Are you "
+ "using the correct file to initialize Mongeez?", file.getFilename());
}
} catch (IOException e) {
logger.error("IOException", e);
} catch (org.xml.sax.SAXException e) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/mongeez/reader/XmlChangeSetReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ public List<ChangeSet> getChangeSets(Resource file) {

try {
ChangeSetList changeFileSet = (ChangeSetList) digester.parse(file.getInputStream());
for (ChangeSet changeSet : changeFileSet.getList()) {
ChangeSetReaderUtil.populateChangeSetResourceInfo(changeSet, file);
}
changeSets.addAll(changeFileSet.getList());
if (changeFileSet.getList() == null) {
logger.warn("Ignoring change file {}, it does not contain any changeSets", file.getFilename());
}
else {
for (ChangeSet changeSet : changeFileSet.getList()) {
ChangeSetReaderUtil.populateChangeSetResourceInfo(changeSet, file);
}
changeSets.addAll(changeFileSet.getList());
}
} catch (IOException e) {
logger.error("IOException", e);
} catch (org.xml.sax.SAXException e) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/mongeez/MongeezTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,23 @@ public void testNoFiles() throws Exception {

assertEquals(db.getCollection("mongeez").count(), 1);
}

@Test(groups = "dao")
public void testNoFailureOnEmptyChangeLog() throws Exception {
assertEquals(db.getCollection("mongeez").count(), 0);

Mongeez mongeez = create("mongeez_empty_changelog.xml");
mongeez.process();

assertEquals(db.getCollection("mongeez").count(), 1);
}

@Test(groups = "dao")
public void testNoFailureOnNoChangeFilesBlock() throws Exception {
assertEquals(db.getCollection("mongeez").count(), 0);

Mongeez mongeez = create("mongeez_no_changefiles_declared.xml");
mongeez.process();
assertEquals(db.getCollection("mongeez").count(), 1);
}
}
4 changes: 4 additions & 0 deletions src/test/resources/empty_changeset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!--A changelog with no changes. This can occur on first setup of new projects-->
<mongoChangeLog>
</mongoChangeLog>

6 changes: 6 additions & 0 deletions src/test/resources/mongeez_empty_changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--
A mongeez setup with a change file that has no changes in the changelog
-->
<changeFiles>
<file path="empty_changeset.xml"/>
</changeFiles>
5 changes: 5 additions & 0 deletions src/test/resources/mongeez_no_changefiles_declared.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--
A mongeez setup with valid xml that does not contain changeFiles root element. Not normal, but causes an NPE
-->
<test>
</test>

0 comments on commit 49b22a5

Please sign in to comment.