package com.google.common.base; import static org.junit.Assert.*; import java.util.List; import java.util.Map; import java.util.regex.Pattern; import org.junit.Test; public class TestSplitter { @Test public void on_Test() { Splitter sp = Splitter.on(','); List result = sp.splitToList("aber,this,is"); assertEquals(3, result.size()); assertEquals(result.get(0), "aber"); assertEquals(result.get(1), "this"); assertEquals(result.get(2), "is"); sp = Splitter.on(CharMatcher.anyOf(";,")); result = sp.splitToList("aber,this;is"); assertEquals(3, result.size()); assertEquals(result.get(0), "aber"); assertEquals(result.get(1), "this"); assertEquals(result.get(2), "is"); sp = Splitter.on(Pattern.compile("[,]")); result = sp.splitToList("aber,this,is"); assertEquals(3, result.size()); assertEquals(result.get(0), "aber"); assertEquals(result.get(1), "this"); assertEquals(result.get(2), "is"); sp = Splitter.on(","); result = sp.splitToList("aber,this,is"); assertEquals(3, result.size()); assertEquals(result.get(0), "aber"); assertEquals(result.get(1), "this"); assertEquals(result.get(2), "is"); } @Test public void limitTest() { List result = Splitter.on(',').limit(2).splitToList("aber,this,is"); assertEquals(2, result.size()); assertEquals(result.get(0), "aber"); assertEquals(result.get(1), "this,is"); } @Test public void omitEmptyStringsTest() { List result = Splitter.on(',').omitEmptyStrings().splitToList("aber,,is"); assertEquals(2, result.size()); assertEquals(result.get(0), "aber"); assertEquals(result.get(1), "is"); } @Test public void trimResultsTest() { List result = Splitter.on(',').trimResults().splitToList(" aber ,is "); assertEquals(2, result.size()); assertEquals(result.get(0), "aber"); assertEquals(result.get(1), "is"); } @Test public void withKeyValueSeparatorTest() { Map result = Splitter.on(';').withKeyValueSeparator(',').split("aber,is"); assertNotNull(result); assertEquals(1, result.size()); assertEquals(result.get("aber"), "is"); } }