EnvironmentScopeMap extends ScopeMap { @SuppressWarnings("unchecked") @Override public Set<String> keySet() { try { return request.getKnownVariableNames(); } catch (TemplateModelException e) { throw new FreemarkerRequestException( "Cannot get known variable names", e); } } EnvironmentScopeMap(Environment request); @SuppressWarnings("unchecked") @Override Set<String> keySet();  }
@SuppressWarnings("unchecked") @Test public void testKeySet() { Template template = createMock(Template.class); TemplateHashModel model = createMock(TemplateHashModel.class); Configuration configuration = createMock(Configuration.class); Set<String> names = createMock(Set.class); 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, configuration, names); Environment env = new Environment(template, model, writer); EnvironmentScopeMap map = new EnvironmentScopeMap(env); assertEquals(names, map.keySet()); verify(template, model, configuration, names); }
@SuppressWarnings("unchecked") @Test(expected = FreemarkerRequestException.class) public void testKeySetException() throws TemplateModelException { Template template = createMock(Template.class); TemplateHashModelEx model = createMock(TemplateHashModelEx.class); Configuration configuration = createMock(Configuration.class); Set<String> names = createMock(Set.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); try { replay(template, model, configuration, names); Environment env = new Environment(template, model, writer); EnvironmentScopeMap map = new EnvironmentScopeMap(env); map.keySet(); } finally { verify(template, model, configuration, names); } }