/*************************************************************************** * Copyright (C) 2003-2010 eXo Platform SAS. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Affero General Public License * as published by the Free Software Foundation; either version 3 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see<http://www.gnu.org/licenses/>. ***************************************************************************/ package org.exoplatform.forum.common; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; public class TestTransformHTML extends TestCase{ private static List<String> bbcs = Arrays.asList(new String[] { "B", "I", "IMG", "CSS", "URL", "LINK", "GOTO", "QUOTE", "LEFT", "CODE"}); public TestTransformHTML() throws Exception { super(); } public void testCleanHtmlCode() { // for text is empty String str = TransformHTML.EMPTY_STR; assertEquals(TransformHTML.EMPTY_STR, TransformHTML.cleanHtmlCode(str, bbcs)); // for text contain bbcode str = "text [b]content[/b] has [I]bbcode[/I], [url]link[/url] and img: [img]http://host.com/abc.jpg[/img]"; assertEquals("text content has bbcode, link and img: http://host.com/abc.jpg", TransformHTML.cleanHtmlCode(str, bbcs)); // for text contain bbcode and tag html str += " defaul data <a href='http://exoplatform.com'>link</a>. new <strong>data</strong>" + " test<style>.css{color:blue;}</style>, <script> function a {alert('abc');}</script>tested."; assertEquals("text content has bbcode, link and img: http://host.com/abc.jpg defaul data link. new data test, tested.", TransformHTML.cleanHtmlCode(str, bbcs)); } public void testGetTitleInHTMLCode() { assertEquals(TransformHTML.EMPTY_STR, TransformHTML.getTitleInHTMLCode(TransformHTML.EMPTY_STR, bbcs)); assertEquals("1 3", TransformHTML.getTitleInHTMLCode("1 3", bbcs)); String title = " title [b]title[/b] <strong>title</strong>   title<br/>title title title " + new String(Character.toChars(20)) + "title\t\ntitle"; assertEquals("title title title title title title title title title", TransformHTML.getTitleInHTMLCode(title, bbcs)); } public void testRemoveCharterStrange() { assertEquals(TransformHTML.EMPTY_STR, TransformHTML.removeCharterStrange(null)); assertEquals(TransformHTML.EMPTY_STR, TransformHTML.removeCharterStrange(TransformHTML.EMPTY_STR)); assertEquals("abc", TransformHTML.removeCharterStrange("abc\n\t" + new String(Character.toChars(30)))); assertEquals("abc ", TransformHTML.removeCharterStrange("abc\n" + new String(Character.toChars(32)))); } public void testEnCodeHTML() { assertEquals(TransformHTML.EMPTY_STR, TransformHTML.enCodeHTMLTitle(null)); // clear space superfluous fix for FCKEditer assertEquals(TransformHTML.EMPTY_STR, TransformHTML.enCodeHTMLTitle("<p>   </p>")); assertEquals(TransformHTML.EMPTY_STR, TransformHTML.enCodeHTMLTitle("<p>    </p>")); assertEquals("abc", TransformHTML.enCodeHTMLTitle("<br/><br/><br/><br/>abc")); assertEquals("abc", TransformHTML.enCodeHTMLTitle("abc<br/><br/><br/><br/>")); // encode: '<' --> '<' ; '>' --> '>' ; ''' --> ' assertEquals("<p> test <br/> test 'test' <div>text</div></p>", TransformHTML.enCodeHTMLTitle("<p> test <br/> test 'test' <div>text</div></p>")); } public void testEnCodeViewSignature() { assertEquals(TransformHTML.EMPTY_STR, TransformHTML.enCodeViewSignature(null)); assertEquals(TransformHTML.EMPTY_STR, TransformHTML.enCodeViewSignature(TransformHTML.EMPTY_STR)); assertEquals("ABC hello<br/> test<div>hello</div><br/>Hello World.", TransformHTML.enCodeViewSignature("ABC hello\n test<div>hello</div>\nHello World.")); } public void testEnCodeHTMLContent() { assertEquals(TransformHTML.EMPTY_STR, TransformHTML.enCodeViewSignature(null)); assertEquals(TransformHTML.EMPTY_STR, TransformHTML.enCodeViewSignature(TransformHTML.EMPTY_STR)); String s = "Abc\txyz \nHelo everybody <div>xxx</div> BBB.\nStar is 'Ngoi Sao'"; assertEquals("Abc    xyz <br/>Helo everybody <div>xxx</div> BBB.<br/>Star is 'Ngoi Sao'", TransformHTML.enCodeHTMLContent(s)); s = "[color=red\"onmouseover=\"(function(){document.write('I\'m evil');}).call(this)\"]hello world[/color]" + "[size=15\"onmouseover=\"(function(){document.write('I\'m evil');}).call(this)\"]hello world[/size]" + "[url=http://example.com\"onmouseover=\"(function(){document.write('I\'m evil');}).call(this)\"]Example[/url]"; assertEquals("[color=red"onmouseover="(function(){document.write('I'm evil');}).call(this)"]hello world[/color][size=15"" + "onmouseover="(function(){document.write('I'm evil');}).call(this)"]hello world[/size][url=http://example.com"onmouseover="" + "(function(){document.write('I'm evil');}).call(this)"]Example[/url]", TransformHTML.enCodeHTMLContent(s)); } public void testFixAddBBcodeAction() { // value input not null. assertEquals(TransformHTML.EMPTY_STR, TransformHTML.fixAddBBcodeAction(TransformHTML.EMPTY_STR)); // check for tag <p> assertEquals("<p>[quote] test [/quote]</p>s<p>[code] text [/code]</p> <p>[QUOTE] QUOTE[/QUOTE]</p> <p>[CODE] CODE[/CODE]</p>", TransformHTML.fixAddBBcodeAction("<p>[quote]</p> test <p>[/quote]</p>s<p>[code]</p> text <p>[/code]</p> " + "<p>[QUOTE]</p> QUOTE<p>[/QUOTE]</p> <p>[CODE]</p> CODE<p>[/CODE]</p>")); // check for tag <span> assertEquals("<span>[quote] test [/quote]</span>s<span>[code] text [/code]</span> <span>[QUOTE] QUOTE[/QUOTE]</span> <span>[CODE] CODE[/CODE]</span>", TransformHTML.fixAddBBcodeAction("<span>[quote]</span> test <span>[/quote]</span>s<span>[code]</span> text <span>[/code]</span> " + "<span>[QUOTE]</span> QUOTE<span>[/QUOTE]</span> <span>[CODE]</span> CODE<span>[/CODE]</span>")); } }