EnvironmentScopeExtractor implements AttributeExtractor { @Override public Object getValue(String key) { try { TemplateModel variable = request.getVariable(key); if (variable != null) { return DeepUnwrap.unwrap(variable); } return null; } catch (TemplateModelException e) { throw new FreemarkerRequestException("Cannot get attribute with name '" + key + "'", e); } } EnvironmentScopeExtractor(Environment request); @Override void removeValue(String name); @SuppressWarnings("unchecked") @Override Enumeration<String> getKeys(); @Override Object getValue(String key); @Override void setValue(String key, Object value);  }
@Test public void testGetValue() throws TemplateModelException { Template template = createMock(Template.class); TemplateHashModel model = createMock(TemplateHashModel.class); TemplateScalarModel valueModel = createMock(TemplateScalarModel.class); Configuration configuration = createMock(Configuration.class); ObjectWrapper objectWrapper = createMock(ObjectWrapper.class); Writer writer = new StringWriter(); expect(template.getMacros()).andReturn(new HashMap<Object, Object>()); expect(valueModel.getAsString()).andReturn("value"); replay(template, model, valueModel, configuration, objectWrapper); Environment env = new Environment(template, model, writer); env.setVariable("key", valueModel); EnvironmentScopeExtractor extractor = new EnvironmentScopeExtractor(env); assertEquals("value", extractor.getValue("key")); verify(template, model, valueModel, configuration, objectWrapper); }
@Test public void testGetValueNull() throws TemplateModelException { Template template = createMock(Template.class); TemplateHashModel model = createMock(TemplateHashModel.class); TemplateScalarModel valueModel = createMock(TemplateScalarModel.class); Configuration configuration = createMock(Configuration.class); ObjectWrapper objectWrapper = createMock(ObjectWrapper.class); Writer writer = new StringWriter(); expect(template.getMacros()).andReturn(new HashMap<Object, Object>()); expect(model.get("key")).andReturn(null); expect(template.getConfiguration()).andReturn(configuration); expect(configuration.getSharedVariable("key")).andReturn(null); replay(template, model, valueModel, configuration, objectWrapper); Environment env = new Environment(template, model, writer); EnvironmentScopeExtractor extractor = new EnvironmentScopeExtractor(env); assertNull(extractor.getValue("key")); verify(template, model, valueModel, configuration, objectWrapper); }
@Test(expected = FreemarkerRequestException.class) public void testGetValueException() throws TemplateModelException { Template template = createMock(Template.class); TemplateHashModel model = createMock(TemplateHashModel.class); TemplateScalarModel valueModel = createMock(TemplateScalarModel.class); Configuration configuration = createMock(Configuration.class); ObjectWrapper objectWrapper = createMock(ObjectWrapper.class); Writer writer = new StringWriter(); expect(template.getMacros()).andReturn(new HashMap<Object, Object>()); expect(model.get("key")).andThrow(new TemplateModelException()); replay(template, model, valueModel, configuration, objectWrapper); try { Environment env = new Environment(template, model, writer); EnvironmentScopeExtractor extractor = new EnvironmentScopeExtractor(env); extractor.getValue("key"); } finally { verify(template, model, valueModel, configuration, objectWrapper); } }