Unit testing CourseSites Apache Filter
Upcoming SlideShare
Loading in...5
×
 

Unit testing CourseSites Apache Filter

on

  • 1,054 views

Unit Testing Apache Filter with Spring Mock and Mockito

Unit Testing Apache Filter with Spring Mock and Mockito

Statistics

Views

Total Views
1,054
Views on SlideShare
1,054
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Unit testing CourseSites Apache Filter Unit testing CourseSites Apache Filter Presentation Transcript

  • Unit Testing Coursesites
  • Context
    PublicHomePageRedirector
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    //on request to the site / or index.html redirect to the servlet
    //we only want to hit on /
    HttpServletRequesthttpRequest = (HttpServletRequest)request;
    HttpServletResponsehttpResponse = (HttpServletResponse)response;
    String serverName = httpRequest.getHeader("Host");
    intfirstperiodIndex = serverName.indexOf('.');
    intlastperiodIndex = serverName.lastIndexOf('.');
    /*
    * so for wayank12teacher2.coursesites-stage.com
    * hostHeader will be wayank12teacher2 setting the stage to check whether
    * to forward to homepage.jsp or redirects to pages/index.html as in the past
    */
    String hostHeader = serverName.substring(0, firstperiodIndex).toLowerCase();
  • “No class is an island …”
    PublicHomePageRedirectorcollaborates with (depends on)
    ServletRequest
    ServletResponse
    FilterChain
  • Context..(2)
    /* taking care of missing www in the request
    * aka coursesites-stage.com, coursesites.com
    * making sure that redirects from OpenID login through Facebook or Twitter
    * happen flawlessly
    * */
    if (!serverName.startsWith("www") && (firstperiodIndex == lastperiodIndex)) {
    String responseUrlPath = "https://" + "www." + serverName + "/webapps/Bb-sites-course-creation-BBLEARN/pages/index.html";
    httpResponse.sendRedirect(responseUrlPath);
    return;
    }
  • Context…(3)
    else if (serverName.equalsIgnoreCase("coursesites.blackboard.com")){
    serverName = "coursesites.com";
    String responseUrlPath = "https://" + "www." + serverName + "/webapps/Bb-sites-course-creation-BBLEARN/pages/index.html";
    httpResponse.sendRedirect(responseUrlPath);
    return;
    }
  • Scenario(s)
    Request with missing www (1)
    Request from coursesites.blackboard.com (2)
  • Unit Test
    Junit (command line through Ant)
    Mock (for collaborators  ServletRequest, ServletResponse, Chain)
  • TestCase(s)
    public class CourseSitesFilterTest {
    @Test public void testMissingWWW() throws Exception{ … }
    @Test public void testBlackBoardDomain() throws Exception{
    …}
  • Scenario 1
    public void testMissingWWW() throws Exception{ MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); //request.setMethod("GET");
    request.addHeader("Host","coursesites.com");
    //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());
    assertTrue("Redirects to index.html", response.getRedirectedUrl().contains("index.html"));
    }
  • Scenario 2
    public void testBlackBoardDomain() throws Exception{ MockHttpServletRequest request = new MockHttpServletRequest();
    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")); }
  • Junit/Ant
    <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>
  • Results