EnvironmentScopeExtractor implements AttributeExtractor { @SuppressWarnings("unchecked") @Override public Enumeration<String> getKeys() { try { return Collections.<String> enumeration(request.getKnownVariableNames()); } catch (TemplateModelException e) { throw new FreemarkerRequestException("Cannot iterate variable names correctly", 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 testGetKeys() { Template template = createMock(Template.class); TemplateHashModel model = createMock(TemplateHashModel.class); TemplateModel valueModel = createMock(TemplateModel.class); Configuration configuration = createMock(Configuration.class); Set<String> names = new HashSet<String>(); names.add("testGetKeys"); Writer writer = new StringWriter(); expect(template.getMacros()).andReturn(new HashMap<Object, Object>()); expect(template.getConfiguration()).andReturn(configuration); expect(configuration.getSharedVariableNames()).andReturn(names); replay(template, model, valueModel, configuration); Environment env = new Environment(template, model, writer); EnvironmentScopeExtractor extractor = new EnvironmentScopeExtractor(env); Enumeration<String> keys = extractor.getKeys(); assertEquals("testGetKeys", keys.nextElement()); assertFalse(keys.hasMoreElements()); verify(template, model, valueModel, configuration); }
@SuppressWarnings("unchecked") @Test(expected = FreemarkerRequestException.class) public void testGetKeysException() throws TemplateModelException { Template template = createMock(Template.class); TemplateHashModelEx model = createMock(TemplateHashModelEx.class); TemplateModel valueModel = createMock(TemplateModel.class); Configuration configuration = createMock(Configuration.class); Set<String> names = createMock(Set.class); Iterator<String> namesIt = createMock(Iterator.class); Writer writer = new StringWriter(); expect(template.getMacros()).andReturn(new HashMap<Object, Object>()); expect(model.keys()).andThrow(new TemplateModelException()); expect(template.getConfiguration()).andReturn(configuration); expect(configuration.getSharedVariableNames()).andReturn(names); replay(template, model, valueModel, configuration, names, namesIt); try { Environment env = new Environment(template, model, writer); EnvironmentScopeExtractor extractor = new EnvironmentScopeExtractor(env); extractor.getKeys(); } finally { verify(template, model, valueModel, configuration, names, namesIt); } }