Merge pull request #58 from Doha2012/master

Modify class GuavaStringTest
This commit is contained in:
Eugen 2014-10-19 17:16:24 +03:00
commit 9b0dfc44a1
1 changed files with 31 additions and 4 deletions

View File

@ -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<String> 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<String> 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<String> 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<String> 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 {
}
}