+ public void givenSoneIsUsedInParseContext() {
+ setupSoneAndVerifyItIsUsedInContext(sone, sone);
+ }
+
+ private void setupSoneAndVerifyItIsUsedInContext(Object soneOrSoneId, Sone sone) {
+ setupParser("text", new PlainTextPart("text"));
+ parameters.put("sone", sone);
+ filter.format(templateContext, "text", parameters);
+ ArgumentCaptor<SoneTextParserContext> context = forClass(SoneTextParserContext.class);
+ verify(soneTextParser).parse(eq("text"), context.capture());
+ assertThat(context.getValue().getPostingSone(), is(sone));
+ }
+
+ @Test
+ public void soneWithGivenSoneIdIsUsedInParseContext() {
+ setupSoneAndVerifyItIsUsedInContext(SONE_IDENTITY, sone);
+ }
+
+ @Test
+ public void plainTextIsRenderedCorrectly() {
+ setupParser("plain text", new PlainTextPart("plain text"));
+ String result = (String) filter.format(templateContext, "plain text", Collections.<String, Object>emptyMap());
+ assertThat(result, is("plain text"));
+ }
+
+ private void setupParser(String text, Part... parsedParts) {
+ when(soneTextParser.parse(eq(text), any(SoneTextParserContext.class))).thenReturn(asList(parsedParts));
+ }
+
+ @Test
+ public void plainTextPartIsShortenedIfLengthExceedsMaxLength() {
+ setupParser("text", new PlainTextPart("This is a long text."));
+ setLengthAndCutOffLength(15, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ assertThat(output, is("This is a …"));
+ }
+
+ @Test
+ public void plainTextPartIsNotShortenedIfLengthDoesNotExceedMaxLength() {
+ setupParser("text", new PlainTextPart("This is a long text."));
+ setLengthAndCutOffLength(20, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ assertThat(output, is("This is a …"));
+ }
+
+ @Test
+ public void shortPartsAreNotShortened() {
+ setupParser("text", new PlainTextPart("This."));
+ setLengthAndCutOffLength(15, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ assertThat(output, is("This."));
+ }
+
+ @Test
+ public void multiplePlainTextPartsAreShortened() {
+ setupParser("text", new PlainTextPart("This "), new PlainTextPart("is a long text."));
+ setLengthAndCutOffLength(15, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ assertThat(output, is("This is a …"));
+ }
+
+ @Test
+ public void partsAfterLengthHasBeenReachedAreIgnored() {
+ setupParser("text", new PlainTextPart("This is a long text."), new PlainTextPart(" And even more."));
+ setLengthAndCutOffLength(15, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ assertThat(output, is("This is a …"));
+ }
+
+ @Test
+ public void linkPartsAreNotShortened() {
+ setupParser("text", new FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false));
+ setLengthAndCutOffLength(15, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ Element linkNode = Jsoup.parseBodyFragment(output).body().child(0);
+ verifyLink(linkNode, "/KSK@gpl.txt", "freenet", "KSK@gpl.txt", "This is a long text.");
+ }
+
+ @Test
+ public void additionalLinkPartsAreIgnored() {
+ setupParser("text", new PlainTextPart("This is a long text."), new FreenetLinkPart("KSK@gpl.txt", "This is a long text.", false));
+ setLengthAndCutOffLength(15, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ assertThat(output, is("This is a …"));
+ }
+
+ private void setLengthAndCutOffLength(int length, int cutOffLength) {
+ parameters.put("length", length);
+ parameters.put("cut-off-length", cutOffLength);
+ }
+
+ @Test
+ public void sonePartsAreAddedButTheirLengthIsIgnored() {
+ setupParser("text", new SonePart(sone), new PlainTextPart("This is a long text."));
+ setLengthAndCutOffLength(15, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ Element body = Jsoup.parseBodyFragment(output).body();
+ Element linkNode = (Element) body.childNode(0);
+ System.out.println(linkNode);
+ verifyLink(linkNode, "viewSone.html?sone=" + SONE_IDENTITY, "in-sone", "First", "First");
+ assertThat(((TextNode) body.childNode(1)).text(), is("This is a …"));
+ }
+
+ @Test
+ public void additionalSonePartsAreIgnored() {
+ setupParser("text", new PlainTextPart("This is a long text."), new SonePart(sone));
+ setLengthAndCutOffLength(15, 10);
+ String output = (String) filter.format(templateContext, "text", parameters);
+ assertThat(output, is("This is a …"));
+ }
+
+ @Test
+ public void freenetLinkIsRenderedCorrectly() {
+ setupParser("KSK@gpl.txt", new FreenetLinkPart("KSK@gpl.txt", "gpl.txt", false));
+ Element linkNode = filterText("KSK@gpl.txt");
+ verifyLink(linkNode, "/KSK@gpl.txt", "freenet", "KSK@gpl.txt", "gpl.txt");
+ }
+
+ private void verifyLink(Element linkNode, String url, String cssClass, String tooltip, String text) {