diff --git a/src/main/java/com/rometools/rome/io/impl/RSS20Parser.java b/src/main/java/com/rometools/rome/io/impl/RSS20Parser.java index d568fb6..b90c4e6 100644 --- a/src/main/java/com/rometools/rome/io/impl/RSS20Parser.java +++ b/src/main/java/com/rometools/rome/io/impl/RSS20Parser.java @@ -14,14 +14,15 @@ * limitations under the License. * */ + package com.rometools.rome.io.impl; +import com.rometools.rome.feed.rss.Description; + import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; -import com.rometools.rome.feed.rss.Description; - public class RSS20Parser extends RSS094Parser { public RSS20Parser() { @@ -50,11 +51,16 @@ public class RSS20Parser extends RSS094Parser { @Override public boolean isMyType(final Document document) { - final Element rssRoot = document.getRootElement(); - final Attribute version = rssRoot.getAttribute("version"); - // as far ROME is concerned RSS 2.0, 2.00 and 2.0.X are all the same, so let's use - // startsWith for leniency. - return rssRoot.getName().equals("rss") && version != null && version.getValue().startsWith(getRSSVersion()); + return rootElementMatches(document) && versionMatches(document); } + private boolean rootElementMatches(final Document document) { + return document.getRootElement().getName().equals("rss"); + } + + private boolean versionMatches(final Document document) { + final Attribute version = document.getRootElement().getAttribute("version"); + return (version != null) + && version.getValue().trim().startsWith(getRSSVersion()); + } } diff --git a/src/test/java/com/rometools/rome/io/impl/RSS20ParserTest.java b/src/test/java/com/rometools/rome/io/impl/RSS20ParserTest.java new file mode 100644 index 0000000..fff46c7 --- /dev/null +++ b/src/test/java/com/rometools/rome/io/impl/RSS20ParserTest.java @@ -0,0 +1,47 @@ +package com.rometools.rome.io.impl; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.rometools.rome.io.WireFeedParser; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.junit.Before; +import org.junit.Test; + +public class RSS20ParserTest { + + private WireFeedParser parser; + private Document document; + + @Before + public void setUp() throws Exception { + parser = new RSS20Parser(); + document = new Document(); + } + + @Test + public void testIsMyType() { + document.setRootElement(new Element("rss").setAttribute("version", "2.0")); + assertTrue(parser.isMyType(document)); + } + + @Test + public void testIsMyTypeNotMyType() { + document.setRootElement(new Element("rss").setAttribute("version", "1.0")); + assertFalse(parser.isMyType(document)); + } + + @Test + public void testIsMyTypeVersionWithSpaces() { + document.setRootElement(new Element("rss").setAttribute("version", " 2.0 ")); + assertTrue(parser.isMyType(document)); + } + + @Test + public void testIsMyTypeVersionWithTrailingText() { + document.setRootElement(new Element("rss").setAttribute("version", "2.0test")); + assertTrue(parser.isMyType(document)); + } +}