@Test public void testTagConversion() throws Exception { assertSameContents(Sets.newHashSet("attribute"), Type.BOOLEAN.toTagSet(true, "attribute")); assertSameContents(Sets.newHashSet("noattribute"), Type.BOOLEAN.toTagSet(false, "attribute")); assertSameContents( Sets.newHashSet("whiskey"), Type.STRING.toTagSet("whiskey", "preferred_cocktail")); assertSameContents( Sets.newHashSet("cheddar", "ementaler", "gruyere"), Type.STRING_LIST.toTagSet( Lists.newArrayList("cheddar", "ementaler", "gruyere"), "cheeses")); }
@Test public void testIllegalTagConversIonFromNullOnSupportedType() throws Exception { try { Type.BOOLEAN.toTagSet(null, "a_boolean"); fail("Expect UnsuportedOperationException"); } catch (IllegalStateException e) { // Success. } }
@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(); }