From 6e8590ba8ecf6801b2787193a94bb34a37941072 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Fri, 8 Jan 2016 19:55:35 +0100 Subject: [PATCH 1/4] The category attribute of outlines will now be exported --- pom.xml | 4 ++ .../opml/io/impl/OPML20Generator.java | 48 ++++++++++++----- .../opml/io/impl/OPML20GeneratorTest.java | 52 +++++++++++++++++++ 3 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 src/test/java/com/rometools/opml/io/impl/OPML20GeneratorTest.java diff --git a/pom.xml b/pom.xml index e58c56b..b8d6cc4 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,10 @@ junit test + + org.hamcrest + hamcrest-library + xmlunit xmlunit diff --git a/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java b/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java index dfc4137..a774291 100644 --- a/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java +++ b/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java @@ -1,5 +1,7 @@ package com.rometools.opml.io.impl; +import java.util.Collection; +import java.util.List; import java.util.Locale; import org.jdom2.Document; @@ -12,7 +14,9 @@ import com.rometools.rome.io.FeedException; import com.rometools.rome.io.impl.DateParser; /** - * @author cooper + * Generator for OPML 2.0 documents. + * + * @see http://dev.opml.org/spec2.html */ public class OPML20Generator extends OPML10Generator { @@ -41,34 +45,54 @@ public class OPML20Generator extends OPML10Generator { */ @Override public Document generate(final WireFeed feed) throws IllegalArgumentException, FeedException { - final Document retValue = super.generate(feed); - retValue.getRootElement().setAttribute("version", "2.0"); - return retValue; + final Document document = super.generate(feed); + document.getRootElement().setAttribute("version", "2.0"); + return document; } @Override protected Element generateHead(final Opml opml) { - final Element docs = new Element("docs"); - docs.setText(opml.getDocs()); + final Element docsElement = new Element("docs"); + docsElement.setText(opml.getDocs()); - final Element retValue = super.generateHead(opml); - retValue.addContent(docs); - return retValue; + final Element headElement = super.generateHead(opml); + headElement.addContent(docsElement); + return headElement; } @Override protected Element generateOutline(final Outline outline) { - final Element retValue = super.generateOutline(outline); + final Element outlineElement = super.generateOutline(outline); if (outline.getCreated() != null) { - retValue.setAttribute("created", DateParser.formatRFC822(outline.getCreated(), Locale.US)); + outlineElement.setAttribute("created", DateParser.formatRFC822(outline.getCreated(), Locale.US)); } - return retValue; + final List categories = outline.getCategories(); + final String categoriesValue = generateCategoriesValue(categories); + addNotNullAttribute(outlineElement, "category", categoriesValue); + + return outlineElement; } + private String generateCategoriesValue(final Collection categories) { + final StringBuilder builder = new StringBuilder(); + for (final String category : categories) { + if (category != null && !category.trim().isEmpty()) { + builder.append("/"); + builder.append(category.trim()); + } + } + final String categoryString = builder.toString(); + if (categoryString == null || categoryString.trim().isEmpty()) { + return null; + } else { + return categoryString; + } + } + } diff --git a/src/test/java/com/rometools/opml/io/impl/OPML20GeneratorTest.java b/src/test/java/com/rometools/opml/io/impl/OPML20GeneratorTest.java new file mode 100644 index 0000000..d4359b8 --- /dev/null +++ b/src/test/java/com/rometools/opml/io/impl/OPML20GeneratorTest.java @@ -0,0 +1,52 @@ +package com.rometools.opml.io.impl; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.custommonkey.xmlunit.XMLUnit; +import org.custommonkey.xmlunit.exceptions.XpathException; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.rometools.opml.feed.opml.Opml; +import com.rometools.opml.feed.opml.Outline; +import com.rometools.rome.io.FeedException; +import com.rometools.rome.io.WireFeedOutput; + +public class OPML20GeneratorTest { + + @Test + public void testCategoryOutput() throws IllegalArgumentException, FeedException, SAXException, IOException, XpathException { + checkCategoryOutput(null, ""); + checkCategoryOutput(Arrays.asList("category1"), "/category1"); + checkCategoryOutput(Arrays.asList("category1", "category2"), "/category1/category2"); + } + + private void checkCategoryOutput(final List categories, final String asserted) + throws IllegalArgumentException, FeedException, SAXException, IOException, XpathException { + + final Outline outline = new Outline("outline1", null); + outline.setCategories(categories); + final List outlines = Arrays.asList(outline); + + final Opml opml = new Opml(); + opml.setFeedType("opml_2.0"); + opml.setTitle("title"); + opml.setOutlines(outlines); + + final WireFeedOutput output = new WireFeedOutput(); + final String xml = output.outputString(opml); + + final Document document = XMLUnit.buildControlDocument(xml); + final String categoryValue = XMLUnit.newXpathEngine().evaluate("/opml/body/outline/@category", document); + assertThat(categoryValue, is(equalTo(asserted))); + + } + +} From db6138b4406aa029ad9b101cebb12a7c2376b947 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Fri, 8 Jan 2016 19:58:28 +0100 Subject: [PATCH 2/4] Removed useless trim --- src/main/java/com/rometools/opml/io/impl/OPML20Generator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java b/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java index a774291..b83379b 100644 --- a/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java +++ b/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java @@ -88,7 +88,7 @@ public class OPML20Generator extends OPML10Generator { } } final String categoryString = builder.toString(); - if (categoryString == null || categoryString.trim().isEmpty()) { + if (categoryString == null || categoryString.isEmpty()) { return null; } else { return categoryString; From 5133609c6e7b0bf3c581d63eececd4f9bc40e861 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Fri, 8 Jan 2016 20:03:46 +0100 Subject: [PATCH 3/4] Refactored category value creation --- .../rometools/opml/io/impl/OPML20Generator.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java b/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java index b83379b..8845c82 100644 --- a/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java +++ b/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java @@ -72,27 +72,30 @@ public class OPML20Generator extends OPML10Generator { } final List categories = outline.getCategories(); - final String categoriesValue = generateCategoriesValue(categories); - addNotNullAttribute(outlineElement, "category", categoriesValue); + final String categoryValue = generateCategoryValue(categories); + addNotNullAttribute(outlineElement, "category", categoryValue); return outlineElement; } - private String generateCategoriesValue(final Collection categories) { + private String generateCategoryValue(final Collection categories) { + final StringBuilder builder = new StringBuilder(); + for (final String category : categories) { if (category != null && !category.trim().isEmpty()) { builder.append("/"); builder.append(category.trim()); } } - final String categoryString = builder.toString(); - if (categoryString == null || categoryString.isEmpty()) { - return null; + + if (builder.length() > 0) { + return builder.toString(); } else { - return categoryString; + return null; } + } } From 4eefb566e86dd48ee16912986daa1cb59461e80f Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Sat, 9 Jan 2016 01:52:09 +0100 Subject: [PATCH 4/4] Fixed wrong category concatenation --- .../opml/io/impl/OPML20Generator.java | 7 +- .../opml/io/impl/OPML20GeneratorTest.java | 67 ++++++++++++------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java b/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java index 8845c82..96ff4ae 100644 --- a/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java +++ b/src/main/java/com/rometools/opml/io/impl/OPML20Generator.java @@ -83,9 +83,14 @@ public class OPML20Generator extends OPML10Generator { final StringBuilder builder = new StringBuilder(); + boolean first = true; for (final String category : categories) { if (category != null && !category.trim().isEmpty()) { - builder.append("/"); + if (first) { + first = false; + } else { + builder.append(","); + } builder.append(category.trim()); } } diff --git a/src/test/java/com/rometools/opml/io/impl/OPML20GeneratorTest.java b/src/test/java/com/rometools/opml/io/impl/OPML20GeneratorTest.java index d4359b8..28dd3d1 100644 --- a/src/test/java/com/rometools/opml/io/impl/OPML20GeneratorTest.java +++ b/src/test/java/com/rometools/opml/io/impl/OPML20GeneratorTest.java @@ -4,49 +4,70 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import java.io.IOException; import java.util.Arrays; -import java.util.List; import org.custommonkey.xmlunit.XMLUnit; -import org.custommonkey.xmlunit.exceptions.XpathException; import org.junit.Test; import org.w3c.dom.Document; -import org.xml.sax.SAXException; +import org.w3c.dom.NodeList; import com.rometools.opml.feed.opml.Opml; import com.rometools.opml.feed.opml.Outline; -import com.rometools.rome.io.FeedException; import com.rometools.rome.io.WireFeedOutput; public class OPML20GeneratorTest { @Test - public void testCategoryOutput() throws IllegalArgumentException, FeedException, SAXException, IOException, XpathException { - checkCategoryOutput(null, ""); - checkCategoryOutput(Arrays.asList("category1"), "/category1"); - checkCategoryOutput(Arrays.asList("category1", "category2"), "/category1/category2"); + public void testOutputOfNullCategory() { + assertThat(categoryOf((String) null).getLength(), is(equalTo(0))); } - private void checkCategoryOutput(final List categories, final String asserted) - throws IllegalArgumentException, FeedException, SAXException, IOException, XpathException { + @Test + public void testOutputOfEmptyCategory() { + assertThat(categoryOf("").getLength(), is(equalTo(0))); + } - final Outline outline = new Outline("outline1", null); - outline.setCategories(categories); - final List outlines = Arrays.asList(outline); + @Test + public void testOutputOfBlankCategory() { + assertThat(categoryOf(" ").getLength(), is(equalTo(0))); + } - final Opml opml = new Opml(); - opml.setFeedType("opml_2.0"); - opml.setTitle("title"); - opml.setOutlines(outlines); + @Test + public void testOutputOfOneCategory() { + assertThat(categoryValueOf("category1"), is(equalTo("category1"))); + } - final WireFeedOutput output = new WireFeedOutput(); - final String xml = output.outputString(opml); + @Test + public void testOutputOfMultipleCategories() { + assertThat(categoryValueOf("category1", "category2"), is(equalTo("category1,category2"))); + } - final Document document = XMLUnit.buildControlDocument(xml); - final String categoryValue = XMLUnit.newXpathEngine().evaluate("/opml/body/outline/@category", document); - assertThat(categoryValue, is(equalTo(asserted))); + private NodeList categoryOf(final String... categories) { + try { + + final Outline outline = new Outline("outline1", null); + outline.setCategories(Arrays.asList(categories)); + + final Opml opml = new Opml(); + opml.setFeedType("opml_2.0"); + opml.setTitle("title"); + opml.setOutlines(Arrays.asList(outline)); + + final WireFeedOutput output = new WireFeedOutput(); + final String xml = output.outputString(opml); + + final Document document = XMLUnit.buildControlDocument(xml); + return XMLUnit.newXpathEngine().getMatchingNodes("/opml/body/outline/@category", document); + + } catch (final Exception e) { + throw new RuntimeException(e); + } + + } + + private String categoryValueOf(final String... categories) { + return categoryOf(categories).item(0).getNodeValue(); } }