From: David ‘Bombe’ Roden Date: Thu, 9 Jun 2011 10:12:11 +0000 (+0200) Subject: Move text extracting and part class validation to its own method. X-Git-Tag: 0.6.5^2~8^2~4 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b8cac39598e016ad4cf4bcc2ccd262b4fda26516;p=Sone.git Move text extracting and part class validation to its own method. --- 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(); } }