EnvironmentScopeExtractor implements AttributeExtractor { @Override public void setValue(String key, Object value) { try { TemplateModel model = request.getObjectWrapper().wrap(value); request.setVariable(key, model); } catch (TemplateModelException e) { throw new FreemarkerRequestException("Error when wrapping an object setting the '" + key + "' attribute", 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 testSetValue() throws TemplateModelException { Template template = createMock(Template.class); TemplateHashModel model = createMock(TemplateHashModel.class); TemplateModel valueModel = createMock(TemplateModel.class); Configuration configuration = createMock(Configuration.class); ObjectWrapper objectWrapper = createMock(ObjectWrapper.class); Writer writer = new StringWriter(); expect(template.getMacros()).andReturn(new HashMap<Object, Object>()); expect(template.getObjectWrapper()).andReturn(objectWrapper); expect(objectWrapper.wrap("value")).andReturn(valueModel); replay(template, model, valueModel, configuration, objectWrapper); Environment env = new Environment(template, model, writer); EnvironmentScopeExtractor extractor = new EnvironmentScopeExtractor(env); extractor.setValue("key", "value"); assertEquals(valueModel, env.getVariable("key")); verify(template, model, valueModel, configuration, objectWrapper); }
@Test(expected = FreemarkerRequestException.class) public void testSetValueException() throws TemplateModelException { Template template = createMock(Template.class); TemplateHashModel model = createMock(TemplateHashModel.class); TemplateModel valueModel = createMock(TemplateModel.class); Configuration configuration = createMock(Configuration.class); ObjectWrapper objectWrapper = createMock(ObjectWrapper.class); Writer writer = new StringWriter(); expect(template.getMacros()).andReturn(new HashMap<Object, Object>()); expect(template.getObjectWrapper()).andReturn(objectWrapper); expect(objectWrapper.wrap("value")).andThrow(new TemplateModelException()); replay(template, model, valueModel, configuration, objectWrapper); try { Environment env = new Environment(template, model, writer); EnvironmentScopeExtractor extractor = new EnvironmentScopeExtractor(env); extractor.setValue("key", "value"); assertEquals(valueModel, env.getVariable("key")); } finally { verify(template, model, valueModel, configuration, objectWrapper); } }