commit 59ec908f77f09f92d902f5ec0639ea0c756c87ea
Author: Stanislav Ochotnicky <sochotnicky(a)redhat.com>
Date: Wed Oct 2 15:47:52 2013 +0200
POM: strip comments and replace CDATA sections for parser
Also verify that requested text nodes have no children
python/javapackages/pom.py | 17 ++++++++++++++---
python/test/data/junit-broken-subnode.pom | 18 ++++++++++++++++++
python/test/data/junit-comments.pom | 17 +++++++++++++++++
python/test/pom_test.py | 14 +++++++++++++-
4 files changed, 62 insertions(+), 4 deletions(-)
---
diff --git a/python/javapackages/pom.py b/python/javapackages/pom.py
index 2051b9e..87a4f4d 100644
--- a/python/javapackages/pom.py
+++ b/python/javapackages/pom.py
@@ -30,7 +30,7 @@
#
# Authors: Stanislav Ochotnicky <sochotnicky(a)redhat.com>
-from lxml.etree import ElementTree
+from lxml.etree import ElementTree, XMLParser
class PomLoadingException(Exception):
pass
@@ -41,7 +41,9 @@ class POM(object):
"""
def __init__(self, path):
et = ElementTree ()
- self.__doc = et.parse(path)
+ parser = XMLParser(remove_comments=True,
+ strip_cdata=True)
+ self.__doc = et.parse(path, parser=parser)
if self.__doc is None:
raise PomLoadingException("Failed to load pom.xml. You have a
problem")
@@ -67,6 +69,8 @@ class POM(object):
gId = self.__find('./pom:groupId')
if gId is None:
gId = self.__find('./pom:parent/pom:groupId')
+ if len(gId) != 0:
+ raise PomLoadingException("Unexpected child nodes under groupId")
return gId.text.strip()
@property
@@ -74,7 +78,10 @@ class POM(object):
"""
Effective artifactId of the pom Artifact
"""
- return self.__find('./pom:artifactId').text.strip()
+ aId = self.__find('./pom:artifactId')
+ if len(aId) != 0:
+ raise PomLoadingException("Unexpected child nodes under
artifactId")
+ return aId.text.strip()
@property
def version(self):
@@ -86,6 +93,8 @@ class POM(object):
if version is None:
version = self.__find('./pom:parent/pom:version')
assert version is not None
+ if len(version) != 0:
+ raise PomLoadingException("Unexpected child nodes under version")
return version.text.strip()
@property
@@ -95,5 +104,7 @@ class POM(object):
"""
p = self.__find('./pom:packaging')
if p is not None:
+ if len(p) != 0:
+ raise PomLoadingException("Unexpected child nodes under
packaging")
return p.text.strip()
return None
diff --git a/python/test/data/junit-broken-subnode.pom
b/python/test/data/junit-broken-subnode.pom
new file mode 100644
index 0000000..4c2fc1d
--- /dev/null
+++ b/python/test/data/junit-broken-subnode.pom
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd ">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>
+ <v2>V2222</v2>
+ <!-- blah -->4.11<!-- some comment -->
+ </version>
+
+ <name>JUnit</name>
+ <url>http://junit.org</url>
+ <description>
+ JUnit is a regression testing framework written by Erich Gamma and Kent Beck.
+ It is used by the developer who implements unit tests in Java.
+ </description>
+</project>
diff --git a/python/test/data/junit-comments.pom b/python/test/data/junit-comments.pom
new file mode 100644
index 0000000..1c548d3
--- /dev/null
+++ b/python/test/data/junit-comments.pom
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd ">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>
+ <!-- blah -->4.11<!-- some comment -->
+ </version>
+
+ <name>JUnit</name>
+ <url>http://junit.org</url>
+ <description>
+ JUnit is a regression testing framework written by Erich Gamma and Kent Beck.
+ It is used by the developer who implements unit tests in Java.
+ </description>
+</project>
diff --git a/python/test/pom_test.py b/python/test/pom_test.py
index 39cb5ce..89e4ee7 100644
--- a/python/test/pom_test.py
+++ b/python/test/pom_test.py
@@ -2,7 +2,7 @@ import os
import unittest
import lxml
-from javapackages import POM
+from javapackages.pom import POM, PomLoadingException
from misc import exception_expected
@@ -62,6 +62,18 @@ class TestPOM(unittest.TestCase):
def test_unparsable_xml(self, p):
self.fail("Unparsable xml successfully parsed")
+ @exception_expected(PomLoadingException)
+ @pomfile("junit-broken-subnode.pom")
+ def test_pom_broken_subnode(self, p):
+ self.fail("Broken POM succesfully loaded")
+
+ @pomfile("junit-comments.pom")
+ def test_pom_comments(self, p):
+ self.assertEqual(p.packaging, None)
+ self.assertEqual(p.groupId, "junit")
+ self.assertEqual(p.artifactId, "junit")
+ self.assertEqual(p.version, "4.11")
+
if __name__ == '__main__':
unittest.main()