Merge pull request #103 from Doha2012/master

Add charset example
This commit is contained in:
Eugen 2014-12-18 22:03:35 +02:00
commit 9163f109d3
1 changed files with 19 additions and 0 deletions

View File

@ -6,6 +6,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -15,6 +17,7 @@ import org.junit.Test;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@ -196,4 +199,20 @@ public class GuavaStringTest {
assertEquals(2, result);
}
@Test
public void whenRemoveCharsNotInCharset_thenRemoved() {
final Charset charset = Charset.forName("cp437");
final CharsetEncoder encoder = charset.newEncoder();
final Predicate<Character> inRange = new Predicate<Character>() {
@Override
public boolean apply(final Character c) {
return encoder.canEncode(c);
}
};
final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
assertEquals("hello", result);
}
}