From b8cac39598e016ad4cf4bcc2ccd262b4fda26516 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 9 Jun 2011 12:12:11 +0200 Subject: [PATCH] Move text extracting and part class validation to its own method. --- .../sone/text/SoneTextParserTest.java | 60 ++++++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index f0b0e1b..568c466 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -38,37 +38,63 @@ public class SoneTextParserTest extends TestCase { public void testPlainText() throws IOException { SoneTextParser soneTextParser = new SoneTextParser(null, null); Iterable parts; - StringBuilder text = new StringBuilder(); /* check basic operation. */ - text.setLength(0); parts = soneTextParser.parse(null, new StringReader("Test.")); assertNotNull("Parts", parts); - for (Part part : parts) { - assertTrue("Part is PlainTextPart", part instanceof PlainTextPart); - text.append(((PlainTextPart) part).getText()); - } - assertEquals("Part Text", "Test.", text.toString()); + assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class)); /* check empty lines at start and end. */ - text.setLength(0); parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n")); assertNotNull("Parts", parts); - for (Part part : parts) { - assertTrue("Part is PlainTextPart", part instanceof PlainTextPart); - text.append(((PlainTextPart) part).getText()); - } - assertEquals("Part Text", "Test.", text.toString()); + assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class)); /* check duplicate empty lines in the text. */ - text.setLength(0); parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest.")); assertNotNull("Parts", parts); + assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class)); + } + + // + // PRIVATE METHODS + // + + /** + * Converts all given {@link Part}s into a string, validating that the + * part’s classes match only the expected classes. + * + * @param parts + * The parts to convert to text + * @param validClasses + * The valid classes; if no classes are given, all classes are + * valid + * @return The converted text + */ + private String convertText(Iterable parts, Class... validClasses) { + StringBuilder text = new StringBuilder(); for (Part part : parts) { - assertTrue("Part is PlainTextPart", part instanceof PlainTextPart); - text.append(((PlainTextPart) part).getText()); + assertNotNull("Part", part); + boolean classValid = validClasses.length == 0; + for (Class validClass : validClasses) { + if (validClass.isAssignableFrom(part.getClass())) { + classValid = true; + break; + } + } + if (!classValid) { + assertEquals("Part’s Class", null, part.getClass()); + } + if (part instanceof PlainTextPart) { + text.append(((PlainTextPart) part).getText()); + } else if (part instanceof FreenetLinkPart) { + FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part; + text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getText()).append(']'); + } else if (part instanceof LinkPart) { + LinkPart linkPart = (LinkPart) part; + text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getText()).append(']'); + } } - assertEquals("Part Text", "Test.\n\nTest.", text.toString()); + return text.toString(); } } -- 2.7.4