• 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 import junit.framework.TestCase;
21 
22 /**
23  * Tests for the {@link HtmlEscapers} class.
24  *
25  * @author David Beaumont
26  */
27 @GwtCompatible
28 public class HtmlEscapersTest extends TestCase {
29 
testHtmlEscaper()30   public void testHtmlEscaper() throws Exception {
31     assertEquals("xxx", htmlEscaper().escape("xxx"));
32     assertEquals(""test"", htmlEscaper().escape("\"test\""));
33     assertEquals("'test'", htmlEscaper().escape("\'test'"));
34     assertEquals("test & test & test", htmlEscaper().escape("test & test & test"));
35     assertEquals("test &lt;&lt; 1", htmlEscaper().escape("test << 1"));
36     assertEquals("test &gt;&gt; 1", htmlEscaper().escape("test >> 1"));
37     assertEquals("&lt;tab&gt;", htmlEscaper().escape("<tab>"));
38 
39     // Test simple escape of '&'.
40     assertEquals("foo&amp;bar", htmlEscaper().escape("foo&bar"));
41 
42     // If the string contains no escapes, it should return the arg.
43     // Note: assert<b>Same</b> for this implementation.
44     String s = "blah blah farhvergnugen";
45     assertSame(s, htmlEscaper().escape(s));
46 
47     // Tests escapes at begin and end of string.
48     assertEquals("&lt;p&gt;", htmlEscaper().escape("<p>"));
49 
50     // Test all escapes.
51     assertEquals("a&quot;b&lt;c&gt;d&amp;", htmlEscaper().escape("a\"b<c>d&"));
52 
53     // Test two escapes in a row.
54     assertEquals("foo&amp;&amp;bar", htmlEscaper().escape("foo&&bar"));
55 
56     // Test many non-escaped characters.
57     s =
58         "!@#$%^*()_+=-/?\\|]}[{,.;:"
59             + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
60             + "1234567890";
61     assertSame(s, htmlEscaper().escape(s));
62   }
63 }
64