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 22 import junit.framework.TestCase; 23 24 import java.util.Map; 25 26 /** 27 * @author David Beaumont 28 */ 29 @GwtCompatible 30 public class ArrayBasedEscaperMapTest extends TestCase { 31 public void testNullMap() { 32 try { 33 ArrayBasedEscaperMap.create(null); 34 fail("expected exception did not occur"); 35 } catch (NullPointerException e) { 36 // pass 37 } 38 } 39 40 public void testEmptyMap() { 41 Map<Character, String> map = ImmutableMap.of(); 42 ArrayBasedEscaperMap fem = ArrayBasedEscaperMap.create(map); 43 // Non-null array of zero length. 44 assertEquals(0, fem.getReplacementArray().length); 45 } 46 47 public void testMapLength() { 48 Map<Character, String> map = ImmutableMap.of( 49 'a', "first", 50 'z', "last"); 51 ArrayBasedEscaperMap fem = ArrayBasedEscaperMap.create(map); 52 // Array length is highest character value + 1 53 assertEquals('z' + 1, fem.getReplacementArray().length); 54 } 55 56 public void testMapping() { 57 Map<Character, String> map = ImmutableMap.of( 58 '\0', "zero", 59 'a', "first", 60 'b', "second", 61 'z', "last", 62 '\uFFFF', "biggest"); 63 ArrayBasedEscaperMap fem = ArrayBasedEscaperMap.create(map); 64 char[][] replacementArray = fem.getReplacementArray(); 65 // Array length is highest character value + 1 66 assertEquals(65536, replacementArray.length); 67 // The final element should always be non null. 68 assertNotNull(replacementArray[replacementArray.length - 1]); 69 // Exhaustively check all mappings (an int index avoids wrapping). 70 for (int n = 0; n < replacementArray.length; ++n) { 71 char c = (char) n; 72 if (replacementArray[n] != null) { 73 assertEquals(map.get(c), new String(replacementArray[n])); 74 } else { 75 assertFalse(map.containsKey(c)); 76 } 77 } 78 } 79 } 80