Filter { public Properties apply(Properties properties) { if (properties == null) { throw new IllegalArgumentException("properties must not be null"); } else { if (properties.isEmpty()) { return new Properties(); } else { Properties filtered = new Properties(); for (Object key : properties.keySet()) { if (!keysToRemove.contains(key)) { filtered.put(key, properties.get(key)); } } return filtered; } } } Filter();  Filter(Set<String> keysToRemove); Properties apply(Properties properties); Set<String> getKeys();  }
@Test public void doesNotAcceptNullInput() { Properties nullProperties = null; Filter f = new Filter(); try { f.apply(nullProperties); fail("IllegalArgumentException expected because input is null"); } catch (IllegalArgumentException e) { } }
@Test public void emptyInputResultsInEmptyOutput() { Properties emptyProperties = new Properties(); Filter f = new Filter(); Properties filtered = f.apply(emptyProperties); assertThat(filtered).hasSameSizeAs(emptyProperties); }
@Test public void filtersNothingByDefault() { Properties anyProperties = System.getProperties(); Filter f = new Filter(); Properties filtered = f.apply(anyProperties); assertThat(filtered).isEqualTo(anyProperties); assertThat(filtered).hasSameSizeAs(anyProperties); }
@Test public void filtersMatchingKey() { Properties properties = new Properties(); properties.put("one", 1); properties.put("two", 2); Set<String> removeOneKey = new HashSet<>(); removeOneKey.add("one"); Filter f = new Filter(removeOneKey); Properties filtered = f.apply(properties); assertThat(filtered).hasSize(properties.size() - 1); assertThat(filtered).doesNotContainKey("one"); assertThat(filtered).containsKey("two"); assertThat(filtered.get("two")).isSameAs(properties.get("two")); }
@Test public void filtersMatchingKeys() { Properties properties = new Properties(); properties.put("one", 1); properties.put("two", 2); properties.put("three", 3); properties.put("four", 4); properties.put("five", 5); Set<String> removeAllKeys = new HashSet<>(); for (Object key : properties.keySet()) { removeAllKeys.add(key.toString()); } Filter f = new Filter(removeAllKeys); Properties filtered = f.apply(properties); assertThat(filtered).hasSize(0); }
@Test public void doesNotFilterMismatchingKeys() { Properties properties = new Properties(); properties.put("one", 1); properties.put("two", 2); Set<String> keysToRemove = new HashSet<>(); keysToRemove.add("three"); Filter f = new Filter(keysToRemove); Properties filtered = f.apply(properties); assertThat(filtered).hasSameSizeAs(properties); assertThat(filtered).containsKeys("one", "two"); }