• 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");
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 {
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    */
assertBasic(Escaper escaper)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 string.
59    *
60    * @param escaper the non-null escaper to test
61    * @param expected the expected output string
62    * @param c the character to escape
63    */
assertEscaping(CharEscaper escaper, String expected, char c)64   public static void assertEscaping(CharEscaper escaper, String expected, char c) {
65 
66     String escaped = computeReplacement(escaper, c);
67     Assert.assertNotNull(escaped);
68     Assert.assertEquals(expected, escaped);
69   }
70 
71   /**
72    * Asserts that a Unicode escaper escapes the given code point into the expected string.
73    *
74    * @param escaper the non-null escaper to test
75    * @param expected the expected output string
76    * @param cp the Unicode code point to escape
77    */
assertEscaping(UnicodeEscaper escaper, String expected, int cp)78   public static void assertEscaping(UnicodeEscaper escaper, String expected, int cp) {
79 
80     String escaped = computeReplacement(escaper, cp);
81     Assert.assertNotNull(escaped);
82     Assert.assertEquals(expected, escaped);
83   }
84 
85   /**
86    * Asserts that an escaper does not escape the given character.
87    *
88    * @param escaper the non-null escaper to test
89    * @param c the character to test
90    */
assertUnescaped(CharEscaper escaper, char c)91   public static void assertUnescaped(CharEscaper escaper, char c) {
92     Assert.assertNull(computeReplacement(escaper, c));
93   }
94 
95   /**
96    * Asserts that a Unicode escaper does not escape the given character.
97    *
98    * @param escaper the non-null escaper to test
99    * @param cp the Unicode code point to test
100    */
assertUnescaped(UnicodeEscaper escaper, int cp)101   public static void assertUnescaped(UnicodeEscaper escaper, int cp) {
102     Assert.assertNull(computeReplacement(escaper, cp));
103   }
104 
105   /**
106    * Asserts that a Unicode escaper escapes the given hi/lo surrogate pair into the expected string.
107    *
108    * @param escaper the non-null escaper to test
109    * @param expected the expected output string
110    * @param hi the high surrogate pair character
111    * @param lo the low surrogate pair character
112    */
assertUnicodeEscaping( UnicodeEscaper escaper, String expected, char hi, char lo)113   public static void assertUnicodeEscaping(
114       UnicodeEscaper escaper, String expected, char hi, char lo) {
115 
116     int cp = Character.toCodePoint(hi, lo);
117     String escaped = computeReplacement(escaper, cp);
118     Assert.assertNotNull(escaped);
119     Assert.assertEquals(expected, escaped);
120   }
121 }
122