TRenderContext implements Appendable, Closeable { public Object getModelValue(String expr) { final int indexOfDot = expr.indexOf('.'); if (indexOfDot > -1) { String key = expr.substring(0, indexOfDot); final String property = expr.substring(indexOfDot + 1); return getModelValue(key, property); } return getModelValue(expr, null); } private  TRenderContext(TRenderContext parent, Map<String, Object> model, OutputStream outputStream, Writer writer);  TRenderContext(OutputStream outputStream, Map<String, Object> model);  TRenderContext(OutputStream outputStream, Charset charset, Map<String, Object> model); @Override TRenderContext append(CharSequence csq); @Override TRenderContext append(CharSequence csq, int start, int end); @Override TRenderContext append(char c); Set<String> getAllModelKeys(); @Override void close(); Object getModelValue(String expr); TRenderContext childContext(Map<String, Object> newModel); Map<String, Object> getModel(boolean mergeParentModels);  }
@Test public void testGetModelValue() throws Exception { final TestObj item = new TestObj(); final Map<String, Object> model = Collections.<String, Object>singletonMap("item", item); final TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), model); assertEquals("KEY", context.getModelValue("item.inner.name")); assertEquals("REL", context.getModelValue("item.inner.rel")); assertEquals(item.toString(), context.getModelValue("item").toString()); assertEquals(item.getInner().toString(), context.getModelValue("item.inner").toString()); }
@Test public void testEmbeddedMap() throws Exception { String itemKey = "item"; final String expr = itemKey + ".inner.name"; final Map<String, Object> inner = Collections.<String, Object>singletonMap("name", "KEY"); Map<String, Object> item = Collections.<String, Object>singletonMap("inner", inner); final Map<String, Object> model = Collections.<String, Object>singletonMap(itemKey, item); final TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), model); assertEquals(item.toString(), context.getModelValue("item").toString()); assertEquals(item.get("inner").toString(), context.getModelValue("item.inner").toString()); context.getModelValue(expr); assertEquals("KEY", context.getModelValue("item.inner.name")); }
@Test public void testEmbeddedList() throws Exception { String itemKey = "item"; final String expr = itemKey + ".0.0"; final List<Object> inner = Collections.<Object>singletonList("KEY"); List<Object> item = Collections.<Object>singletonList(inner); final Map<String, Object> model = Collections.<String, Object>singletonMap(itemKey, item); final TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), model); assertEquals(item.toString(), context.getModelValue("item").toString()); assertEquals(item.get(0).toString(), context.getModelValue("item.0").toString()); context.getModelValue(expr); assertEquals("KEY", context.getModelValue("item.0.0")); }
@Test public void testGPath() throws Exception { String itemKey = "item"; final XmlSlurper xmlSlurper = new XmlSlurper(false, false); final String xml = "<gmd:MD_Metadata xmlns:gmd=\"http: + "xmlns:gco=\"http: + "</gmd:fileIdentifier></gmd:MD_Metadata>"; Map<String, String> nsMapping = Maps.newHashMap(); nsMapping.put("gmd", "http: nsMapping.put("gco", "http: final GPathResult gpath = xmlSlurper.parseText(xml).declareNamespace(nsMapping); final Map<String, Object> model = Collections.<String, Object>singletonMap(itemKey, gpath); final TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), model); assertEquals("gmd:MD_Metadata", ((GPathResult) context.getModelValue("item")).name()); assertEquals("gmd:fileIdentifier", ((GPathResult) context.getModelValue("item.gmd:fileIdentifier")).name()); assertEquals("gmd:fileIdentifier", context.getModelValue("item.gmd:fileIdentifier.name()")); assertEquals("gco:CharacterString", ((GPathResult) context.getModelValue("item.gmd:fileIdentifier.gco:CharacterString")).name()); }
@Test public void testEmptyPathSection() throws Exception { String itemKey = "item"; final String expr = itemKey + "." + ".name"; try { final TestObj item = new TestObj(); final Map<String, Object> model = Collections.<String, Object>singletonMap(itemKey, item); final TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), model); context.getModelValue(expr); fail("Expected an exception"); } catch (TemplateException e) { assertTrue(e.getMessage(), e.getMessage().contains(expr)); assertTrue(e.getMessage(), e.getMessage().contains("empty section")); } }
@Test public void testNoProperty() throws Exception { String itemKey = "item"; final String expr = itemKey + "." + "name"; try { final TestObj item = new TestObj(); final Map<String, Object> model = Collections.<String, Object>singletonMap(itemKey, item); final TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), model); context.getModelValue(expr); fail("Expected an exception"); } catch (TemplateException e) { assertTrue(e.getMessage(), e.getMessage().contains(expr)); assertTrue(e.getMessage(), e.getMessage().contains("does not exist")); } }
@Test public void testNoItem() throws Exception { String itemKey = "item"; final String expr = "wrongKey.inner.name"; try { final TestObj item = new TestObj(); final Map<String, Object> model = Collections.<String, Object>singletonMap(itemKey, item); final TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), model); context.getModelValue(expr); fail("Expected an exception"); } catch (TemplateException e) { assertTrue(e.getMessage(), e.getMessage().contains(expr)); assertTrue(e.getMessage(), e.getMessage().contains("There is no object in the model")); } }
@Test public void testNullItem() throws Exception { String itemKey = "item"; final String expr = itemKey + ".other.name"; try { final TestObj item = new TestObj(); final Map<String, Object> model = Collections.<String, Object>singletonMap(itemKey, item); final TRenderContext context = new TRenderContext(new ByteArrayOutputStream(), model); context.getModelValue(expr); fail("Expected an exception"); } catch (TemplateException e) { assertTrue(e.getMessage(), e.getMessage().contains(expr)); assertTrue(e.getMessage(), e.getMessage().contains("value of the property")); assertTrue(e.getMessage(), e.getMessage().contains("is null")); } }