Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #32 from exell-christopher/master

Fix NPE issues in xml file parsing
  • Loading branch information...
commit 660c1373b4fcdca75f4e06866837cf2e05e9aca2 2 parents 6bf91e0 + ea7e75e
@jozic jozic authored
View
8 src/main/java/org/mongeez/commands/ChangeSetList.java
@@ -19,6 +19,9 @@
List<ChangeSet> list = null;
public List<ChangeSet> getList() {
+ if (list == null) {
+ list = new ArrayList<ChangeSet>();
+ }
return list;
}
@@ -27,9 +30,6 @@ public void setList(List<ChangeSet> list) {
}
public void add(ChangeSet changeSet) {
- if (list == null) {
- list = new ArrayList<ChangeSet>();
- }
- this.list.add(changeSet);
+ getList().add(changeSet);
}
}
View
14 src/main/java/org/mongeez/reader/FilesetXMLReader.java
@@ -41,11 +41,17 @@
digester.addSetProperties("changeFiles/file");
digester.addSetNext("changeFiles/file", "add");
+ logger.info("Parsing XML Fileset file {}", file.getFilename());
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 found " + 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);
View
15 src/main/java/org/mongeez/reader/XmlChangeSetReader.java
@@ -51,21 +51,26 @@ public boolean supports(Resource file) {
}
@Override
- public List<ChangeSet> getChangeSets(Resource file) {
+ public List<ChangeSet> getChangeSets(Resource file) {
List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
try {
+ logger.info("Parsing XML Change Set File {}", file.getFilename());
ChangeSetList changeFileSet = (ChangeSetList) digester.parse(file.getInputStream());
- for (ChangeSet changeSet : changeFileSet.getList()) {
- ChangeSetReaderUtil.populateChangeSetResourceInfo(changeSet, file);
+ if (changeFileSet == null) {
+ logger.warn("Ignoring change file {}, the parser returned null. Please check your formatting.", file.getFilename());
+ }
+ else {
+ for (ChangeSet changeSet : changeFileSet.getList()) {
+ ChangeSetReaderUtil.populateChangeSetResourceInfo(changeSet, file);
+ }
+ changeSets.addAll(changeFileSet.getList());
}
- changeSets.addAll(changeFileSet.getList());
} catch (IOException e) {
logger.error("IOException", e);
} catch (org.xml.sax.SAXException e) {
logger.error("SAXException", e);
}
-
return changeSets;
}
}
View
19 src/test/java/org/mongeez/MongeezTest.java
@@ -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);
+ }
}
View
4 src/test/resources/empty_changeset.xml
@@ -0,0 +1,4 @@
+<!--A changelog with no changes. This can occur on first setup of new projects-->
+<mongoChangeLog>
+</mongoChangeLog>
+
View
6 src/test/resources/mongeez_empty_changelog.xml
@@ -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>
View
5 src/test/resources/mongeez_no_changefiles_declared.xml
@@ -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>
Please sign in to comment.
Something went wrong with that request. Please try again.