StringUtils { public static String replaceWildcardConfigs(String contentWithWildcards, Map<String, Object> properties) { if(properties == null || contentWithWildcards == null) { return contentWithWildcards; } Engine engine = new Engine(); engine.setModelAdaptor(new DefaultModelAdaptor() { @Override public Object getValue(TemplateContext context, Token arg1, List<String> arg2, String expression) { Object value = context.model.get(expression); if (value != null) { return value; } else { return super.getValue(context, arg1, arg2, expression); } } @Override protected Object traverse(Object obj, List<String> arg1, int arg2, ErrorHandler arg3, Token token) { if (obj == null) { throw new IllegalArgumentException("Could not resolve " + token); } return super.traverse(obj, arg1, arg2, arg3, token); } }); try { return engine.transform(contentWithWildcards, properties); } catch (com.floreysoft.jmte.message.ParseException e) { throw new IllegalArgumentException(e); } } private  StringUtils(); static boolean isEmpty(CharSequence cs); static boolean isNotEmpty(CharSequence cs); static boolean isNotEmptyTrimmed(String str); static String trim(String str); static String getStringOrEmpty(String inputString); static String getStringOrDefault(String inputString, String def); static String replaceWildcardConfigs(String contentWithWildcards, Map<String, Object> properties);  }
@Test public void testReplaceWildcardConfigs(TestContext context){ String contentWithWildcards = "This is a very ${adjective} helper method"; Map<String, Object> properties = ImmutableMap.of("adjective", "nice"); context.assertEquals("This is a very nice helper method", StringUtils.replaceWildcardConfigs(contentWithWildcards, properties)); context.assertEquals("This is a very ${adjective} helper method", StringUtils.replaceWildcardConfigs(contentWithWildcards, null)); context.assertNull(null, StringUtils.replaceWildcardConfigs(contentWithWildcards, properties)); context.assertNull(null, StringUtils.replaceWildcardConfigs(contentWithWildcards, null)); contentWithWildcards = "This is text does not contain any wildcard"; context.assertEquals("This is text does not contain any wildcard", StringUtils.replaceWildcardConfigs(contentWithWildcards, properties)); context.assertEquals("This is text does not contain any wildcard", StringUtils.replaceWildcardConfigs(contentWithWildcards, new HashMap<>())); }
@Test public void testReplaceWildcardConfigsWithEmptyProperties(TestContext context){ thrown.expect( IllegalArgumentException.class ); thrown.expectMessage("Could not resolve adjective"); String contentWithWildcards = "This is a very ${adjective} helper method"; StringUtils.replaceWildcardConfigs(contentWithWildcards, new HashMap<>()); }