### Question:
ResourceAction extends Action { @Override public void execute() throws ServletException, IOException, ActionException { InputStream resourceIStream = null; try { resourceIStream = getClass().getResourceAsStream("/org/javasimon/console/resource" + resourcePath); if (resourceIStream == null) { getContext().getResponse().setStatus(HttpServletResponse.SC_NOT_FOUND); throw new ActionException("Resource " + resourcePath + " not found"); } String extension = resourcePath.substring(resourcePath.lastIndexOf('.') + 1).toLowerCase(); String contentType = CONTENT_TYPES.get(extension); if (contentType != null) { getContext().setContentType(contentType); if (contentType.startsWith("text")) { getContext().getResponse().setCharacterEncoding("UTF-8"); } } getContext().getResponse().setHeader("Cache-Control", "public; max-age=300"); copyStream(resourceIStream); } finally { if (resourceIStream != null) { resourceIStream.close(); } } } ResourceAction(ActionContext context, String resourcePath); @Override void execute(); static final String PREFIX; }### Answer:
@Test public void testExecute() throws Exception { TestActionContext context = new TestActionContext("/resource/index.html"); ResourceAction action = new ResourceAction(context, "/index.html"); action.execute(); assertEquals(context.getContentType(), "text/html"); assertTrue(context.toByteArray().length > 128); }
@Test(expectedExceptions = ActionException.class) public void test4xxErrorWhenResourceDoesNotExists() throws Exception { TestActionContext context = new TestActionContext("/resource/non/existing"); try { ResourceAction action = new ResourceAction(context, "/non/existing"); action.execute(); } finally { HttpServletResponse response = context.getResponse(); verify(response).setStatus(HttpServletResponse.SC_NOT_FOUND); } }