Author: cbrisson Date: Mon Jun 25 22:00:57 2018 New Revision: 1834382 URL: http://svn.apache.org/viewvc?rev=1834382&view=rev Log: [tools/view] Fix VelocityViewTest and add some debugging
Modified: velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java Modified: velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java?rev=1834382&r1=1834381&r2=1834382&view=diff ============================================================================== --- velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java (original) +++ velocity/tools/trunk/velocity-tools-view/src/test/java/org/apache/velocity/tools/view/VelocityViewTest.java Mon Jun 25 22:00:57 2018 @@ -24,6 +24,7 @@ import static org.easymock.EasyMock.*; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.StringWriter; import javax.servlet.ServletContext; @@ -35,7 +36,10 @@ import org.apache.velocity.context.Conte import org.apache.velocity.exception.MethodInvocationException; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; +import org.easymock.IAnswer; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Tests {@link VelocityView}. @@ -44,6 +48,43 @@ import org.junit.Test; public class VelocityViewTest { + static Logger logger = LoggerFactory.getLogger(VelocityViewTest.class); + + /** + * Unique point of passage for non-void calls + * @param value value to return + * @param <T> type of returned value + * @return value + */ + static <T> IAnswer<T> eval(final T value) + { + return new IAnswer<T>() + { + public T answer() throws Throwable + { + String caller = null; + String callee = null; + StackTraceElement stackTrace[] = Thread.currentThread().getStackTrace(); + String previous = null; + for (StackTraceElement line : stackTrace) + { + String at = line.toString(); + if (at.contains("org.apache") && !at.contains("VelocityViewTest")) + { + caller = at; + break; + } + previous = at; + } + if (caller != null) callee = previous; + else caller = callee = "???"; + // good place for a breakpoint + logger.trace("[view] method {} called, expecting {}, at {}", value, caller); + return value; + } + }; + } + /** * Test method for {@link org.apache.velocity.tools.view.VelocityView#getTemplate(javax.servlet.http.HttpServletRequest)}. * Tests VELTOOLS-119 @@ -61,30 +102,21 @@ public class VelocityViewTest HttpServletResponse response = createMock(HttpServletResponse.class); Context context = createMock(Context.class); - expect(config.getServletContext()).andReturn(servletContext); - expect(config.findInitParameter(VelocityView.USER_OVERWRITE_KEY)).andReturn(null); - expect(config.findInitParameter(VelocityView.LOAD_DEFAULTS_KEY)).andReturn("false"); - expect(servletContext.getInitParameter(VelocityView.PROPERTIES_KEY)).andReturn(null); + expect(config.getServletContext()).andAnswer(eval(servletContext)); + expect(config.findInitParameter(VelocityView.USER_OVERWRITE_KEY)).andAnswer(eval((String)null)); + expect(config.findInitParameter(VelocityView.LOAD_DEFAULTS_KEY)).andAnswer(eval("false")); + expect(servletContext.getInitParameter(VelocityView.PROPERTIES_KEY)).andAnswer(eval((String)null)); expect(servletContext.getResourceAsStream(VelocityView.USER_PROPERTIES_PATH)) - .andReturn(getClass().getResourceAsStream("/WEB-INF/velocity.properties")); - expect(servletContext.getResourceAsStream("/VM_global_library.vm")) - .andReturn(getClass().getResourceAsStream("/VM_global_library.vm")); - expect(servletContext.getResourceAsStream("/VM_global_library.vm")) - .andReturn(getClass().getResourceAsStream("/VM_global_library.vm")); + .andAnswer(eval(getClass().getResourceAsStream("/WEB-INF/velocity.properties"))); String root = new File(getClass().getResource("/").getFile()).getAbsolutePath(); - expect(servletContext.getRealPath("/")).andReturn(root); - expect(config.getInitParameter(VelocityView.PROPERTIES_KEY)).andReturn(null); - expect(config.findInitParameter(VelocityView.CLEAN_CONFIGURATION_KEY)).andReturn(null); - expect(servletContext.getInitParameter(VelocityView.TOOLS_KEY)).andReturn(null); - expect(config.getInitParameter(VelocityView.TOOLS_KEY)).andReturn(null); - expect(servletContext.getAttribute(ServletUtils.CONFIGURATION_KEY)).andReturn(null); - expect(servletContext.getResourceAsStream(VelocityView.USER_TOOLS_PATH)) - .andReturn(getClass().getResourceAsStream("/org/apache/velocity/tools/view/tools.xml")); - expect(request.getAttribute("javax.servlet.include.servlet_path")).andReturn("/charset-test.vm"); - expect(request.getAttribute("javax.servlet.include.path_info")).andReturn(null); - expect(servletContext.getResourceAsStream("/charset-test.vm")) - .andReturn(getClass().getResourceAsStream("/charset-test.vm")); - expect(servletContext.getRealPath("/")).andReturn(root); + expect(config.getInitParameter(VelocityView.PROPERTIES_KEY)).andAnswer(eval((String)null)); + expect(config.findInitParameter(VelocityView.CLEAN_CONFIGURATION_KEY)).andAnswer(eval((String)null)); + expect(servletContext.getInitParameter(VelocityView.TOOLS_KEY)).andAnswer(eval((String)null)); + expect(config.getInitParameter(VelocityView.TOOLS_KEY)).andAnswer(eval((String)null)); + expect(servletContext.getAttribute(ServletUtils.CONFIGURATION_KEY)).andAnswer(eval((String)null)); + expect(servletContext.getResourceAsStream(VelocityView.USER_TOOLS_PATH)).andAnswer(eval((InputStream)null)); + expect(request.getAttribute("javax.servlet.include.servlet_path")).andAnswer(eval("/charset-test.vm")); + expect(request.getAttribute("javax.servlet.include.path_info")).andAnswer(eval((String)null)); // This was necessary to verify the bug, now it is not called at all. // expect(response.getCharacterEncoding()).andReturn("ISO-8859-1");