添加Maven依賴html
JoinerTestjava
import com.google.common.base.Joiner; import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; public class JoinerTest { private static final List<String> stringList = new ArrayList<String>(); static { stringList.add("a"); stringList.add("b"); } /** * 基本用法 * * @see Joiner#join(Object[]) * @see Joiner#join(Iterable) * @see Joiner#join(Object, Object, Object...) */ @Test public void test_base() { String join = Joiner.on("|").join(new String[]{"a", "b"}); Assert.assertEquals("a|b", join); join = Joiner.on("@").join(stringList); Assert.assertEquals("a@b", join); join = Joiner.on(",").join("a", "b"); Assert.assertEquals("a,b", join); } /** * 處理有null的狀況,跳過或賦予默認值 */ @Test public void test_null() { stringList.add(null); String join = Joiner.on("|").skipNulls().join(stringList); Assert.assertEquals("a|b", join); join = Joiner.on("|").useForNull("no value").join(stringList); Assert.assertEquals("a|b|no value", join); } /** * 處理追加情況 */ @Test public void test_append() { StringBuilder stringBuilder = new StringBuilder("test:{"); String string = Joiner.on("|").appendTo(stringBuilder, stringList).append("}").toString(); Assert.assertEquals("test:{a|b}", string); } @Test public void test_mapJoiner() { Joiner.MapJoiner mapJoiner = Joiner.on(",").withKeyValueSeparator("="); LinkedHashMap<Object, Object> linkedHashMap = new LinkedHashMap<Object, Object>(); linkedHashMap.put("a", 65); linkedHashMap.put("b", 66); String join = mapJoiner.join(linkedHashMap); Assert.assertEquals("a=65,b=66", join); } }
ObjectsTestapp
import com.google.common.base.MoreObjects; import com.google.common.base.Objects; import com.google.common.collect.ComparisonChain; import org.hamcrest.core.Is; import org.junit.Assert; import org.junit.Test; import java.util.Arrays; import java.util.Date; import java.util.List; public class ObjectsTest { private String a; private List<Integer> b; private Date date; @Test public void test_compareTo() { ObjectsTest objectsTest = new ObjectsTest(); objectsTest.setA("a"); ObjectsTest otherObjectsTest = new ObjectsTest(); otherObjectsTest.setA("b"); Assert.assertNotEquals(0, objectsTest.compareTo(otherObjectsTest)); } @Test public void test_toString() { b = Arrays.asList(1, 3); String string = MoreObjects.toStringHelper(this).omitNullValues().add("a", a).add("b", b).toString(); Assert.assertThat(string, Is.is("ObjectsTest{b=[1, 3]}")); a = "hah"; string = MoreObjects.toStringHelper(this).omitNullValues().add("a", a).add("b", b).toString(); Assert.assertThat(string, Is.is("ObjectsTest{a=hah, b=[1, 3]}")); } @Test public void test_firstNonNull() { String s = null; String default_value = MoreObjects.firstNonNull(s, "default value"); Assert.assertEquals("default value", default_value); } @Test public void test_hashCode() { int i = Objects.hashCode(a, b); Assert.assertNotEquals(0, i); } public String getA() { return a; } public void setA(String a) { this.a = a; } public List<Integer> getB() { return b; } public void setB(List<Integer> b) { this.b = b; } public int compareTo(ObjectsTest objectsTest) { return ComparisonChain.start().compare(this.a, objectsTest.a).compare(this.date, objectsTest.date).result(); } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
SplitterTestui
import com.google.common.base.Splitter; import org.junit.Assert; import org.junit.Test; import java.util.LinkedHashMap; import java.util.Map; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; public class SplitterTest { @Test public void test_base() { Iterable<String> split = Splitter.on("|").split("a|b|c"); String next = split.iterator().next(); Assert.assertEquals("a", next); } @Test public void test_mapSplitter() { LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>(); linkedHashMap.put("a", "65"); linkedHashMap.put("b", "66"); Map<String, String> stringMap = Splitter.on(",").withKeyValueSeparator("=").split("a=65,b=66"); assertThat(linkedHashMap, is(stringMap)); } }
StringsTestthis
import com.google.common.base.CharMatcher; import org.junit.Test; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; public class StringsTest { /** * 返回輸入字符序列的字符串副本,每一個匹配此匹配器的連續字符組由單個替換字符替換。 例如: * CharMatcher.anyOf(「eko」).collapseFrom(「bookkeeper」,「 - 」) 返回「b-p-r」。 * 默認實現使用indexIn(CharSequence)來查找第一個匹配字符,而後迭代序列的其他部分,爲每一個字符調用matches(char)。 */ @Test public void test_RemoveWhiteSpace() { String tabsAndSpaces = "String with spaces and tabs "; String expected = "String with spaces and tabs "; String scrubbed = CharMatcher.WHITESPACE. collapseFrom(tabsAndSpaces, ' '); assertThat(scrubbed, is(expected)); } /** * 除了替換連續字符外,將原字符串中的先後空格去掉 */ @Test public void test_TrimRemoveWhiteSpace() { String tabsAndSpaces = " String with spaces and tabs "; String expected = "String with spaces and tabs"; String scrubbed = CharMatcher.WHITESPACE. trimAndCollapseFrom(tabsAndSpaces, ' '); assertThat(scrubbed, is(expected)); } /** * 保留數字,另外CharMatcher還有邏輯運算 * * @see CharMatcher#or(CharMatcher) * @see CharMatcher#and(CharMatcher) */ @Test public void testRetainFrom() { String lettersAndNumbers = "foo989yxbar234"; String expected = "989234"; String retained = CharMatcher.JAVA_DIGIT. retainFrom(lettersAndNumbers); assertThat(expected, is(retained)); } }