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; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.collect.ImmutableMap; 21 import com.google.common.escape.testing.EscaperAsserts; 22 import java.io.IOException; 23 import junit.framework.TestCase; 24 25 /** 26 * @author David Beaumont 27 */ 28 @GwtCompatible 29 @ElementTypesAreNonnullByDefault 30 public class ArrayBasedCharEscaperTest extends TestCase { 31 private static final ImmutableMap<Character, String> NO_REPLACEMENTS = ImmutableMap.of(); 32 private static final ImmutableMap<Character, String> SIMPLE_REPLACEMENTS = 33 ImmutableMap.of( 34 '\n', "<newline>", 35 '\t', "<tab>", 36 '&', "<and>"); 37 testSafeRange()38 public void testSafeRange() throws IOException { 39 // Basic escaping of unsafe chars (wrap them in {,}'s) 40 CharEscaper wrappingEscaper = 41 new ArrayBasedCharEscaper(NO_REPLACEMENTS, 'A', 'Z') { 42 @Override 43 protected char[] escapeUnsafe(char c) { 44 return ("{" + c + "}").toCharArray(); 45 } 46 }; 47 EscaperAsserts.assertBasic(wrappingEscaper); 48 // '[' and '@' lie either side of [A-Z]. 49 assertEquals("{[}FOO{@}BAR{]}", wrappingEscaper.escape("[FOO@BAR]")); 50 } 51 testSafeRange_maxLessThanMin()52 public void testSafeRange_maxLessThanMin() throws IOException { 53 // Basic escaping of unsafe chars (wrap them in {,}'s) 54 CharEscaper wrappingEscaper = 55 new ArrayBasedCharEscaper(NO_REPLACEMENTS, 'Z', 'A') { 56 @Override 57 protected char[] escapeUnsafe(char c) { 58 return ("{" + c + "}").toCharArray(); 59 } 60 }; 61 EscaperAsserts.assertBasic(wrappingEscaper); 62 // escape everything. 63 assertEquals("{[}{F}{O}{O}{]}", wrappingEscaper.escape("[FOO]")); 64 } 65 testDeleteUnsafeChars()66 public void testDeleteUnsafeChars() throws IOException { 67 CharEscaper deletingEscaper = 68 new ArrayBasedCharEscaper(NO_REPLACEMENTS, ' ', '~') { 69 private final char[] noChars = new char[0]; 70 71 @Override 72 protected char[] escapeUnsafe(char c) { 73 return noChars; 74 } 75 }; 76 EscaperAsserts.assertBasic(deletingEscaper); 77 assertEquals( 78 "Everything outside the printable ASCII range is deleted.", 79 deletingEscaper.escape( 80 "\tEverything\0 outside the\uD800\uDC00 " 81 + "printable ASCII \uFFFFrange is \u007Fdeleted.\n")); 82 } 83 testReplacementPriority()84 public void testReplacementPriority() throws IOException { 85 CharEscaper replacingEscaper = 86 new ArrayBasedCharEscaper(SIMPLE_REPLACEMENTS, ' ', '~') { 87 private final char[] unknown = new char[] {'?'}; 88 89 @Override 90 protected char[] escapeUnsafe(char c) { 91 return unknown; 92 } 93 }; 94 EscaperAsserts.assertBasic(replacingEscaper); 95 96 // Replacements are applied first regardless of whether the character is in 97 // the safe range or not ('&' is a safe char while '\t' and '\n' are not). 98 assertEquals( 99 "<tab>Fish <and>? Chips?<newline>", replacingEscaper.escape("\tFish &\0 Chips\r\n")); 100 } 101 } 102