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.Beta;
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.escape.CharEscaper;
024import com.google.common.escape.Escaper;
025import com.google.common.escape.UnicodeEscaper;
026import java.io.IOException;
027import junit.framework.Assert;
028
029/**
030 * Extra assert methods for testing Escaper implementations.
031 *
032 * @author David Beaumont
033 * @since 15.0
034 */
035@Beta
036@GwtCompatible
037public final class EscaperAsserts {
038  private EscaperAsserts() {}
039
040  /**
041   * Asserts that an escaper behaves correctly with respect to null inputs.
042   *
043   * @param escaper the non-null escaper to test
044   */
045  public static void assertBasic(Escaper escaper) throws IOException {
046    // Escapers operate on characters: no characters, no escaping.
047    Assert.assertEquals("", escaper.escape(""));
048    // Assert that escapers throw null pointer exceptions.
049    try {
050      escaper.escape((String) null);
051      Assert.fail("exception not thrown when escaping a null string");
052    } catch (NullPointerException e) {
053      // pass
054    }
055  }
056
057  /**
058   * Asserts that an escaper escapes the given character into the expected string.
059   *
060   * @param escaper the non-null escaper to test
061   * @param expected the expected output string
062   * @param c the character to escape
063   */
064  public static void assertEscaping(CharEscaper escaper, String expected, char c) {
065
066    String escaped = computeReplacement(escaper, c);
067    Assert.assertNotNull(escaped);
068    Assert.assertEquals(expected, escaped);
069  }
070
071  /**
072   * Asserts that an escaper does not escape the given character.
073   *
074   * @param escaper the non-null escaper to test
075   * @param c the character to test
076   */
077  public static void assertUnescaped(CharEscaper escaper, char c) {
078    Assert.assertNull(computeReplacement(escaper, c));
079  }
080
081  /**
082   * Asserts that a Unicode escaper escapes the given code point into the expected string.
083   *
084   * @param escaper the non-null escaper to test
085   * @param expected the expected output string
086   * @param cp the Unicode code point to escape
087   */
088  public static void assertEscaping(UnicodeEscaper escaper, String expected, int cp) {
089
090    String escaped = computeReplacement(escaper, cp);
091    Assert.assertNotNull(escaped);
092    Assert.assertEquals(expected, escaped);
093  }
094
095  /**
096   * Asserts that a Unicode escaper does not escape the given character.
097   *
098   * @param escaper the non-null escaper to test
099   * @param cp the Unicode code point to test
100   */
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   */
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}