Fixed wrong category concatenation

This commit is contained in:
Patrick Gotthard 2016-01-09 01:52:09 +01:00
parent 5133609c6e
commit 4eefb566e8
2 changed files with 50 additions and 24 deletions

View file

@ -83,9 +83,14 @@ public class OPML20Generator extends OPML10Generator {
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
boolean first = true;
for (final String category : categories) { for (final String category : categories) {
if (category != null && !category.trim().isEmpty()) { if (category != null && !category.trim().isEmpty()) {
builder.append("/"); if (first) {
first = false;
} else {
builder.append(",");
}
builder.append(category.trim()); builder.append(category.trim());
} }
} }

View file

@ -4,49 +4,70 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.custommonkey.xmlunit.XMLUnit; import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.exceptions.XpathException;
import org.junit.Test; import org.junit.Test;
import org.w3c.dom.Document; 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.Opml;
import com.rometools.opml.feed.opml.Outline; import com.rometools.opml.feed.opml.Outline;
import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.WireFeedOutput; import com.rometools.rome.io.WireFeedOutput;
public class OPML20GeneratorTest { public class OPML20GeneratorTest {
@Test @Test
public void testCategoryOutput() throws IllegalArgumentException, FeedException, SAXException, IOException, XpathException { public void testOutputOfNullCategory() {
checkCategoryOutput(null, ""); assertThat(categoryOf((String) null).getLength(), is(equalTo(0)));
checkCategoryOutput(Arrays.asList("category1"), "/category1");
checkCategoryOutput(Arrays.asList("category1", "category2"), "/category1/category2");
} }
private void checkCategoryOutput(final List<String> categories, final String asserted) @Test
throws IllegalArgumentException, FeedException, SAXException, IOException, XpathException { public void testOutputOfEmptyCategory() {
assertThat(categoryOf("").getLength(), is(equalTo(0)));
}
final Outline outline = new Outline("outline1", null); @Test
outline.setCategories(categories); public void testOutputOfBlankCategory() {
final List<Outline> outlines = Arrays.asList(outline); assertThat(categoryOf(" ").getLength(), is(equalTo(0)));
}
final Opml opml = new Opml(); @Test
opml.setFeedType("opml_2.0"); public void testOutputOfOneCategory() {
opml.setTitle("title"); assertThat(categoryValueOf("category1"), is(equalTo("category1")));
opml.setOutlines(outlines); }
final WireFeedOutput output = new WireFeedOutput(); @Test
final String xml = output.outputString(opml); public void testOutputOfMultipleCategories() {
assertThat(categoryValueOf("category1", "category2"), is(equalTo("category1,category2")));
}
final Document document = XMLUnit.buildControlDocument(xml); private NodeList categoryOf(final String... categories) {
final String categoryValue = XMLUnit.newXpathEngine().evaluate("/opml/body/outline/@category", document);
assertThat(categoryValue, is(equalTo(asserted)));
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();
} }
} }