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