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;
024
025/**
026 * Unit test for {@link StripHtmlFilterTest}.
027 *
028 * @author Richard "Shred" Körber
029 */
030public class StripHtmlFilterTest {
031
032    @Test
033    public void simpleTest() {
034        StripHtmlFilter filter = new StripHtmlFilter();
035
036        StringBuilder sb = new StringBuilder();
037        sb.append("<h1>wow!</h1><hr>This is <b>a bad content</b>.");
038        sb.append("<script>window.alert('Oops!')</script>");
039        sb.append("<br /><IMG \nsrc=\"foo.gif\"><img src='foo2.gif'>");
040
041        CharSequence out = filter.apply(sb);
042
043        StringBuilder expect = new StringBuilder();
044        expect.append("wow! This is a bad content.");
045        expect.append(" window.alert('Oops!')");
046
047        Assert.assertEquals(expect.toString(), out.toString());
048    }
049
050    @Test
051    public void attributeTest() {
052        StripHtmlFilter filter = new StripHtmlFilter();
053
054        StringBuilder sb = new StringBuilder();
055        sb.append("An <img src=\"foo.jpg\" title=\"with &lt;em>nested&lt;em> HTML\"> image");
056
057        CharSequence out = filter.apply(sb);
058
059        StringBuilder expect = new StringBuilder();
060        expect.append("An  image");
061
062        Assert.assertEquals(expect.toString(), out.toString());
063    }
064
065    @Test
066    public void brokenTest() {
067        StripHtmlFilter filter = new StripHtmlFilter();
068
069        StringBuilder sb = new StringBuilder();
070        sb.append(">broken content<br");
071
072        CharSequence out = filter.apply(sb);
073
074        // Incomplete tags at the end are stripped as well
075        StringBuilder expect = new StringBuilder();
076        expect.append(">broken content<br");
077
078        Assert.assertEquals(expect.toString(), out.toString());
079    }
080
081}