001/* 002 * Shredzone Commons 003 * 004 * Copyright (C) 2016 Richard "Shred" Körber 005 * http://commons.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Library General Public License as 009 * published by the Free Software Foundation, either version 3 of the 010 * License, or (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU Library General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020package org.shredzone.commons.text.filter; 021 022import org.junit.Assert; 023import org.junit.Test; 024import org.shredzone.commons.text.LinkAnalyzer; 025 026/** 027 * Unit test for {@link MarkdownFilter}. 028 * 029 * @author Richard "Shred" Körber 030 */ 031public class MarkdownFilterTest { 032 033 @Test 034 public void simpleTest() { 035 MarkdownFilter filter = new MarkdownFilter(); 036 037 StringBuilder sb = new StringBuilder(); 038 sb.append("A **bold** text."); 039 040 CharSequence out = filter.apply(sb); 041 042 StringBuilder expect = new StringBuilder(); 043 expect.append("<p>A <strong>bold</strong> text.</p>\n"); 044 045 Assert.assertEquals(expect.toString(), out.toString()); 046 } 047 048 @Test 049 public void linkAnalyzerTest() { 050 LinkAnalyzer analyzer = new LinkAnalyzer() { 051 @Override 052 public String linkUrl(String url) { 053 return url + "?passed"; 054 } 055 056 @Override 057 public String linkType(String url) { 058 return "external"; 059 } 060 061 @Override 062 public String imageUrl(String url) { 063 return url + "?image"; 064 } 065 }; 066 067 MarkdownFilter filter = new MarkdownFilter(); 068 filter.setAnalyzer(analyzer); 069 070 StringBuilder sb = new StringBuilder(); 071 sb.append("A [link](http://example.com/page/1) to somewhere."); 072 sb.append(" ![Image](/img/photo.jpeg)"); 073 074 CharSequence out = filter.apply(sb); 075 076 StringBuilder expect = new StringBuilder(); 077 expect.append("<p>"); 078 expect.append("A <a href=\"http://example.com/page/1?passed\" class=\"external\">link</a> to somewhere."); 079 expect.append(" <img src=\"/img/photo.jpeg?image\" alt=\"Image\" />"); 080 expect.append("</p>\n"); 081 082 Assert.assertEquals(expect.toString(), out.toString()); 083 } 084 085 @Test 086 public void preClassTest() { 087 MarkdownFilter filter = new MarkdownFilter(); 088 filter.setPreClass("prettyprint"); 089 090 StringBuilder sb = new StringBuilder(); 091 sb.append("```java\n"); 092 sb.append("System.out.println(\"Hello World\");\n"); 093 sb.append("```\n"); 094 095 CharSequence out = filter.apply(sb); 096 097 StringBuilder expect = new StringBuilder(); 098 expect.append("<pre class=\"prettyprint\"><code class=\"language-java\">"); 099 expect.append("System.out.println("Hello World");\n"); 100 expect.append("</code></pre>\n"); 101 102 Assert.assertEquals(expect.toString(), out.toString()); 103 } 104 105}