@Override public String lookup(String key) { String staticResult = staticLookup.lookup(key); if(staticResult != null) { return staticResult; } if(key.equals("date")) { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(clock.currentTime()); } return null; }
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup<?> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }
@Test public void testMapLookup() { final Map<String, Object> map = new HashMap<>(); map.put("key", "value"); map.put("number", Integer.valueOf(2)); assertEquals("value", StrLookup.mapLookup(map).lookup("key")); assertEquals("2", StrLookup.mapLookup(map).lookup("number")); assertNull(StrLookup.mapLookup(map).lookup(null)); assertNull(StrLookup.mapLookup(map).lookup("")); assertNull(StrLookup.mapLookup(map).lookup("other")); }
/** * Tests that a lookup object for system properties can deal with a full * replacement of the system properties object. This test is related to * LANG-1055. */ @Test public void testSystemPropertiesLookupReplacedProperties() { final Properties oldProperties = System.getProperties(); final String osName = "os.name"; final String newOsName = oldProperties.getProperty(osName) + "_changed"; final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup(); final Properties newProps = new Properties(); newProps.setProperty(osName, newOsName); System.setProperties(newProps); try { assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName)); } finally { System.setProperties(oldProperties); } }
@Test public void testSystemPropertiesLookup() { assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().lookup("os.name")); assertNull(StrLookup.systemPropertiesLookup().lookup("")); assertNull(StrLookup.systemPropertiesLookup().lookup("other")); try { StrLookup.systemPropertiesLookup().lookup(null); fail(); } catch (final NullPointerException ex) { // expected } }
/** * Tests that a lookup object for system properties sees changes on system * properties. This test is related to LANG-1141. */ @Test public void testSystemPropertiesLookupUpdatedProperty() { final String osName = "os.name"; final String oldOs = System.getProperty(osName); final String newOsName = oldOs + "_changed"; final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup(); System.setProperty(osName, newOsName); try { assertEquals("Changed properties not detected", newOsName, sysLookup.lookup(osName)); } finally { System.setProperty(osName, oldOs); } }
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup<?> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup<?> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }
/** * Internal method that resolves the value of a variable. * <p> * Most users of this class do not need to call this method. This method is * called automatically by the substitution process. * <p> * Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. * * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup<?> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); }