/** * Set property. * * @param property the name of the property * @param value the value for the property * @return the previous value for the property, or {@code null}. */ public Object set(final String property, final Object value) { Objects.requireNonNull(value, "value"); final Object parsed = parser.parse(property, value); return props.put(property, parsed); }
private static Object coerceType(final String key, final Object value) { try { final String stringValue = value == null ? null : String.valueOf(value); return PROPERTY_PARSER.parse(key, stringValue); } catch (final Exception e) { throw new KsqlException("Failed to set '" + key + "' to '" + value + "'", e); } } }
@Test public void shouldInvokeParserCorrectly() { // Given: reset(parser); expect(parser.parse("prop-1", "new-val")).andReturn("parsed-new-val"); replay(parser); // When: propsWithMockParser.set("prop-1", "new-val"); // Then: assertThat(propsWithMockParser.toMap().get("prop-1"), is("parsed-new-val")); verify(parser); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowIfParserThrows() { // Given: reset(parser); expect(parser.parse("prop-1", "new-val")) .andThrow(new IllegalArgumentException("Boom")); replay(parser); // When: propsWithMockParser.set("prop-1", "new-val"); }
@Before public void setUp() { propsWithMockParser = new LocalProperties(INITIAL, parser); expect(parser.parse(anyString(), anyObject())) .andReturn("parsed-val"); replay(parser); realProps = new LocalProperties(ImmutableMap.of()); }