/** * Copyright (C) 2011 eXo Platform SAS. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.exoplatform.commons.utils; import junit.framework.TestCase; /** * @author <a href="trongtt@gmail.com">Trong Tran</a> * @version $Revision$ */ public class TestHTMLEntityEncoder extends TestCase { private HTMLEntityEncoder htmlEncoder = HTMLEntityEncoder.getInstance(); public void testHTMLEncoding() { assertEquals("<h1>HELLO WORLD</h1>", htmlEncoder.encode("<h1>HELLO WORLD</h1>")); assertEquals("<h1>HELLO WORLD</h1>", htmlEncoder.encodeHTML("<h1>HELLO WORLD</h1>")); assertEquals("alert('HELLO WORLD')", htmlEncoder.encode("alert('HELLO WORLD')")); assertEquals("alert('HELLO WORLD')", htmlEncoder.encodeHTML("alert('HELLO WORLD')")); assertEquals( "<a href="http://example.com/?name1=value1&name2=value2&name3=a+b">link</a>", htmlEncoder.encode("<a href=\"http://example.com/?name1=value1&name2=value2&name3=a+b\">link</a>")); assertEquals( "<a href="http://example.com/?name1=value1&name2=value2&name3=a+b">link</a>", htmlEncoder.encodeHTML("<a href=\"http://example.com/?name1=value1&name2=value2&name3=a+b\">link</a>")); } public void testHTMLAttributeEncoding() { assertEquals("<h1>HELLO WORLD</h1>", htmlEncoder.encodeHTMLAttribute("<h1>HELLO WORLD</h1>")); assertEquals("alert('HELLO WORLD')", htmlEncoder.encodeHTMLAttribute("alert('HELLO WORLD')")); assertEquals( "<a href="http://example.com/?name1=value1&name2=value2&name3=a+b">link</a>", htmlEncoder.encodeHTMLAttribute("<a href=\"http://example.com/?name1=value1&name2=value2&name3=a+b\">link</a>")); } public void testIsEncoded() { assertTrue(htmlEncoder.isEncoded("ção")); assertFalse(htmlEncoder.isEncoded("not encoded")); assertFalse(htmlEncoder.isEncoded("not encoded;")); assertTrue(htmlEncoder.isEncoded("encoded;")); assertFalse(htmlEncoder.isEncoded("¬ encoded;")); assertFalse(htmlEncoder.isEncoded("¬ encoded")); assertFalse(htmlEncoder.isEncoded("not encoded&;")); assertTrue(htmlEncoder.isEncoded("encoded&")); assertTrue(htmlEncoder.isEncoded("&encoded;")); assertTrue(htmlEncoder.isEncoded("<h1>HELLO WORLD</h1>")); assertTrue(htmlEncoder.isEncoded("alert('HELLO WORLD')")); } public void testEncodeIfNotEncoded() { assertEquals("<h1>HELLO WORLD</h1>", htmlEncoder.encodeIfNotEncoded("<h1>HELLO WORLD</h1>")); assertEquals("ção", htmlEncoder.encodeIfNotEncoded("ção")); assertEquals("not encoded", htmlEncoder.encodeIfNotEncoded("not encoded")); assertEquals("not encoded;", htmlEncoder.encodeIfNotEncoded("not encoded;")); assertEquals("encoded;", htmlEncoder.encodeIfNotEncoded("encoded;")); assertEquals("&not encoded;", htmlEncoder.encodeIfNotEncoded("¬ encoded;")); assertEquals("&not encoded", htmlEncoder.encodeIfNotEncoded("¬ encoded")); assertEquals("not encoded&;", htmlEncoder.encodeIfNotEncoded("not encoded&;")); assertEquals("encoded&", htmlEncoder.encodeIfNotEncoded("encoded&")); assertEquals("&encoded;", htmlEncoder.encodeIfNotEncoded("&encoded;")); assertEquals("<h1>HELLO WORLD</h1>", htmlEncoder.encodeIfNotEncoded("<h1>HELLO WORLD</h1>")); assertEquals("alert('HELLO WORLD')", htmlEncoder.encodeIfNotEncoded("alert('HELLO WORLD')")); } }