@Test public void testNonBoolean() throws Exception { try { Type.BOOLEAN.convert("unexpected", null); fail(); } catch (Type.ConversionException e) { assertThat(e).hasMessage("expected value of type 'int', but got \"unexpected\" (string)"); } // Integers other than [0, 1] should fail. try { Type.BOOLEAN.convert(2, null); fail(); } catch (Type.ConversionException e) { assertEquals(e.getMessage(), "boolean is not one of [0, 1]"); } try { Type.BOOLEAN.convert(-1, null); fail(); } catch (Type.ConversionException e) { assertEquals(e.getMessage(), "boolean is not one of [0, 1]"); } }
@Test public void testBoolean() throws Exception { Object myTrue = true; Object myFalse = false; assertEquals(Boolean.TRUE, Type.BOOLEAN.convert(1, null)); assertEquals(Boolean.FALSE, Type.BOOLEAN.convert(0, null)); assertTrue(Type.BOOLEAN.convert(true, null)); assertTrue(Type.BOOLEAN.convert(myTrue, null)); assertFalse(Type.BOOLEAN.convert(false, null)); assertFalse(Type.BOOLEAN.convert(myFalse, null)); assertThat(Type.BOOLEAN.flatten(myTrue)).isEmpty(); }