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.GwtCompatible; 22 import com.google.common.escape.CharEscaper; 23 import com.google.common.escape.Escaper; 24 import com.google.common.escape.UnicodeEscaper; 25 import java.io.IOException; 26 import junit.framework.Assert; 27 28 /** 29 * Extra assert methods for testing Escaper implementations. 30 * 31 * @author David Beaumont 32 * @since 15.0 33 */ 34 @GwtCompatible 35 public final class EscaperAsserts { EscaperAsserts()36 private EscaperAsserts() {} 37 38 /** 39 * Asserts that an escaper behaves correctly with respect to null inputs. 40 * 41 * @param escaper the non-null escaper to test 42 */ assertBasic(Escaper escaper)43 public static void assertBasic(Escaper escaper) throws IOException { 44 // Escapers operate on characters: no characters, no escaping. 45 Assert.assertEquals("", escaper.escape("")); 46 // Assert that escapers throw null pointer exceptions. 47 try { 48 escaper.escape((String) null); 49 Assert.fail("exception not thrown when escaping a null string"); 50 } catch (NullPointerException e) { 51 // pass 52 } 53 } 54 55 /** 56 * Asserts that an escaper escapes the given character into the expected string. 57 * 58 * @param escaper the non-null escaper to test 59 * @param expected the expected output string 60 * @param c the character to escape 61 */ assertEscaping(CharEscaper escaper, String expected, char c)62 public static void assertEscaping(CharEscaper escaper, String expected, char c) { 63 64 String escaped = computeReplacement(escaper, c); 65 Assert.assertNotNull(escaped); 66 Assert.assertEquals(expected, escaped); 67 } 68 69 /** 70 * Asserts that a Unicode escaper escapes the given code point into the expected string. 71 * 72 * @param escaper the non-null escaper to test 73 * @param expected the expected output string 74 * @param cp the Unicode code point to escape 75 */ assertEscaping(UnicodeEscaper escaper, String expected, int cp)76 public static void assertEscaping(UnicodeEscaper escaper, String expected, int cp) { 77 78 String escaped = computeReplacement(escaper, cp); 79 Assert.assertNotNull(escaped); 80 Assert.assertEquals(expected, escaped); 81 } 82 83 /** 84 * Asserts that an escaper does not escape the given character. 85 * 86 * @param escaper the non-null escaper to test 87 * @param c the character to test 88 */ assertUnescaped(CharEscaper escaper, char c)89 public static void assertUnescaped(CharEscaper escaper, char c) { 90 Assert.assertNull(computeReplacement(escaper, c)); 91 } 92 93 /** 94 * Asserts that a Unicode escaper does not escape the given character. 95 * 96 * @param escaper the non-null escaper to test 97 * @param cp the Unicode code point to test 98 */ assertUnescaped(UnicodeEscaper escaper, int cp)99 public static void assertUnescaped(UnicodeEscaper escaper, int cp) { 100 Assert.assertNull(computeReplacement(escaper, cp)); 101 } 102 103 /** 104 * Asserts that a Unicode escaper escapes the given hi/lo surrogate pair into the expected string. 105 * 106 * @param escaper the non-null escaper to test 107 * @param expected the expected output string 108 * @param hi the high surrogate pair character 109 * @param lo the low surrogate pair character 110 */ assertUnicodeEscaping( UnicodeEscaper escaper, String expected, char hi, char lo)111 public static void assertUnicodeEscaping( 112 UnicodeEscaper escaper, String expected, char hi, char lo) { 113 114 int cp = Character.toCodePoint(hi, lo); 115 String escaped = computeReplacement(escaper, cp); 116 Assert.assertNotNull(escaped); 117 Assert.assertEquals(expected, escaped); 118 } 119 } 120