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