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.Before;
024import org.junit.Test;
025
026/**
027 * Unit test for {@link SimplifyHtmlFilterTest}.
028 *
029 * @author Richard "Shred" Körber
030 */
031public class SimplifyHtmlFilterTest {
032
033    private SimplifyHtmlFilter filter;
034
035    @Before
036    public void setup() {
037        filter = new SimplifyHtmlFilter();
038        filter.addAcceptedTag("b");
039        filter.addAcceptedTag("br");
040        filter.addAcceptedTag("img", "src", "alt");
041    }
042
043    @Test
044    public void simpleTest() {
045        StringBuilder sb = new StringBuilder();
046        sb.append("<hr>This is <b  >a bad content</b>.");
047        sb.append("<script>window.alert('Oops!')</script>");
048        sb.append("<br /><img \nsrc=\"foo.gif\"><img src='foo2.gif'>");
049        sb.append("<img src=type1.gif alt>");
050        sb.append("<img src='type\"2.gif' alt=f\"oo noscale>");
051        sb.append("<img     src = \"type3.gif\"  alt=\"bar foo\" noscale=\"no\">");
052
053        CharSequence out = filter.apply(sb);
054
055        StringBuilder expect = new StringBuilder();
056        expect.append("This is <b>a bad content</b>.");
057        expect.append("window.alert('Oops!')");
058        expect.append("<br /><img src=\"foo.gif\"><img src=\"foo2.gif\">");
059        expect.append("<img src=\"type1.gif\" alt=\"alt\">");
060        expect.append("<img src=\"type&quot;2.gif\" alt=\"f&quot;oo\">");
061        expect.append("<img src=\"type3.gif\" alt=\"bar foo\">");
062
063        Assert.assertEquals(expect.toString(), out.toString());
064    }
065
066    @Test
067    public void brokenTest() {
068        StringBuilder sb = new StringBuilder();
069        sb.append(">broken content<br");
070
071        CharSequence out = filter.apply(sb);
072
073        // Incomplete tags at the end are stripped as well
074        StringBuilder expect = new StringBuilder();
075        expect.append(">broken content");
076
077        Assert.assertEquals(expect.toString(), out.toString());
078    }
079
080}