public Builder property(final String name, final Object value) { properties.put(name, value); return this; }
public Properties(final String name, final Object property) { super(); put(name, property); }
public void putProperty(final String name, final Object value) { properties.put(name, value); }
@Override public void put(final String propertyName, final Object value) { properties.put(propertyName, value); }
public Builder property(final String name, final Object value) { properties.put(name, value); return this; }
@Override public Object put(final String name, final Object property) { final Object result = properties.put(name, property); loadedProperties.add(name); return result; }
@Test public void shouldRemovePropertyIfAddedWithNullValue() { // Given final Properties properties = new Properties(); properties.put("property1", "propertyValue1"); properties.put("property2", "propertyValue2"); // When properties.put("property1", null); // Then assertEquals(1, properties.size()); assertEquals(null, properties.get("property1")); }
@Test public void shouldSerialiseAndDeserialiseProperties() throws SerialisationException { // Given final Element element = newElement("group"); final Properties properties = new Properties(); properties.put("property1", 1L); properties.put("property2", 2); properties.put("property3", (double) 3); properties.put("property4", "4"); properties.put("property5", new Date(5L)); element.setProperties(properties); // When final byte[] serialisedElement = JSONSerialiser.serialise(element); final Element deserialisedElement = JSONSerialiser.deserialise(serialisedElement, element.getClass()); // Then assertTrue(StringUtil.toString(serialisedElement).contains("{\"java.util.Date\":5}")); assertEquals(element, deserialisedElement); }
@Test public void shouldReturnHumanReadableToString() { // Given final String property1 = "property 1"; final String property2 = "property 2"; final String propertyValue1 = "property value 1"; final String propertyValue2 = "property value 2"; final Properties properties = new Properties(); properties.put(property1, propertyValue1); properties.put(property2, propertyValue2); // When final String toString = properties.toString(); // Then assertTrue(toString.contains("property 2=" + "<java.lang.String>property value 2")); assertTrue(toString.contains("property 1=" + "<java.lang.String>property value 1")); } }
@Test public void shouldRemoveProperties() { // Given final String property1 = "property 1"; final String property2 = "property 2"; final String property3 = "property 3"; final String property4 = "property 4"; final String propertyValue1 = "property value 1"; final String propertyValue2 = "property value 2"; final String propertyValue3 = "property value 3"; final String propertyValue4 = "property value 4"; final Collection<String> propertiesToRemove = Arrays.asList(property2, property4); final Properties properties = new Properties(); properties.put(property1, propertyValue1); properties.put(property2, propertyValue2); properties.put(property3, propertyValue3); properties.put(property4, propertyValue4); // When properties.remove(propertiesToRemove); // Then assertEquals(2, properties.size()); assertEquals(propertyValue1, properties.get(property1)); assertEquals(propertyValue3, properties.get(property3)); }
@Test public void shouldKeepOnlyGivenProperties() { // Given final String property1 = "property 1"; final String property2 = "property 2"; final String property3 = "property 3"; final String property4 = "property 4"; final String propertyValue1 = "property value 1"; final String propertyValue2 = "property value 2"; final String propertyValue3 = "property value 3"; final String propertyValue4 = "property value 4"; final Collection<String> propertiesToKeep = Arrays.asList(property1, property3); final Properties properties = new Properties(); properties.put(property1, propertyValue1); properties.put(property2, propertyValue2); properties.put(property3, propertyValue3); properties.put(property4, propertyValue4); // When properties.keepOnly(propertiesToKeep); // Then assertEquals(2, properties.size()); assertEquals(propertyValue1, properties.get(property1)); assertEquals(propertyValue3, properties.get(property3)); }
@Test public void shouldAggregateWithTuple2BinaryOperator() { // Given final String property1 = "property 1"; final String property2 = "property 2"; final BinaryOperator func1 = new ExampleTuple2BinaryOperator(); final ElementAggregator aggregator = new ElementAggregator.Builder() .select(property1, property2) .execute(func1) .build(); final Properties props1 = new Properties(); props1.put(property1, 1); props1.put(property2, "value1"); final Properties props2 = new Properties(); props2.put(property1, 10); props2.put(property2, "value10"); final Properties props3 = new Properties(); props3.put(property1, 5); props3.put(property2, "value5"); // When Properties state = props1; state = aggregator.apply(state, props2); state = aggregator.apply(state, props3); // Then assertEquals(props2, state); }
@Test public void shouldNotAddPropertyIfPropertyNameIsNull() { // When final Properties properties = new Properties(); properties.put(null, "propertyValue1"); // Then assertEquals(0, properties.size()); }
@Test public void shouldAggregatePropertiesWithMultipleOfFunctions() { // Given final BinaryOperator<Integer> max = Math::max; final BinaryOperator<Integer> min = Math::min; final ElementAggregator aggregator = new ElementAggregator.Builder() .select("max") .execute(max) .select("min") .execute(min) .build(); final Properties properties1 = new Properties(); properties1.put("max", 10); properties1.put("min", 10); final Properties properties2 = new Properties(); properties2.put("max", 100); properties2.put("min", 100); final Properties properties3 = new Properties(); properties3.put("max", 1000); properties3.put("min", 1000); // When Properties state = aggregator.apply(properties1, properties2); state = aggregator.apply(state, properties3); // Then assertEquals(1000, state.get("max")); assertEquals(10, state.get("min")); }
@Test public void shouldAggregatePropertiesWithMultipleSelection() { // Given final BinaryOperator<Tuple3<Integer, Integer, Integer>> maxMinRange = (t1, t2) -> new Tuple3<>( Math.max(t1.get0(), t2.get0()), Math.min(t1.get1(), t2.get1()), Math.max(t1.get0(), t2.get0()) - Math.min(t1.get1(), t2.get1()) ); final ElementAggregator aggregator = new ElementAggregator.Builder() .select("max", "min", "range") .execute(maxMinRange) .build(); final Properties properties1 = new Properties(); properties1.put("max", 10); properties1.put("min", 10); final Properties properties2 = new Properties(); properties2.put("max", 100); properties2.put("min", 100); final Properties properties3 = new Properties(); properties3.put("max", 1000); properties3.put("min", 1000); // When Properties state = aggregator.apply(properties1, properties2); state = aggregator.apply(state, properties3); // Then assertEquals(1000, state.get("max")); assertEquals(10, state.get("min")); assertEquals(1000 - 10, state.get("range")); }
@Test public void shouldCloneProperties() { // Given final String property1 = "property 1"; final String property2 = "property 2"; final String propertyValue1 = "property value 1"; final String propertyValue2 = "property value 2"; final Properties properties = new Properties(); properties.put(property1, propertyValue1); properties.put(property2, propertyValue2); // When final Properties clone = properties.clone(); // Then assertEquals(2, clone.size()); assertNotSame(properties, clone); assertEquals(propertyValue1, clone.get(property1)); assertEquals(propertyValue2, clone.get(property2)); }
public Properties(final String name, final Object property) { super(); put(name, property); }
public Builder property(final String name, final Object value) { properties.put(name, value); return this; }
@Override public void put(final String propertyName, final Object value) { properties.put(propertyName, value); }
@Override public Object put(final String name, final Object property) { final Object result = properties.put(name, property); loadedProperties.add(name); return result; }