UkelonnServlet extends FrontendServlet { @Override @Reference public void setLogService(LogService logservice) { super.setLogService(logservice); } UkelonnServlet(); @Override @Reference void setLogService(LogService logservice);  }
@Test public void testGet() throws Exception { MockLogService logservice = new MockLogService(); UkelonnServlet servlet = new UkelonnServlet(); ServletConfig servletConfig = mock(ServletConfig.class); when(servletConfig.getInitParameter("from")).thenReturn("to"); servlet.init(servletConfig); servlet.setLogService(logservice); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn("GET"); when(request.getRequestURI()).thenReturn("http: when(request.getPathInfo()).thenReturn("/"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("text/html", response.getContentType()); assertEquals(200, response.getStatus()); assertThat(response.getBufferSize()).isPositive(); }
@Test public void testDoGetAddTrailingSlash() throws Exception { MockLogService logservice = new MockLogService(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn("GET"); when(request.getRequestURL()).thenReturn(new StringBuffer("http: when(request.getRequestURI()).thenReturn("http: when(request.getServletPath()).thenReturn("/frontend-karaf-demo"); MockHttpServletResponse response = new MockHttpServletResponse(); UkelonnServlet servlet = new UkelonnServlet(); servlet.setLogService(logservice); servlet.service(request, response); assertEquals(302, response.getStatus()); }
@Test public void testDoGetResponseThrowsIOException() throws Exception { MockLogService logservice = new MockLogService(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn("GET"); when(request.getRequestURL()).thenReturn(new StringBuffer("http: when(request.getRequestURI()).thenReturn("http: when(request.getPathInfo()).thenReturn("/"); MockHttpServletResponse response = mock(MockHttpServletResponse.class, CALLS_REAL_METHODS); response.resetAll(); ServletOutputStream streamThrowingIOException = mock(ServletOutputStream.class); doThrow(IOException.class).when(streamThrowingIOException).write(anyInt()); when(response.getOutputStream()).thenReturn(streamThrowingIOException); UkelonnServlet servlet = new UkelonnServlet(); servlet.setLogService(logservice); servlet.service(request, response); assertEquals(500, response.getStatus()); }
@SuppressWarnings("unchecked") @Test public void testDoGetResponseStreamMethodThrowsIOException() throws Exception { MockLogService logservice = new MockLogService(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn("GET"); when(request.getRequestURL()).thenReturn(new StringBuffer("http: when(request.getRequestURI()).thenReturn("http: when(request.getPathInfo()).thenReturn("/"); MockHttpServletResponse response = mock(MockHttpServletResponse.class, CALLS_REAL_METHODS); response.resetAll(); when(response.getOutputStream()).thenThrow(IOException.class); UkelonnServlet servlet = new UkelonnServlet(); servlet.setLogService(logservice); servlet.service(request, response); assertEquals(500, response.getStatus()); }
@Test public void testDoGetResourceNotFound() throws Exception { MockLogService logservice = new MockLogService(); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn("GET"); when(request.getRequestURL()).thenReturn(new StringBuffer("http: when(request.getRequestURI()).thenReturn("http: when(request.getPathInfo()).thenReturn("/static/nosuchname.png"); MockHttpServletResponse response = new MockHttpServletResponse(); UkelonnServlet servlet = new UkelonnServlet(); servlet.setLogService(logservice); servlet.service(request, response); assertEquals(404, response.getErrorCode()); }