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