Apache Common Lang의 StringUtils 메소드를 많이 활용하는 부분을 작성.
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
public class StringUtilsTest {
@Test
public void test() {
Assert.assertEquals("", StringUtils.EMPTY);
Assert.assertEquals(-1, StringUtils.INDEX_NOT_FOUND);
String string = "";
Assert.assertSame(true, StringUtils.isEmpty(string));
String ironman="This is a Iron man";
Assert.assertEquals("This is...", StringUtils.abbreviate(ironman, 10));
Assert.assertEquals(" 2", StringUtils.leftPad("2", 2));
Assert.assertEquals("02", StringUtils.leftPad("2", 2, "0"));
Assert.assertEquals(true, StringUtils.isBlank(""));
Assert.assertEquals(true, StringUtils.isBlank(null));
Assert.assertEquals(true, StringUtils.isEmpty(""));
Assert.assertEquals(true, StringUtils.isEmpty(null));
Assert.assertEquals(false, StringUtils.isNotEmpty(""));
Assert.assertEquals(true, StringUtils.isNotEmpty("1111"));
String[] split = StringUtils.split("000_111_AA_222_333", "_");
Assert.assertEquals("000111AA222333", split[0]+split[1]+split[2]+split[3]+split[4]);
String test = "Test";
StringUtils.equals("Test", test);
Assert.assertEquals(2, StringUtils.countMatches("/aaa/bbb", "/"));
Assert.assertEquals("AAABBB", StringUtils.join("AAA", "BBB"));
String[] array = new String[]{ "LA", "Seattle", "SanJose"};
Assert.assertEquals("LA,Seattle,SanJose", StringUtils.join(array, ","));
Assert.assertEquals(true, StringUtils.startsWith("https://www.google.com", "https://"));
Assert.assertEquals(true, StringUtils.endsWith("https://www.google.com", ".com"));
Assert.assertEquals("st", StringUtils.left("string", 2));
Assert.assertEquals("ing", StringUtils.right("string", 3));
Assert.assertEquals("A-Z", StringUtils.upperCase("a-z"));
Assert.assertEquals("A-z", StringUtils.capitalize("a-z"));
Assert.assertEquals("a-z", StringUtils.lowerCase("A-Z"));
Assert.assertEquals(true, StringUtils.isNumeric("11"));
Assert.assertEquals(true, StringUtils.isAlphanumeric("11"));
Assert.assertEquals(true, StringUtils.isAlpha("aa"));
Assert.assertEquals("AAA", StringUtils.defaultString("AAA"));
Assert.assertEquals("", StringUtils.defaultString(""));
Assert.assertEquals("", StringUtils.defaultString(null));
Assert.assertEquals(6, StringUtils.length("string"));
Assert.assertEquals("fter", StringUtils.substringAfter("after", "a"));
Assert.assertEquals("abc@google.com", StringUtils.trim((" abc@google.com ")));
Assert.assertEquals("google", StringUtils.deleteWhitespace(" goo gle "));
Assert.assertEquals("msle", StringUtils.replace("google", "goog", "ms"));
Assert.assertEquals("bbb", StringUtils.defaultIfBlank("", "bbb"));
Assert.assertEquals(4, StringUtils.indexOfAny("google", new String[] { "le", "ms" }));
Assert.assertEquals(1, StringUtils.indexOfAny("google", new char[] { 'o', 'l' }));
Assert.assertEquals("ass", StringUtils.substringAfterLast("gooogle-glass", "gl"));
Assert.assertEquals("0820", StringUtils.center("82", 4, '0'));
Assert.assertEquals("leass", StringUtils.difference("google", "gleass"));
Assert.assertEquals("Google <--> Google <--> Google" , StringUtils.repeat("Google", " <--> ", 3));
Assert.assertEquals("Google", StringUtils.removeEnd("Googled", "d"));
Assert.assertEquals("Google", StringUtils.chomp("Google\r\n"));
}
}
'general java' 카테고리의 다른 글
[Guava] HashBiMap (0) | 2015.03.25 |
---|---|
godeps와 비슷하게 git의 revision으로 dependency 관리하기 (0) | 2015.03.25 |
[자바] padding 채우기 (0) | 2015.03.19 |
Apache commons collection 3.x의 MapUtils 예제 (그리고 주의사항) (0) | 2015.03.15 |
Spring에서 hbase rest client 설정 예제 및 StringHttpMessageConverter 한글 인코딩 (0) | 2015.03.09 |