JavaEvaluator implements Evaluator { public Object get(Object root, String path) { if (root == null) { return null; } String[] paths = path.split("[\\]\\.]"); Object current = root; for (int i = 1; i < paths.length; i++) { try { current = navigate(current, paths[i]); } catch (Exception e) { throw new VRaptorException("Unable to evaluate expression " + path, e); } if (current == null) { return ""; } } return current; }  Object get(Object root, String path);  }
@Test public void shouldInvokeAGetter() { assertThat((Long) evaluator.get(client(1L), "client.id"), is(equalTo(1L))); }
@Test public void shouldInvokeAIs() { Client c = client(1L); c.ugly=true; assertThat((Boolean) evaluator.get(c, "client.ugly"), is(equalTo(true))); }
@Test public void shouldAccessArray() { Client c = client(1L); c.favoriteColors = new String[] {"blue", "red"}; assertThat((String) evaluator.get(c, "client.favoriteColors[1]"), is(equalTo("red"))); }
@Test public void shouldAccessList() { Client c = client(1L); c.emails = Arrays.asList("blue", "red"); assertThat((String) evaluator.get(c, "client.emails[1]"), is(equalTo("red"))); }
@SuppressWarnings("unchecked") @Test public void shouldAccessCollection() { Client c = client(1L); c.favoriteNumbers = new TreeSet(Arrays.asList("blue", "red")); assertThat((String) evaluator.get(c, "client.favoriteNumbers[1]"), is(equalTo("red"))); }
@Test public void shouldReturnEmptyStringIfNullWasFoundOnTheWay() { Client c = client(1L); assertThat((String) evaluator.get(c, "client.child.id"), is(equalTo(""))); }
@Test public void shouldReturnEmptyStringIfTheResultIsNull() { Client c = client(null); assertThat((String) evaluator.get(c, "client.id"), is(equalTo(""))); }
@Test public void shouldInvokeAGetterDeclaredOnSuperClass() { Client c = vipClient(1L); assertThat((Long) evaluator.get(c, "client.id"), is(equalTo(1L))); }
@Test public void shouldInvokeAIsDeclaredOnSuperClass() { Client c = vipClient(1L); c.ugly = true; assertThat((Boolean) evaluator.get(c, "client.ugly"), is(equalTo(true))); }