package com.wizzardo.servlet.war;

import com.wizzardo.servlet.WarBuilder;
import com.wizzardo.servlet.WarTest;
import org.junit.Assert;
import org.junit.Test;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by wizzardo on 08.02.15.
 */
public class TestDispatcher extends WarTest {
    @Override
    protected void customizeWar(WarBuilder builder) {
        builder.addClass(ServletForward.class);
        builder.addClass(Servlet1.class);
        builder.addClass(Servlet2.class);
        builder.addClass(ServletInclude.class);
        builder.addClass(Servlet3.class);
        builder.addClass(Servlet4.class);
        builder.addClass(ServletIncludeByName.class);
        builder.addClass(ServletForwardByName.class);
        builder.getWebXmlBuilder()
                .append(new WarBuilder.ServletMapping(ServletForward.class).url("/test_forwarded"))
                .append(new WarBuilder.ServletMapping(Servlet1.class).url("/forward"))
                .append(new WarBuilder.ServletMapping(Servlet2.class).url("/forward_by_name"))
                .append(new WarBuilder.ServletMapping(ServletInclude.class).url("/test_included"))
                .append(new WarBuilder.ServletMapping(Servlet3.class).url("/include"))
                .append(new WarBuilder.ServletMapping(Servlet4.class).url("/include_by_name"))
                .append(new WarBuilder.ServletMapping(ServletIncludeByName.class).url("/test_included_name"))
                .append(new WarBuilder.ServletMapping(ServletForwardByName.class).url("/test_forwarded_name"))
        ;
    }

    @Test
    public void test_dispatcher() throws IOException {
        servletPath = "/test_forwarded";
        Assert.assertEquals("forwarded", jettyRequest().get().asString());
        Assert.assertEquals("forwarded", myRequest().get().asString());

        servletPath = "/test_forwarded_name";
        Assert.assertEquals("forwarded", jettyRequest().get().asString());
        Assert.assertEquals("forwarded", myRequest().get().asString());


        servletPath = "/test_included";
        Assert.assertEquals("<b>included</b>", jettyRequest().get().asString());
        Assert.assertEquals("<b>included</b>", myRequest().get().asString());

        servletPath = "/test_included_name";
        Assert.assertEquals("<b>included</b>", jettyRequest().get().asString());
        Assert.assertEquals("<b>included</b>", myRequest().get().asString());
    }

    public static class ServletForward extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("/forward");
            requestDispatcher.forward(req, resp);
        }
    }

    public static class Servlet1 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("forwarded");
            Assert.assertEquals("/forward", req.getServletPath());
        }
    }

    public static class Servlet2 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("forwarded");
            Assert.assertEquals("/test_forwarded_name", req.getServletPath());
        }
    }

    public static class ServletInclude extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("/include");
            resp.getWriter().write("<b>");
            requestDispatcher.include(req, resp);
            resp.getWriter().write("</b>");
        }
    }

    public static class Servlet3 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("included");
            Assert.assertEquals("/test_included", req.getServletPath());
        }
    }

    public static class Servlet4 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("included");
            Assert.assertEquals("/test_included_name", req.getServletPath());
        }
    }

    public static class ServletForwardByName extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            RequestDispatcher requestDispatcher = getServletContext().getNamedDispatcher("Servlet2");
            requestDispatcher.forward(req, resp);
        }
    }

    public static class ServletIncludeByName extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            RequestDispatcher requestDispatcher = getServletContext().getNamedDispatcher("Servlet4");
            resp.getWriter().write("<b>");
            requestDispatcher.include(req, resp);
            resp.getWriter().write("</b>");
        }
    }
}

@@ -916,32 +916,130 @@
 dByName.class);%0A
+        builder.addClass(ServletForwardRelative.class);%0A        builder.addClass(Servlet5.class);%0A
         builder.
@@ -1057,16 +1057,16 @@
 ilder()%0A
-
         
@@ -1833,32 +1833,243 @@
 rwarded_name%22))%0A
+                .append(new WarBuilder.ServletMapping(ServletForwardRelative.class).url(%22/relative/test_forward%22))%0A                .append(new WarBuilder.ServletMapping(Servlet5.class).url(%22/relative/forward%22))%0A
         ;%0A    %7D%0A
@@ -2836,32 +2836,32 @@
 ().asString());%0A
-
         Assert.a
@@ -2918,24 +2918,239 @@
 sString());%0A
+%0A%0A        servletPath = %22/relative/test_forward%22;%0A        Assert.assertEquals(%22relative forwarded%22, jettyRequest().get().asString());%0A        Assert.assertEquals(%22relative forwarded%22, myRequest().get().asString());%0A
     %7D%0A%0A    p
@@ -6087,11 +6087,712 @@
  %7D%0A    %7D
+%0A%0A    public static class ServletForwardRelative extends HttpServlet %7B%0A        @Override%0A        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException %7B%0A            RequestDispatcher requestDispatcher = req.getRequestDispatcher(%22forward%22);%0A            requestDispatcher.forward(req, resp);%0A        %7D%0A    %7D%0A%0A    public static class Servlet5 extends HttpServlet %7B%0A        @Override%0A        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException %7B%0A            resp.getWriter().write(%22relative forwarded%22);%0A            Assert.assertEquals(%22/relative/forward%22, req.getServletPath());%0A        %7D%0A    %7D
 %0A%7D%0A

test relative dispatcher