• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.testing.NullPointerTester;
24 import junit.framework.TestCase;
25 
26 /**
27  * Unit test for {@link Strings}.
28  *
29  * @author Kevin Bourrillion
30  */
31 @GwtCompatible(emulated = true)
32 public class StringsTest extends TestCase {
testNullToEmpty()33   public void testNullToEmpty() {
34     assertEquals("", Strings.nullToEmpty(null));
35     assertEquals("", Strings.nullToEmpty(""));
36     assertEquals("a", Strings.nullToEmpty("a"));
37   }
38 
testEmptyToNull()39   public void testEmptyToNull() {
40     assertNull(Strings.emptyToNull(null));
41     assertNull(Strings.emptyToNull(""));
42     assertEquals("a", Strings.emptyToNull("a"));
43   }
44 
testIsNullOrEmpty()45   public void testIsNullOrEmpty() {
46     assertTrue(Strings.isNullOrEmpty(null));
47     assertTrue(Strings.isNullOrEmpty(""));
48     assertFalse(Strings.isNullOrEmpty("a"));
49   }
50 
testPadStart_noPadding()51   public void testPadStart_noPadding() {
52     assertSame("", Strings.padStart("", 0, '-'));
53     assertSame("x", Strings.padStart("x", 0, '-'));
54     assertSame("x", Strings.padStart("x", 1, '-'));
55     assertSame("xx", Strings.padStart("xx", 0, '-'));
56     assertSame("xx", Strings.padStart("xx", 2, '-'));
57   }
58 
testPadStart_somePadding()59   public void testPadStart_somePadding() {
60     assertEquals("-", Strings.padStart("", 1, '-'));
61     assertEquals("--", Strings.padStart("", 2, '-'));
62     assertEquals("-x", Strings.padStart("x", 2, '-'));
63     assertEquals("--x", Strings.padStart("x", 3, '-'));
64     assertEquals("-xx", Strings.padStart("xx", 3, '-'));
65   }
66 
testPadStart_negativeMinLength()67   public void testPadStart_negativeMinLength() {
68     assertSame("x", Strings.padStart("x", -1, '-'));
69   }
70 
71   // TODO: could remove if we got NPT working in GWT somehow
testPadStart_null()72   public void testPadStart_null() {
73     try {
74       Strings.padStart(null, 5, '0');
75       fail();
76     } catch (NullPointerException expected) {
77     }
78   }
79 
testPadEnd_noPadding()80   public void testPadEnd_noPadding() {
81     assertSame("", Strings.padEnd("", 0, '-'));
82     assertSame("x", Strings.padEnd("x", 0, '-'));
83     assertSame("x", Strings.padEnd("x", 1, '-'));
84     assertSame("xx", Strings.padEnd("xx", 0, '-'));
85     assertSame("xx", Strings.padEnd("xx", 2, '-'));
86   }
87 
testPadEnd_somePadding()88   public void testPadEnd_somePadding() {
89     assertEquals("-", Strings.padEnd("", 1, '-'));
90     assertEquals("--", Strings.padEnd("", 2, '-'));
91     assertEquals("x-", Strings.padEnd("x", 2, '-'));
92     assertEquals("x--", Strings.padEnd("x", 3, '-'));
93     assertEquals("xx-", Strings.padEnd("xx", 3, '-'));
94   }
95 
testPadEnd_negativeMinLength()96   public void testPadEnd_negativeMinLength() {
97     assertSame("x", Strings.padEnd("x", -1, '-'));
98   }
99 
100   // TODO: could remove if we got NPT working in GWT somehow
testPadEnd_null()101   public void testPadEnd_null() {
102     try {
103       Strings.padEnd(null, 5, '0');
104       fail();
105     } catch (NullPointerException expected) {
106     }
107   }
108 
testRepeat()109   public void testRepeat() {
110     String input = "20";
111     assertEquals("", Strings.repeat(input, 0));
112     assertEquals("20", Strings.repeat(input, 1));
113     assertEquals("2020", Strings.repeat(input, 2));
114     assertEquals("202020", Strings.repeat(input, 3));
115 
116     assertEquals("", Strings.repeat("", 4));
117 
118     for (int i = 0; i < 100; ++i) {
119       assertEquals(2 * i, Strings.repeat(input, i).length());
120     }
121 
122     try {
123       Strings.repeat("x", -1);
124       fail();
125     } catch (IllegalArgumentException expected) {
126     }
127     try {
128       // Massive string
129       Strings.repeat("12345678", (1 << 30) + 3);
130       fail();
131     } catch (ArrayIndexOutOfBoundsException expected) {
132     }
133   }
134 
135   // TODO: could remove if we got NPT working in GWT somehow
testRepeat_null()136   public void testRepeat_null() {
137     try {
138       Strings.repeat(null, 5);
139       fail();
140     } catch (NullPointerException expected) {
141     }
142   }
143 
testCommonPrefix()144   public void testCommonPrefix() {
145     assertEquals("", Strings.commonPrefix("", ""));
146     assertEquals("", Strings.commonPrefix("abc", ""));
147     assertEquals("", Strings.commonPrefix("", "abc"));
148     assertEquals("", Strings.commonPrefix("abcde", "xyz"));
149     assertEquals("", Strings.commonPrefix("xyz", "abcde"));
150     assertEquals("", Strings.commonPrefix("xyz", "abcxyz"));
151     assertEquals("a", Strings.commonPrefix("abc", "aaaaa"));
152     assertEquals("aa", Strings.commonPrefix("aa", "aaaaa"));
153     assertEquals("abc", Strings.commonPrefix(new StringBuffer("abcdef"), "abcxyz"));
154 
155     // Identical valid surrogate pairs.
156     assertEquals(
157         "abc\uD8AB\uDCAB", Strings.commonPrefix("abc\uD8AB\uDCABdef", "abc\uD8AB\uDCABxyz"));
158     // Differing valid surrogate pairs.
159     assertEquals("abc", Strings.commonPrefix("abc\uD8AB\uDCABdef", "abc\uD8AB\uDCACxyz"));
160     // One invalid pair.
161     assertEquals("abc", Strings.commonPrefix("abc\uD8AB\uDCABdef", "abc\uD8AB\uD8ABxyz"));
162     // Two identical invalid pairs.
163     assertEquals(
164         "abc\uD8AB\uD8AC", Strings.commonPrefix("abc\uD8AB\uD8ACdef", "abc\uD8AB\uD8ACxyz"));
165     // Two differing invalid pairs.
166     assertEquals("abc\uD8AB", Strings.commonPrefix("abc\uD8AB\uD8ABdef", "abc\uD8AB\uD8ACxyz"));
167     // One orphan high surrogate.
168     assertEquals("", Strings.commonPrefix("\uD8AB\uDCAB", "\uD8AB"));
169     // Two orphan high surrogates.
170     assertEquals("\uD8AB", Strings.commonPrefix("\uD8AB", "\uD8AB"));
171   }
172 
testCommonSuffix()173   public void testCommonSuffix() {
174     assertEquals("", Strings.commonSuffix("", ""));
175     assertEquals("", Strings.commonSuffix("abc", ""));
176     assertEquals("", Strings.commonSuffix("", "abc"));
177     assertEquals("", Strings.commonSuffix("abcde", "xyz"));
178     assertEquals("", Strings.commonSuffix("xyz", "abcde"));
179     assertEquals("", Strings.commonSuffix("xyz", "xyzabc"));
180     assertEquals("c", Strings.commonSuffix("abc", "ccccc"));
181     assertEquals("aa", Strings.commonSuffix("aa", "aaaaa"));
182     assertEquals("abc", Strings.commonSuffix(new StringBuffer("xyzabc"), "xxxabc"));
183 
184     // Identical valid surrogate pairs.
185     assertEquals(
186         "\uD8AB\uDCABdef", Strings.commonSuffix("abc\uD8AB\uDCABdef", "xyz\uD8AB\uDCABdef"));
187     // Differing valid surrogate pairs.
188     assertEquals("def", Strings.commonSuffix("abc\uD8AB\uDCABdef", "abc\uD8AC\uDCABdef"));
189     // One invalid pair.
190     assertEquals("def", Strings.commonSuffix("abc\uD8AB\uDCABdef", "xyz\uDCAB\uDCABdef"));
191     // Two identical invalid pairs.
192     assertEquals(
193         "\uD8AB\uD8ABdef", Strings.commonSuffix("abc\uD8AB\uD8ABdef", "xyz\uD8AB\uD8ABdef"));
194     // Two differing invalid pairs.
195     assertEquals("\uDCABdef", Strings.commonSuffix("abc\uDCAB\uDCABdef", "abc\uDCAC\uDCABdef"));
196     // One orphan low surrogate.
197     assertEquals("", Strings.commonSuffix("x\uD8AB\uDCAB", "\uDCAB"));
198     // Two orphan low surrogates.
199     assertEquals("\uDCAB", Strings.commonSuffix("\uDCAB", "\uDCAB"));
200   }
201 
testValidSurrogatePairAt()202   public void testValidSurrogatePairAt() {
203     assertTrue(Strings.validSurrogatePairAt("\uD8AB\uDCAB", 0));
204     assertTrue(Strings.validSurrogatePairAt("abc\uD8AB\uDCAB", 3));
205     assertTrue(Strings.validSurrogatePairAt("abc\uD8AB\uDCABxyz", 3));
206     assertFalse(Strings.validSurrogatePairAt("\uD8AB\uD8AB", 0));
207     assertFalse(Strings.validSurrogatePairAt("\uDCAB\uDCAB", 0));
208     assertFalse(Strings.validSurrogatePairAt("\uD8AB\uDCAB", -1));
209     assertFalse(Strings.validSurrogatePairAt("\uD8AB\uDCAB", 1));
210     assertFalse(Strings.validSurrogatePairAt("\uD8AB\uDCAB", -2));
211     assertFalse(Strings.validSurrogatePairAt("\uD8AB\uDCAB", 2));
212     assertFalse(Strings.validSurrogatePairAt("x\uDCAB", 0));
213     assertFalse(Strings.validSurrogatePairAt("\uD8ABx", 0));
214   }
215 
testLenientFormat()216   public void testLenientFormat() {
217     assertEquals("%s", Strings.lenientFormat("%s"));
218     assertEquals("5", Strings.lenientFormat("%s", 5));
219     assertEquals("foo [5]", Strings.lenientFormat("foo", 5));
220     assertEquals("foo [5, 6, 7]", Strings.lenientFormat("foo", 5, 6, 7));
221     assertEquals("%s 1 2", Strings.lenientFormat("%s %s %s", "%s", 1, 2));
222     assertEquals(" [5, 6]", Strings.lenientFormat("", 5, 6));
223     assertEquals("123", Strings.lenientFormat("%s%s%s", 1, 2, 3));
224     assertEquals("1%s%s", Strings.lenientFormat("%s%s%s", 1));
225     assertEquals("5 + 6 = 11", Strings.lenientFormat("%s + 6 = 11", 5));
226     assertEquals("5 + 6 = 11", Strings.lenientFormat("5 + %s = 11", 6));
227     assertEquals("5 + 6 = 11", Strings.lenientFormat("5 + 6 = %s", 11));
228     assertEquals("5 + 6 = 11", Strings.lenientFormat("%s + %s = %s", 5, 6, 11));
229     assertEquals("null [null, null]", Strings.lenientFormat("%s", null, null, null));
230     assertEquals("null [5, 6]", Strings.lenientFormat(null, 5, 6));
231     assertEquals("null", Strings.lenientFormat("%s", (Object) null));
232     assertEquals("(Object[])null", Strings.lenientFormat("%s", (Object[]) null));
233   }
234 
235   @GwtIncompatible // GWT reflection includes less data
testLenientFormat_badArgumentToString()236   public void testLenientFormat_badArgumentToString() {
237     assertThat(Strings.lenientFormat("boiler %s plate", new ThrowsOnToString()))
238         .matches(
239             "boiler <com\\.google\\.common\\.base\\.StringsTest\\$ThrowsOnToString@[0-9a-f]+ "
240                 + "threw java\\.lang\\.UnsupportedOperationException> plate");
241   }
242 
testLenientFormat_badArgumentToString_gwtFriendly()243   public void testLenientFormat_badArgumentToString_gwtFriendly() {
244     assertThat(Strings.lenientFormat("boiler %s plate", new ThrowsOnToString()))
245         .matches("boiler <.*> plate");
246   }
247 
248   private static class ThrowsOnToString {
249     @Override
toString()250     public String toString() {
251       throw new UnsupportedOperationException();
252     }
253   }
254 
255   @GwtIncompatible // NullPointerTester
testNullPointers()256   public void testNullPointers() {
257     NullPointerTester tester = new NullPointerTester();
258     tester.testAllPublicStaticMethods(Strings.class);
259   }
260 }
261