From 4eefb566e86dd48ee16912986daa1cb59461e80f Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Sat, 9 Jan 2016 01:52:09 +0100 Subject: [PATCH] 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(); } }