Skip to content

Commit 17f92b3

Browse files
committed
Simplifying the Servlet and adding a test for POST
1 parent 0ba3d65 commit 17f92b3

File tree

2 files changed

+22
-62
lines changed

2 files changed

+22
-62
lines changed

servlet/metadata-complete/src/main/java/org/javaee7/servlet/metadata/complete/TestServlet.java

+2-55
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
package org.javaee7.servlet.metadata.complete;
4141

4242
import java.io.IOException;
43-
import java.io.PrintWriter;
4443
import javax.servlet.ServletException;
4544
import javax.servlet.annotation.WebServlet;
4645
import javax.servlet.http.HttpServlet;
@@ -52,67 +51,15 @@
5251
*/
5352
@WebServlet(urlPatterns = "/RandomName")
5453
public class TestServlet extends HttpServlet {
55-
56-
/**
57-
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
58-
* methods.
59-
*
60-
* @param request servlet request
61-
* @param response servlet response
62-
* @throws ServletException if a servlet-specific error occurs
63-
* @throws IOException if an I/O error occurs
64-
*/
65-
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
66-
throws ServletException, IOException {
67-
response.setContentType("text/html;charset=UTF-8");
68-
PrintWriter out = response.getWriter();
69-
out.println("<!DOCTYPE html>");
70-
out.println("<html>");
71-
out.println("<head>");
72-
out.println("<title>Servlet url-pattern in web.xml</title>");
73-
out.println("</head>");
74-
out.println("<body>");
75-
out.println("<h1>Servlet url-pattern in web.xml</h1>");
76-
out.println("</body>");
77-
out.println("</html>");
78-
}
79-
80-
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
81-
/**
82-
* Handles the HTTP <code>GET</code> method.
83-
*
84-
* @param request servlet request
85-
* @param response servlet response
86-
* @throws ServletException if a servlet-specific error occurs
87-
* @throws IOException if an I/O error occurs
88-
*/
8954
@Override
9055
protected void doGet(HttpServletRequest request, HttpServletResponse response)
9156
throws ServletException, IOException {
92-
processRequest(request, response);
57+
response.getWriter().print("my GET");
9358
}
9459

95-
/**
96-
* Handles the HTTP <code>POST</code> method.
97-
*
98-
* @param request servlet request
99-
* @param response servlet response
100-
* @throws ServletException if a servlet-specific error occurs
101-
* @throws IOException if an I/O error occurs
102-
*/
10360
@Override
10461
protected void doPost(HttpServletRequest request, HttpServletResponse response)
10562
throws ServletException, IOException {
106-
processRequest(request, response);
63+
response.getWriter().print("my POST");
10764
}
108-
109-
/**
110-
* Returns a short description of the servlet.
111-
*
112-
* @return a String containing servlet description
113-
*/
114-
@Override
115-
public String getServletInfo() {
116-
return "Short description";
117-
}// </editor-fold>
11865
}

servlet/metadata-complete/src/test/java/org/javaee7/servlet/metadata/complete/TestServletTest.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.javaee7.servlet.metadata.complete;
22

3+
import com.gargoylesoftware.htmlunit.HttpMethod;
4+
import com.gargoylesoftware.htmlunit.TextPage;
35
import com.gargoylesoftware.htmlunit.WebClient;
4-
import com.gargoylesoftware.htmlunit.html.HtmlPage;
6+
import com.gargoylesoftware.htmlunit.WebRequest;
57
import java.io.File;
68
import java.io.IOException;
79
import java.net.URL;
@@ -11,6 +13,7 @@
1113
import org.jboss.shrinkwrap.api.ShrinkWrap;
1214
import org.jboss.shrinkwrap.api.spec.WebArchive;
1315
import static org.junit.Assert.*;
16+
import org.junit.Before;
1417
import org.junit.Test;
1518
import org.junit.runner.RunWith;
1619
import org.xml.sax.SAXException;
@@ -25,6 +28,8 @@ public class TestServletTest {
2528

2629
@ArquillianResource
2730
private URL base;
31+
32+
WebClient webClient;
2833

2934
@Deployment(testable = false)
3035
public static WebArchive createDeployment() {
@@ -33,14 +38,22 @@ public static WebArchive createDeployment() {
3338
addAsWebInfResource((new File(WEBAPP_SRC + "/WEB-INF", "web.xml")));
3439
return war;
3540
}
41+
42+
@Before
43+
public void setup() {
44+
webClient = new WebClient();
45+
}
3646

37-
/**
38-
* Test of processRequest method, of class TestServlet.
39-
*/
4047
@Test
4148
public void testGet() throws IOException, SAXException {
42-
WebClient webClient = new WebClient();
43-
HtmlPage page = webClient.getPage(base + "/TestServlet");
44-
assertEquals("Servlet url-pattern in web.xml", page.getTitleText());
49+
TextPage page = webClient.getPage(base + "/TestServlet");
50+
assertEquals("my GET", page.getContent());
51+
}
52+
53+
@Test
54+
public void testPost() throws IOException, SAXException {
55+
WebRequest request = new WebRequest(new URL(base + "/TestServlet"), HttpMethod.POST);
56+
TextPage page = webClient.getPage(request);
57+
assertEquals("my POST", page.getContent());
4558
}
4659
}

0 commit comments

Comments
 (0)