### Question:
PathInterpreter { static Map<String, String> resolveVariables(String expression, String str) { if (expression == null || str == null) return Collections.emptyMap(); Map<String, String> resolvedVariables = new HashMap<>(); StringBuilder variableBuilder = new StringBuilder(); State state = State.TEXT; int j = 0; int expressionLength = expression.length(); for (int i = 0; i < expressionLength; i++) { char e = expression.charAt(i); switch (e) { case '{': if (state == END_VAR) return Collections.emptyMap(); state = VAR; break; case '}': if (state != VAR) return Collections.emptyMap(); state = END_VAR; if (i != expressionLength - 1) break; default: switch (state) { case VAR: variableBuilder.append(e); break; case END_VAR: String replacement; boolean ec = i == expressionLength - 1; if (ec) { replacement = str.substring(j); } else { int k = str.indexOf(e, j); if (k == -1) return Collections.emptyMap(); replacement = str.substring(j, str.indexOf(e, j)); } resolvedVariables.put(variableBuilder.toString(), replacement); j += replacement.length(); if (j == str.length() && ec) return resolvedVariables; variableBuilder.setLength(0); state = TEXT; case TEXT: if (str.charAt(j) != e) return Collections.emptyMap(); j++; } } } return resolvedVariables; }    }### Answer:
@Test public void testSingleVariable() { Map<String, String> res = resolveVariables("{id}", "5435"); assertEquals(1, res.size()); assertEquals(res.get("id"), "5435"); }
@Test public void testSingleVariable2() { Map<String, String> res = resolveVariables("{variable_name}", "a"); assertEquals(1, res.size()); assertEquals(res.get("variable_name"), "a"); }
@Test public void testDualVariables() { Map<String, String> res = resolveVariables("{cachemanager}-{cache}", "default-mycache"); assertEquals(2, res.size()); assertEquals(res.get("cachemanager"), "default"); assertEquals(res.get("cache"), "mycache"); }
@Test public void testDualVariables2() { Map<String, String> res = resolveVariables("{cachemanager}{cache}", "defaultmycache"); assertEquals(0, res.size()); }
@Test public void testDualVariables3() { Map<String, String> res = resolveVariables("{a}:{b}", "value1:value2"); assertEquals(2, res.size()); assertEquals(res.get("a"), "value1"); assertEquals(res.get("b"), "value2"); }
@Test public void testPrefixSufix() { Map<String, String> res = resolveVariables("prefix_{variable1}_{variable2}_suffix", "prefix_value1_value2_suffix"); assertEquals(2, res.size()); assertEquals(res.get("variable1"), "value1"); assertEquals(res.get("variable2"), "value2"); }
@Test public void testSingleVariableWithPrefix() { Map<String, String> res = resolveVariables("counter-{id}", "counter-2345"); assertEquals(1, res.size()); assertEquals(res.get("id"), "2345"); }
@Test public void testNull() { Map<String, String> res1 = resolveVariables(null, "whatever"); Map<String, String> res2 = resolveVariables("{hello}", null); assertEquals(0, res1.size()); assertEquals(0, res2.size()); }
@Test public void testNonConformantPath() { Map<String, String> res = resolveVariables("{cachemanager}-{cache}", "default"); assertEquals(0, res.size()); }
@Test public void testMalformedExpression() { Map<String, String> res = resolveVariables("{counter {id}}", "whatever"); assertEquals(0, res.size()); }
@Test public void testMalformedExpression2() { Map<String, String> res = resolveVariables("{counter }id}-", "whatever"); assertEquals(0, res.size()); }