Apache-common-lang org.apache.commons.lang3.BooleanUtils 우혁이 아빠 2012. 8. 9. 15:34 public class BooleanUtilsTest { /** * org.apache.commons.lang3.BooleanUtils.and(boolean... array) * 논리의 집합에서 and 연산을 수행합니다. * * org.apache.commons.lang3.BooleanUtils.and(Boolean... array) * org.apache.commons.lang3.BooleanUtils.or(boolean... array) * org.apache.commons.lang3.BooleanUtils.or(Boolean... array) */ @Test public void and_Or_test() { boolean[] booleanArray = {true, false, true}; boolean andResult = BooleanUtils.and(booleanArray); assertThat(andResult, is(false)); boolean orResult = BooleanUtils.or(booleanArray); assertThat(orResult, is(true)); } /** * org.apache.commons.lang3.BooleanUtils.isFalse(Boolean bool) * Boolean 객체의 값이 false인지 체크합니다. * null은 false로 체크합니다. * * org.apache.commons.lang3.BooleanUtils.isNotFalse(Boolean bool) * org.apache.commons.lang3.BooleanUtils.isTrue(Boolean bool) * org.apache.commons.lang3.BooleanUtils.isNotTrue(Boolean bool) */ @Test public void isFalse_isTrue_test() { Boolean trueOrFalse1 = new Boolean(true); Boolean trueOrFalse2 = new Boolean(false); Boolean trueOrFalse3 = null; boolean isFalseResult = BooleanUtils.isFalse(trueOrFalse1); assertThat(isFalseResult, is(false)); boolean isTrueResult = BooleanUtils.isTrue(trueOrFalse2); assertThat(isTrueResult, is(false)); isFalseResult = BooleanUtils.isFalse(trueOrFalse3); assertThat(isFalseResult, is(false)); } /** * org.apache.commons.lang3.BooleanUtils.toBoolean(Boolean bool) * Boolean 객체를 primitive boolean으로 변환합니다. * * org.apache.commons.lang3.BooleanUtils.toBoolean(int value) * org.apache.commons.lang3.BooleanUtils.toBoolean(Integer value, Integer trueValue, Integer falseValue) * org.apache.commons.lang3.BooleanUtils.toBoolean(int value, int trueValue, int falseValue) * org.apache.commons.lang3.BooleanUtils.toBoolean(String str) * org.apache.commons.lang3.BooleanUtils.toBoolean(String str, String trueString, String falseString) */ @Test public void toBoolean_test() { boolean trueOrFalse1 = BooleanUtils.toBoolean(1); assertThat(trueOrFalse1, is(true)); boolean trueOrFalse2 = BooleanUtils.toBoolean(0); assertThat(trueOrFalse2, is(false)); assertThat(BooleanUtils.toBoolean("true"), is(true)); assertThat(BooleanUtils.toBoolean("false"), is(false)); assertThat(BooleanUtils.toBoolean("yes"), is(true)); assertThat(BooleanUtils.toBoolean("no"), is(false)); assertThat(BooleanUtils.toBoolean("on"), is(true)); assertThat(BooleanUtils.toBoolean("off"), is(false)); assertThat(BooleanUtils.toBoolean("참", "참", "거짓"), is(true)); assertThat(BooleanUtils.toBoolean("거짓", "참", "거짓"), is(false)); } /** * org.apache.commons.lang3.BooleanUtils.toInteger(boolean bool) * boolean 값을 int로 변환합니다. * * org.apache.commons.lang3.BooleanUtils.toInteger(boolean bool, int trueValue, int falseValue) * org.apache.commons.lang3.BooleanUtils.toInteger(Boolean bool, int trueValue, int falseValue, int nullValue) */ @Test public void toInteger_test() { assertThat(BooleanUtils.toInteger(true), is(1)); assertThat(BooleanUtils.toInteger(false), is(0)); Boolean trueOrFalse = null; assertThat(BooleanUtils.toInteger(trueOrFalse, 1, 0, -1), is(-1)); } /** * org.apache.commons.lang3.BooleanUtils.toString(boolean bool, String trueString, String falseString) * boolean 값을 String으로 입력받은 문자열들 중 하나로 변환합니다. * * org.apache.commons.lang3.BooleanUtils.toString(Boolean bool, String trueString, String falseString, String nullString) * org.apache.commons.lang3.BooleanUtils.toStringOnOff(boolean bool) * org.apache.commons.lang3.BooleanUtils.toStringOnOff(Boolean bool) * org.apache.commons.lang3.BooleanUtils.toStringTrueFalse(boolean bool) * org.apache.commons.lang3.BooleanUtils.toStringTrueFalse(Boolean bool) * org.apache.commons.lang3.BooleanUtils.toStringYesNo(boolean bool) * org.apache.commons.lang3.BooleanUtils.toStringYesNo(Boolean bool) */ @Test public void toString_test() { assertThat(BooleanUtils.toString(true, "Y", "N"), is("Y")); assertThat(BooleanUtils.toString(false, "Y", "N"), is("N")); assertThat(BooleanUtils.toString(new Boolean(true), "Y", "N"), is("Y")); assertThat(BooleanUtils.toString(new Boolean(false), "Y", "N"), is("N")); assertThat(BooleanUtils.toStringOnOff(true), is("on")); assertThat(BooleanUtils.toStringOnOff(false), is("off")); assertThat(BooleanUtils.toStringTrueFalse(true), is("true")); assertThat(BooleanUtils.toStringTrueFalse(false), is("false")); assertThat(BooleanUtils.toStringYesNo(true), is("yes")); assertThat(BooleanUtils.toStringYesNo(false), is("no")); } } 저작자표시