alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ArrayBasedCharEscaperTest.java)

This example Java source code file (ArrayBasedCharEscaperTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

arraybasedcharescaper, arraybasedcharescapertest, ascii, charescaper, chips\r\n, everything, foo, foo@bar, gwtcompatible, ioexception, map, override, string, util

The ArrayBasedCharEscaperTest.java Java example source code

/*
 * Copyright (C) 2009 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.escape;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableMap;
import com.google.common.escape.testing.EscaperAsserts;

import junit.framework.TestCase;

import java.io.IOException;
import java.util.Map;

/**
 * @author David Beaumont
 */
@GwtCompatible
public class ArrayBasedCharEscaperTest extends TestCase {
  private static final Map<Character, String> NO_REPLACEMENTS =
      ImmutableMap.of();
  private static final Map<Character, String> SIMPLE_REPLACEMENTS =
      ImmutableMap.of(
          '\n', "<newline>",
          '\t', "<tab>",
          '&', "<and>");

  public void testSafeRange() throws IOException {
    // Basic escaping of unsafe chars (wrap them in {,}'s)
    CharEscaper wrappingEscaper =
        new ArrayBasedCharEscaper(NO_REPLACEMENTS, 'A', 'Z') {
          @Override protected char[] escapeUnsafe(char c) {
            return ("{" + c + "}").toCharArray();
          }
        };
    EscaperAsserts.assertBasic(wrappingEscaper);
    // '[' and '@' lie either side of [A-Z].
    assertEquals("{[}FOO{@}BAR{]}", wrappingEscaper.escape("[FOO@BAR]"));
  }

  public void testSafeRange_maxLessThanMin() throws IOException {
    // Basic escaping of unsafe chars (wrap them in {,}'s)
    CharEscaper wrappingEscaper =
        new ArrayBasedCharEscaper(NO_REPLACEMENTS, 'Z', 'A') {
          @Override protected char[] escapeUnsafe(char c) {
            return ("{" + c + "}").toCharArray();
          }
        };
    EscaperAsserts.assertBasic(wrappingEscaper);
    // escape everything.
    assertEquals("{[}{F}{O}{O}{]}", wrappingEscaper.escape("[FOO]"));
  }

  public void testDeleteUnsafeChars() throws IOException {
    CharEscaper deletingEscaper =
        new ArrayBasedCharEscaper(NO_REPLACEMENTS, ' ', '~') {
          private final char[] noChars = new char[0];
          @Override protected char[] escapeUnsafe(char c) {
            return noChars;
          }
        };
    EscaperAsserts.assertBasic(deletingEscaper);
    assertEquals("Everything outside the printable ASCII range is deleted.",
        deletingEscaper.escape("\tEverything\0 outside the\uD800\uDC00 " +
            "printable ASCII \uFFFFrange is \u007Fdeleted.\n"));
  }

  public void testReplacementPriority() throws IOException {
    CharEscaper replacingEscaper =
        new ArrayBasedCharEscaper(SIMPLE_REPLACEMENTS, ' ', '~') {
          private final char[] unknown = new char[] { '?' };
          @Override protected char[] escapeUnsafe(char c) {
            return unknown;
          }
        };
    EscaperAsserts.assertBasic(replacingEscaper);

    // Replacements are applied first regardless of whether the character is in
    // the safe range or not ('&' is a safe char while '\t' and '\n' are not).
    assertEquals("<tab>Fish ? Chips?",
        replacingEscaper.escape("\tFish &\0 Chips\r\n"));
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java ArrayBasedCharEscaperTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.