2 * Copyright 2000-2016 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.util.text;
18 import com.intellij.openapi.util.Comparing;
19 import com.intellij.openapi.util.text.StringUtil;
20 import com.intellij.util.LineSeparator;
21 import com.intellij.xml.util.XmlStringUtil;
22 import org.jdom.Verifier;
23 import org.junit.Test;
25 import java.nio.CharBuffer;
28 import static org.junit.Assert.*;
31 * @author Eugene Zhuravlev
34 public class StringUtilTest {
36 public void testTrimLeadingChar() throws Exception {
37 assertEquals("", StringUtil.trimLeading("", ' '));
38 assertEquals("", StringUtil.trimLeading(" ", ' '));
39 assertEquals("", StringUtil.trimLeading(" ", ' '));
40 assertEquals("a ", StringUtil.trimLeading("a ", ' '));
41 assertEquals("a ", StringUtil.trimLeading(" a ", ' '));
45 public void testTrimTrailingChar() throws Exception {
46 assertEquals("", StringUtil.trimTrailing("", ' '));
47 assertEquals("", StringUtil.trimTrailing(" ", ' '));
48 assertEquals("", StringUtil.trimTrailing(" ", ' '));
49 assertEquals(" a", StringUtil.trimTrailing(" a", ' '));
50 assertEquals(" a", StringUtil.trimTrailing(" a ", ' '));
54 public void testToUpperCase() {
55 assertEquals('/', StringUtil.toUpperCase('/'));
56 assertEquals(':', StringUtil.toUpperCase(':'));
57 assertEquals('A', StringUtil.toUpperCase('a'));
58 assertEquals('A', StringUtil.toUpperCase('A'));
59 assertEquals('K', StringUtil.toUpperCase('k'));
60 assertEquals('K', StringUtil.toUpperCase('K'));
62 assertEquals('\u2567', StringUtil.toUpperCase(Character.toLowerCase('\u2567')));
66 public void testToLowerCase() {
67 assertEquals('/', StringUtil.toLowerCase('/'));
68 assertEquals(':', StringUtil.toLowerCase(':'));
69 assertEquals('a', StringUtil.toLowerCase('a'));
70 assertEquals('a', StringUtil.toLowerCase('A'));
71 assertEquals('k', StringUtil.toLowerCase('k'));
72 assertEquals('k', StringUtil.toLowerCase('K'));
74 assertEquals('\u2567', StringUtil.toUpperCase(Character.toLowerCase('\u2567')));
78 public void testIsEmptyOrSpaces() throws Exception {
79 assertTrue(StringUtil.isEmptyOrSpaces(null));
80 assertTrue(StringUtil.isEmptyOrSpaces(""));
81 assertTrue(StringUtil.isEmptyOrSpaces(" "));
83 assertFalse(StringUtil.isEmptyOrSpaces("1"));
84 assertFalse(StringUtil.isEmptyOrSpaces(" 12345 "));
85 assertFalse(StringUtil.isEmptyOrSpaces("test"));
89 public void testSplitWithQuotes() {
90 final List<String> strings = StringUtil.splitHonorQuotes("aaa bbb ccc \"ddd\" \"e\\\"e\\\"e\" ", ' ');
91 assertEquals(5, strings.size());
92 assertEquals("aaa", strings.get(0));
93 assertEquals("bbb", strings.get(1));
94 assertEquals("ccc", strings.get(2));
95 assertEquals("\"ddd\"", strings.get(3));
96 assertEquals("\"e\\\"e\\\"e\"", strings.get(4));
100 public void testUnPluralize() {
101 assertEquals("s", StringUtil.unpluralize("s"));
102 assertEquals("z", StringUtil.unpluralize("zs"));
106 public void testPluralize() {
107 assertEquals("values", StringUtil.pluralize("value"));
108 assertEquals("values", StringUtil.pluralize("values"));
109 assertEquals("indices", StringUtil.pluralize("index"));
110 assertEquals("matrices", StringUtil.pluralize("matrix"));
111 assertEquals("fixes", StringUtil.pluralize("fix"));
112 assertEquals("men", StringUtil.pluralize("man"));
113 assertEquals("media", StringUtil.pluralize("medium"));
114 assertEquals("stashes", StringUtil.pluralize("stash"));
118 public void testStartsWithConcatenation() {
119 assertTrue(StringUtil.startsWithConcatenation("something.with.dot", "something", "."));
120 assertTrue(StringUtil.startsWithConcatenation("something.with.dot", "", "something."));
121 assertTrue(StringUtil.startsWithConcatenation("something.", "something", "."));
122 assertTrue(StringUtil.startsWithConcatenation("something", "something", "", "", ""));
123 assertFalse(StringUtil.startsWithConcatenation("something", "something", "", "", "."));
124 assertFalse(StringUtil.startsWithConcatenation("some", "something", ""));
128 public void testNaturalCompare() {
129 assertEquals(1, StringUtil.naturalCompare("test011", "test10"));
130 assertEquals(1, StringUtil.naturalCompare("test10a", "test010"));
131 final List<String> strings = new ArrayList<>(Arrays.asList("Test99", "tes0", "test0", "testing", "test", "test99", "test011", "test1",
132 "test 3", "test2", "test10a", "test10", "1.2.10.5", "1.2.9.1"));
133 final Comparator<String> c = (o1, o2) -> StringUtil.naturalCompare(o1, o2);
134 Collections.sort(strings, c);
135 assertEquals(Arrays.asList("1.2.9.1", "1.2.10.5", "tes0", "test", "test0", "test1", "test2", "test 3", "test10", "test10a",
136 "test011", "Test99", "test99", "testing"), strings);
137 final List<String> strings2 = new ArrayList<>(Arrays.asList("t1", "t001", "T2", "T002", "T1", "t2"));
138 Collections.sort(strings2, c);
139 assertEquals(Arrays.asList("T1", "t1", "t001", "T2", "t2", "T002"), strings2);
140 assertEquals(1 ,StringUtil.naturalCompare("7403515080361171695", "07403515080361171694"));
141 assertEquals(-14, StringUtil.naturalCompare("_firstField", "myField1"));
143 final List<String> strings3 = new ArrayList<>(
144 Arrays.asList("C148A_InsomniaCure", "C148B_Escape", "C148C_TersePrincess", "C148D_BagOfMice", "C148E_Porcelain"));
145 Collections.sort(strings3, c);
146 assertEquals(Arrays.asList("C148A_InsomniaCure", "C148B_Escape", "C148C_TersePrincess", "C148D_BagOfMice", "C148E_Porcelain"), strings3);
150 public void testFormatLinks() {
151 assertEquals("<a href=\"http://a-b+c\">http://a-b+c</a>", StringUtil.formatLinks("http://a-b+c"));
155 public void testCopyHeapCharBuffer() {
157 CharBuffer buffer = CharBuffer.allocate(s.length());
161 assertNotNull(CharArrayUtil.fromSequenceWithoutCopying(buffer));
162 assertNotNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(0, 5)));
163 //assertNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(0, 4))); // end index is not checked
164 assertNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(1, 5)));
165 assertNull(CharArrayUtil.fromSequenceWithoutCopying(buffer.subSequence(1, 2)));
169 public void testTitleCase() {
170 assertEquals("Couldn't Connect to Debugger", StringUtil.wordsToBeginFromUpperCase("Couldn't connect to debugger"));
171 assertEquals("Let's Make Abbreviations Like I18n, SQL and CSS", StringUtil.wordsToBeginFromUpperCase("Let's make abbreviations like I18n, SQL and CSS"));
175 public void testSentenceCapitalization() {
176 assertEquals("couldn't connect to debugger", StringUtil.wordsToBeginFromLowerCase("Couldn't Connect to Debugger"));
177 assertEquals("let's make abbreviations like I18n, SQL and CSS s SQ sq", StringUtil.wordsToBeginFromLowerCase("Let's Make Abbreviations Like I18n, SQL and CSS S SQ Sq"));
181 public void testEscapeStringCharacters() {
182 assertEquals("\\\"\\n", StringUtil.escapeStringCharacters(3, "\\\"\n", "\"", false, new StringBuilder()).toString());
183 assertEquals("\\\"\\n", StringUtil.escapeStringCharacters(2, "\"\n", "\"", false, new StringBuilder()).toString());
184 assertEquals("\\\\\\\"\\n", StringUtil.escapeStringCharacters(3, "\\\"\n", "\"", true, new StringBuilder()).toString());
188 public void testEscapeSlashes() {
189 assertEquals("\\/", StringUtil.escapeSlashes("/"));
190 assertEquals("foo\\/bar\\foo\\/", StringUtil.escapeSlashes("foo/bar\\foo/"));
192 assertEquals("\\\\\\\\server\\\\share\\\\extension.crx", StringUtil.escapeBackSlashes("\\\\server\\share\\extension.crx"));
196 public void testEscapeQuotes() {
197 assertEquals("\\\"", StringUtil.escapeQuotes("\""));
198 assertEquals("foo\\\"bar'\\\"", StringUtil.escapeQuotes("foo\"bar'\""));
202 public void testUnquote() {
203 assertEquals("", StringUtil.unquoteString(""));
204 assertEquals("\"", StringUtil.unquoteString("\""));
205 assertEquals("", StringUtil.unquoteString("\"\""));
206 assertEquals("\"", StringUtil.unquoteString("\"\"\""));
207 assertEquals("foo", StringUtil.unquoteString("\"foo\""));
208 assertEquals("\"foo", StringUtil.unquoteString("\"foo"));
209 assertEquals("foo\"", StringUtil.unquoteString("foo\""));
210 assertEquals("", StringUtil.unquoteString(""));
211 assertEquals("\'", StringUtil.unquoteString("\'"));
212 assertEquals("", StringUtil.unquoteString("\'\'"));
213 assertEquals("\'", StringUtil.unquoteString("\'\'\'"));
214 assertEquals("foo", StringUtil.unquoteString("\'foo\'"));
215 assertEquals("\'foo", StringUtil.unquoteString("\'foo"));
216 assertEquals("foo\'", StringUtil.unquoteString("foo\'"));
218 assertEquals("\'\"", StringUtil.unquoteString("\'\""));
219 assertEquals("\"\'", StringUtil.unquoteString("\"\'"));
220 assertEquals("\"foo\'", StringUtil.unquoteString("\"foo\'"));
223 @SuppressWarnings("SSBasedInspection")
225 public void testStripQuotesAroundValue() {
226 assertEquals("", StringUtil.stripQuotesAroundValue(""));
227 assertEquals("", StringUtil.stripQuotesAroundValue("'"));
228 assertEquals("", StringUtil.stripQuotesAroundValue("\""));
229 assertEquals("", StringUtil.stripQuotesAroundValue("''"));
230 assertEquals("", StringUtil.stripQuotesAroundValue("\"\""));
231 assertEquals("", StringUtil.stripQuotesAroundValue("'\""));
232 assertEquals("foo", StringUtil.stripQuotesAroundValue("'foo'"));
233 assertEquals("foo", StringUtil.stripQuotesAroundValue("'foo"));
234 assertEquals("foo", StringUtil.stripQuotesAroundValue("foo'"));
235 assertEquals("f'o'o", StringUtil.stripQuotesAroundValue("'f'o'o'"));
236 assertEquals("f\"o'o", StringUtil.stripQuotesAroundValue("\"f\"o'o'"));
237 assertEquals("f\"o'o", StringUtil.stripQuotesAroundValue("f\"o'o"));
238 assertEquals("\"'f\"o'o\"", StringUtil.stripQuotesAroundValue("\"\"'f\"o'o\"\""));
239 assertEquals("''f\"o'o''", StringUtil.stripQuotesAroundValue("'''f\"o'o'''"));
240 assertEquals("foo' 'bar", StringUtil.stripQuotesAroundValue("foo' 'bar"));
244 public void testUnquoteWithQuotationChar() {
245 assertEquals("", StringUtil.unquoteString("", '|'));
246 assertEquals("|", StringUtil.unquoteString("|", '|'));
247 assertEquals("", StringUtil.unquoteString("||", '|'));
248 assertEquals("|", StringUtil.unquoteString("|||", '|'));
249 assertEquals("foo", StringUtil.unquoteString("|foo|", '|'));
250 assertEquals("|foo", StringUtil.unquoteString("|foo", '|'));
251 assertEquals("foo|", StringUtil.unquoteString("foo|", '|'));
255 public void testIsQuotedString() {
256 assertFalse(StringUtil.isQuotedString(""));
257 assertFalse(StringUtil.isQuotedString("'"));
258 assertFalse(StringUtil.isQuotedString("\""));
259 assertTrue(StringUtil.isQuotedString("\"\""));
260 assertTrue(StringUtil.isQuotedString("''"));
261 assertTrue(StringUtil.isQuotedString("'ab'"));
262 assertTrue(StringUtil.isQuotedString("\"foo\""));
266 public void testJoin() {
267 assertEquals("", StringUtil.join(Collections.<String>emptyList(), ","));
268 assertEquals("qqq", StringUtil.join(Collections.singletonList("qqq"), ","));
269 assertEquals("", StringUtil.join(Collections.<String>singletonList(null), ","));
270 assertEquals("a,b", StringUtil.join(Arrays.asList("a", "b"), ","));
271 assertEquals("foo,,bar", StringUtil.join(Arrays.asList("foo", "", "bar"), ","));
272 assertEquals("foo,,bar", StringUtil.join(new String[]{"foo", "", "bar"}, ","));
276 public void testSplitByLineKeepingSeparators() {
277 assertEquals(Collections.singletonList(""), Arrays.asList(StringUtil.splitByLinesKeepSeparators("")));
278 assertEquals(Collections.singletonList("aa"), Arrays.asList(StringUtil.splitByLinesKeepSeparators("aa")));
279 assertEquals(Arrays.asList("\n", "\n", "aa\n", "\n", "bb\n", "cc\n", "\n"),
280 Arrays.asList(StringUtil.splitByLinesKeepSeparators("\n\naa\n\nbb\ncc\n\n")));
282 assertEquals(Arrays.asList("\r", "\r\n", "\r"), Arrays.asList(StringUtil.splitByLinesKeepSeparators("\r\r\n\r")));
283 assertEquals(Arrays.asList("\r\n", "\r", "\r\n"), Arrays.asList(StringUtil.splitByLinesKeepSeparators("\r\n\r\r\n")));
285 assertEquals(Arrays.asList("\n", "\r\n", "\n", "\r\n", "\r", "\r", "aa\r", "bb\r\n", "cc\n", "\r", "dd\n", "\n", "\r\n", "\r"),
286 Arrays.asList(StringUtil.splitByLinesKeepSeparators("\n\r\n\n\r\n\r\raa\rbb\r\ncc\n\rdd\n\n\r\n\r")));
290 public void testReplaceReturnReplacementIfTextEqualsToReplacedText() {
291 String newS = "/tmp";
292 assertSame(StringUtil.replace("$PROJECT_FILE$", "$PROJECT_FILE$".toLowerCase().toUpperCase() /* ensure new String instance */, newS), newS);
296 public void testReplace() {
297 assertEquals("/tmp/filename", StringUtil.replace("$PROJECT_FILE$/filename", "$PROJECT_FILE$", "/tmp"));
301 public void testEqualsIgnoreWhitespaces() {
302 assertTrue(StringUtil.equalsIgnoreWhitespaces(null, null));
303 assertFalse(StringUtil.equalsIgnoreWhitespaces("", null));
305 assertTrue(StringUtil.equalsIgnoreWhitespaces("", ""));
306 assertTrue(StringUtil.equalsIgnoreWhitespaces("\n\t ", ""));
307 assertTrue(StringUtil.equalsIgnoreWhitespaces("", "\t\n \n\t"));
308 assertTrue(StringUtil.equalsIgnoreWhitespaces("\t", "\n"));
310 assertTrue(StringUtil.equalsIgnoreWhitespaces("x", " x"));
311 assertTrue(StringUtil.equalsIgnoreWhitespaces("x", "x "));
312 assertTrue(StringUtil.equalsIgnoreWhitespaces("x\n", "x"));
314 assertTrue(StringUtil.equalsIgnoreWhitespaces("abc", "a\nb\nc\n"));
315 assertTrue(StringUtil.equalsIgnoreWhitespaces("x y x", "x y x"));
316 assertTrue(StringUtil.equalsIgnoreWhitespaces("xyx", "x y x"));
318 assertFalse(StringUtil.equalsIgnoreWhitespaces("x", "\t\n "));
319 assertFalse(StringUtil.equalsIgnoreWhitespaces("", " x "));
320 assertFalse(StringUtil.equalsIgnoreWhitespaces("", "x "));
321 assertFalse(StringUtil.equalsIgnoreWhitespaces("", " x"));
322 assertFalse(StringUtil.equalsIgnoreWhitespaces("xyx", "xxx"));
323 assertFalse(StringUtil.equalsIgnoreWhitespaces("xyx", "xYx"));
327 public void testStringHashCodeIgnoreWhitespaces() {
328 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces("")));
329 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("\n\t "), StringUtil.stringHashCodeIgnoreWhitespaces("")));
330 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces("\t\n \n\t")));
331 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("\t"), StringUtil.stringHashCodeIgnoreWhitespaces("\n")));
333 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x"), StringUtil.stringHashCodeIgnoreWhitespaces(" x")));
334 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x"), StringUtil.stringHashCodeIgnoreWhitespaces("x ")));
335 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x\n"), StringUtil.stringHashCodeIgnoreWhitespaces("x")));
337 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("abc"), StringUtil.stringHashCodeIgnoreWhitespaces("a\nb\nc\n")));
338 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x y x"), StringUtil.stringHashCodeIgnoreWhitespaces("x y x")));
339 assertTrue(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("xyx"), StringUtil.stringHashCodeIgnoreWhitespaces("x y x")));
341 assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("x"), StringUtil.stringHashCodeIgnoreWhitespaces("\t\n ")));
342 assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces(" x ")));
343 assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces("x ")));
344 assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces(""), StringUtil.stringHashCodeIgnoreWhitespaces(" x")));
345 assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("xyx"), StringUtil.stringHashCodeIgnoreWhitespaces("xxx")));
346 assertFalse(Comparing.equal(StringUtil.stringHashCodeIgnoreWhitespaces("xyx"), StringUtil.stringHashCodeIgnoreWhitespaces("xYx")));
350 public void testContains() {
351 assertTrue(StringUtil.contains("1", "1"));
352 assertFalse(StringUtil.contains("1", "12"));
353 assertTrue(StringUtil.contains("12", "1"));
354 assertTrue(StringUtil.contains("12", "2"));
358 public void testDetectSeparators() {
359 assertEquals(null, StringUtil.detectSeparators(""));
360 assertEquals(null, StringUtil.detectSeparators("asd"));
361 assertEquals(null, StringUtil.detectSeparators("asd\t"));
363 assertEquals(LineSeparator.LF, StringUtil.detectSeparators("asd\n"));
364 assertEquals(LineSeparator.LF, StringUtil.detectSeparators("asd\nads\r"));
365 assertEquals(LineSeparator.LF, StringUtil.detectSeparators("asd\nads\n"));
367 assertEquals(LineSeparator.CR, StringUtil.detectSeparators("asd\r"));
368 assertEquals(LineSeparator.CR, StringUtil.detectSeparators("asd\rads\r"));
369 assertEquals(LineSeparator.CR, StringUtil.detectSeparators("asd\rads\n"));
371 assertEquals(LineSeparator.CRLF, StringUtil.detectSeparators("asd\r\n"));
372 assertEquals(LineSeparator.CRLF, StringUtil.detectSeparators("asd\r\nads\r"));
373 assertEquals(LineSeparator.CRLF, StringUtil.detectSeparators("asd\r\nads\n"));
377 public void testFindStartingLineSeparator() {
378 assertEquals(null, StringUtil.findStartingLineSeparator("", -1));
379 assertEquals(null, StringUtil.findStartingLineSeparator("", 0));
380 assertEquals(null, StringUtil.findStartingLineSeparator("", 1));
381 assertEquals(null, StringUtil.findStartingLineSeparator("\nHello", -1));
382 assertEquals(null, StringUtil.findStartingLineSeparator("\nHello", 1));
383 assertEquals(null, StringUtil.findStartingLineSeparator("\nH\rel\nlo", 6));
385 assertEquals(LineSeparator.LF, StringUtil.findStartingLineSeparator("\nHello", 0));
386 assertEquals(LineSeparator.LF, StringUtil.findStartingLineSeparator("\nH\rel\nlo", 5));
387 assertEquals(LineSeparator.LF, StringUtil.findStartingLineSeparator("Hello\n", 5));
389 assertEquals(LineSeparator.CR, StringUtil.findStartingLineSeparator("\rH\r\nello", 0));
390 assertEquals(LineSeparator.CR, StringUtil.findStartingLineSeparator("Hello\r", 5));
391 assertEquals(LineSeparator.CR, StringUtil.findStartingLineSeparator("Hello\b\r", 6));
393 assertEquals(LineSeparator.CRLF, StringUtil.findStartingLineSeparator("\rH\r\nello", 2));
394 assertEquals(LineSeparator.CRLF, StringUtil.findStartingLineSeparator("\r\nH\r\nello", 0));
395 assertEquals(LineSeparator.CRLF, StringUtil.findStartingLineSeparator("\r\nH\r\nello\r\n", 9));
399 public void testFormatFileSize() {
400 assertEquals("0B", StringUtil.formatFileSize(0));
401 assertEquals("1B", StringUtil.formatFileSize(1));
402 assertEquals("2.15G", StringUtil.formatFileSize(Integer.MAX_VALUE));
403 assertEquals("9.22E", StringUtil.formatFileSize(Long.MAX_VALUE));
405 assertEquals("60.10K", StringUtil.formatFileSize(60100));
407 assertEquals("1.23K", StringUtil.formatFileSize(1234));
408 assertEquals("12.35K", StringUtil.formatFileSize(12345));
409 assertEquals("123.46K", StringUtil.formatFileSize(123456));
410 assertEquals("1.23M", StringUtil.formatFileSize(1234567));
411 assertEquals("12.35M", StringUtil.formatFileSize(12345678));
412 assertEquals("123.46M", StringUtil.formatFileSize(123456789));
413 assertEquals("1.23G", StringUtil.formatFileSize(1234567890));
417 public void testFormatDuration() {
418 assertEquals("0ms", StringUtil.formatDuration(0));
419 assertEquals("1ms", StringUtil.formatDuration(1));
420 assertEquals("3w 3d 20h 31m 23s 647ms", StringUtil.formatDuration(Integer.MAX_VALUE));
421 assertEquals("31ep 7714ml 2c 59yr 5mo 0w 3d 7h 12m 55s 807ms", StringUtil.formatDuration(Long.MAX_VALUE));
423 assertEquals("1m 0s 100ms", StringUtil.formatDuration(60100));
425 assertEquals("1s 234ms", StringUtil.formatDuration(1234));
426 assertEquals("12s 345ms", StringUtil.formatDuration(12345));
427 assertEquals("2m 3s 456ms", StringUtil.formatDuration(123456));
428 assertEquals("20m 34s 567ms", StringUtil.formatDuration(1234567));
429 assertEquals("3h 25m 45s 678ms", StringUtil.formatDuration(12345678));
430 assertEquals("1d 10h 17m 36s 789ms", StringUtil.formatDuration(123456789));
431 assertEquals("2w 0d 6h 56m 7s 890ms", StringUtil.formatDuration(1234567890));
435 public void testXmlWrapInCDATA() {
436 assertEquals("<![CDATA[abc]]>", XmlStringUtil.wrapInCDATA("abc"));
437 assertEquals("<![CDATA[abc]]]><![CDATA[]>]]>", XmlStringUtil.wrapInCDATA("abc]]>"));
438 assertEquals("<![CDATA[abc]]]><![CDATA[]>def]]>", XmlStringUtil.wrapInCDATA("abc]]>def"));
439 assertEquals("<![CDATA[123<![CDATA[wow<&>]]]><![CDATA[]>]]]><![CDATA[]><![CDATA[123]]>", XmlStringUtil.wrapInCDATA("123<![CDATA[wow<&>]]>]]><![CDATA[123"));
443 public void testGetPackageName() {
444 assertEquals("java.lang", StringUtil.getPackageName("java.lang.String"));
445 assertEquals("java.util.Map", StringUtil.getPackageName("java.util.Map.Entry"));
446 assertEquals("Map", StringUtil.getPackageName("Map.Entry"));
447 assertEquals("", StringUtil.getPackageName("Number"));
450 @SuppressWarnings("SpellCheckingInspection")
452 public void testIndexOf_1() {
453 char[] chars = new char[]{'a','b','c','d','a','b','c','d','A','B','C','D'};
454 assertEquals(2, StringUtil.indexOf(chars, 'c', 0, 12, false));
455 assertEquals(2, StringUtil.indexOf(chars, 'C', 0, 12, false));
456 assertEquals(10, StringUtil.indexOf(chars, 'C', 0, 12, true));
457 assertEquals(2, StringUtil.indexOf(chars, 'c', -42, 99, false));
460 @SuppressWarnings("SpellCheckingInspection")
462 public void testIndexOf_2() {
463 assertEquals(1, StringUtil.indexOf("axaxa", 'x', 0, 5));
464 assertEquals(2, StringUtil.indexOf("abcd", 'c', -42, 99));
467 @SuppressWarnings("SpellCheckingInspection")
469 public void testIndexOf_3() {
470 assertEquals(1, StringUtil.indexOf("axaXa", 'x', 0, 5, false));
471 assertEquals(3, StringUtil.indexOf("axaXa", 'X', 0, 5, true));
472 assertEquals(2, StringUtil.indexOf("abcd", 'c', -42, 99, false));
475 @SuppressWarnings("SpellCheckingInspection")
477 public void testIndexOfAny() {
478 assertEquals(1, StringUtil.indexOfAny("axa", "x", 0, 5));
479 assertEquals(1, StringUtil.indexOfAny("axa", "zx", 0, 5));
480 assertEquals(2, StringUtil.indexOfAny("abcd", "c", -42, 99));
483 @SuppressWarnings("SpellCheckingInspection")
485 public void testLastIndexOf() {
486 assertEquals(1, StringUtil.lastIndexOf("axaxa", 'x', 0, 2));
487 assertEquals(1, StringUtil.lastIndexOf("axaxa", 'x', 0, 3));
488 assertEquals(3, StringUtil.lastIndexOf("axaxa", 'x', 0, 5));
489 assertEquals(2, StringUtil.lastIndexOf("abcd", 'c', -42, 99)); // #IDEA-144968
493 public void testEscapingIllegalXmlChars() {
494 for (String s : new String[]{"ab\n\0\r\tde", "\\abc\1\2\3\uFFFFdef"}) {
495 String escapedText = XmlStringUtil.escapeIllegalXmlChars(s);
496 assertNull(Verifier.checkCharacterData(escapedText));
497 assertEquals(s, XmlStringUtil.unescapeIllegalXmlChars(escapedText));
502 public void testCountChars() {
503 assertEquals(0, StringUtil.countChars("abcdefgh", 'x'));
504 assertEquals(1, StringUtil.countChars("abcdefgh", 'd'));
505 assertEquals(5, StringUtil.countChars("abcddddefghd", 'd'));
506 assertEquals(4, StringUtil.countChars("abcddddefghd", 'd', 4, false));
507 assertEquals(3, StringUtil.countChars("abcddddefghd", 'd', 4, true));
508 assertEquals(2, StringUtil.countChars("abcddddefghd", 'd', 4, 6, false));