public void testNullEscaper() throws IOException { Escaper escaper = Escapers.nullEscaper(); EscaperAsserts.assertBasic(escaper); String s = "\0\n\t\\az09~\uD800\uDC00\uFFFF"; assertEquals("null escaper should have no effect", s, escaper.escape(s)); }
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() throws IOException { // Basic escaping of unsafe chars (wrap them in {,}'s) UnicodeEscaper wrappingEscaper = new ArrayBasedUnicodeEscaper(NO_REPLACEMENTS, 'A', 'Z', null) { @Override protected char[] escapeUnsafe(int c) { return ("{" + (char) 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 { UnicodeEscaper deletingEscaper = new ArrayBasedUnicodeEscaper(NO_REPLACEMENTS, ' ', '~', null) { @Override protected char[] escapeUnsafe(int c) { return NO_CHARS; } }; 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 { UnicodeEscaper replacingEscaper = new ArrayBasedUnicodeEscaper(SIMPLE_REPLACEMENTS, ' ', '~', null) { private final char[] unknown = new char[] {'?'}; @Override protected char[] escapeUnsafe(int 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 <and>? Chips?<newline>", replacingEscaper.escape("\tFish &\0 Chips\r\n")); }
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 <and>? Chips?<newline>", replacingEscaper.escape("\tFish &\0 Chips\r\n")); } }
public void testCodePointsFromSurrogatePairs() throws IOException { UnicodeEscaper surrogateEscaper = new ArrayBasedUnicodeEscaper(NO_REPLACEMENTS, 0, 0x20000, null) { private final char[] escaped = new char[] {'X'}; @Override protected char[] escapeUnsafe(int c) { return escaped; } }; EscaperAsserts.assertBasic(surrogateEscaper); // A surrogate pair defining a code point within the safe range. String safeInput = "\uD800\uDC00"; // 0x10000 assertEquals(safeInput, surrogateEscaper.escape(safeInput)); // A surrogate pair defining a code point outside the safe range (but both // of the surrogate characters lie within the safe range). It is important // not to accidentally treat this as a sequence of safe characters. String unsafeInput = "\uDBFF\uDFFF"; // 0x10FFFF assertEquals("X", surrogateEscaper.escape(unsafeInput)); } }
public void testAsUnicodeEscaper() throws IOException { CharEscaper charEscaper = createSimpleCharEscaper( ImmutableMap.<Character, char[]>builder() .put('x', "<hello>".toCharArray()) .put('\uD800', "<hi>".toCharArray()) .put('\uDC00', "<lo>".toCharArray()) .build()); UnicodeEscaper unicodeEscaper = Escapers.asUnicodeEscaper(charEscaper); EscaperAsserts.assertBasic(unicodeEscaper); assertEquals("<hello><hi><lo>", charEscaper.escape("x\uD800\uDC00")); assertEquals("<hello><hi><lo>", unicodeEscaper.escape("x\uD800\uDC00")); // Test that wrapped escapers acquire good Unicode semantics. assertEquals("<hi><hello><lo>", charEscaper.escape("\uD800x\uDC00")); try { unicodeEscaper.escape("\uD800x\uDC00"); fail("should have failed for bad Unicode input"); } catch (IllegalArgumentException e) { // pass } assertEquals("<lo><hi>", charEscaper.escape("\uDC00\uD800")); try { unicodeEscaper.escape("\uDC00\uD800"); fail("should have failed for bad Unicode input"); } catch (IllegalArgumentException e) { // pass } }
public void testReplacements() throws IOException { // In reality this is not a very sensible escaper to have (if you are only // escaping elements from a map you would use a ArrayBasedCharEscaper). UnicodeEscaper escaper = new ArrayBasedUnicodeEscaper( SIMPLE_REPLACEMENTS, Character.MIN_VALUE, Character.MAX_CODE_POINT, null) { @Override protected char[] escapeUnsafe(int c) { return NO_CHARS; } }; EscaperAsserts.assertBasic(escaper); assertEquals("<tab>Fish <and> Chips<newline>", escaper.escape("\tFish & Chips\n")); // Verify that everything else is left unescaped. String safeChars = "\0\u0100\uD800\uDC00\uFFFF"; assertEquals(safeChars, escaper.escape(safeChars)); // Ensure that Unicode escapers behave correctly wrt badly formed input. String badUnicode = "\uDC00\uD800"; try { escaper.escape(badUnicode); fail("should fail for bad Unicode"); } catch (IllegalArgumentException e) { // Pass } }
public void testNullEscaper() throws IOException { Escaper escaper = Escapers.nullEscaper(); EscaperAsserts.assertBasic(escaper); String s = "\0\n\t\\az09~\uD800\uDC00\uFFFF"; assertEquals("null escaper should have no effect", s, escaper.escape(s)); }
public void testSafeRange() throws IOException { // Basic escaping of unsafe chars (wrap them in {,}'s) UnicodeEscaper wrappingEscaper = new ArrayBasedUnicodeEscaper(NO_REPLACEMENTS, 'A', 'Z', null) { @Override protected char[] escapeUnsafe(int c) { return ("{" + (char) c + "}").toCharArray(); } }; EscaperAsserts.assertBasic(wrappingEscaper); // '[' and '@' lie either side of [A-Z]. assertEquals("{[}FOO{@}BAR{]}", wrappingEscaper.escape("[FOO@BAR]")); }
public void testDeleteUnsafeChars() throws IOException { UnicodeEscaper deletingEscaper = new ArrayBasedUnicodeEscaper(NO_REPLACEMENTS, ' ', '~', null) { @Override protected char[] escapeUnsafe(int c) { return NO_CHARS; } }; 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 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 { UnicodeEscaper replacingEscaper = new ArrayBasedUnicodeEscaper(SIMPLE_REPLACEMENTS, ' ', '~', null) { private final char[] unknown = new char[] { '?' }; @Override protected char[] escapeUnsafe(int 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 <and>? Chips?<newline>", replacingEscaper.escape("\tFish &\0 Chips\r\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 <and>? Chips?<newline>", replacingEscaper.escape("\tFish &\0 Chips\r\n")); } }
public void testCodePointsFromSurrogatePairs() throws IOException { UnicodeEscaper surrogateEscaper = new ArrayBasedUnicodeEscaper(NO_REPLACEMENTS, 0, 0x20000, null) { private final char[] escaped = new char[] { 'X' }; @Override protected char[] escapeUnsafe(int c) { return escaped; } }; EscaperAsserts.assertBasic(surrogateEscaper); // A surrogate pair defining a code point within the safe range. String safeInput = "\uD800\uDC00"; // 0x10000 assertEquals(safeInput, surrogateEscaper.escape(safeInput)); // A surrogate pair defining a code point outside the safe range (but both // of the surrogate characters lie within the safe range). It is important // not to accidentally treat this as a sequence of safe characters. String unsafeInput = "\uDBFF\uDFFF"; // 0x10FFFF assertEquals("X", surrogateEscaper.escape(unsafeInput)); } }