001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.escape.testing; 018 019import static com.google.common.escape.Escapers.computeReplacement; 020 021import com.google.common.annotations.GwtCompatible; 022import com.google.common.escape.CharEscaper; 023import com.google.common.escape.Escaper; 024import com.google.common.escape.UnicodeEscaper; 025import java.io.IOException; 026import junit.framework.Assert; 027 028/** 029 * Extra assert methods for testing Escaper implementations. 030 * 031 * @author David Beaumont 032 * @since 15.0 033 */ 034@GwtCompatible 035public final class EscaperAsserts { 036 private EscaperAsserts() {} 037 038 /** 039 * Asserts that an escaper behaves correctly with respect to null inputs. 040 * 041 * @param escaper the non-null escaper to test 042 */ 043 public static void assertBasic(Escaper escaper) throws IOException { 044 // Escapers operate on characters: no characters, no escaping. 045 Assert.assertEquals("", escaper.escape("")); 046 // Assert that escapers throw null pointer exceptions. 047 try { 048 escaper.escape((String) null); 049 Assert.fail("exception not thrown when escaping a null string"); 050 } catch (NullPointerException e) { 051 // pass 052 } 053 } 054 055 /** 056 * Asserts that an escaper escapes the given character into the expected string. 057 * 058 * @param escaper the non-null escaper to test 059 * @param expected the expected output string 060 * @param c the character to escape 061 */ 062 public static void assertEscaping(CharEscaper escaper, String expected, char c) { 063 064 String escaped = computeReplacement(escaper, c); 065 Assert.assertNotNull(escaped); 066 Assert.assertEquals(expected, escaped); 067 } 068 069 /** 070 * Asserts that a Unicode escaper escapes the given code point into the expected string. 071 * 072 * @param escaper the non-null escaper to test 073 * @param expected the expected output string 074 * @param cp the Unicode code point to escape 075 */ 076 public static void assertEscaping(UnicodeEscaper escaper, String expected, int cp) { 077 078 String escaped = computeReplacement(escaper, cp); 079 Assert.assertNotNull(escaped); 080 Assert.assertEquals(expected, escaped); 081 } 082 083 /** 084 * Asserts that an escaper does not escape the given character. 085 * 086 * @param escaper the non-null escaper to test 087 * @param c the character to test 088 */ 089 public static void assertUnescaped(CharEscaper escaper, char c) { 090 Assert.assertNull(computeReplacement(escaper, c)); 091 } 092 093 /** 094 * Asserts that a Unicode escaper does not escape the given character. 095 * 096 * @param escaper the non-null escaper to test 097 * @param cp the Unicode code point to test 098 */ 099 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 */ 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}