11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.base; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static org.junit.contrib.truth.Truth.ASSERT; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtIncompatible; 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.ImmutableList; 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.collect.ImmutableMap; 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.testing.NullPointerTester; 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport junit.framework.TestCase; 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collection; 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Iterator; 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.List; 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Map; 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.regex.Pattern; 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Julien Silland 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(emulated = true) 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic class SplitterTest extends TestCase { 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final Splitter COMMA_SPLITTER = Splitter.on(','); 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testSplitNullString() { 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert COMMA_SPLITTER.split(null); 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (NullPointerException expected) { 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSimpleSplit() { 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "a,b,c"; 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = COMMA_SPLITTER.split(simple); 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c"); 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSimpleSplitWithNoDelimiter() { 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "a,b,c"; 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on('.').split(simple); 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a,b,c"); 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitWithDoubleDelimiter() { 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a,,b,c"; 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = COMMA_SPLITTER.split(doubled); 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", "b", "c"); 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitWithDoubleDelimiterAndSpace() { 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a,, b,c"; 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = COMMA_SPLITTER.split(doubled); 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", " b", "c"); 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitWithTrailingDelimiter() { 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String trailing = "a,b,c,"; 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = COMMA_SPLITTER.split(trailing); 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c", ""); 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitWithLeadingDelimiter() { 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String leading = ",a,b,c"; 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = COMMA_SPLITTER.split(leading); 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("", "a", "b", "c"); 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitWithMulitpleLetters() { 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> testCharacteringMotto = Splitter.on('-').split( 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "Testing-rocks-Debugging-sucks"); 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(testCharacteringMotto).hasContentsInOrder( 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "Testing", "rocks", "Debugging", "sucks"); 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitWithMatcherDelimiter() { 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> testCharacteringMotto = Splitter 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .on(CharMatcher.WHITESPACE) 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split("Testing\nrocks\tDebugging sucks"); 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(testCharacteringMotto).hasContentsInOrder( 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "Testing", "rocks", "Debugging", "sucks"); 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitWithDoubleDelimiterOmitEmptyStrings() { 1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a..b.c"; 1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on('.') 1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .omitEmptyStrings().split(doubled); 1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c"); 1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitEmptyToken() { 1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String emptyToken = "a. .c"; 1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on('.').trimResults() 1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split(emptyToken); 1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", "c"); 1141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitEmptyTokenOmitEmptyStrings() { 1171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String emptyToken = "a. .c"; 1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on('.') 1191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .omitEmptyStrings().trimResults().split(emptyToken); 1201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "c"); 1211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitOnEmptyString() { 1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> nothing = Splitter.on('.').split(""); 1251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(nothing).hasContentsInOrder(""); 1261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitOnEmptyStringOmitEmptyStrings() { 1291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(Splitter.on('.').omitEmptyStrings().split("")).isEmpty(); 1301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitOnOnlyDelimiter() { 1331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> blankblank = Splitter.on('.').split("."); 1341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(blankblank).hasContentsInOrder("", ""); 1351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitOnOnlyDelimitersOmitEmptyStrings() { 1381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> empty = Splitter.on('.').omitEmptyStrings().split("..."); 1391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(empty); 1401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testCharacterSplitWithTrim() { 1431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String jacksons = "arfo(Marlon)aorf, (Michael)orfa, afro(Jackie)orfa, " 1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert + "ofar(Jemaine), aff(Tito)"; 1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> family = COMMA_SPLITTER 1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .trimResults(CharMatcher.anyOf("afro").or(CharMatcher.WHITESPACE)) 1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split(jacksons); 1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(family).hasContentsInOrder( 1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "(Marlon)", "(Michael)", "(Jackie)", "(Jemaine)", "(Tito)"); 1501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSimpleSplit() { 1531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "a,b,c"; 1541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(",").split(simple); 1551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c"); 1561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSimpleSplitWithNoDelimiter() { 1591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "a,b,c"; 1601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(".").split(simple); 1611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a,b,c"); 1621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithDoubleDelimiter() { 1651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a,,b,c"; 1661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(",").split(doubled); 1671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", "b", "c"); 1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithDoubleDelimiterAndSpace() { 1711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a,, b,c"; 1721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(",").split(doubled); 1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", " b", "c"); 1741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithTrailingDelimiter() { 1771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String trailing = "a,b,c,"; 1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(",").split(trailing); 1791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c", ""); 1801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithLeadingDelimiter() { 1831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String leading = ",a,b,c"; 1841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(",").split(leading); 1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("", "a", "b", "c"); 1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithMultipleLetters() { 1891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> testStringingMotto = Splitter.on("-").split( 1901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "Testing-rocks-Debugging-sucks"); 1911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(testStringingMotto).hasContentsInOrder( 1921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "Testing", "rocks", "Debugging", "sucks"); 1931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithDoubleDelimiterOmitEmptyStrings() { 1961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a..b.c"; 1971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(".") 1981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .omitEmptyStrings().split(doubled); 1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c"); 2001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitEmptyToken() { 2031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String emptyToken = "a. .c"; 2041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(".").trimResults() 2051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split(emptyToken); 2061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", "c"); 2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitEmptyTokenOmitEmptyStrings() { 2101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String emptyToken = "a. .c"; 2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(".") 2121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .omitEmptyStrings().trimResults().split(emptyToken); 2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "c"); 2141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithLongDelimiter() { 2171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String longDelimiter = "a, b, c"; 2181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(", ").split(longDelimiter); 2191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c"); 2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithLongLeadingDelimiter() { 2231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String longDelimiter = ", a, b, c"; 2241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(", ").split(longDelimiter); 2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("", "a", "b", "c"); 2261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithLongTrailingDelimiter() { 2291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String longDelimiter = "a, b, c, "; 2301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(", ").split(longDelimiter); 2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c", ""); 2321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithDelimiterSubstringInValue() { 2351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String fourCommasAndFourSpaces = ",,,, "; 2361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> threeCommasThenThreeSpaces = Splitter.on(", ").split( 2371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fourCommasAndFourSpaces); 2381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(threeCommasThenThreeSpaces).hasContentsInOrder(",,,", " "); 2391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithEmptyString() { 2421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 2431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Splitter.on(""); 2441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 2451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IllegalArgumentException expected) { 2461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitOnEmptyString() { 2501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> notMuch = Splitter.on(".").split(""); 2511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(notMuch).hasContentsInOrder(""); 2521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitOnEmptyStringOmitEmptyString() { 2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(Splitter.on(".").omitEmptyStrings().split("")).isEmpty(); 2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitOnOnlyDelimiter() { 2591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> blankblank = Splitter.on(".").split("."); 2601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(blankblank).hasContentsInOrder("", ""); 2611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitOnOnlyDelimitersOmitEmptyStrings() { 2641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> empty = Splitter.on(".").omitEmptyStrings().split("..."); 2651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(empty).isEmpty(); 2661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testStringSplitWithTrim() { 2691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String jacksons = "arfo(Marlon)aorf, (Michael)orfa, afro(Jackie)orfa, " 2701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert + "ofar(Jemaine), aff(Tito)"; 2711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> family = Splitter.on(",") 2721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .trimResults(CharMatcher.anyOf("afro").or(CharMatcher.WHITESPACE)) 2731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split(jacksons); 2741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(family).hasContentsInOrder( 2751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "(Marlon)", "(Michael)", "(Jackie)", "(Jemaine)", "(Tito)"); 2761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Splitter.onPattern") 2791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSimpleSplit() { 2801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "a,b,c"; 2811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.onPattern(",").split(simple); 2821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c"); 2831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Splitter.onPattern") 2861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSimpleSplitWithNoDelimiter() { 2871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "a,b,c"; 2881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.onPattern("foo").split(simple); 2891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a,b,c"); 2901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Splitter.onPattern") 2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithDoubleDelimiter() { 2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a,,b,c"; 2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.onPattern(",").split(doubled); 2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", "b", "c"); 2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Splitter.onPattern") 3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithDoubleDelimiterAndSpace() { 3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a,, b,c"; 3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.onPattern(",").split(doubled); 3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", " b", "c"); 3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Splitter.onPattern") 3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithTrailingDelimiter() { 3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String trailing = "a,b,c,"; 3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.onPattern(",").split(trailing); 3101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c", ""); 3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Splitter.onPattern") 3141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithLeadingDelimiter() { 3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String leading = ",a,b,c"; 3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.onPattern(",").split(leading); 3171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("", "a", "b", "c"); 3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO(kevinb): the name of this method suggests it might not actually be testing what it 3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // intends to be testing? 3221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("Splitter.onPattern") 3231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithMultipleLetters() { 3241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> testPatterningMotto = Splitter.onPattern("-").split( 3251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "Testing-rocks-Debugging-sucks"); 3261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(testPatterningMotto).hasContentsInOrder("Testing", "rocks", "Debugging", "sucks"); 3271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static Pattern literalDotPattern() { 3311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return Pattern.compile("\\."); 3321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithDoubleDelimiterOmitEmptyStrings() { 3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String doubled = "a..b.c"; 3371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(literalDotPattern()) 3381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .omitEmptyStrings().split(doubled); 3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c"); 3401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitEmptyToken() { 3441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String emptyToken = "a. .c"; 3451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(literalDotPattern()).trimResults().split(emptyToken); 3461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "", "c"); 3471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitEmptyTokenOmitEmptyStrings() { 3511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String emptyToken = "a. .c"; 3521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(literalDotPattern()) 3531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .omitEmptyStrings().trimResults().split(emptyToken); 3541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "c"); 3551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitOnOnlyDelimiter() { 3591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> blankblank = Splitter.on(literalDotPattern()).split("."); 3601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(blankblank).hasContentsInOrder("", ""); 3621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitOnOnlyDelimitersOmitEmptyStrings() { 3661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> empty = Splitter.on(literalDotPattern()).omitEmptyStrings() 3671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split("..."); 3681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(empty).isEmpty(); 3691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitMatchingIsGreedy() { 3731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String longDelimiter = "a, b, c"; 3741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(Pattern.compile(",\\s*")) 3751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split(longDelimiter); 3761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c"); 3771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithLongLeadingDelimiter() { 3811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String longDelimiter = ", a, b, c"; 3821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(Pattern.compile(", ")) 3831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split(longDelimiter); 3841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("", "a", "b", "c"); 3851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithLongTrailingDelimiter() { 3891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String longDelimiter = "a, b, c/ "; 3901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.on(Pattern.compile("[,/]\\s")) 3911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split(longDelimiter); 3921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c", ""); 3931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 3941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 3951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 3961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitInvalidPattern() { 3971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 3981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Splitter.on(Pattern.compile("a*")); 3991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 4001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IllegalArgumentException expected) { 4011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 4051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testPatternSplitWithTrim() { 4061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String jacksons = "arfo(Marlon)aorf, (Michael)orfa, afro(Jackie)orfa, " 4071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert + "ofar(Jemaine), aff(Tito)"; 4081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> family = Splitter.on(Pattern.compile(",")) 4091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .trimResults(CharMatcher.anyOf("afro").or(CharMatcher.WHITESPACE)) 4101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split(jacksons); 4111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(family).hasContentsInOrder( 4121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "(Marlon)", "(Michael)", "(Jackie)", "(Jemaine)", "(Tito)"); 4131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testSplitterIterableIsUnmodifiable_char() { 4161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertIteratorIsUnmodifiable(COMMA_SPLITTER.split("a,b").iterator()); 4171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testSplitterIterableIsUnmodifiable_string() { 4201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertIteratorIsUnmodifiable(Splitter.on(",").split("a,b").iterator()); 4211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 4241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testSplitterIterableIsUnmodifiable_pattern() { 4251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertIteratorIsUnmodifiable( 4261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Splitter.on(Pattern.compile(",")).split("a,b").iterator()); 4271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private void assertIteratorIsUnmodifiable(Iterator<?> iterator) { 4301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator.next(); 4311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 4321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator.remove(); 4331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 4341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (UnsupportedOperationException expected) { 4351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testSplitterIterableIsLazy_char() { 4391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertSplitterIterableIsLazy(COMMA_SPLITTER); 4401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testSplitterIterableIsLazy_string() { 4431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertSplitterIterableIsLazy(Splitter.on(",")); 4441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("java.util.regex.Pattern") 4471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testSplitterIterableIsLazy_pattern() { 4481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertSplitterIterableIsLazy(Splitter.onPattern(",")); 4491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 4521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * This test really pushes the boundaries of what we support. In general the 4531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * splitter's behaviour is not well defined if the char sequence it's 4541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * splitting is mutated during iteration. 4551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 4561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private void assertSplitterIterableIsLazy(Splitter splitter) { 4571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert StringBuilder builder = new StringBuilder(); 4581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterator<String> iterator = splitter.split(builder).iterator(); 4591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builder.append("A,"); 4611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("A", iterator.next()); 4621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builder.append("B,"); 4631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("B", iterator.next()); 4641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert builder.append("C"); 4651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals("C", iterator.next()); 4661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertFalse(iterator.hasNext()); 4671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSimpleSplit() { 4701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "abcde"; 4711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(2).split(simple); 4721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("ab", "cd", "e"); 4731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSplitEqualChunkLength() { 4761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "abcdef"; 4771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(2).split(simple); 4781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("ab", "cd", "ef"); 4791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSplitOnlyOneChunk() { 4821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "abc"; 4831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(3).split(simple); 4841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("abc"); 4851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSplitSmallerString() { 4881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "ab"; 4891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(3).split(simple); 4901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("ab"); 4911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSplitEmptyString() { 4941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = ""; 4951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(3).split(simple); 4961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder(""); 4971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 4981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 4991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSplitEmptyStringWithOmitEmptyStrings() { 5001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(Splitter.fixedLength(3).omitEmptyStrings().split("")).isEmpty(); 5011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSplitIntoChars() { 5041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "abcd"; 5051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(1).split(simple); 5061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c", "d"); 5071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSplitZeroChunkLen() { 5101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 5111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Splitter.fixedLength(0); 5121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 5131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IllegalArgumentException expected) { 5141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAtEachSplitNegativeChunkLen() { 5181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 5191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Splitter.fixedLength(-1); 5201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 5211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IllegalArgumentException expected) { 5221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitLarge() { 5261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "abcd"; 5271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(1).limit(100).split(simple); 5281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "b", "c", "d"); 5291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitOne() { 5321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "abcd"; 5331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(1).limit(1).split(simple); 5341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("abcd"); 5351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitFixedLength() { 5381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "abcd"; 5391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> letters = Splitter.fixedLength(1).limit(2).split(simple); 5401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(letters).hasContentsInOrder("a", "bcd"); 5411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitSeparator() { 5441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String simple = "a,b,c,d"; 5451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(2).split(simple); 5461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder("a", "b,c,d"); 5471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparators() { 5501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = "a,,,b,,c,d"; 5511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(2).split(text); 5521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder("a", ",,b,,c,d"); 5531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparatorsOmitEmpty() { 5561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = "a,,,b,,c,d"; 5571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(2).omitEmptyStrings().split(text); 5581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder("a", "b,,c,d"); 5591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparatorsOmitEmpty3() { 5621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = "a,,,b,,c,d"; 5631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(3).omitEmptyStrings().split(text); 5641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder("a", "b", "c,d"); 5651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparatorsTrim() { 5681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = ",,a,, , b ,, c,d "; 5691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(2).omitEmptyStrings().trimResults().split(text); 5701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder("a", "b ,, c,d"); 5711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparatorsTrim3() { 5741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = ",,a,, , b ,, c,d "; 5751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(3).omitEmptyStrings().trimResults().split(text); 5761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder("a", "b", "c,d"); 5771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparatorsTrim1() { 5801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = ",,a,, , b ,, c,d "; 5811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(1).omitEmptyStrings().trimResults().split(text); 5821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder("a,, , b ,, c,d"); 5831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparatorsTrim1NoOmit() { 5861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = ",,a,, , b ,, c,d "; 5871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(1).trimResults().split(text); 5881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder(",,a,, , b ,, c,d"); 5891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparatorsTrim1Empty() { 5921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = ""; 5931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.limit(1).split(text); 5941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).hasContentsInOrder(""); 5951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 5961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 5971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testLimitExtraSeparatorsTrim1EmptyOmit() { 5981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert String text = ""; 5991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Iterable<String> items = COMMA_SPLITTER.omitEmptyStrings().limit(1).split(text); 6001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(items).isEmpty(); 6011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testInvalidZeroLimit() { 6041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 6051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert COMMA_SPLITTER.limit(0); 6061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 6071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IllegalArgumentException expected) { 6081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @GwtIncompatible("NullPointerTester") 6121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testNullPointers() throws Exception { 6131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert NullPointerTester tester = new NullPointerTester(); 6141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert tester.testAllPublicStaticMethods(Splitter.class); 6151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert tester.testAllPublicInstanceMethods(Splitter.on(",")); 6161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert tester.testAllPublicInstanceMethods(Splitter.on(",").trimResults()); 6171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static <E> List<E> asList(Collection<E> collection){ 6201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return ImmutableList.copyOf(collection); 6211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_trimmedBoth() { 6241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Map<String, String> m = COMMA_SPLITTER 6251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .trimResults() 6261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .withKeyValueSeparator(Splitter.on(':').trimResults()) 6271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split("boy : tom , girl: tina , cat : kitty , dog: tommy "); 6281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap<String, String> expected = 6291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy"); 6301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m).isEqualTo(expected); 6311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(asList(m.entrySet())).is(asList(expected.entrySet())); 6321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_trimmedEntries() { 6351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Map<String, String> m = COMMA_SPLITTER 6361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .trimResults() 6371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .withKeyValueSeparator(":") 6381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split("boy : tom , girl: tina , cat : kitty , dog: tommy "); 6391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap<String, String> expected = 6401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap.of("boy ", " tom", "girl", " tina", "cat ", " kitty", "dog", " tommy"); 6411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m).isEqualTo(expected); 6431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(asList(m.entrySet())).is(asList(expected.entrySet())); 6441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_trimmedKeyValue() { 6471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Map<String, String> m = 6481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert COMMA_SPLITTER.withKeyValueSeparator(Splitter.on(':').trimResults()).split( 6491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert "boy : tom , girl: tina , cat : kitty , dog: tommy "); 6501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap<String, String> expected = 6511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy"); 6521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m).isEqualTo(expected); 6531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(asList(m.entrySet())).is(asList(expected.entrySet())); 6541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_notTrimmed() { 6571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Map<String, String> m = COMMA_SPLITTER.withKeyValueSeparator(":").split( 6581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert " boy:tom , girl: tina , cat :kitty , dog: tommy "); 6591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap<String, String> expected = 6601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap.of(" boy", "tom ", " girl", " tina ", " cat ", "kitty ", " dog", " tommy "); 6611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m).isEqualTo(expected); 6621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(asList(m.entrySet())).is(asList(expected.entrySet())); 6631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_multiCharacterSeparator() { 6661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // try different delimiters. 6671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Map<String, String> m = Splitter 6681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .on(",") 6691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .withKeyValueSeparator(":^&") 6701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split("boy:^&tom,girl:^&tina,cat:^&kitty,dog:^&tommy"); 6711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap<String, String> expected = 6721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy"); 6731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m).isEqualTo(expected); 6751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(asList(m.entrySet())).is(asList(expected.entrySet())); 6761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_emptySeparator() { 6791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 6801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert COMMA_SPLITTER.withKeyValueSeparator(""); 6811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 6821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IllegalArgumentException expected) { 6831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_malformedEntry() { 6871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 6881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert COMMA_SPLITTER.withKeyValueSeparator("=").split("a=1,b,c=2"); 6891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 6901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch(IllegalArgumentException expected) { 6911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 6931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_orderedResults() { 6951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Map<String, String> m = Splitter.on(",") 6961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .withKeyValueSeparator(":") 6971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split("boy:tom,girl:tina,cat:kitty,dog:tommy"); 6981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 6991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m.keySet()).hasContentsInOrder("boy", "girl", "cat", "dog"); 7001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m).isEqualTo( 7011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy")); 7021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // try in a different order 7041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert m = Splitter.on(",") 7051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .withKeyValueSeparator(":") 7061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert .split("girl:tina,boy:tom,dog:tommy,cat:kitty"); 7071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m.keySet()).hasContentsInOrder("girl", "boy", "dog", "cat"); 7091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ASSERT.that(m).isEqualTo( 7101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ImmutableMap.of("boy", "tom", "girl", "tina", "cat", "kitty", "dog", "tommy")); 7111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 7131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testMapSplitter_duplicateKeys() { 7141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 7151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert Splitter.on(",").withKeyValueSeparator(":").split("a:1,b:2,a:3"); 7161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert fail(); 7171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } catch (IllegalArgumentException expected) { 7181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 7201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 721