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