VelocityAutotagRuntime extends Directive implements AutotagRuntime<Request> { @Override @SuppressWarnings("unchecked") public <T> T getParameter(String name, Class<T> type, T defaultValue) { if (params == null) { ASTMap astMap = (ASTMap) node.jjtGetChild(0); params = (Map<String, Object>) astMap.value(context); } T result = (T) params.get(name); if (result == null) { result = defaultValue; } return result; }  @Override Request createRequest(); @Override ModelBody createModelBody(); @Override @SuppressWarnings("unchecked") T getParameter(String name, Class<T> type, T defaultValue); @Override String getName(); @Override int getType(); @Override boolean render(InternalContextAdapter context, Writer writer, Node node);  }
@Test public void testGetParameter() { InternalContextAdapter context = createMock(InternalContextAdapter.class); Writer writer = createMock(Writer.class); Node node = createMock(Node.class); ASTMap astMap = createMock(ASTMap.class); @SuppressWarnings("unchecked") Map<String, Object> params = createMock(Map.class); expect(node.jjtGetChild(0)).andReturn(astMap); expect(astMap.value(context)).andReturn(params); expect(params.get(eq("notnullParam"))).andReturn(new Integer(42)).anyTimes(); expect(params.get(eq("nullParam"))).andReturn(null).anyTimes(); replay(context, writer, node, astMap, params); VelocityAutotagRuntime runtime = new VelocityAutotagRuntime(); runtime.render(context, writer, node); Object notnullParam = runtime.getParameter("notnullParam", Object.class, null); Object nullParam = runtime.getParameter("nullParam", Object.class, null); int notnullParamDefault = runtime.getParameter("notnullParam", Integer.class, new Integer(24)); int nullParamDefault = runtime.getParameter("nullParam", Integer.class, new Integer(24)); assertEquals(42, notnullParam); assertEquals(null, nullParam); assertEquals(42, notnullParamDefault); assertEquals(24, nullParamDefault); verify(context, writer, node, astMap, params); }