• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.html;
16 
17 import static com.google.common.html.HtmlEscapers.htmlEscaper;
18 
19 import com.google.common.annotations.GwtCompatible;
20 
21 import junit.framework.TestCase;
22 
23 /**
24  * Tests for the {@link HtmlEscapers} class.
25  *
26  * @author David Beaumont
27  */
28 @GwtCompatible
29 public class HtmlEscapersTest extends TestCase {
30 
testHtmlEscaper()31   public void testHtmlEscaper() throws Exception {
32     assertEquals("xxx", htmlEscaper().escape("xxx"));
33     assertEquals(""test"", htmlEscaper().escape("\"test\""));
34     assertEquals("'test'", htmlEscaper().escape("\'test'"));
35     assertEquals("test & test & test", htmlEscaper().escape("test & test & test"));
36     assertEquals("test &lt;&lt; 1", htmlEscaper().escape("test << 1"));
37     assertEquals("test &gt;&gt; 1", htmlEscaper().escape("test >> 1"));
38     assertEquals("&lt;tab&gt;", htmlEscaper().escape("<tab>"));
39 
40     // Test simple escape of '&'.
41     assertEquals("foo&amp;bar", htmlEscaper().escape("foo&bar"));
42 
43     // If the string contains no escapes, it should return the arg.
44     // Note: assert<b>Same</b> for this implementation.
45     String s = "blah blah farhvergnugen";
46     assertSame(s, htmlEscaper().escape(s));
47 
48     // Tests escapes at begin and end of string.
49     assertEquals("&lt;p&gt;", htmlEscaper().escape("<p>"));
50 
51     // Test all escapes.
52     assertEquals("a&quot;b&lt;c&gt;d&amp;", htmlEscaper().escape("a\"b<c>d&"));
53 
54     // Test two escapes in a row.
55     assertEquals("foo&amp;&amp;bar", htmlEscaper().escape("foo&&bar"));
56 
57     // Test many non-escaped characters.
58     s = "!@#$%^*()_+=-/?\\|]}[{,.;:"
59         + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
60         + "1234567890";
61     assertSame(s, htmlEscaper().escape(s));
62   }
63 }
64