1 /*
2  * Copyright (C) 2014 The Android Open Source Project
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.android.inputmethod.latin.common;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import androidx.test.filters.SmallTest;
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import java.util.Locale;
30 
31 @SmallTest
32 @RunWith(AndroidJUnit4.class)
33 public class StringUtilsTests {
34     private static final Locale US = Locale.US;
35     private static final Locale GERMAN = Locale.GERMAN;
36     private static final Locale TURKEY = new Locale("tr", "TR");
37     private static final Locale GREECE = new Locale("el", "GR");
38 
assert_toTitleCaseOfKeyLabel(final Locale locale, final String lowerCase, final String expected)39     private static void assert_toTitleCaseOfKeyLabel(final Locale locale,
40             final String lowerCase, final String expected) {
41         assertEquals(lowerCase + " in " + locale, expected,
42                 StringUtils.toTitleCaseOfKeyLabel(lowerCase, locale));
43     }
44 
45     @Test
test_toTitleCaseOfKeyLabel()46     public void test_toTitleCaseOfKeyLabel() {
47         assert_toTitleCaseOfKeyLabel(US, null, null);
48         assert_toTitleCaseOfKeyLabel(US, "", "");
49         assert_toTitleCaseOfKeyLabel(US, "aeiou", "AEIOU");
50         // U+00E0: "à" LATIN SMALL LETTER A WITH GRAVE
51         // U+00E8: "è" LATIN SMALL LETTER E WITH GRAVE
52         // U+00EE: "î" LATIN SMALL LETTER I WITH CIRCUMFLEX
53         // U+00F6: "ö" LATIN SMALL LETTER O WITH DIAERESIS
54         // U+016B: "ū" LATIN SMALL LETTER U WITH MACRON
55         // U+00F1: "ñ" LATIN SMALL LETTER N WITH TILDE
56         // U+00E7: "ç" LATIN SMALL LETTER C WITH CEDILLA
57         // U+00C0: "À" LATIN CAPITAL LETTER A WITH GRAVE
58         // U+00C8: "È" LATIN CAPITAL LETTER E WITH GRAVE
59         // U+00CE: "Î" LATIN CAPITAL LETTER I WITH CIRCUMFLEX
60         // U+00D6: "Ö" LATIN CAPITAL LETTER O WITH DIAERESIS
61         // U+016A: "Ū" LATIN CAPITAL LETTER U WITH MACRON
62         // U+00D1: "Ñ" LATIN CAPITAL LETTER N WITH TILDE
63         // U+00C7: "Ç" LATIN CAPITAL LETTER C WITH CEDILLA
64         assert_toTitleCaseOfKeyLabel(US,
65                 "\u00E0\u00E8\u00EE\u00F6\u016B\u00F1\u00E7",
66                 "\u00C0\u00C8\u00CE\u00D6\u016A\u00D1\u00C7");
67         // U+00DF: "ß" LATIN SMALL LETTER SHARP S
68         // U+015B: "ś" LATIN SMALL LETTER S WITH ACUTE
69         // U+0161: "š" LATIN SMALL LETTER S WITH CARON
70         // U+015A: "Ś" LATIN CAPITAL LETTER S WITH ACUTE
71         // U+0160: "Š" LATIN CAPITAL LETTER S WITH CARONZ
72         assert_toTitleCaseOfKeyLabel(GERMAN,
73                 "\u00DF\u015B\u0161",
74                 "SS\u015A\u0160");
75         // U+0259: "ə" LATIN SMALL LETTER SCHWA
76         // U+0069: "i" LATIN SMALL LETTER I
77         // U+0131: "ı" LATIN SMALL LETTER DOTLESS I
78         // U+018F: "Ə" LATIN SMALL LETTER SCHWA
79         // U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
80         // U+0049: "I" LATIN SMALL LETTER I
81         assert_toTitleCaseOfKeyLabel(TURKEY,
82                 "\u0259\u0069\u0131",
83                 "\u018F\u0130\u0049");
84         // U+03C3: "σ" GREEK SMALL LETTER SIGMA
85         // U+03C2: "ς" GREEK SMALL LETTER FINAL SIGMA
86         // U+03A3: "Σ" GREEK CAPITAL LETTER SIGMA
87         assert_toTitleCaseOfKeyLabel(GREECE,
88                 "\u03C3\u03C2",
89                 "\u03A3\u03A3");
90         // U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
91         // U+03AD: "έ" GREEK SMALL LETTER EPSILON WITH TONOS
92         // U+03AE: "ή" GREEK SMALL LETTER ETA WITH TONOS
93         // U+03AF: "ί" GREEK SMALL LETTER IOTA WITH TONOS
94         // U+03CC: "ό" GREEK SMALL LETTER OMICRON WITH TONOS
95         // U+03CD: "ύ" GREEK SMALL LETTER UPSILON WITH TONOS
96         // U+03CE: "ώ" GREEK SMALL LETTER OMEGA WITH TONOS
97         // U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
98         // U+0388: "Έ" GREEK CAPITAL LETTER EPSILON WITH TONOS
99         // U+0389: "Ή" GREEK CAPITAL LETTER ETA WITH TONOS
100         // U+038A: "Ί" GREEK CAPITAL LETTER IOTA WITH TONOS
101         // U+038C: "Ό" GREEK CAPITAL LETTER OMICRON WITH TONOS
102         // U+038E: "Ύ" GREEK CAPITAL LETTER UPSILON WITH TONOS
103         // U+038F: "Ώ" GREEK CAPITAL LETTER OMEGA WITH TONOS
104         assert_toTitleCaseOfKeyLabel(GREECE,
105                 "\u03AC\u03AD\u03AE\u03AF\u03CC\u03CD\u03CE",
106                 "\u0386\u0388\u0389\u038A\u038C\u038E\u038F");
107         // U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
108         // U+03CB: "ϋ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA
109         // U+0390: "ΐ" GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
110         // U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
111         // U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
112         // U+03AB: "Ϋ" GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA
113         // U+0399: "Ι" GREEK CAPITAL LETTER IOTA
114         // U+03A5: "Υ" GREEK CAPITAL LETTER UPSILON
115         // U+0308: COMBINING DIAERESIS
116         // U+0301: COMBINING GRAVE ACCENT
117         assert_toTitleCaseOfKeyLabel(GREECE,
118                 "\u03CA\u03CB\u0390\u03B0",
119                 "\u03AA\u03AB\u0399\u0308\u0301\u03A5\u0308\u0301");
120     }
121 
assert_toTitleCaseOfKeyCode(final Locale locale, final int lowerCase, final int expected)122     private static void assert_toTitleCaseOfKeyCode(final Locale locale, final int lowerCase,
123             final int expected) {
124         assertEquals(lowerCase + " in " + locale, expected,
125                 StringUtils.toTitleCaseOfKeyCode(lowerCase, locale));
126     }
127 
128     @Test
test_toTitleCaseOfKeyCode()129     public void test_toTitleCaseOfKeyCode() {
130         assert_toTitleCaseOfKeyCode(US, Constants.CODE_ENTER, Constants.CODE_ENTER);
131         assert_toTitleCaseOfKeyCode(US, Constants.CODE_SPACE, Constants.CODE_SPACE);
132         assert_toTitleCaseOfKeyCode(US, Constants.CODE_COMMA, Constants.CODE_COMMA);
133         // U+0069: "i" LATIN SMALL LETTER I
134         // U+0131: "ı" LATIN SMALL LETTER DOTLESS I
135         // U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
136         // U+0049: "I" LATIN SMALL LETTER I
137         assert_toTitleCaseOfKeyCode(US, 0x0069, 0x0049); // i -> I
138         assert_toTitleCaseOfKeyCode(US, 0x0131, 0x0049); // ı -> I
139         assert_toTitleCaseOfKeyCode(TURKEY, 0x0069, 0x0130); // i -> İ
140         assert_toTitleCaseOfKeyCode(TURKEY, 0x0131, 0x0049); // ı -> I
141         // U+00DF: "ß" LATIN SMALL LETTER SHARP S
142         // The title case of "ß" is "SS".
143         assert_toTitleCaseOfKeyCode(US, 0x00DF, Constants.CODE_UNSPECIFIED);
144         // U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
145         // U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
146         assert_toTitleCaseOfKeyCode(GREECE, 0x03AC, 0x0386);
147         // U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
148         // U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
149         assert_toTitleCaseOfKeyCode(GREECE, 0x03CA, 0x03AA);
150         // U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
151         // The title case of "ΰ" is "\u03A5\u0308\u0301".
152         assert_toTitleCaseOfKeyCode(GREECE, 0x03B0, Constants.CODE_UNSPECIFIED);
153     }
154 
assert_capitalizeFirstCodePoint(final Locale locale, final String text, final String expected)155     private static void assert_capitalizeFirstCodePoint(final Locale locale, final String text,
156             final String expected) {
157         assertEquals(text + " in " + locale, expected,
158                 StringUtils.capitalizeFirstCodePoint(text, locale));
159     }
160 
161     @Test
test_capitalizeFirstCodePoint()162     public void test_capitalizeFirstCodePoint() {
163         assert_capitalizeFirstCodePoint(US, "", "");
164         assert_capitalizeFirstCodePoint(US, "a", "A");
165         assert_capitalizeFirstCodePoint(US, "à", "À");
166         assert_capitalizeFirstCodePoint(US, "ß", "SS");
167         assert_capitalizeFirstCodePoint(US, "text", "Text");
168         assert_capitalizeFirstCodePoint(US, "iGoogle", "IGoogle");
169         assert_capitalizeFirstCodePoint(TURKEY, "iyi", "İyi");
170         assert_capitalizeFirstCodePoint(TURKEY, "ısırdı", "Isırdı");
171         assert_capitalizeFirstCodePoint(GREECE, "ά", "Ά");
172         assert_capitalizeFirstCodePoint(GREECE, "άνεση", "Άνεση");
173     }
174 
assert_capitalizeFirstAndDowncaseRest(final Locale locale, final String text, final String expected)175     private static void assert_capitalizeFirstAndDowncaseRest(final Locale locale,
176             final String text, final String expected) {
177         assertEquals(text + " in " + locale, expected,
178                 StringUtils.capitalizeFirstAndDowncaseRest(text, locale));
179     }
180 
181     @Test
test_capitalizeFirstAndDowncaseRest()182     public void test_capitalizeFirstAndDowncaseRest() {
183         assert_capitalizeFirstAndDowncaseRest(US, "", "");
184         assert_capitalizeFirstAndDowncaseRest(US, "a", "A");
185         assert_capitalizeFirstAndDowncaseRest(US, "à", "À");
186         assert_capitalizeFirstAndDowncaseRest(US, "ß", "SS");
187         assert_capitalizeFirstAndDowncaseRest(US, "text", "Text");
188         assert_capitalizeFirstAndDowncaseRest(US, "iGoogle", "Igoogle");
189         assert_capitalizeFirstAndDowncaseRest(US, "invite", "Invite");
190         assert_capitalizeFirstAndDowncaseRest(US, "INVITE", "Invite");
191         assert_capitalizeFirstAndDowncaseRest(TURKEY, "iyi", "İyi");
192         assert_capitalizeFirstAndDowncaseRest(TURKEY, "İYİ", "İyi");
193         assert_capitalizeFirstAndDowncaseRest(TURKEY, "ısırdı", "Isırdı");
194         assert_capitalizeFirstAndDowncaseRest(TURKEY, "ISIRDI", "Isırdı");
195         assert_capitalizeFirstAndDowncaseRest(GREECE, "ά", "Ά");
196         assert_capitalizeFirstAndDowncaseRest(GREECE, "άνεση", "Άνεση");
197         assert_capitalizeFirstAndDowncaseRest(GREECE, "ΆΝΕΣΗ", "Άνεση");
198     }
199 
200     @Test
testContainsInArray()201     public void testContainsInArray() {
202         assertFalse("empty array", StringUtils.containsInArray("key", new String[0]));
203         assertFalse("not in 1 element", StringUtils.containsInArray("key", new String[] {
204                 "key1"
205         }));
206         assertFalse("not in 2 elements", StringUtils.containsInArray("key", new String[] {
207                 "key1", "key2"
208         }));
209 
210         assertTrue("in 1 element", StringUtils.containsInArray("key", new String[] {
211                 "key"
212         }));
213         assertTrue("in 2 elements", StringUtils.containsInArray("key", new String[] {
214                 "key1", "key"
215         }));
216     }
217 
218     @Test
testContainsInCommaSplittableText()219     public void testContainsInCommaSplittableText() {
220         assertFalse("null", StringUtils.containsInCommaSplittableText("key", null));
221         assertFalse("empty", StringUtils.containsInCommaSplittableText("key", ""));
222         assertFalse("not in 1 element",
223                 StringUtils.containsInCommaSplittableText("key", "key1"));
224         assertFalse("not in 2 elements",
225                 StringUtils.containsInCommaSplittableText("key", "key1,key2"));
226 
227         assertTrue("in 1 element", StringUtils.containsInCommaSplittableText("key", "key"));
228         assertTrue("in 2 elements", StringUtils.containsInCommaSplittableText("key", "key1,key"));
229     }
230 
231     @Test
testRemoveFromCommaSplittableTextIfExists()232     public void testRemoveFromCommaSplittableTextIfExists() {
233         assertEquals("null", "", StringUtils.removeFromCommaSplittableTextIfExists("key", null));
234         assertEquals("empty", "", StringUtils.removeFromCommaSplittableTextIfExists("key", ""));
235 
236         assertEquals("not in 1 element", "key1",
237                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key1"));
238         assertEquals("not in 2 elements", "key1,key2",
239                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key2"));
240 
241         assertEquals("in 1 element", "",
242                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key"));
243         assertEquals("in 2 elements at position 1", "key2",
244                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key,key2"));
245         assertEquals("in 2 elements at position 2", "key1",
246                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key"));
247         assertEquals("in 3 elements at position 2", "key1,key3",
248                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key1,key,key3"));
249 
250         assertEquals("in 3 elements at position 1,2,3", "",
251                 StringUtils.removeFromCommaSplittableTextIfExists("key", "key,key,key"));
252         assertEquals("in 5 elements at position 2,4", "key1,key3,key5",
253                 StringUtils.removeFromCommaSplittableTextIfExists(
254                         "key", "key1,key,key3,key,key5"));
255     }
256 
257     @Test
testCapitalizeFirstCodePoint()258     public void testCapitalizeFirstCodePoint() {
259         assertEquals("SSaa",
260                 StringUtils.capitalizeFirstCodePoint("ßaa", Locale.GERMAN));
261         assertEquals("Aßa",
262                 StringUtils.capitalizeFirstCodePoint("aßa", Locale.GERMAN));
263         assertEquals("Iab",
264                 StringUtils.capitalizeFirstCodePoint("iab", Locale.ENGLISH));
265         assertEquals("CAmElCaSe",
266                 StringUtils.capitalizeFirstCodePoint("cAmElCaSe", Locale.ENGLISH));
267         assertEquals("İab",
268                 StringUtils.capitalizeFirstCodePoint("iab", new Locale("tr")));
269         assertEquals("AİB",
270                 StringUtils.capitalizeFirstCodePoint("AİB", new Locale("tr")));
271         assertEquals("A",
272                 StringUtils.capitalizeFirstCodePoint("a", Locale.ENGLISH));
273         assertEquals("A",
274                 StringUtils.capitalizeFirstCodePoint("A", Locale.ENGLISH));
275     }
276 
277     @Test
testCapitalizeFirstAndDowncaseRest()278     public void testCapitalizeFirstAndDowncaseRest() {
279         assertEquals("SSaa",
280                 StringUtils.capitalizeFirstAndDowncaseRest("ßaa", Locale.GERMAN));
281         assertEquals("Aßa",
282                 StringUtils.capitalizeFirstAndDowncaseRest("aßa", Locale.GERMAN));
283         assertEquals("Iab",
284                 StringUtils.capitalizeFirstAndDowncaseRest("iab", Locale.ENGLISH));
285         assertEquals("Camelcase",
286                 StringUtils.capitalizeFirstAndDowncaseRest("cAmElCaSe", Locale.ENGLISH));
287         assertEquals("İab",
288                 StringUtils.capitalizeFirstAndDowncaseRest("iab", new Locale("tr")));
289         assertEquals("Aib",
290                 StringUtils.capitalizeFirstAndDowncaseRest("AİB", new Locale("tr")));
291         assertEquals("A",
292                 StringUtils.capitalizeFirstAndDowncaseRest("a", Locale.ENGLISH));
293         assertEquals("A",
294                 StringUtils.capitalizeFirstAndDowncaseRest("A", Locale.ENGLISH));
295     }
296 
297     @Test
testGetCapitalizationType()298     public void testGetCapitalizationType() {
299         assertEquals(StringUtils.CAPITALIZE_NONE,
300                 StringUtils.getCapitalizationType("capitalize"));
301         assertEquals(StringUtils.CAPITALIZE_NONE,
302                 StringUtils.getCapitalizationType("cApITalize"));
303         assertEquals(StringUtils.CAPITALIZE_NONE,
304                 StringUtils.getCapitalizationType("capitalizE"));
305         assertEquals(StringUtils.CAPITALIZE_NONE,
306                 StringUtils.getCapitalizationType("__c a piu$@tali56ze"));
307         assertEquals(StringUtils.CAPITALIZE_FIRST,
308                 StringUtils.getCapitalizationType("A__c a piu$@tali56ze"));
309         assertEquals(StringUtils.CAPITALIZE_FIRST,
310                 StringUtils.getCapitalizationType("Capitalize"));
311         assertEquals(StringUtils.CAPITALIZE_FIRST,
312                 StringUtils.getCapitalizationType("     Capitalize"));
313         assertEquals(StringUtils.CAPITALIZE_ALL,
314                 StringUtils.getCapitalizationType("CAPITALIZE"));
315         assertEquals(StringUtils.CAPITALIZE_ALL,
316                 StringUtils.getCapitalizationType("  PI26LIE"));
317         assertEquals(StringUtils.CAPITALIZE_NONE,
318                 StringUtils.getCapitalizationType(""));
319     }
320 
321     @Test
testIsIdenticalAfterUpcaseIsIdenticalAfterDowncase()322     public void testIsIdenticalAfterUpcaseIsIdenticalAfterDowncase() {
323         assertFalse(StringUtils.isIdenticalAfterUpcase("capitalize"));
324         assertTrue(StringUtils.isIdenticalAfterDowncase("capitalize"));
325         assertFalse(StringUtils.isIdenticalAfterUpcase("cApITalize"));
326         assertFalse(StringUtils.isIdenticalAfterDowncase("cApITalize"));
327         assertFalse(StringUtils.isIdenticalAfterUpcase("capitalizE"));
328         assertFalse(StringUtils.isIdenticalAfterDowncase("capitalizE"));
329         assertFalse(StringUtils.isIdenticalAfterUpcase("__c a piu$@tali56ze"));
330         assertTrue(StringUtils.isIdenticalAfterDowncase("__c a piu$@tali56ze"));
331         assertFalse(StringUtils.isIdenticalAfterUpcase("A__c a piu$@tali56ze"));
332         assertFalse(StringUtils.isIdenticalAfterDowncase("A__c a piu$@tali56ze"));
333         assertFalse(StringUtils.isIdenticalAfterUpcase("Capitalize"));
334         assertFalse(StringUtils.isIdenticalAfterDowncase("Capitalize"));
335         assertFalse(StringUtils.isIdenticalAfterUpcase("     Capitalize"));
336         assertFalse(StringUtils.isIdenticalAfterDowncase("     Capitalize"));
337         assertTrue(StringUtils.isIdenticalAfterUpcase("CAPITALIZE"));
338         assertFalse(StringUtils.isIdenticalAfterDowncase("CAPITALIZE"));
339         assertTrue(StringUtils.isIdenticalAfterUpcase("  PI26LIE"));
340         assertFalse(StringUtils.isIdenticalAfterDowncase("  PI26LIE"));
341         assertTrue(StringUtils.isIdenticalAfterUpcase(""));
342         assertTrue(StringUtils.isIdenticalAfterDowncase(""));
343     }
344 
checkCapitalize(final String src, final String dst, final int[] sortedSeparators, final Locale locale)345     private static void checkCapitalize(final String src, final String dst,
346             final int[] sortedSeparators, final Locale locale) {
347         assertEquals(dst, StringUtils.capitalizeEachWord(src, sortedSeparators, locale));
348         assert(src.equals(dst)
349                 == StringUtils.isIdenticalAfterCapitalizeEachWord(src, sortedSeparators));
350     }
351 
352     private static final int[] SPACE = { Constants.CODE_SPACE };
353     private static final int[] SPACE_PERIOD = StringUtils.toSortedCodePointArray(" .");
354     private static final int[] SENTENCE_SEPARATORS =
355             StringUtils.toSortedCodePointArray(" \n.!?*()&");
356     private static final int[] WORD_SEPARATORS = StringUtils.toSortedCodePointArray(" \n.!?*,();&");
357 
358     @Test
testCapitalizeEachWord()359     public void testCapitalizeEachWord() {
360         checkCapitalize("", "", SPACE, Locale.ENGLISH);
361         checkCapitalize("test", "Test", SPACE, Locale.ENGLISH);
362         checkCapitalize("    test", "    Test", SPACE, Locale.ENGLISH);
363         checkCapitalize("Test", "Test", SPACE, Locale.ENGLISH);
364         checkCapitalize("    Test", "    Test", SPACE, Locale.ENGLISH);
365         checkCapitalize(".Test", ".test", SPACE, Locale.ENGLISH);
366         checkCapitalize(".Test", ".Test", SPACE_PERIOD, Locale.ENGLISH);
367         checkCapitalize("test and retest", "Test And Retest", SPACE_PERIOD, Locale.ENGLISH);
368         checkCapitalize("Test and retest", "Test And Retest", SPACE_PERIOD, Locale.ENGLISH);
369         checkCapitalize("Test And Retest", "Test And Retest", SPACE_PERIOD, Locale.ENGLISH);
370         checkCapitalize("Test And.Retest  ", "Test And.Retest  ", SPACE_PERIOD, Locale.ENGLISH);
371         checkCapitalize("Test And.retest  ", "Test And.Retest  ", SPACE_PERIOD, Locale.ENGLISH);
372         checkCapitalize("Test And.retest  ", "Test And.retest  ", SPACE, Locale.ENGLISH);
373         checkCapitalize("Test And.Retest  ", "Test And.retest  ", SPACE, Locale.ENGLISH);
374         checkCapitalize("test and ietest", "Test And İetest", SPACE_PERIOD, new Locale("tr"));
375         checkCapitalize("test and ietest", "Test And Ietest", SPACE_PERIOD, Locale.ENGLISH);
376         checkCapitalize("Test&Retest", "Test&Retest", SENTENCE_SEPARATORS, Locale.ENGLISH);
377         checkCapitalize("Test&retest", "Test&Retest", SENTENCE_SEPARATORS, Locale.ENGLISH);
378         checkCapitalize("test&Retest", "Test&Retest", SENTENCE_SEPARATORS, Locale.ENGLISH);
379         checkCapitalize("rest\nrecreation! And in the end...",
380                 "Rest\nRecreation! And In The End...", WORD_SEPARATORS, Locale.ENGLISH);
381         checkCapitalize("lorem ipsum dolor sit amet", "Lorem Ipsum Dolor Sit Amet",
382                 WORD_SEPARATORS, Locale.ENGLISH);
383         checkCapitalize("Lorem!Ipsum (Dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
384                 WORD_SEPARATORS, Locale.ENGLISH);
385         checkCapitalize("Lorem!Ipsum (dolor) Sit * Amet", "Lorem!Ipsum (Dolor) Sit * Amet",
386                 WORD_SEPARATORS, Locale.ENGLISH);
387     }
388 
389     @Test
testLooksLikeURL()390     public void testLooksLikeURL() {
391         assertTrue(StringUtils.lastPartLooksLikeURL("http://www.google."));
392         assertFalse(StringUtils.lastPartLooksLikeURL("word wo"));
393         assertTrue(StringUtils.lastPartLooksLikeURL("/etc/foo"));
394         assertFalse(StringUtils.lastPartLooksLikeURL("left/right"));
395         assertTrue(StringUtils.lastPartLooksLikeURL("www.goo"));
396         assertTrue(StringUtils.lastPartLooksLikeURL("www."));
397         assertFalse(StringUtils.lastPartLooksLikeURL("U.S.A"));
398         assertFalse(StringUtils.lastPartLooksLikeURL("U.S.A."));
399         assertTrue(StringUtils.lastPartLooksLikeURL("rtsp://foo."));
400         assertTrue(StringUtils.lastPartLooksLikeURL("://"));
401         assertFalse(StringUtils.lastPartLooksLikeURL("abc/"));
402         assertTrue(StringUtils.lastPartLooksLikeURL("abc.def/ghi"));
403         assertFalse(StringUtils.lastPartLooksLikeURL("abc.def"));
404         // TODO: ideally this would not look like a URL, but to keep down the complexity of the
405         // code for now True is acceptable.
406         assertTrue(StringUtils.lastPartLooksLikeURL("abc./def"));
407         // TODO: ideally this would not look like a URL, but to keep down the complexity of the
408         // code for now True is acceptable.
409         assertTrue(StringUtils.lastPartLooksLikeURL(".abc/def"));
410     }
411 
412     @Test
testHexStringUtils()413     public void testHexStringUtils() {
414         final byte[] bytes = new byte[] { (byte)0x01, (byte)0x11, (byte)0x22, (byte)0x33,
415                 (byte)0x55, (byte)0x88, (byte)0xEE };
416         final String bytesStr = StringUtils.byteArrayToHexString(bytes);
417         final byte[] bytes2 = StringUtils.hexStringToByteArray(bytesStr);
418         for (int i = 0; i < bytes.length; ++i) {
419             assertTrue(bytes[i] == bytes2[i]);
420         }
421         final String bytesStr2 = StringUtils.byteArrayToHexString(bytes2);
422         assertTrue(bytesStr.equals(bytesStr2));
423     }
424 
425     @Test
testToCodePointArray()426     public void testToCodePointArray() {
427         final String STR_WITH_SUPPLEMENTARY_CHAR = "abcde\uD861\uDED7fgh\u0000\u2002\u2003\u3000xx";
428         final int[] EXPECTED_RESULT = new int[] { 'a', 'b', 'c', 'd', 'e', 0x286D7, 'f', 'g', 'h',
429                 0, 0x2002, 0x2003, 0x3000, 'x', 'x'};
430         final int[] codePointArray = StringUtils.toCodePointArray(STR_WITH_SUPPLEMENTARY_CHAR, 0,
431                 STR_WITH_SUPPLEMENTARY_CHAR.length());
432         assertEquals("toCodePointArray, size matches", codePointArray.length,
433                 EXPECTED_RESULT.length);
434         for (int i = 0; i < EXPECTED_RESULT.length; ++i) {
435             assertEquals("toCodePointArray position " + i, codePointArray[i], EXPECTED_RESULT[i]);
436         }
437     }
438 
439     @Test
testCopyCodePointsAndReturnCodePointCount()440     public void testCopyCodePointsAndReturnCodePointCount() {
441         final String STR_WITH_SUPPLEMENTARY_CHAR = "AbcDE\uD861\uDED7fGh\u0000\u2002\u3000あx";
442         final int[] EXPECTED_RESULT = new int[] { 'A', 'b', 'c', 'D', 'E', 0x286D7,
443                 'f', 'G', 'h', 0, 0x2002, 0x3000, 'あ', 'x'};
444         final int[] EXPECTED_RESULT_DOWNCASE = new int[] { 'a', 'b', 'c', 'd', 'e', 0x286D7,
445                 'f', 'g', 'h', 0, 0x2002, 0x3000, 'あ', 'x'};
446 
447         int[] codePointArray = new int[50];
448         int codePointCount = StringUtils.copyCodePointsAndReturnCodePointCount(codePointArray,
449                 STR_WITH_SUPPLEMENTARY_CHAR, 0,
450                 STR_WITH_SUPPLEMENTARY_CHAR.length(), false /* downCase */);
451         assertEquals("copyCodePointsAndReturnCodePointCount, size matches", codePointCount,
452                 EXPECTED_RESULT.length);
453         for (int i = 0; i < codePointCount; ++i) {
454             assertEquals("copyCodePointsAndReturnCodePointCount position " + i, codePointArray[i],
455                     EXPECTED_RESULT[i]);
456         }
457 
458         codePointCount = StringUtils.copyCodePointsAndReturnCodePointCount(codePointArray,
459                 STR_WITH_SUPPLEMENTARY_CHAR, 0,
460                 STR_WITH_SUPPLEMENTARY_CHAR.length(), true /* downCase */);
461         assertEquals("copyCodePointsAndReturnCodePointCount downcase, size matches", codePointCount,
462                 EXPECTED_RESULT_DOWNCASE.length);
463         for (int i = 0; i < codePointCount; ++i) {
464             assertEquals("copyCodePointsAndReturnCodePointCount position " + i, codePointArray[i],
465                     EXPECTED_RESULT_DOWNCASE[i]);
466         }
467 
468         final int JAVA_CHAR_COUNT = 8;
469         final int CODEPOINT_COUNT = 7;
470         codePointCount = StringUtils.copyCodePointsAndReturnCodePointCount(codePointArray,
471                 STR_WITH_SUPPLEMENTARY_CHAR, 0, JAVA_CHAR_COUNT, false /* downCase */);
472         assertEquals("copyCodePointsAndReturnCodePointCount, size matches", codePointCount,
473                 CODEPOINT_COUNT);
474         for (int i = 0; i < codePointCount; ++i) {
475             assertEquals("copyCodePointsAndReturnCodePointCount position " + i, codePointArray[i],
476                     EXPECTED_RESULT[i]);
477         }
478 
479         boolean exceptionHappened = false;
480         codePointArray = new int[5];
481         try {
482             codePointCount = StringUtils.copyCodePointsAndReturnCodePointCount(codePointArray,
483                     STR_WITH_SUPPLEMENTARY_CHAR, 0, JAVA_CHAR_COUNT, false /* downCase */);
484         } catch (ArrayIndexOutOfBoundsException e) {
485             exceptionHappened = true;
486         }
487         assertTrue("copyCodePointsAndReturnCodePointCount throws when array is too small",
488                 exceptionHappened);
489     }
490 
491     @Test
testGetTrailingSingleQuotesCount()492     public void testGetTrailingSingleQuotesCount() {
493         assertEquals(0, StringUtils.getTrailingSingleQuotesCount(""));
494         assertEquals(1, StringUtils.getTrailingSingleQuotesCount("'"));
495         assertEquals(5, StringUtils.getTrailingSingleQuotesCount("'''''"));
496         assertEquals(0, StringUtils.getTrailingSingleQuotesCount("a"));
497         assertEquals(0, StringUtils.getTrailingSingleQuotesCount("'this"));
498         assertEquals(1, StringUtils.getTrailingSingleQuotesCount("'word'"));
499         assertEquals(0, StringUtils.getTrailingSingleQuotesCount("I'm"));
500     }
501 }
502