@Test public void toLong() throws Exception { assertThat(RubyUtils.toLong(null)).isNull(); assertThat(RubyUtils.toLong(2)).isEqualTo(2L); assertThat(RubyUtils.toLong(3L)).isEqualTo(3L); assertThat(RubyUtils.toLong("4")).isEqualTo(4L); }
@Test public void should_parse_list_of_strings() { assertThat(RubyUtils.toStrings(null)).isNull(); assertThat(RubyUtils.toStrings("")).isEmpty(); assertThat(RubyUtils.toStrings("foo")).containsOnly("foo"); assertThat(RubyUtils.toStrings("foo,bar")).containsOnly("foo", "bar"); assertThat(RubyUtils.toStrings(asList("foo", "bar"))).containsOnly("foo", "bar"); }
@Test public void toDouble() throws Exception { assertThat(RubyUtils.toDouble(null)).isNull(); assertThat(RubyUtils.toDouble("")).isNull(); assertThat(RubyUtils.toDouble(" ")).isNull(); assertThat(RubyUtils.toDouble("123")).isEqualTo(123.0); assertThat(RubyUtils.toDouble("3.14")).isEqualTo(3.14); assertThat(RubyUtils.toDouble(3.14)).isEqualTo(3.14); assertThat(RubyUtils.toDouble(123)).isEqualTo(123.0); assertThat(RubyUtils.toDouble(123L)).isEqualTo(123.0); }
@Test public void toDate() throws Exception { assertThat(RubyUtils.toDate(null)).isNull(); assertThat(RubyUtils.toDate("")).isNull(); assertThat(RubyUtils.toDate(" ")).isNull(); assertThat(RubyUtils.toDate("2013-01-18").getDate()).isEqualTo(18); assertThat(RubyUtils.toDate("2013-01-18T15:38:19+0200").getDate()).isEqualTo(18); assertThat(RubyUtils.toDate("2013-01-18T15:38:19+0200").getMinutes()).isEqualTo(38); assertThat(RubyUtils.toDate(DateUtils.parseDate("2013-01-18")).getDate()).isEqualTo(18); }
@Test public void toBoolean() throws Exception { assertThat(RubyUtils.toBoolean(null)).isNull(); assertThat(RubyUtils.toBoolean("")).isNull(); assertThat(RubyUtils.toBoolean(" ")).isNull(); assertThat(RubyUtils.toBoolean("true")).isTrue(); assertThat(RubyUtils.toBoolean(true)).isTrue(); assertThat(RubyUtils.toBoolean("false")).isFalse(); assertThat(RubyUtils.toBoolean(false)).isFalse(); }
@Test public void toInteger() throws Exception { assertThat(RubyUtils.toInteger(null)).isNull(); assertThat(RubyUtils.toInteger("")).isNull(); assertThat(RubyUtils.toInteger(" ")).isNull(); assertThat(RubyUtils.toInteger("123")).isEqualTo(123); assertThat(RubyUtils.toInteger(123)).isEqualTo(123); assertThat(RubyUtils.toInteger(123L)).isEqualTo(123); }
@Test public void toEnums() { assertThat(RubyUtils.toEnums(null, RuleStatus.class)).isNull(); assertThat(RubyUtils.toEnums("", RuleStatus.class)).isEmpty(); assertThat(RubyUtils.toEnums("BETA", RuleStatus.class)).containsOnly(RuleStatus.BETA); assertThat(RubyUtils.toEnums("BETA,READY", RuleStatus.class)) .containsOnly(RuleStatus.BETA, RuleStatus.READY); assertThat(RubyUtils.toEnums(asList("BETA", "READY"), RuleStatus.class)) .containsOnly(RuleStatus.BETA, RuleStatus.READY); try { RubyUtils.toEnums("xxx", RuleStatus.class); fail(); } catch (IllegalArgumentException e) { // success } try { RubyUtils.toEnums(1, RuleStatus.class); fail(); } catch (IllegalArgumentException e) { assertThat(e).hasMessage("Unsupported type: class java.lang.Integer"); } }
@Test public void toBoolean_unexpected_class() throws Exception { throwable.expect(IllegalArgumentException.class); RubyUtils.toBoolean(333); }
@Test public void toDouble_unexpected_class() throws Exception { throwable.expect(IllegalArgumentException.class); RubyUtils.toDouble(true); }
@Test public void toInteger_unexpected_class() throws Exception { throwable.expect(IllegalArgumentException.class); RubyUtils.toInteger(1.2); }
@Test public void toLong_unexpected_class() throws Exception { throwable.expect(IllegalArgumentException.class); RubyUtils.toLong(false); }
@Test public void toDate_bad_format() throws Exception { throwable.expect(SonarException.class); RubyUtils.toDate("01/02/2013"); }
@Test public void toDate_unexpected_class() { throwable.expect(IllegalArgumentException.class); RubyUtils.toDate(333); }