diff --git a/guava/src/test/java/org/baeldung/guava/GuavaStringTest.java b/guava/src/test/java/org/baeldung/guava/GuavaStringTest.java index 05e6f48d95..0206530c47 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaStringTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaStringTest.java @@ -1,14 +1,12 @@ package org.baeldung.guava; import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; @@ -29,7 +27,7 @@ public class GuavaStringTest { final List names = Lists.newArrayList("John", "Jane", "Adam", "Tom"); final String result = Joiner.on(",").join(names); - assertThat(Arrays.asList(result.split(",")), containsInAnyOrder("John", "Jane", "Adam", "Tom")); + assertEquals(result, "John,Jane,Adam,Tom"); } @Test @@ -37,7 +35,7 @@ public class GuavaStringTest { final List names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom"); final String result = Joiner.on(",").skipNulls().join(names); - assertThat(Arrays.asList(result.split(",")), containsInAnyOrder("John", "Jane", "Adam", "Tom")); + assertEquals(result, "John,Jane,Adam,Tom"); } @Test @@ -67,6 +65,14 @@ public class GuavaStringTest { assertThat(result, containsString("apple-banana-orange")); } + @Test + public void whenUseForNull_thenUsed() { + final List names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom"); + final String result = Joiner.on(",").useForNull("nameless").join(names); + + assertEquals(result, "John,nameless,Jane,Adam,Tom"); + } + @Test public void whenCreateListFromString_thenCreated() { final String input = "apple - banana - orange"; @@ -100,6 +106,15 @@ public class GuavaStringTest { assertThat(result, contains("Hel", "lo ", "wor", "ld")); } + @Test + public void whenLimitSplitting_thenLimited() { + final String input = "a,b,c,d,e"; + final List result = Splitter.on(",").limit(4).splitToList(input); + + assertEquals(4, result.size()); + assertThat(result, contains("a", "b", "c", "d,e")); + } + @Test public void whenRemoveSpecialCharacters_thenRemoved(){ final String input = "H*el.lo,}12"; @@ -160,6 +175,17 @@ public class GuavaStringTest { assertEquals("hel-lo", result); } + @Test + public void whenReplaceFromString_thenReplaced() { + final String input = "apple-banana."; + + String result = CharMatcher.anyOf("-.").replaceFrom(input, '!'); + assertEquals("apple!banana!", result); + + result = CharMatcher.is('-').replaceFrom(input, " and "); + assertEquals("apple and banana.", result); + } + @Test public void whenCountCharInString_thenCorrect() { final String input = "a, c, z, 1, 2"; @@ -172,5 +198,6 @@ public class GuavaStringTest { } + }