/** * Retrieves the data at the given Properties file. * * @param path the file where the data resides * @return the configuration data */ public ConfigData get(String path) { Map<String, String> data = new HashMap<>(); if (path == null || path.isEmpty()) { return new ConfigData(data); } try (Reader reader = reader(path)) { Properties properties = new Properties(); properties.load(reader); Enumeration<Object> keys = properties.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement().toString(); String value = properties.getProperty(key); if (value != null) { data.put(key, value); } } return new ConfigData(data); } catch (IOException e) { throw new ConfigException("Could not read properties from file " + path); } }
@Test public void testGetAllKeysAtPath() throws Exception { ConfigData configData = configProvider.get("dummy"); Map<String, String> result = new HashMap<>(); result.put("testKey", "testResult"); result.put("testKey2", "testResult2"); assertEquals(result, configData.data()); assertEquals(null, configData.ttl()); }
/** * Retrieves the data with the given keys at the given Properties file. * * @param path the file where the data resides * @param keys the keys whose values will be retrieved * @return the configuration data */ public ConfigData get(String path, Set<String> keys) { Map<String, String> data = new HashMap<>(); if (path == null || path.isEmpty()) { return new ConfigData(data); } try (Reader reader = reader(path)) { Properties properties = new Properties(); properties.load(reader); for (String key : keys) { String value = properties.getProperty(key); if (value != null) { data.put(key, value); } } return new ConfigData(data); } catch (IOException e) { throw new ConfigException("Could not read properties from file " + path); } }
@Test public void testGetOneKeyAtPath() throws Exception { ConfigData configData = configProvider.get("dummy", Collections.singleton("testKey")); Map<String, String> result = new HashMap<>(); result.put("testKey", "testResult"); assertEquals(result, configData.data()); assertEquals(null, configData.ttl()); }
public ConfigData get(String path, Set<String> keys) { Map<String, String> data = new HashMap<>(); Long ttl = null; if (TEST_PATH.equals(path)) { if (keys.contains(TEST_KEY)) { data.put(TEST_KEY, TEST_RESULT); } if (keys.contains(TEST_KEY_WITH_TTL)) { data.put(TEST_KEY_WITH_TTL, TEST_RESULT_WITH_TTL); ttl = 1L; } if (keys.contains(TEST_INDIRECTION)) { data.put(TEST_INDIRECTION, "${test:testPath:testResult}"); } } else { if (keys.contains(TEST_KEY)) { data.put(TEST_KEY, TEST_RESULT_NO_PATH); } } return new ConfigData(data, ttl); }