IconServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { this.logger.finer( "An icon was requested for " + req.getPathInfo()); File f = this.manager.configurationMngr().findIconFromPath( req.getPathInfo()); if( f == null ) { resp.setStatus( HttpServletResponse.SC_NOT_FOUND ); } else { resp.setContentType( IconUtils.findMimeType( f )); resp.setContentLength((int) f.length()); OutputStream out = resp.getOutputStream(); Utils.copyStream( f, out ); out.flush(); Utils.closeQuietly( out ); } } IconServlet( Manager manager );   }
@Test public void testDoGet() throws Exception { File configurationDirectory = this.folder.newFolder(); this.manager = new Manager(); this.manager.configurationMngr().setWorkingDirectory( configurationDirectory ); File appDir = ConfigurationUtils.findApplicationDirectory( "app", configurationDirectory ); File descDir = new File( appDir, Constants.PROJECT_DIR_DESC ); Assert.assertTrue( descDir.mkdirs()); IconServlet servlet = new IconServlet( this.manager ); File trickFile = new File( descDir, "directory.jpg" ); Assert.assertTrue( trickFile.mkdirs()); HttpServletRequest req = Mockito.mock( HttpServletRequest.class ); HttpServletResponse resp = Mockito.mock( HttpServletResponse.class ); Mockito.when( req.getPathInfo()).thenReturn( "/app/whatever.jpg" ); servlet.doGet( req, resp ); Mockito.verify( resp ).setStatus( HttpServletResponse.SC_NOT_FOUND ); File singleJpgFile = new File( descDir, "whatever.jpg" ); Assert.assertTrue( singleJpgFile.createNewFile()); ServletOutputStream out = Mockito.mock( ServletOutputStream.class ); Mockito.when( resp.getOutputStream()).thenReturn( out ); servlet.doGet( req, resp ); Mockito.verify( resp ).setContentType( IconUtils.MIME_JPG ); }