ConfigUtils { public static Properties translateDeprecated(Properties props, String[][] synonymGroups) { HashSet<String> synonymSet = new HashSet<>(); for (String[] synonymGroup: synonymGroups) { for (String synonym : synonymGroup) { if (!synonymSet.add(synonym)) { log.warn("reused synonym={}", synonym); } } } Properties newProps = new Properties(); for (Enumeration<String> keyEnumerator = (Enumeration<String>) props.propertyNames(); keyEnumerator.hasMoreElements(); ) { String key = keyEnumerator.nextElement(); if (!synonymSet.contains(key)) { newProps.put(key, props.get(key)); } } for (String[] synonymGroup: synonymGroups) { String target = synonymGroup[0]; List<String> deprecated = new ArrayList<>(); for (int i = 1; i < synonymGroup.length; i++) { if (props.containsKey(synonymGroup[i])) { deprecated.add(synonymGroup[i]); } } if (deprecated.isEmpty()) { if (props.containsKey(target)) { newProps.put(target, props.get(target)); } continue; } StringBuilder synonymString = new StringBuilder(deprecated.get(0)); for (int i = 1; i < deprecated.size(); i++) { synonymString.append(", "); synonymString.append(deprecated.get(i)); } if (props.containsKey(target)) { log.error(target + " was configured, as well as the deprecated synonym(s) " + synonymString + ". Using the value of " + target); newProps.put(target, props.get(target)); } else if (deprecated.size() > 1) { log.error("The configuration keys " + synonymString + " are deprecated and may be " + "removed in the future. Additionally, this configuration is ambigous because " + "these configuration keys are all synonyms for " + target + ". Please update " + "your configuration to have only " + target + " set."); newProps.put(target, props.get(deprecated.get(0))); } else { log.warn("Configuration key " + deprecated.get(0) + " is deprecated and may be removed " + "in the future. Please update your configuration to use " + target + " instead."); newProps.put(target, props.get(deprecated.get(0))); } } return newProps; }  static Properties translateDeprecated(Properties props, String[][] synonymGroups);  }
@Test public void testTranslateDeprecated() throws Exception { Properties props = new Properties(); props.setProperty("foo.bar", "baz"); props.setProperty("foo.bar.deprecated", "quux"); props.setProperty("chicken", "1"); props.setProperty("rooster", "2"); props.setProperty("hen", "3"); props.setProperty("heifer", "moo"); props.setProperty("blah", "blah"); props.put("unexpected.non.string.object", new Integer(42)); Properties newProps = ConfigUtils.translateDeprecated(props, new String[][]{ {"foo.bar", "foo.bar.deprecated"}, {"chicken", "rooster", "hen"}, {"cow", "beef", "heifer", "steer"} }); assertEquals("baz", newProps.getProperty("foo.bar")); assertEquals(null, newProps.getProperty("foo.bar.deprecated")); assertEquals("1", newProps.getProperty("chicken")); assertEquals(null, newProps.getProperty("rooster")); assertEquals(null, newProps.getProperty("hen")); assertEquals("moo", newProps.getProperty("cow")); assertEquals(null, newProps.getProperty("beef")); assertEquals(null, newProps.getProperty("heifer")); assertEquals(null, newProps.getProperty("steer")); assertEquals(null, props.getProperty("cow")); assertEquals("blah", props.getProperty("blah")); assertEquals("blah", newProps.getProperty("blah")); assertEquals(null, newProps.getProperty("unexpected.non.string.object")); assertEquals(new Integer(42), newProps.get("unexpected.non.string.object")); assertEquals(null, props.getProperty("unexpected.non.string.object")); assertEquals(new Integer(42), props.get("unexpected.non.string.object")); }
@Test public void testAllowsNewKey() throws Exception { Properties props = new Properties(); props.setProperty("foo.bar", "baz"); Properties newProps = ConfigUtils.translateDeprecated(props, new String[][]{ {"foo.bar", "foo.bar.deprecated"}, {"chicken", "rooster", "hen"}, {"cow", "beef", "heifer", "steer"} }); assertNotNull(newProps); assertEquals("baz", newProps.getProperty("foo.bar")); assertNull(newProps.getProperty("foo.bar.deprecated")); }
@Test public void testDuplicateSynonyms() throws Exception { Properties props = new Properties(); props.setProperty("foo.bar", "baz"); props.setProperty("foo.bar.deprecated", "derp"); Properties newProps = ConfigUtils.translateDeprecated(props, new String[][]{ {"foo.bar", "foo.bar.deprecated"}, {"chicken", "foo.bar.deprecated"} }); assertNotNull(newProps); assertEquals("baz", newProps.getProperty("foo.bar")); assertEquals("derp", newProps.getProperty("chicken")); assertNull(newProps.getProperty("foo.bar.deprecated")); }
@Test public void testMultipleDeprecations() throws Exception { Properties props = new Properties(); props.setProperty("foo.bar.deprecated", "derp"); props.setProperty("foo.bar.even.more.deprecated", "very old configuration"); Properties newProps = ConfigUtils.translateDeprecated(props, new String[][]{ {"foo.bar", "foo.bar.deprecated", "foo.bar.even.more.deprecated"} }); assertNotNull(newProps); assertEquals("derp", newProps.getProperty("foo.bar")); assertNull(newProps.getProperty("foo.bar.deprecated")); assertNull(newProps.getProperty("foo.bar.even.more.deprecated")); }