Unit testing CourseSites Apache Filter

997 views

Published on

Unit Testing Apache Filter with Spring Mock and Mockito

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
997
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit testing CourseSites Apache Filter

  1. 1. Unit Testing Coursesites<br />
  2. 2. Context<br />PublicHomePageRedirector<br />public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {<br />//on request to the site / or index.html redirect to the servlet<br />//we only want to hit on /<br />HttpServletRequesthttpRequest = (HttpServletRequest)request;<br />HttpServletResponsehttpResponse = (HttpServletResponse)response;<br />String serverName = httpRequest.getHeader("Host");<br />intfirstperiodIndex = serverName.indexOf('.');<br />intlastperiodIndex = serverName.lastIndexOf('.');<br />/* <br /> * so for wayank12teacher2.coursesites-stage.com <br /> * hostHeader will be wayank12teacher2 setting the stage to check whether <br /> * to forward to homepage.jsp or redirects to pages/index.html as in the past<br /> */<br />String hostHeader = serverName.substring(0, firstperiodIndex).toLowerCase();<br />
  3. 3. “No class is an island …”<br />PublicHomePageRedirectorcollaborates with (depends on)<br />ServletRequest<br />ServletResponse<br />FilterChain<br />
  4. 4. Context..(2)<br />/* taking care of missing www in the request <br /> * aka coursesites-stage.com, coursesites.com <br /> * making sure that redirects from OpenID login through Facebook or Twitter <br /> * happen flawlessly<br /> * */<br />if (!serverName.startsWith("www") && (firstperiodIndex == lastperiodIndex)) {<br />String responseUrlPath = "https://" + "www." + serverName + "/webapps/Bb-sites-course-creation-BBLEARN/pages/index.html";<br />httpResponse.sendRedirect(responseUrlPath);<br />return;<br />}<br />
  5. 5. Context…(3)<br />else if (serverName.equalsIgnoreCase("coursesites.blackboard.com")){<br />serverName = "coursesites.com";<br />String responseUrlPath = "https://" + "www." + serverName + "/webapps/Bb-sites-course-creation-BBLEARN/pages/index.html";<br />httpResponse.sendRedirect(responseUrlPath);<br />return;<br />}<br />
  6. 6. Scenario(s)<br />Request with missing www (1)<br />Request from coursesites.blackboard.com (2)<br />
  7. 7. Unit Test<br />Junit (command line through Ant)<br />Mock (for collaborators  ServletRequest, ServletResponse, Chain)<br />
  8. 8. TestCase(s)<br />public class CourseSitesFilterTest {<br /> @Test public void testMissingWWW() throws Exception{ … }<br /> @Test public void testBlackBoardDomain() throws Exception{<br />…}<br />
  9. 9. Scenario 1<br />public void testMissingWWW() throws Exception{ MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); //request.setMethod("GET");<br />request.addHeader("Host","coursesites.com"); <br /> //MockHttpServletRequest request = new FilterChain chain = Mockito.mock(FilterChain.class); CourseSitesFilter filter = new CourseSitesFilter(); filter.doFilter(request, response, chain); //System.out.println(request.getAttribute("hello")); //System.out.println(request.getRequestURI()); //System.out.println(response.getForwardedUrl()); System.out.println("Output: " + response.getRedirectedUrl()); <br />assertTrue("Redirects to index.html", response.getRedirectedUrl().contains("index.html")); <br />} <br />
  10. 10. Scenario 2<br /> public void testBlackBoardDomain() throws Exception{ MockHttpServletRequest request = new MockHttpServletRequest(); <br />MockHttpServletResponse response = new MockHttpServletResponse(); //request.setMethod("GET"); request.addHeader("Host","coursesites.blackboard.com"); //MockHttpServletRequest request = new MockHttpServletRequest("GET", "/invoiceView.app"); FilterChain chain = Mockito.mock(FilterChain.class); CourseSitesFilter filter = new CourseSitesFilter(); filter.doFilter(request, response, chain); //System.out.println(request.getRequestURI()); //System.out.println(response.getForwardedUrl()); System.out.println("Output: " + response.getRedirectedUrl()); assertTrue("Redirects to index.html", response.getRedirectedUrl().contains("index.html")); }<br />
  11. 11. Junit/Ant<br /> <target name="junit" depends="compile"> <mkdir dir="${report.dir}"/> <junitprintsummary="yes" haltonfailure="yes" showoutput="yes"> <classpath> <path refid="classpath"/> <path location="${build.dest}"/> <path location="${build.test.dest}"/> </classpath> <formatter type="xml"/> <batchtest fork="yes"> <fileset dir="${test.dir}" includes="**/*Test.java"/> </batchtest> </junit> </target><br />
  12. 12. Results<br />

×