Joiner 做连接
public class JoinerTest {
private final List<String> stringList = Arrays.asList(
"Google", "Guava", "Java", "Scala", "Kafka"
);
private final List<String> stringListWithNullValue = Arrays.asList(
"Google", "Guava", "Java", "Scala", null
);
private final Map<String, String> stringMap = of("Hello", "Guava", "Java", "Scala");
private final String targetFileName = "G:\\Teaching\\xxxx\\guava-joiner.txt";
private final String targetFileNameToMap = "G:\\Teaching\\xxx\\guava-joiner-map.txt";
@Test
public void testJoinOnJoin() {
String result = Joiner.on("#").join(stringList);
assertThat(result, equalTo("Google#Guava#Java#Scala#Kafka"));
}
@Test(expected = NullPointerException.class)
public void testJoinOnJoinWithNullValue() {
String result = Joiner.on("#").join(stringListWithNullValue);
assertThat(result, equalTo("Google#Guava#Java#Scala#Kafka"));
}
@Test
public void testJoinOnJoinWithNullValueButSkip() {
String result = Joiner.on("#").skipNulls().join(stringListWithNullValue);
assertThat(result, equalTo("Google#Guava#Java#Scala"));
}
@Test
public void testJoin_On_Join_WithNullValue_UseDefaultValue() {
String result = Joiner.on("#").useForNull("DEFAULT").join(stringListWithNullValue);
assertThat(result, equalTo("Google#Guava#Java#Scala#DEFAULT"));
}
@Test
public void testJoin_On_Append_To_StringBuilder() {
final StringBuilder builder = new StringBuilder();
StringBuilder resultBuilder = Joiner.on("#").useForNull("DEFAULT").appendTo(builder, stringListWithNullValue);
assertThat(resultBuilder, sameInstance(builder));
assertThat(resultBuilder.toString(), equalTo("Google#Guava#Java#Scala#DEFAULT"));
assertThat(builder.toString(), equalTo("Google#Guava#Java#Scala#DEFAULT"));
}
@Test
public void testJoin_On_Append_To_Writer() {
try (FileWriter writer = new FileWriter(new File(targetFileName))) {
Joiner.on("#").useForNull("DEFAULT").appendTo(writer, stringListWithNullValue);
assertThat(Files.isFile().test(new File(targetFileName)), equalTo(true));
} catch (IOException e) {
fail("append to the writer occur fetal error.");
}
}
@Test
public void testJoiningByStreamSkipNullValues() {
String result = stringListWithNullValue.stream().filter(item -> item != null && !item.isEmpty()).collect(joining("#"));
assertThat(result, equalTo("Google#Guava#Java#Scala"));
}
@Test
public void testJoiningByStreamWithDefaultValue() {
String result = stringListWithNullValue.stream().map(this::defaultValue).collect(joining("#"));
assertThat(result, equalTo("Google#Guava#Java#Scala#DEFAULT"));
}
private String defaultValue(final String item) {
return item == null || item.isEmpty() ? "DEFAULT" : item;
}
@Test
public void testJoinOnWithMap() {
assertThat(Joiner.on('#').withKeyValueSeparator("=").join(stringMap), equalTo("Hello=Guava#Java=Scala"));
}
@Test
public void testJoinOnWithMapToAppendable() {
try (FileWriter writer = new FileWriter(new File(targetFileNameToMap))) {
Joiner.on("#").withKeyValueSeparator("=").appendTo(writer, stringMap);
assertThat(Files.isFile().test(new File(targetFileNameToMap)), equalTo(true));
} catch (IOException e) {
fail("append to the writer occur fetal error.");
}
}
}
Spliter 做分解
public class SplitterTest {
@Test
public void testSplitOnSplit() {
List<String> result = Splitter.on("|").splitToList("hello|world");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
assertThat(result.get(0), equalTo("hello"));
assertThat(result.get(1), equalTo("world"));
}
@Test
public void testSplit_On_Split_OmitEmpty() {
List<String> result = Splitter.on("|").splitToList("hello|world|||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(5));
result = Splitter.on("|").omitEmptyStrings().splitToList("hello|world|||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
}
@Test
public void testSplit_On_Split_OmitEmpty_TrimResult() {
List<String> result = Splitter.on("|").omitEmptyStrings().splitToList("hello | world|||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
assertThat(result.get(0), equalTo("hello "));
assertThat(result.get(1), equalTo(" world"));
result = Splitter.on("|").trimResults().omitEmptyStrings().splitToList("hello | world|||");
assertThat(result.get(0), equalTo("hello"));
assertThat(result.get(1), equalTo("world"));
}
/**
* aaaabbbbccccdddd
*/
@Test
public void testSplitFixLength() {
List<String> result = Splitter.fixedLength(4).splitToList("aaaabbbbccccdddd");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(4));
assertThat(result.get(0), equalTo("aaaa"));
assertThat(result.get(3), equalTo("dddd"));
}
@Test
public void testSplitOnSplitLimit() {
List<String> result = Splitter.on("#").limit(3).splitToList("hello#world#java#google#scala");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(3));
assertThat(result.get(0), equalTo("hello"));
assertThat(result.get(1), equalTo("world"));
assertThat(result.get(2), equalTo("java#google#scala"));
}
@Test
public void testSplitOnPatternString() {
List<String> result = Splitter.onPattern("\\|").trimResults().omitEmptyStrings().splitToList("hello | world|||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
assertThat(result.get(0), equalTo("hello"));
assertThat(result.get(1), equalTo("world"));
}
@Test
public void testSplitOnPattern() {
List<String> result = Splitter.on(Pattern.compile("\\|")).trimResults().omitEmptyStrings().splitToList("hello | world|||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
assertThat(result.get(0), equalTo("hello"));
assertThat(result.get(1), equalTo("world"));
}
@Test
public void testSplitOnSplitToMap() {
Map<String, String> result = Splitter.on(Pattern.compile("\\|")).trimResults()
.omitEmptyStrings().withKeyValueSeparator("=").split("hello=HELLO| world=WORLD|||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
assertThat(result.get("hello"),equalTo("HELLO"));
assertThat(result.get("world"),equalTo("WORLD"));
}
}
网友评论