@Test public void initializationTest() throws Exception { UrlTranslationFilter translationFilter = new UrlTranslationFilter(); final ServletContext servletContext = filterMockery.mock(ServletContext.class); filterConfig = filterMockery.mock(FilterConfig.class); filterMockery.checking( new Expectations() { { allowing(filterConfig).getServletContext(); will(returnValue(servletContext)); allowing(filterConfig).getInitParameter(UrlTranslationFilter.ENABLED_PARAM_NAME); will(returnValue("true")); one(filterConfig).getInitParameter(UrlTranslationFilter.CONFIG_RESOURCE_URL_PARAM_NAME); will(returnValue("org/xchain/framework/filter/translation-filter-test.xml")); } }); translationFilter.init(filterConfig); assertTrue(translationFilter.isEnabled()); }
@Test public void filterTest() throws Exception { UrlTranslationFilter translationFilter = new UrlTranslationFilter(); URL controlUrl = new URL("http://api.flickr.com/crossdomain.xml"); ByteArrayOutputStream controlOutputStream = new ByteArrayOutputStream(); InputStream controlInputStream = controlUrl.openConnection().getInputStream(); byte[] buffer = new byte[2048]; int lengthRead; while ((lengthRead = controlInputStream.read(buffer)) != -1) { controlOutputStream.write(buffer, 0, lengthRead); } final HttpServletRequest request = filterMockery.mock(HttpServletRequest.class); final HttpServletResponse response = filterMockery.mock(HttpServletResponse.class); final ServletContext servletContext = filterMockery.mock(ServletContext.class); // Create a SimpleServletOutputStream to hold the response from the servlet. final SimpleServletOutputStream output = new SimpleServletOutputStream(); filterMockery.checking( new Expectations() { { // Request the index.xchain allowing(request).getServletPath(); will(returnValue("/test/crossdomain.xml")); // Return the SimpleServletOutputStream. one(response).getOutputStream(); will(returnValue(output)); allowing(response).setContentLength(with(any(int.class))); allowing(servletContext).getMimeType("/test/crossdomain.xml"); will(returnValue("text/xml")); allowing(response).setContentType(with(any(String.class))); } }); filterConfig = filterMockery.mock(FilterConfig.class); filterMockery.checking( new Expectations() { { allowing(filterConfig).getServletContext(); will(returnValue(servletContext)); allowing(filterConfig).getInitParameter(UrlTranslationFilter.ENABLED_PARAM_NAME); will(returnValue("true")); one(filterConfig).getInitParameter(UrlTranslationFilter.CONFIG_RESOURCE_URL_PARAM_NAME); will(returnValue("org/xchain/framework/filter/translation-filter-test.xml")); } }); translationFilter.init(filterConfig); assertTrue(translationFilter.isEnabled()); translationFilter.doFilter(request, response, null); assertEquals(controlOutputStream.toString(), output.getOutput()); }