001/*
002 * Shredzone Commons
003 *
004 * Copyright (C) 2012 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 TextileFilterTest}.
028 *
029 * @author Richard "Shred" Körber
030 */
031public class TextileFilterTest {
032
033    @Test
034    public void simpleTest() {
035        TextileFilter filter = new TextileFilter();
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>");
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        TextileFilter filter = new TextileFilter();
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(" !/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 border=\"0\" src=\"/img/photo.jpeg?image\"/>");
080        expect.append("</p>");
081
082        Assert.assertEquals(expect.toString(), out.toString());
083    }
084
085}