View Javadoc
1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.escape.testing;
18  
19  import static com.google.common.escape.Escapers.computeReplacement;
20  
21  import com.google.common.annotations.Beta;
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.escape.CharEscaper;
24  import com.google.common.escape.Escaper;
25  import com.google.common.escape.UnicodeEscaper;
26  import java.io.IOException;
27  import junit.framework.Assert;
28  
29  /**
30   * Extra assert methods for testing Escaper implementations.
31   *
32   * @author David Beaumont
33   * @since 15.0
34   */
35  @Beta
36  @GwtCompatible
37  public final class EscaperAsserts {
38    private EscaperAsserts() {}
39  
40    /**
41     * Asserts that an escaper behaves correctly with respect to null inputs.
42     *
43     * @param escaper the non-null escaper to test
44     */
45    public static void assertBasic(Escaper escaper) throws IOException {
46      // Escapers operate on characters: no characters, no escaping.
47      Assert.assertEquals("", escaper.escape(""));
48      // Assert that escapers throw null pointer exceptions.
49      try {
50        escaper.escape((String) null);
51        Assert.fail("exception not thrown when escaping a null string");
52      } catch (NullPointerException e) {
53        // pass
54      }
55    }
56  
57    /**
58     * Asserts that an escaper escapes the given character into the expected
59     * string.
60     *
61     * @param escaper the non-null escaper to test
62     * @param expected the expected output string
63     * @param c the character to escape
64     */
65    public static void assertEscaping(CharEscaper escaper, String expected,
66        char c) {
67  
68      String escaped = computeReplacement(escaper, c);
69      Assert.assertNotNull(escaped);
70      Assert.assertEquals(expected, escaped);
71    }
72  
73    /**
74     * Asserts that an escaper does not escape the given character.
75     *
76     * @param escaper the non-null escaper to test
77     * @param c the character to test
78     */
79    public static void assertUnescaped(CharEscaper escaper, char c) {
80      Assert.assertNull(computeReplacement(escaper, c));
81    }
82  
83    /**
84     * Asserts that a Unicode escaper escapes the given code point into the
85     * expected string.
86     *
87     * @param escaper the non-null escaper to test
88     * @param expected the expected output string
89     * @param cp the Unicode code point to escape
90     */
91    public static void assertEscaping(UnicodeEscaper escaper, String expected,
92        int cp) {
93  
94      String escaped = computeReplacement(escaper, cp);
95      Assert.assertNotNull(escaped);
96      Assert.assertEquals(expected, escaped);
97    }
98  
99    /**
100    * Asserts that a Unicode escaper does not escape the given character.
101    *
102    * @param escaper the non-null escaper to test
103    * @param cp the Unicode code point to test
104    */
105   public static void assertUnescaped(UnicodeEscaper escaper, int cp) {
106     Assert.assertNull(computeReplacement(escaper, cp));
107   }
108 
109   /**
110    * Asserts that a Unicode escaper escapes the given hi/lo surrogate pair into
111    * the expected string.
112    *
113    * @param escaper the non-null escaper to test
114    * @param expected the expected output string
115    * @param hi the high surrogate pair character
116    * @param lo the low surrogate pair character
117    */
118   public static void assertUnicodeEscaping(UnicodeEscaper escaper,
119       String expected, char hi, char lo) {
120 
121     int cp = Character.toCodePoint(hi, lo);
122     String escaped = computeReplacement(escaper, cp);
123     Assert.assertNotNull(escaped);
124     Assert.assertEquals(expected, escaped);
125   }
126 }