From 48da29925ab027187fb9bbf4cec832f6f32aadd2 Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 11 Mar 2021 15:43:06 +0200 Subject: [PATCH 01/13] add all the files --- 2-0-servlet-api/hello-servlet-api/pom.xml | 102 ++++++++++++++++++ .../welcome_servlet_web/WelcomeServlet.java | 44 ++++++++ .../src/main/webapp/WEB-INF/web.xml | 6 ++ .../src/main/webapp/index.jsp | 20 ++++ .../src/main/webapp/logo_white.svg | 47 ++++++++ .../com/bobocode/HttpServletConnection.java | 93 ++++++++++++++++ .../java/com/bobocode/WelcomeServletTest.java | 28 +++++ 2-0-servlet-api/pom.xml | 4 + pom.xml | 15 +++ 9 files changed, 359 insertions(+) create mode 100644 2-0-servlet-api/hello-servlet-api/pom.xml create mode 100644 2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/welcome_servlet_web/WelcomeServlet.java create mode 100644 2-0-servlet-api/hello-servlet-api/src/main/webapp/WEB-INF/web.xml create mode 100644 2-0-servlet-api/hello-servlet-api/src/main/webapp/index.jsp create mode 100644 2-0-servlet-api/hello-servlet-api/src/main/webapp/logo_white.svg create mode 100644 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java create mode 100644 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java diff --git a/2-0-servlet-api/hello-servlet-api/pom.xml b/2-0-servlet-api/hello-servlet-api/pom.xml new file mode 100644 index 0000000..838cdfa --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/pom.xml @@ -0,0 +1,102 @@ + + + + 2-0-servlet-api + com.bobocode + 1.0-SNAPSHOT + + 4.0.0 + + hello-servlet-api + war + + + 11 + 11 + UTF-8 + UTF-8 + + + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + org.assertj + assertj-core + 3.18.1 + test + + + + + + + org.apache.maven.plugins + maven-war-plugin + 3.3.0 + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/WelcomeServletTest.java + + + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + + run-tomcat + pre-integration-test + + run + + + + stop-tomcat + post-integration-test + + shutdown + + + + + true + 8080 + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.0.0-M5 + + + + integration-test + + + + + + **/WelcomeServletTest.java + + + + + + + \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/welcome_servlet_web/WelcomeServlet.java b/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/welcome_servlet_web/WelcomeServlet.java new file mode 100644 index 0000000..c05f86d --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/welcome_servlet_web/WelcomeServlet.java @@ -0,0 +1,44 @@ +package com.bobocode.welcome_servlet_web; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * To create a servlet you have to extend this class from {@link HttpServlet}. + * Also add annotation {@link WebServlet} with parameter "/hello-servlet" to map a path. + */ +@WebServlet("/hello-servlet") //todo: initialize the class as a servlet +public class WelcomeServlet extends HttpServlet { + + /** + * This method is overridden from {@link HttpServlet} class. + * It called by the server (container) to allow a servlet to handle a GET request. + * + * @param request an {@link HttpServletRequest} object that contains the request + * the client has made of the servlet. + * @param response an {@link HttpServletResponse} object that contains the response + * the servlet sends to the client. + */ + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("text/html"); + + PrintWriter out = response.getWriter(); + + String parameter = request.getParameter("name"); + out.println(""); + if (parameter == null) { + out.println("

" + "Good job! This page is a response of the servlet." + "

"); + out.println("

" + "You should add your name as a parameter in the URL." + "
"); + out.println("Just add ?name=YourName to the end of the URL" + "

"); + } else { + out.println("

" + "Awesome " + parameter + "! " + + "You've just passed your name to the servlet!" + "

"); + } + out.println(""); + } +} \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/main/webapp/WEB-INF/web.xml b/2-0-servlet-api/hello-servlet-api/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..d80081d --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/main/webapp/index.jsp b/2-0-servlet-api/hello-servlet-api/src/main/webapp/index.jsp new file mode 100644 index 0000000..60dc555 --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/src/main/webapp/index.jsp @@ -0,0 +1,20 @@ +<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + +<%-- +JSP (Java Server Pages) is a server side technology which provides using HTML and Java code in same file. +Its an advanced version of servlets which responsible for a view role in MVC approach. +--%> + + + + Welcome JSP + + +Bobocode +

<%= "Hello! This is index.jsp page." + + "
You can pass a request to the servlet using http://localhost:8080/hello-servlet URL" %> +

+
+Or press this link + + \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/main/webapp/logo_white.svg b/2-0-servlet-api/hello-servlet-api/src/main/webapp/logo_white.svg new file mode 100644 index 0000000..8e3ae0e --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/src/main/webapp/logo_white.svg @@ -0,0 +1,47 @@ + + + + + + + diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java new file mode 100644 index 0000000..18249c9 --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java @@ -0,0 +1,93 @@ +package com.bobocode; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +public class HttpServletConnection { + private static final String USER_AGENT = "Mozilla/5.0"; + + private static final String LOCALHOST_8080_URL = "http://localhost:8080/"; + + private static final String POST_URL = "https://localhost:9090/SpringMVCExample/home"; + + private static final String POST_PARAMS = "userName=Bob"; + +// public static void main(String[] args) throws IOException { +// +// sendGET(); +// System.out.println("GET DONE"); +//// sendPOST(); +//// System.out.println("POST DONE"); +// } + + public StringBuffer sendGET() throws IOException { + HttpURLConnection connection = getHttpURLConnection(LOCALHOST_8080_URL); + int responseCode = connection.getResponseCode(); + System.out.println("GET Response Code :: " + responseCode); + StringBuffer response = null; + if (responseCode == HttpURLConnection.HTTP_OK) { // success + BufferedReader in = new BufferedReader(new InputStreamReader( + connection.getInputStream())); + String inputLine; + response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // print result + System.out.println(response.toString()); + } else { + System.out.println("GET request not worked"); + } + return response; + } + + public HttpURLConnection getHttpURLConnection(String url) throws IOException { + URL obj = new URL(LOCALHOST_8080_URL); + HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("User-Agent", USER_AGENT); + return connection; + } + + public void sendPOST() throws IOException { + URL obj = new URL(POST_URL); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("User-Agent", USER_AGENT); + + // For POST only - START + con.setDoOutput(true); + OutputStream os = con.getOutputStream(); + os.write(POST_PARAMS.getBytes()); + os.flush(); + os.close(); + // For POST only - END + + int responseCode = con.getResponseCode(); + System.out.println("POST Response Code :: " + responseCode); + + if (responseCode == HttpURLConnection.HTTP_OK) { //success + BufferedReader in = new BufferedReader(new InputStreamReader( + con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // print result + System.out.println(response.toString()); + } else { + System.out.println("POST request not worked"); + } + } +} diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java new file mode 100644 index 0000000..8e29068 --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java @@ -0,0 +1,28 @@ +package com.bobocode; + +import com.bobocode.welcome_servlet_web.WelcomeServlet; +import org.junit.jupiter.api.Test; + + +import java.io.IOException; +import java.net.HttpURLConnection; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +public class WelcomeServletTest { + HttpServletConnection servletConnection = new HttpServletConnection(); + + @Test + void contextResponseCodeIsOk() throws IOException { + HttpURLConnection connection = servletConnection.getHttpURLConnection("http://localhost:8080/"); + int code = connection.getResponseCode(); + assertThat(code).isEqualTo(HttpURLConnection.HTTP_OK); + } + + @Test + void requestWhenNameParameterIsAdded() throws IOException { + StringBuffer responseBuffer = servletConnection.sendGET(); + assertThat(responseBuffer.toString().contains("Hello!")).isTrue(); + } +} diff --git a/2-0-servlet-api/pom.xml b/2-0-servlet-api/pom.xml index 2aae7d5..a5b7bf7 100644 --- a/2-0-servlet-api/pom.xml +++ b/2-0-servlet-api/pom.xml @@ -10,6 +10,10 @@ 4.0.0 2-0-servlet-api + pom + + hello-servlet-api + 15 diff --git a/pom.xml b/pom.xml index 674ac9d..72f7c5b 100644 --- a/pom.xml +++ b/pom.xml @@ -23,5 +23,20 @@ 11 + + + org.junit.jupiter + junit-jupiter-engine + 5.7.0 + test + + + org.junit.jupiter + junit-jupiter-params + 5.7.0 + test + + + \ No newline at end of file From b0c44cada1be6d75467e401e23415bab0f9d3aef Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 11 Mar 2021 18:25:21 +0200 Subject: [PATCH 02/13] add http endpoint tests --- 2-0-servlet-api/hello-servlet-api/pom.xml | 26 ++++++-- .../com/bobocode/HttpServletConnection.java | 64 ++----------------- .../java/com/bobocode/WelcomeServletTest.java | 21 ++++-- 3 files changed, 40 insertions(+), 71 deletions(-) diff --git a/2-0-servlet-api/hello-servlet-api/pom.xml b/2-0-servlet-api/hello-servlet-api/pom.xml index 838cdfa..0d83941 100644 --- a/2-0-servlet-api/hello-servlet-api/pom.xml +++ b/2-0-servlet-api/hello-servlet-api/pom.xml @@ -19,6 +19,10 @@ UTF-8 + + 2.2.1 + + javax.servlet @@ -32,6 +36,12 @@ 3.18.1 test + + org.testng + testng + 6.8.8 + test + @@ -47,7 +57,7 @@ maven-surefire-plugin - **/WelcomeServletTest.java + WelcomeServletTest.java @@ -56,12 +66,17 @@ org.apache.tomcat.maven tomcat7-maven-plugin 2.2 + + http://localhost:8080/ + localhost + true + run-tomcat pre-integration-test - run + run-war-only @@ -72,11 +87,7 @@ - - true - 8080 - @@ -87,12 +98,13 @@ integration-test + verify - **/WelcomeServletTest.java + WelcomeServletTest.java diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java index 18249c9..1335e4a 100644 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java @@ -3,33 +3,17 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpServletConnection { private static final String USER_AGENT = "Mozilla/5.0"; - private static final String LOCALHOST_8080_URL = "http://localhost:8080/"; - - private static final String POST_URL = "https://localhost:9090/SpringMVCExample/home"; - - private static final String POST_PARAMS = "userName=Bob"; - -// public static void main(String[] args) throws IOException { -// -// sendGET(); -// System.out.println("GET DONE"); -//// sendPOST(); -//// System.out.println("POST DONE"); -// } - - public StringBuffer sendGET() throws IOException { - HttpURLConnection connection = getHttpURLConnection(LOCALHOST_8080_URL); + public StringBuffer sendGET(String url) throws IOException { + HttpURLConnection connection = getHttpURLConnection(url); int responseCode = connection.getResponseCode(); - System.out.println("GET Response Code :: " + responseCode); StringBuffer response = null; - if (responseCode == HttpURLConnection.HTTP_OK) { // success + if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String inputLine; @@ -39,55 +23,15 @@ public StringBuffer sendGET() throws IOException { response.append(inputLine); } in.close(); - - // print result - System.out.println(response.toString()); - } else { - System.out.println("GET request not worked"); } return response; } public HttpURLConnection getHttpURLConnection(String url) throws IOException { - URL obj = new URL(LOCALHOST_8080_URL); + URL obj = new URL(url); HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("User-Agent", USER_AGENT); return connection; } - - public void sendPOST() throws IOException { - URL obj = new URL(POST_URL); - HttpURLConnection con = (HttpURLConnection) obj.openConnection(); - con.setRequestMethod("POST"); - con.setRequestProperty("User-Agent", USER_AGENT); - - // For POST only - START - con.setDoOutput(true); - OutputStream os = con.getOutputStream(); - os.write(POST_PARAMS.getBytes()); - os.flush(); - os.close(); - // For POST only - END - - int responseCode = con.getResponseCode(); - System.out.println("POST Response Code :: " + responseCode); - - if (responseCode == HttpURLConnection.HTTP_OK) { //success - BufferedReader in = new BufferedReader(new InputStreamReader( - con.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - - // print result - System.out.println(response.toString()); - } else { - System.out.println("POST request not worked"); - } - } } diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java index 8e29068..55df864 100644 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java @@ -1,9 +1,7 @@ package com.bobocode; -import com.bobocode.welcome_servlet_web.WelcomeServlet; import org.junit.jupiter.api.Test; - import java.io.IOException; import java.net.HttpURLConnection; @@ -21,8 +19,23 @@ void contextResponseCodeIsOk() throws IOException { } @Test - void requestWhenNameParameterIsAdded() throws IOException { - StringBuffer responseBuffer = servletConnection.sendGET(); + void contextResponseHasHello() throws IOException { + StringBuffer responseBuffer = servletConnection.sendGET("http://localhost:8080/"); assertThat(responseBuffer.toString().contains("Hello!")).isTrue(); } + + @Test + void servletResponseCodeIsOk() throws IOException { + HttpURLConnection connection = servletConnection + .getHttpURLConnection("http://localhost:8080/hello-servlet"); + int code = connection.getResponseCode(); + assertThat(code).isEqualTo(HttpURLConnection.HTTP_OK); + } + + @Test + void servletResponseWhenParameterIsPresent() throws IOException { + StringBuffer responseBuffer = servletConnection + .sendGET("http://localhost:8080/hello-servlet?name=Bob"); + assertThat(responseBuffer.toString().contains("Bob")).isTrue(); + } } From 4a58c299497205e9bc3d45e7fc326e24d6dbf7e1 Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 11 Mar 2021 18:32:33 +0200 Subject: [PATCH 03/13] fix pom.xml --- 2-0-servlet-api/hello-servlet-api/pom.xml | 60 +------------------ .../com/bobocode/HttpServletConnection.java | 4 +- .../java/com/bobocode/WelcomeServletTest.java | 4 +- 3 files changed, 5 insertions(+), 63 deletions(-) diff --git a/2-0-servlet-api/hello-servlet-api/pom.xml b/2-0-servlet-api/hello-servlet-api/pom.xml index 0d83941..10b6d8b 100644 --- a/2-0-servlet-api/hello-servlet-api/pom.xml +++ b/2-0-servlet-api/hello-servlet-api/pom.xml @@ -19,10 +19,6 @@ UTF-8 - - 2.2.1 - - javax.servlet @@ -36,12 +32,6 @@ 3.18.1 test - - org.testng - testng - 6.8.8 - test - @@ -57,58 +47,10 @@ maven-surefire-plugin - WelcomeServletTest.java + **/WelcomeServletTest.java - - - org.apache.tomcat.maven - tomcat7-maven-plugin - 2.2 - - http://localhost:8080/ - localhost - true - - - - run-tomcat - pre-integration-test - - run-war-only - - - - stop-tomcat - post-integration-test - - shutdown - - - - - - - - org.apache.maven.plugins - maven-failsafe-plugin - 3.0.0-M5 - - - - integration-test - verify - - - - - - WelcomeServletTest.java - - - - \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java index 1335e4a..f08b92e 100644 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java @@ -10,7 +10,7 @@ public class HttpServletConnection { private static final String USER_AGENT = "Mozilla/5.0"; public StringBuffer sendGET(String url) throws IOException { - HttpURLConnection connection = getHttpURLConnection(url); + HttpURLConnection connection = getHttpURLConnectionGet(url); int responseCode = connection.getResponseCode(); StringBuffer response = null; if (responseCode == HttpURLConnection.HTTP_OK) { @@ -27,7 +27,7 @@ public StringBuffer sendGET(String url) throws IOException { return response; } - public HttpURLConnection getHttpURLConnection(String url) throws IOException { + public HttpURLConnection getHttpURLConnectionGet(String url) throws IOException { URL obj = new URL(url); HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); connection.setRequestMethod("GET"); diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java index 55df864..8b68416 100644 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java @@ -13,7 +13,7 @@ public class WelcomeServletTest { @Test void contextResponseCodeIsOk() throws IOException { - HttpURLConnection connection = servletConnection.getHttpURLConnection("http://localhost:8080/"); + HttpURLConnection connection = servletConnection.getHttpURLConnectionGet("http://localhost:8080/"); int code = connection.getResponseCode(); assertThat(code).isEqualTo(HttpURLConnection.HTTP_OK); } @@ -27,7 +27,7 @@ void contextResponseHasHello() throws IOException { @Test void servletResponseCodeIsOk() throws IOException { HttpURLConnection connection = servletConnection - .getHttpURLConnection("http://localhost:8080/hello-servlet"); + .getHttpURLConnectionGet("http://localhost:8080/hello-servlet"); int code = connection.getResponseCode(); assertThat(code).isEqualTo(HttpURLConnection.HTTP_OK); } From 43817ddac2272b42f22f4d1aed4d07dd43e5c548 Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 11 Mar 2021 19:09:20 +0200 Subject: [PATCH 04/13] add README.MD --- 2-0-servlet-api/hello-servlet-api/README.MD | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2-0-servlet-api/hello-servlet-api/README.MD diff --git a/2-0-servlet-api/hello-servlet-api/README.MD b/2-0-servlet-api/hello-servlet-api/README.MD new file mode 100644 index 0000000..a2c92ff --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/README.MD @@ -0,0 +1,42 @@ +# Welcome Servlet +Your first acquaintance with Servlets, JSP and servlet Container πŸ‘€ + +[Servlets are the Java platform technology of choice for extending and enhancing Web servers.](https://www.oracle.com/java/technologies/servlet-technology.html) + +### Pre-conditions ❗ +You're supposed to be familiar with [Java Fundamentals](https://github.com/bobocode-projects/java-fundamentals-course) + +### Objectives +* **install** [Apache Tomcat Container](https://tomcat.apache.org/download-90.cgi) if you didn't βœ… + + -*Servlets don’t have any main() method. Instead, are used Containers to control Servlets. + Containers are other Java applications such as Tomcat, GlassFish or Jetty. You have to install one of it if you didn't. + We suggest to use Tomcat as more lightweight and easier in configuration.* +* βš™ **configure a Container** depending on your environment βœ… + - [IntelliJ IDEA:](https://www.jetbrains.com/help/idea/run-debug-configuration-tomcat-server.html) + 1. Create a new server configuration: + + **Run** β–Ά **Edit configurations...** β–Ά (βž•)Add New Configuration β–Ά Tomcat Server (Local) + 2. Configure or choose Application Server in **Run/Debug Configurations**: + + **Server** tab β–Ά **Configure** β–Ά (βž•)**Add Application Server** β–Ά Add a path to folder with Tomcat + 3. Deploy welcome-servlet application into Container in **Run/Debug Configurations**: + + **Deployment** tab β–Ά (βž•)Artifact β–Ά `welcome-servlet:war exploded` β–Ά ❗ Clear **Application Context** field + + - Eclipse + - Open Eclipse Java EE (Enterprise edition ) environment. Click on Servers tab at bottom. Click on No servers are available. + - A dialog box will appear. Select Tomcat 9.0 server folder. Click Next. + - Browse to Apache Tomcat 9.0 folder select it. Click Finish. +* **complete TODO section** in `WelcomeServlet` class βœ… +* **run configured server** βœ… + - If everything is configured properly, and the server is run index.jsp will appear on [http://localhost:8080/](http://localhost:8080/) +* **run `WelcomeServletTest`** while the server is running to check all the endpoints βœ… +* **send request to the servlet** using [http://localhost:8080/welcome-servlet](http://localhost:8080/welcome-servlet) URL βœ… +* **pass your name** as property of servlets' URL βœ… + +### Related Materials ℹ️ +todo + +--- +#### πŸ†• First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-course/tree/main/0-0-intro#introduction) From 5577348f475ef080507cbdd3989fda33ea9b6371 Mon Sep 17 00:00:00 2001 From: Serhii Date: Fri, 12 Mar 2021 19:40:36 +0200 Subject: [PATCH 05/13] redo exercise --- 2-0-servlet-api/hello-servlet-api/pom.xml | 33 ++++++------ .../bobocode/hello_servlet/DateServlet.java | 24 +++++++++ .../WelcomeServlet.java | 24 ++++----- .../src/main/webapp/index.jsp | 20 ------- .../src/main/webapp/logo_white.svg | 47 ----------------- ...omeServletTest.java => DateServletIT.java} | 17 +++--- .../java/com/bobocode/DateServletTest.java | 52 +++++++++++++++++++ 2-0-servlet-api/pom.xml | 9 ++++ pom.xml | 13 ++++- 9 files changed, 133 insertions(+), 106 deletions(-) create mode 100644 2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java rename 2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/{welcome_servlet_web => hello_servlet}/WelcomeServlet.java (55%) delete mode 100644 2-0-servlet-api/hello-servlet-api/src/main/webapp/index.jsp delete mode 100644 2-0-servlet-api/hello-servlet-api/src/main/webapp/logo_white.svg rename 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/{WelcomeServletTest.java => DateServletIT.java} (69%) create mode 100644 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java diff --git a/2-0-servlet-api/hello-servlet-api/pom.xml b/2-0-servlet-api/hello-servlet-api/pom.xml index 10b6d8b..153ff39 100644 --- a/2-0-servlet-api/hello-servlet-api/pom.xml +++ b/2-0-servlet-api/hello-servlet-api/pom.xml @@ -19,21 +19,6 @@ UTF-8 - - - javax.servlet - javax.servlet-api - 4.0.1 - provided - - - org.assertj - assertj-core - 3.18.1 - test - - - @@ -45,12 +30,28 @@ org.apache.maven.plugins maven-surefire-plugin + 3.0.0-M3 - **/WelcomeServletTest.java + **/DateServletIT.java + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.12.4 + + + integration-test + + integration-test + verify + + + + \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java b/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java new file mode 100644 index 0000000..aaa09bd --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java @@ -0,0 +1,24 @@ +package com.bobocode.hello_servlet; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.time.LocalDate; + +@WebServlet("/date") +public class DateServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + out.println(""); + LocalDate date = LocalDate.now(); + out.println("

" + date + "

"); + out.println(""); + out.close(); + } +} diff --git a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/welcome_servlet_web/WelcomeServlet.java b/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/WelcomeServlet.java similarity index 55% rename from 2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/welcome_servlet_web/WelcomeServlet.java rename to 2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/WelcomeServlet.java index c05f86d..10aa6f9 100644 --- a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/welcome_servlet_web/WelcomeServlet.java +++ b/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/WelcomeServlet.java @@ -1,4 +1,4 @@ -package com.bobocode.welcome_servlet_web; +package com.bobocode.hello_servlet; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -8,15 +8,15 @@ import java.io.PrintWriter; /** - * To create a servlet you have to extend this class from {@link HttpServlet}. - * Also add annotation {@link WebServlet} with parameter "/hello-servlet" to map a path. + * To create a servlet you have to extend your class from {@link HttpServlet}. + * Also add annotation {@link WebServlet} with parameter to map a path for URL. */ -@WebServlet("/hello-servlet") //todo: initialize the class as a servlet +@WebServlet("/") public class WelcomeServlet extends HttpServlet { /** * This method is overridden from {@link HttpServlet} class. - * It called by the server (container) to allow a servlet to handle a GET request. + * It called by the server (container) to allow servlets to handle GET requests. * * @param request an {@link HttpServletRequest} object that contains the request * the client has made of the servlet. @@ -29,16 +29,12 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro PrintWriter out = response.getWriter(); - String parameter = request.getParameter("name"); out.println(""); - if (parameter == null) { - out.println("

" + "Good job! This page is a response of the servlet." + "

"); - out.println("

" + "You should add your name as a parameter in the URL." + "
"); - out.println("Just add ?name=YourName to the end of the URL" + "

"); - } else { - out.println("

" + "Awesome " + parameter + "! " - + "You've just passed your name to the servlet!" + "

"); - } + out.println("\"Bobocode\""); + out.println("

" + "Good job! This page is a response of WelcomeServlet object." + "

"); + out.println("

" + "You should create your own class DateServlet which returns " + + "current date as a response on /date request.
Use LocalDate.now() " + + "to get current date." + "

"); out.println(""); } } \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/main/webapp/index.jsp b/2-0-servlet-api/hello-servlet-api/src/main/webapp/index.jsp deleted file mode 100644 index 60dc555..0000000 --- a/2-0-servlet-api/hello-servlet-api/src/main/webapp/index.jsp +++ /dev/null @@ -1,20 +0,0 @@ -<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> - -<%-- -JSP (Java Server Pages) is a server side technology which provides using HTML and Java code in same file. -Its an advanced version of servlets which responsible for a view role in MVC approach. ---%> - - - - Welcome JSP - - -Bobocode -

<%= "Hello! This is index.jsp page." + - "
You can pass a request to the servlet using http://localhost:8080/hello-servlet URL" %> -

-
-Or press this link - - \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/main/webapp/logo_white.svg b/2-0-servlet-api/hello-servlet-api/src/main/webapp/logo_white.svg deleted file mode 100644 index 8e3ae0e..0000000 --- a/2-0-servlet-api/hello-servlet-api/src/main/webapp/logo_white.svg +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java similarity index 69% rename from 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java rename to 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java index 8b68416..ab5c0b6 100644 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/WelcomeServletTest.java +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java @@ -4,11 +4,12 @@ import java.io.IOException; import java.net.HttpURLConnection; +import java.time.LocalDate; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -public class WelcomeServletTest { +public class DateServletIT { HttpServletConnection servletConnection = new HttpServletConnection(); @Test @@ -21,21 +22,23 @@ void contextResponseCodeIsOk() throws IOException { @Test void contextResponseHasHello() throws IOException { StringBuffer responseBuffer = servletConnection.sendGET("http://localhost:8080/"); - assertThat(responseBuffer.toString().contains("Hello!")).isTrue(); + assertThat(responseBuffer.toString().contains("Good job!")).isTrue(); } @Test - void servletResponseCodeIsOk() throws IOException { + void dateServletResponseCodeIsOk() throws IOException { HttpURLConnection connection = servletConnection - .getHttpURLConnectionGet("http://localhost:8080/hello-servlet"); + .getHttpURLConnectionGet("http://localhost:8080/date"); int code = connection.getResponseCode(); assertThat(code).isEqualTo(HttpURLConnection.HTTP_OK); } @Test - void servletResponseWhenParameterIsPresent() throws IOException { + void dateServletResponseHasDate() throws IOException { StringBuffer responseBuffer = servletConnection - .sendGET("http://localhost:8080/hello-servlet?name=Bob"); - assertThat(responseBuffer.toString().contains("Bob")).isTrue(); + .sendGET("http://localhost:8080/date"); + String date = LocalDate.now().toString(); + System.out.println(date); + assertThat(responseBuffer.toString().contains(date)).isTrue(); } } diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java new file mode 100644 index 0000000..58cf3a3 --- /dev/null +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java @@ -0,0 +1,52 @@ +package com.bobocode; + +import org.junit.jupiter.api.*; +import org.reflections.Reflections; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import java.util.Set; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class DateServletTest { + public static final String SERVLET_PACKAGE = "com.bobocode.hello_servlet"; + public static final String DATE_SERVLET = "DateServlet"; + Reflections reflections; + + @BeforeEach + public void init() { + reflections = new Reflections(SERVLET_PACKAGE); + } + + @Test + @Order(1) + void dateServletClassExisting() throws ClassNotFoundException { + Class.forName(SERVLET_PACKAGE + "." + DATE_SERVLET); + } + + @Test + @Order(2) + void dateServletIsHttpServlet() { + + Set> httpServlets = + reflections.getSubTypesOf(HttpServlet.class); + + httpServlets.stream() + .map(Class::getSimpleName) + .filter(servlet -> servlet.equals(DATE_SERVLET)) + .findAny() + .orElseThrow(); + } + + @Test + @Order(3) + void dateServletIsAnnotated() { + Set> servlets = + reflections.getTypesAnnotatedWith(WebServlet.class); + servlets.stream() + .map(Class::getSimpleName) + .filter(servlet -> servlet.equals(DATE_SERVLET)) + .findAny() + .orElseThrow(); + } +} \ No newline at end of file diff --git a/2-0-servlet-api/pom.xml b/2-0-servlet-api/pom.xml index a5b7bf7..b2c5d44 100644 --- a/2-0-servlet-api/pom.xml +++ b/2-0-servlet-api/pom.xml @@ -20,4 +20,13 @@ 15 + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 72f7c5b..483c8b6 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,16 @@ 5.7.0 test + + org.assertj + assertj-core + 3.18.1 + test + + + org.reflections + reflections + 0.9.12 + - - \ No newline at end of file From 29b052153105b9c0a91dc6584e36192209e62b61 Mon Sep 17 00:00:00 2001 From: Serhii Date: Sun, 14 Mar 2021 17:35:42 +0200 Subject: [PATCH 06/13] add mockito tests --- 2-0-servlet-api/hello-servlet-api/pom.xml | 15 ++++ .../bobocode/hello_servlet/DateServlet.java | 2 +- .../test/java/com/bobocode/DateServletIT.java | 4 +- .../java/com/bobocode/DateServletTest.java | 70 ++++++++++++++++++- 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/2-0-servlet-api/hello-servlet-api/pom.xml b/2-0-servlet-api/hello-servlet-api/pom.xml index 153ff39..08830a2 100644 --- a/2-0-servlet-api/hello-servlet-api/pom.xml +++ b/2-0-servlet-api/hello-servlet-api/pom.xml @@ -19,6 +19,21 @@ UTF-8 + + + org.mockito + mockito-core + 3.8.0 + test + + + org.mockito + mockito-junit-jupiter + 3.8.0 + test + + + diff --git a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java b/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java index aaa09bd..6d13b23 100644 --- a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java +++ b/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java @@ -12,7 +12,7 @@ public class DateServlet extends HttpServlet { @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java index ab5c0b6..7c246b8 100644 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java @@ -8,7 +8,9 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - +/** + * These tests are optional and must run after starting and deploying. +*/ public class DateServletIT { HttpServletConnection servletConnection = new HttpServletConnection(); diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java index 58cf3a3..89e77a5 100644 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java +++ b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java @@ -1,21 +1,56 @@ package com.bobocode; import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.reflections.Reflections; +import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.time.LocalDate; import java.util.Set; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + @TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@ExtendWith(MockitoExtension.class) public class DateServletTest { + + @Mock + private HttpServletRequest request; + + @Mock + private HttpServletResponse response; + + @Mock + private ServletOutputStream outputStream; + + private Object dateServletObject; + private Class dateServletClass; + public static final String SERVLET_PACKAGE = "com.bobocode.hello_servlet"; public static final String DATE_SERVLET = "DateServlet"; Reflections reflections; @BeforeEach - public void init() { + public void init() throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, + InstantiationException { reflections = new Reflections(SERVLET_PACKAGE); + dateServletClass = Class.forName(SERVLET_PACKAGE + "." + DATE_SERVLET); + dateServletObject = dateServletClass.getConstructors()[0].newInstance(); } @Test @@ -49,4 +84,37 @@ void dateServletIsAnnotated() { .findAny() .orElseThrow(); } + + @Test + @Order(4) + void dateServletContentTypeIsProper() throws IllegalAccessException, + InvocationTargetException, NoSuchMethodException, IOException { + Method doGetMethod = getDoGetMethod(); + + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + when(response.getWriter()).thenReturn(printWriter); + + doGetMethod.invoke(dateServletObject, request, response); + verify(response).setContentType("text/html"); + } + + @Test + @Order(5) + void dateServletReturnsDateInResponse() throws IOException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException { + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + when(response.getWriter()).thenReturn(printWriter); + Method doGetMethod = getDoGetMethod(); + + doGetMethod.invoke(dateServletObject, request, response); + assertThat(stringWriter.getBuffer().toString().contains(LocalDate.now().toString())).isTrue(); + } + + private Method getDoGetMethod() throws NoSuchMethodException { + Method doGetMethod = dateServletClass + .getMethod("doGet", HttpServletRequest.class, HttpServletResponse.class); + return doGetMethod; + } } \ No newline at end of file From 467aef96ea00b129041547ea520f281f82d3a399 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 15 Mar 2021 11:26:41 +0200 Subject: [PATCH 07/13] update README.MD --- 2-0-servlet-api/hello-servlet-api/README.MD | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/2-0-servlet-api/hello-servlet-api/README.MD b/2-0-servlet-api/hello-servlet-api/README.MD index a2c92ff..d1e117e 100644 --- a/2-0-servlet-api/hello-servlet-api/README.MD +++ b/2-0-servlet-api/hello-servlet-api/README.MD @@ -1,4 +1,4 @@ -# Welcome Servlet +# Welcome Date Servlet Your first acquaintance with Servlets, JSP and servlet Container πŸ‘€ [Servlets are the Java platform technology of choice for extending and enhancing Web servers.](https://www.oracle.com/java/technologies/servlet-technology.html) @@ -28,12 +28,16 @@ You're supposed to be familiar with [Java Fundamentals](https://github.com/boboc - Open Eclipse Java EE (Enterprise edition ) environment. Click on Servers tab at bottom. Click on No servers are available. - A dialog box will appear. Select Tomcat 9.0 server folder. Click Next. - Browse to Apache Tomcat 9.0 folder select it. Click Finish. -* **complete TODO section** in `WelcomeServlet` class βœ… +* **create a new Servlet** with a name `DateServlet` βœ… + 1. Create a new class `DateServlet` in the same package + 2. Extend the class from `HttpServlet` + 3. Annotate the class as `@WebServlet` with parameter `"/date""` to create a path to the servlet. + 4. Override `doGet` method to return present date in the response using `LocalDate.now()`. +* **run `DateServletTest`** to check your code βœ… * **run configured server** βœ… - - If everything is configured properly, and the server is run index.jsp will appear on [http://localhost:8080/](http://localhost:8080/) + - If everything is configured properly, the server should run `WelcomeServlet` on [http://localhost:8080/](http://localhost:8080/) * **run `WelcomeServletTest`** while the server is running to check all the endpoints βœ… -* **send request to the servlet** using [http://localhost:8080/welcome-servlet](http://localhost:8080/welcome-servlet) URL βœ… -* **pass your name** as property of servlets' URL βœ… +* **send request to `DateServlet`** using [http://localhost:8080/date](http://localhost:8080/date) URL in order to get present date βœ… ### Related Materials ℹ️ todo From b84cc7d4307e178fb4bc85b48a22c8b5aae639e5 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 15 Mar 2021 13:07:52 +0200 Subject: [PATCH 08/13] PR review update --- .../README.MD | 0 .../pom.xml | 14 +----- .../com/bobocode/servlet}/DateServlet.java | 6 +-- .../com/bobocode/servlet}/WelcomeServlet.java | 4 +- .../src/main/webapp/WEB-INF/web.xml | 0 .../java/com/bobocode/DateServletTest.java | 11 ++--- .../test/java/com/bobocode/DateServletIT.java | 46 ------------------- .../com/bobocode/HttpServletConnection.java | 37 --------------- 2-0-servlet-api/pom.xml | 2 +- 9 files changed, 12 insertions(+), 108 deletions(-) rename 2-0-servlet-api/{hello-servlet-api => 2-0-1-date-servlet-api}/README.MD (100%) rename 2-0-servlet-api/{hello-servlet-api => 2-0-1-date-servlet-api}/pom.xml (80%) rename 2-0-servlet-api/{hello-servlet-api/src/main/java/com/bobocode/hello_servlet => 2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet}/DateServlet.java (77%) rename 2-0-servlet-api/{hello-servlet-api/src/main/java/com/bobocode/hello_servlet => 2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet}/WelcomeServlet.java (94%) rename 2-0-servlet-api/{hello-servlet-api => 2-0-1-date-servlet-api}/src/main/webapp/WEB-INF/web.xml (100%) rename 2-0-servlet-api/{hello-servlet-api => 2-0-1-date-servlet-api}/src/test/java/com/bobocode/DateServletTest.java (92%) delete mode 100644 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java delete mode 100644 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java diff --git a/2-0-servlet-api/hello-servlet-api/README.MD b/2-0-servlet-api/2-0-1-date-servlet-api/README.MD similarity index 100% rename from 2-0-servlet-api/hello-servlet-api/README.MD rename to 2-0-servlet-api/2-0-1-date-servlet-api/README.MD diff --git a/2-0-servlet-api/hello-servlet-api/pom.xml b/2-0-servlet-api/2-0-1-date-servlet-api/pom.xml similarity index 80% rename from 2-0-servlet-api/hello-servlet-api/pom.xml rename to 2-0-servlet-api/2-0-1-date-servlet-api/pom.xml index 08830a2..80e0930 100644 --- a/2-0-servlet-api/hello-servlet-api/pom.xml +++ b/2-0-servlet-api/2-0-1-date-servlet-api/pom.xml @@ -9,7 +9,7 @@ 4.0.0 - hello-servlet-api + 2-0-1-date-servlet-api war @@ -41,17 +41,7 @@ maven-war-plugin 3.3.0 - - - org.apache.maven.plugins - maven-surefire-plugin - 3.0.0-M3 - - - **/DateServletIT.java - - - + org.apache.maven.plugins diff --git a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java similarity index 77% rename from 2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java rename to 2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java index 6d13b23..ec18db6 100644 --- a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/DateServlet.java +++ b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java @@ -1,4 +1,4 @@ -package com.bobocode.hello_servlet; +package com.bobocode.servlet; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -15,10 +15,8 @@ public class DateServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); - out.println(""); LocalDate date = LocalDate.now(); - out.println("

" + date + "

"); - out.println(""); + out.println(date); out.close(); } } diff --git a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/WelcomeServlet.java b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/WelcomeServlet.java similarity index 94% rename from 2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/WelcomeServlet.java rename to 2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/WelcomeServlet.java index 10aa6f9..c185265 100644 --- a/2-0-servlet-api/hello-servlet-api/src/main/java/com/bobocode/hello_servlet/WelcomeServlet.java +++ b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/WelcomeServlet.java @@ -1,4 +1,4 @@ -package com.bobocode.hello_servlet; +package com.bobocode.servlet; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -33,7 +33,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro out.println("\"Bobocode\""); out.println("

" + "Good job! This page is a response of WelcomeServlet object." + "

"); out.println("

" + "You should create your own class DateServlet which returns " + - "current date as a response on /date request.
Use LocalDate.now() " + + "current date as a response on /date path.
Use LocalDate.now() " + "to get current date." + "

"); out.println(""); } diff --git a/2-0-servlet-api/hello-servlet-api/src/main/webapp/WEB-INF/web.xml b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from 2-0-servlet-api/hello-servlet-api/src/main/webapp/WEB-INF/web.xml rename to 2-0-servlet-api/2-0-1-date-servlet-api/src/main/webapp/WEB-INF/web.xml diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java b/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java similarity index 92% rename from 2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java rename to 2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java index 89e77a5..31793d7 100644 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletTest.java +++ b/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java @@ -41,7 +41,7 @@ public class DateServletTest { private Object dateServletObject; private Class dateServletClass; - public static final String SERVLET_PACKAGE = "com.bobocode.hello_servlet"; + public static final String SERVLET_PACKAGE = "com.bobocode.servlet"; public static final String DATE_SERVLET = "DateServlet"; Reflections reflections; @@ -55,13 +55,13 @@ public void init() throws ClassNotFoundException, IllegalAccessException, Invoca @Test @Order(1) - void dateServletClassExisting() throws ClassNotFoundException { + void dateServletClassExists() throws ClassNotFoundException { Class.forName(SERVLET_PACKAGE + "." + DATE_SERVLET); } @Test @Order(2) - void dateServletIsHttpServlet() { + void dateServletExtendsHttpServlet() { Set> httpServlets = reflections.getSubTypesOf(HttpServlet.class); @@ -75,7 +75,7 @@ void dateServletIsHttpServlet() { @Test @Order(3) - void dateServletIsAnnotated() { + void dateServletIsMarkedAsWebServlet() { Set> servlets = reflections.getTypesAnnotatedWith(WebServlet.class); servlets.stream() @@ -113,8 +113,7 @@ void dateServletReturnsDateInResponse() throws IOException, NoSuchMethodExceptio } private Method getDoGetMethod() throws NoSuchMethodException { - Method doGetMethod = dateServletClass + return dateServletClass .getMethod("doGet", HttpServletRequest.class, HttpServletResponse.class); - return doGetMethod; } } \ No newline at end of file diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java deleted file mode 100644 index 7c246b8..0000000 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/DateServletIT.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.bobocode; - -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.time.LocalDate; - -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - -/** - * These tests are optional and must run after starting and deploying. -*/ -public class DateServletIT { - HttpServletConnection servletConnection = new HttpServletConnection(); - - @Test - void contextResponseCodeIsOk() throws IOException { - HttpURLConnection connection = servletConnection.getHttpURLConnectionGet("http://localhost:8080/"); - int code = connection.getResponseCode(); - assertThat(code).isEqualTo(HttpURLConnection.HTTP_OK); - } - - @Test - void contextResponseHasHello() throws IOException { - StringBuffer responseBuffer = servletConnection.sendGET("http://localhost:8080/"); - assertThat(responseBuffer.toString().contains("Good job!")).isTrue(); - } - - @Test - void dateServletResponseCodeIsOk() throws IOException { - HttpURLConnection connection = servletConnection - .getHttpURLConnectionGet("http://localhost:8080/date"); - int code = connection.getResponseCode(); - assertThat(code).isEqualTo(HttpURLConnection.HTTP_OK); - } - - @Test - void dateServletResponseHasDate() throws IOException { - StringBuffer responseBuffer = servletConnection - .sendGET("http://localhost:8080/date"); - String date = LocalDate.now().toString(); - System.out.println(date); - assertThat(responseBuffer.toString().contains(date)).isTrue(); - } -} diff --git a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java b/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java deleted file mode 100644 index f08b92e..0000000 --- a/2-0-servlet-api/hello-servlet-api/src/test/java/com/bobocode/HttpServletConnection.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.bobocode; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - -public class HttpServletConnection { - private static final String USER_AGENT = "Mozilla/5.0"; - - public StringBuffer sendGET(String url) throws IOException { - HttpURLConnection connection = getHttpURLConnectionGet(url); - int responseCode = connection.getResponseCode(); - StringBuffer response = null; - if (responseCode == HttpURLConnection.HTTP_OK) { - BufferedReader in = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - String inputLine; - response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - } - return response; - } - - public HttpURLConnection getHttpURLConnectionGet(String url) throws IOException { - URL obj = new URL(url); - HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); - connection.setRequestMethod("GET"); - connection.setRequestProperty("User-Agent", USER_AGENT); - return connection; - } -} diff --git a/2-0-servlet-api/pom.xml b/2-0-servlet-api/pom.xml index b2c5d44..20d5b4f 100644 --- a/2-0-servlet-api/pom.xml +++ b/2-0-servlet-api/pom.xml @@ -12,7 +12,7 @@ 2-0-servlet-api pom - hello-servlet-api + 2-0-1-date-servlet-api From 93666c476ec46bd30a22d01be75f81c23eb7242a Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 15 Mar 2021 13:34:55 +0200 Subject: [PATCH 09/13] update tests --- .../java/com/bobocode/DateServletTest.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java b/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java index 31793d7..a0422c8 100644 --- a/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java +++ b/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java @@ -17,10 +17,10 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.time.LocalDate; +import java.util.Optional; import java.util.Set; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -66,11 +66,11 @@ void dateServletExtendsHttpServlet() { Set> httpServlets = reflections.getSubTypesOf(HttpServlet.class); - httpServlets.stream() + Optional anyDateHttpServlet = httpServlets.stream() .map(Class::getSimpleName) .filter(servlet -> servlet.equals(DATE_SERVLET)) - .findAny() - .orElseThrow(); + .findAny(); + assertThat(anyDateHttpServlet).isNotEmpty(); } @Test @@ -78,15 +78,23 @@ void dateServletExtendsHttpServlet() { void dateServletIsMarkedAsWebServlet() { Set> servlets = reflections.getTypesAnnotatedWith(WebServlet.class); - servlets.stream() + Optional anyMarkedDateServlet = servlets.stream() .map(Class::getSimpleName) .filter(servlet -> servlet.equals(DATE_SERVLET)) - .findAny() - .orElseThrow(); + .findAny(); + assertThat(anyMarkedDateServlet).isNotEmpty(); } @Test @Order(4) + void dateServletIsMarkedWithProperPath() throws ClassNotFoundException { + String[] value = Class.forName(SERVLET_PACKAGE + "." + DATE_SERVLET) + .getAnnotation(WebServlet.class).value(); + assertThat(value).contains("/date"); + } + + @Test + @Order(5) void dateServletContentTypeIsProper() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException { Method doGetMethod = getDoGetMethod(); @@ -100,16 +108,17 @@ void dateServletContentTypeIsProper() throws IllegalAccessException, } @Test - @Order(5) + @Order(6) void dateServletReturnsDateInResponse() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method doGetMethod = getDoGetMethod(); + StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); when(response.getWriter()).thenReturn(printWriter); - Method doGetMethod = getDoGetMethod(); doGetMethod.invoke(dateServletObject, request, response); - assertThat(stringWriter.getBuffer().toString().contains(LocalDate.now().toString())).isTrue(); + assertThat(stringWriter.getBuffer().toString()).contains(LocalDate.now().toString()); } private Method getDoGetMethod() throws NoSuchMethodException { From 7601b019f7d9275b242df96d2f5758b39ea0ea53 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 15 Mar 2021 13:37:04 +0200 Subject: [PATCH 10/13] remove DateServlet from MAIN --- .../com/bobocode/servlet/DateServlet.java | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java diff --git a/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java deleted file mode 100644 index ec18db6..0000000 --- a/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/DateServlet.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.bobocode.servlet; - -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; -import java.time.LocalDate; - -@WebServlet("/date") -public class DateServlet extends HttpServlet { - - @Override - public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { - response.setContentType("text/html"); - PrintWriter out = response.getWriter(); - LocalDate date = LocalDate.now(); - out.println(date); - out.close(); - } -} From a0e27815a87c9567a199bb01c17268a66be2d953 Mon Sep 17 00:00:00 2001 From: Serhii Date: Mon, 15 Mar 2021 13:40:31 +0200 Subject: [PATCH 11/13] fix readme --- 2-0-servlet-api/2-0-1-date-servlet-api/README.MD | 1 - 1 file changed, 1 deletion(-) diff --git a/2-0-servlet-api/2-0-1-date-servlet-api/README.MD b/2-0-servlet-api/2-0-1-date-servlet-api/README.MD index d1e117e..b8999b8 100644 --- a/2-0-servlet-api/2-0-1-date-servlet-api/README.MD +++ b/2-0-servlet-api/2-0-1-date-servlet-api/README.MD @@ -36,7 +36,6 @@ You're supposed to be familiar with [Java Fundamentals](https://github.com/boboc * **run `DateServletTest`** to check your code βœ… * **run configured server** βœ… - If everything is configured properly, the server should run `WelcomeServlet` on [http://localhost:8080/](http://localhost:8080/) -* **run `WelcomeServletTest`** while the server is running to check all the endpoints βœ… * **send request to `DateServlet`** using [http://localhost:8080/date](http://localhost:8080/date) URL in order to get present date βœ… ### Related Materials ℹ️ From 7b61e2159919cd2d96ad105f402683587f8df8ee Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 18 Mar 2021 11:40:37 +0200 Subject: [PATCH 12/13] review fixes --- 2-0-servlet-api/2-0-1-date-servlet-api/pom.xml | 16 ---------------- .../com/bobocode/servlet/WelcomeServlet.java | 3 ++- .../src/main/webapp/WEB-INF/web.xml | 6 ------ 3 files changed, 2 insertions(+), 23 deletions(-) delete mode 100644 2-0-servlet-api/2-0-1-date-servlet-api/src/main/webapp/WEB-INF/web.xml diff --git a/2-0-servlet-api/2-0-1-date-servlet-api/pom.xml b/2-0-servlet-api/2-0-1-date-servlet-api/pom.xml index 80e0930..b1c6a26 100644 --- a/2-0-servlet-api/2-0-1-date-servlet-api/pom.xml +++ b/2-0-servlet-api/2-0-1-date-servlet-api/pom.xml @@ -41,22 +41,6 @@ maven-war-plugin 3.3.0
- - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.12.4 - - - integration-test - - integration-test - verify - - - -
\ No newline at end of file diff --git a/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/WelcomeServlet.java b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/WelcomeServlet.java index c185265..d6c54a4 100644 --- a/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/WelcomeServlet.java +++ b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/java/com/bobocode/servlet/WelcomeServlet.java @@ -30,7 +30,8 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro PrintWriter out = response.getWriter(); out.println(""); - out.println("\"Bobocode\""); + out.println(""); out.println("

" + "Good job! This page is a response of WelcomeServlet object." + "

"); out.println("

" + "You should create your own class DateServlet which returns " + "current date as a response on /date path.
Use LocalDate.now() " + diff --git a/2-0-servlet-api/2-0-1-date-servlet-api/src/main/webapp/WEB-INF/web.xml b/2-0-servlet-api/2-0-1-date-servlet-api/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index d80081d..0000000 --- a/2-0-servlet-api/2-0-1-date-servlet-api/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - \ No newline at end of file From 5fb4e2168f4a8abb56e56bd80d315b2bbbe8e163 Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 18 Mar 2021 11:47:37 +0200 Subject: [PATCH 13/13] remove content type test --- .../test/java/com/bobocode/DateServletTest.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java b/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java index a0422c8..acb0053 100644 --- a/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java +++ b/2-0-servlet-api/2-0-1-date-servlet-api/src/test/java/com/bobocode/DateServletTest.java @@ -95,20 +95,6 @@ void dateServletIsMarkedWithProperPath() throws ClassNotFoundException { @Test @Order(5) - void dateServletContentTypeIsProper() throws IllegalAccessException, - InvocationTargetException, NoSuchMethodException, IOException { - Method doGetMethod = getDoGetMethod(); - - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - when(response.getWriter()).thenReturn(printWriter); - - doGetMethod.invoke(dateServletObject, request, response); - verify(response).setContentType("text/html"); - } - - @Test - @Order(6) void dateServletReturnsDateInResponse() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method doGetMethod = getDoGetMethod();