1/* 2 * Copyright (C) 2010 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 17package com.google.common.base; 18 19import com.google.common.annotations.GwtCompatible; 20 21import junit.framework.TestCase; 22 23/** 24 * Unit test for {@link Ascii}. 25 * 26 * @author Craig Berry 27 */ 28@GwtCompatible 29public class AsciiTest extends TestCase { 30 31 /** 32 * The Unicode points {@code 00c1} and {@code 00e1} are the upper- and 33 * lowercase forms of A-with-acute-accent, {@code Á} and {@code á}. 34 */ 35 private static final String IGNORED = 36 "`10-=~!@#$%^&*()_+[]\\{}|;':\",./<>?'\u00c1\u00e1\n"; 37 private static final String LOWER = "abcdefghijklmnopqrstuvwxyz"; 38 private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 39 40 public void testToLowerCase() { 41 assertEquals(LOWER, Ascii.toLowerCase(UPPER)); 42 assertSame(LOWER, Ascii.toLowerCase(LOWER)); 43 assertEquals(IGNORED, Ascii.toLowerCase(IGNORED)); 44 assertEquals("foobar", Ascii.toLowerCase("fOobaR")); 45 } 46 47 public void testToUpperCase() { 48 assertEquals(UPPER, Ascii.toUpperCase(LOWER)); 49 assertSame(UPPER, Ascii.toUpperCase(UPPER)); 50 assertEquals(IGNORED, Ascii.toUpperCase(IGNORED)); 51 assertEquals("FOOBAR", Ascii.toUpperCase("FoOBAr")); 52 } 53 54 public void testCharsIgnored() { 55 for (char c : IGNORED.toCharArray()) { 56 String str = String.valueOf(c); 57 assertTrue(str, c == Ascii.toLowerCase(c)); 58 assertTrue(str, c == Ascii.toUpperCase(c)); 59 assertFalse(str, Ascii.isLowerCase(c)); 60 assertFalse(str, Ascii.isUpperCase(c)); 61 } 62 } 63 64 public void testCharsLower() { 65 for (char c : LOWER.toCharArray()) { 66 String str = String.valueOf(c); 67 assertTrue(str, c == Ascii.toLowerCase(c)); 68 assertFalse(str, c == Ascii.toUpperCase(c)); 69 assertTrue(str, Ascii.isLowerCase(c)); 70 assertFalse(str, Ascii.isUpperCase(c)); 71 } 72 } 73 74 public void testCharsUpper() { 75 for (char c : UPPER.toCharArray()) { 76 String str = String.valueOf(c); 77 assertFalse(str, c == Ascii.toLowerCase(c)); 78 assertTrue(str, c == Ascii.toUpperCase(c)); 79 assertFalse(str, Ascii.isLowerCase(c)); 80 assertTrue(str, Ascii.isUpperCase(c)); 81 } 82 } 83 84 public void testTruncate() { 85 assertEquals("foobar", Ascii.truncate("foobar", 10, "...")); 86 assertEquals("fo...", Ascii.truncate("foobar", 5, "...")); 87 assertEquals("foobar", Ascii.truncate("foobar", 6, "...")); 88 assertEquals("...", Ascii.truncate("foobar", 3, "...")); 89 assertEquals("foobar", Ascii.truncate("foobar", 10, "…")); 90 assertEquals("foo…", Ascii.truncate("foobar", 4, "…")); 91 assertEquals("fo--", Ascii.truncate("foobar", 4, "--")); 92 assertEquals("foobar", Ascii.truncate("foobar", 6, "…")); 93 assertEquals("foob…", Ascii.truncate("foobar", 5, "…")); 94 assertEquals("foo", Ascii.truncate("foobar", 3, "")); 95 assertEquals("", Ascii.truncate("", 5, "")); 96 assertEquals("", Ascii.truncate("", 5, "...")); 97 assertEquals("", Ascii.truncate("", 0, "")); 98 } 99 100 public void testTruncateIllegalArguments() { 101 String truncated = null; 102 try { 103 truncated = Ascii.truncate("foobar", 2, "..."); 104 fail(); 105 } catch (IllegalArgumentException expected) {} 106 107 try { 108 truncated = Ascii.truncate("foobar", 8, "1234567890"); 109 fail(); 110 } catch (IllegalArgumentException expected) {} 111 112 try { 113 truncated = Ascii.truncate("foobar", -1, "..."); 114 fail(); 115 } catch (IllegalArgumentException expected) {} 116 117 try { 118 truncated = Ascii.truncate("foobar", -1, ""); 119 fail(); 120 } catch (IllegalArgumentException expected) {} 121 } 122 123 public void testEqualsIgnoreCase() { 124 assertTrue(Ascii.equalsIgnoreCase("", "")); 125 assertFalse(Ascii.equalsIgnoreCase("", "x")); 126 assertFalse(Ascii.equalsIgnoreCase("x", "")); 127 assertTrue(Ascii.equalsIgnoreCase(LOWER, UPPER)); 128 assertTrue(Ascii.equalsIgnoreCase(UPPER, LOWER)); 129 // Create new strings here to avoid early-out logic. 130 assertTrue(Ascii.equalsIgnoreCase(new String(IGNORED), new String(IGNORED))); 131 // Compare to: "\u00c1".equalsIgnoreCase("\u00e1") == true 132 assertFalse(Ascii.equalsIgnoreCase("\u00c1", "\u00e1")); 133 // Test chars just outside the alphabetic range ('A'-1 vs 'a'-1, 'Z'+1 vs 'z'+1) 134 assertFalse(Ascii.equalsIgnoreCase("@", "`")); 135 assertFalse(Ascii.equalsIgnoreCase("[", "{")); 136 } 137 138 public void testEqualsIgnoreCaseUnicodeEquivalence() { 139 // Note that it's possible in future that the JDK's idea to toUpperCase() or equalsIgnoreCase() 140 // may change and break assumptions in this test [*]. This is not a bug in the implementation of 141 // Ascii.equalsIgnoreCase(), but it is a signal that its documentation may need updating as 142 // regards edge cases. 143 144 // The Unicode point {@code 00df} is the lowercase form of sharp-S (ß), whose uppercase is "SS". 145 assertEquals("pa\u00dfword".toUpperCase(), "PASSWORD"); // [*] 146 assertFalse("pa\u00dfword".equalsIgnoreCase("PASSWORD")); // [*] 147 assertFalse(Ascii.equalsIgnoreCase("pa\u00dfword", "PASSWORD")); 148 } 149} 150