diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..fbf9358b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,14 @@ +# Set the default behavior, in case people don't have core.autocrlf set. +* text=auto + +# Explicitly declare text files you want to always be normalized and converted +# to native line endings on checkout. +*.c text +*.h text + +# Declare files that will always have CRLF line endings on checkout. +*.sln text eol=crlf + +# Denote all files that are truly binary and should not be modified. +*.png binary +*.jpg binary diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..65ec9fd9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM maven:3.6-jdk-8-slim + +WORKDIR /src/app/ + +COPY ./pom.xml . + +RUN ["mkdir", "/home/projects"] + +RUN groupadd projects && useradd -g projects projects && \ + chown -R projects:projects /src/app && \ + chown -R projects:projects /home/projects + +USER projects + +RUN ["mvn", "clean"] + +RUN ["mvn", "de.qaware.maven:go-offline-maven-plugin:resolve-dependencies", "-P", "integration"] + +COPY . . + +ENTRYPOINT ["sh"] + diff --git a/book_store.db b/book_store.db index 4a86d6e6..7ff26bcd 100644 Binary files a/book_store.db and b/book_store.db differ diff --git a/pom.xml b/pom.xml index 1a90b05e..6391e3d4 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,15 @@ 3.1.0 provided + + + javax.servlet.jsp + javax.servlet.jsp-api + 2.3.1 + provided + + + javax.servlet @@ -87,8 +96,24 @@ 8080 / + true - + + + de.qaware.maven + go-offline-maven-plugin + 1.1.0 + + + + org.apache.maven.surefire + surefire-junit4 + 2.18.1 + PLUGIN + + + + @@ -117,6 +142,35 @@ + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + 8080 + / + true + + + + start-tomcat + pre-integration-test + + run + + + true + + + + stop-tomcat + post-integration-test + + shutdown + + + + diff --git a/projects-cli.json b/projects-cli.json new file mode 100644 index 00000000..80d77fa6 --- /dev/null +++ b/projects-cli.json @@ -0,0 +1 @@ +{ "tagPattern": "_\\w+" } diff --git a/src/main/java/com/pluralsight/BookDAO.java b/src/main/java/com/pluralsight/BookDAO.java index 262e495e..6acd6b8f 100644 --- a/src/main/java/com/pluralsight/BookDAO.java +++ b/src/main/java/com/pluralsight/BookDAO.java @@ -90,36 +90,4 @@ public boolean insertBook(Book book) { return false; } - - // public void deleteBook(int id) { - // String sql = "DELETE FROM book WHERE id = ?"; - // - // try { - // PreparedStatement statement = jdbcConnection.prepareStatement(sql); - // statement.setInt(1, id); - // statement.executeUpdate(); - // - // statement.close(); - // } catch (SQLException e) { - // e.printStackTrace(); - // } - // } - // - // public void updateBook(Book book) { - // String sql = "UPDATE book SET title = ?, author = ?, price = ?" + - // " WHERE id = ?"; - // - // try { - // PreparedStatement statement = jdbcConnection.prepareStatement(sql); - // statement.setString(1, book.getTitle()); - // statement.setString(2, book.getAuthor()); - // statement.setFloat(3, book.getPrice()); - // statement.setInt(4, book.getId()); - // - // statement.executeUpdate(); - // statement.close(); - // } catch(SQLException e) { - // e.printStackTrace(); - // } - // } } diff --git a/src/main/java/com/pluralsight/ControllerServlet.java b/src/main/java/com/pluralsight/ControllerServlet.java index 08ac0cf3..1fdd645b 100644 --- a/src/main/java/com/pluralsight/ControllerServlet.java +++ b/src/main/java/com/pluralsight/ControllerServlet.java @@ -44,7 +44,7 @@ public ControllerServlet() { /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ - protected void doGet(HttpServletRequest request, HttpServletResponse response) + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getPathInfo(); @@ -59,15 +59,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) case "/insert": insertBook(request, response); break; - // case "/edit": - // showEditForm(request, response); - // break; - // case "/delete": - // deleteBook(request, response); - // break; - // case "/update": - // updateBook(request, response); - // break; default: listBooks(request, response); break; @@ -102,22 +93,6 @@ private void showNewForm(HttpServletRequest request, HttpServletResponse respons dispatcher.forward(request, response); } - // private void showEditForm(HttpServletRequest request, HttpServletResponse response) - // throws ServletException, IOException { - // int id = Integer.parseInt(request.getParameter("id")); - // Book existingBook = bookDAO.getBook(id); - // RequestDispatcher dispatcher = request.getRequestDispatcher("/BookForm.jsp"); - // request.setAttribute("book", existingBook); - // dispatcher.forward(request, response); - // } - - // private void deleteBook(HttpServletRequest request, HttpServletResponse response) - // throws ServletException, IOException { - // int id = Integer.parseInt(request.getParameter("id")); - // bookDAO.deleteBook(id); - // response.sendRedirect("list"); - // } - private void insertBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException { String title = request.getParameter("booktitle"); @@ -130,20 +105,6 @@ private void insertBook(HttpServletRequest request, HttpServletResponse response response.sendRedirect("list"); } - // private void updateBook(HttpServletRequest request, HttpServletResponse response) - // throws ServletException, IOException, ClassNotFoundException, SQLException { - // String idStr = request.getParameter("id"); - // int id = Integer.parseInt(idStr); - // String title = request.getParameter("booktitle"); - // String author = request.getParameter("bookauthor"); - // String priceString = request.getParameter("bookprice"); - // - // Book newBook = new Book(id, title, author, Float.parseFloat(priceString)); - // - // bookDAO.updateBook(newBook); - // response.sendRedirect("list"); - // } - /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ diff --git a/src/main/java/com/pluralsight/LoginServlet.java b/src/main/java/com/pluralsight/LoginServlet.java deleted file mode 100644 index ec7ca71d..00000000 --- a/src/main/java/com/pluralsight/LoginServlet.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.pluralsight; - -import java.io.IOException; -import java.io.PrintWriter; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * Servlet implementation class LoginServlet - */ -public class LoginServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - - /** - * @see HttpServlet#HttpServlet() - */ - public LoginServlet() { - super(); - // TODO Auto-generated constructor stub - } - - /** - * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) - */ - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // TODO Auto-generated method stub - response.getWriter().append("Served at: ").append(request.getContextPath()); - } - - /** - * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) - */ - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // Get parameters - String username = request.getParameter("username"); - String password = request.getParameter("password"); - - PrintWriter writer = response.getWriter(); - writer.println("username = " + username + ", password = " + password); - } - -} diff --git a/src/main/java/com/pluralsight/NameHandler.java b/src/main/java/com/pluralsight/NameHandler.java deleted file mode 100644 index b0f08951..00000000 --- a/src/main/java/com/pluralsight/NameHandler.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.pluralsight; - -public class NameHandler { - - private String name; - - /** Creates a new instance of NameHandler */ - public NameHandler() { - name = null; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - - -} diff --git a/src/main/webapp/BookAdmin.jsp b/src/main/webapp/BookAdmin.jsp index 9e9c25da..420c8e95 100644 --- a/src/main/webapp/BookAdmin.jsp +++ b/src/main/webapp/BookAdmin.jsp @@ -14,7 +14,6 @@
@@ -25,7 +24,6 @@ Title Author Price - In Stock Add Book @@ -34,11 +32,8 @@ ${ item.getTitle() } ${ item.getAuthor() } - 10 Edit Delete - <%-- Edit - Delete --%> diff --git a/src/main/webapp/BookForm.jsp b/src/main/webapp/BookForm.jsp index 353c2a51..c71ce22d 100644 --- a/src/main/webapp/BookForm.jsp +++ b/src/main/webapp/BookForm.jsp @@ -11,37 +11,19 @@
- - <%-- -
- - --%> - <%-- --%>

- <%-- - Edit Book Form - - --%> New Book Form - <%-- --%>

- <%-- - - --%>

- <%--

--%>

- <%--

--%> -

+

- <%--

--%>

diff --git a/src/main/webapp/BookList.jsp b/src/main/webapp/BookList.jsp index ad0950cb..ac40509a 100644 --- a/src/main/webapp/BookList.jsp +++ b/src/main/webapp/BookList.jsp @@ -14,7 +14,6 @@
@@ -25,16 +24,15 @@ Title Author Price - - +
+ ${ item.getTitle() } ${ item.getAuthor() } - Add to Cart - +
diff --git a/src/main/webapp/ShoppingCart.jsp b/src/main/webapp/ShoppingCart.jsp deleted file mode 100644 index fab13b4b..00000000 --- a/src/main/webapp/ShoppingCart.jsp +++ /dev/null @@ -1,45 +0,0 @@ -<%@ page language="java" contentType="text/html; charset=UTF-8" - pageEncoding="UTF-8"%> -<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> -<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %> - - - - Book Store - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - -
List of Books
TitleAuthorPriceIn StockAdd Book
${ item.getTitle() } ${ item.getAuthor() } 10 Edit Delete
-
-
- - diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index dc5c5392..449850f0 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -8,23 +8,11 @@ Archetype Created Web Application - LoginServlet - LoginServlet - - com.pluralsight.LoginServlet - - - LoginServlet - /LoginServlet - - - - ControllerServlet - com.pluralsight.ControllerServlet + ControllerServlet + com.pluralsight.ControllerServlet - - ControllerServlet - /books/* + ControllerServlet + /books/* diff --git a/src/test/java/com/pluralsight/module1/Module1_Task1_IT.java b/src/test/java/com/pluralsight/module1/Module1_Task1_IT.java index 2be2c318..e1ed5583 100644 --- a/src/test/java/com/pluralsight/module1/Module1_Task1_IT.java +++ b/src/test/java/com/pluralsight/module1/Module1_Task1_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module1; + +import com.pluralsight.*; import org.junit.After; import org.junit.Before; @@ -18,59 +20,59 @@ public class Module1_Task1_IT { private String indexUrl; private WebClient webClient; - HtmlPage page; + HtmlPage page; @Before - public void setUp() throws IOException { - indexUrl = "http://localhost:8080"; //System.getProperty("integration.base.url"); - webClient = new WebClient(); - // Open the admin page - page = webClient.getPage(indexUrl + "/books/admin"); - } - @After - public void tearDown() { - webClient.closeAllWindows(); - } + public void setUp() throws IOException { + indexUrl = "http://localhost:8080"; // System.getProperty("integration.base.url"); + webClient = new WebClient(); + // Open the admin page + page = webClient.getPage(indexUrl + "/books/admin"); + } + + @After + public void tearDown() { + webClient.closeAllWindows(); + } - // Verify the edit and delete hrefs, in BookAdmin.jsp contain the id - @Test - public void module1_task1() { - url_contains_id("Delete"); - url_contains_id("Edit"); - } + // Verify the edit and delete hrefs, in BookAdmin.jsp contain the id + @Test + public void _task1() { + url_contains_id("Delete"); + url_contains_id("Edit"); + } - public void url_contains_id(String textStr) { - // First check if an anchor with text "Edit" exists - HtmlAnchor anchor = null; - try { - anchor = page.getAnchorByText(textStr); - } - catch ( ElementNotFoundException e) {} + public void url_contains_id(String textStr) { + // First check if an anchor with text "Edit" exists + HtmlAnchor anchor = null; + try { + anchor = page.getAnchorByText(textStr); + } catch (ElementNotFoundException e) { + } - assertNotNull("An anchor with the text " + textStr + " does not exist.", anchor); + assertNotNull("An anchor with the text " + textStr + " does not exist.", anchor); - boolean found = findURLWithID(textStr.toLowerCase()); - assertTrue("The " + textStr + " anchor's href does not contain the id.", found); - } + boolean found = findURLWithID(textStr.toLowerCase()); + assertTrue("The " + textStr + " anchor's href does not contain the id.", found); + } - private boolean findURLWithID(String urlStr) { - String foundURL = ""; - try { - for ( HtmlAnchor a : page.getAnchors()) { - String href = a.getHrefAttribute(); - if (href.contains(urlStr)) { - foundURL = a.getHrefAttribute().toString(); - break; - } - } - } - catch ( ElementNotFoundException e) { - return false; - } - foundURL = foundURL.replaceAll("\\s+",""); - // Might have different id's in the database so remove them. - foundURL = foundURL.replaceAll("[0-9]",""); - String testingURL = urlStr+"?id="; - return foundURL.equals(testingURL); - } + private boolean findURLWithID(String urlStr) { + String foundURL = ""; + try { + for (HtmlAnchor a : page.getAnchors()) { + String href = a.getHrefAttribute(); + if (href.contains(urlStr)) { + foundURL = a.getHrefAttribute().toString(); + break; + } + } + } catch (ElementNotFoundException e) { + return false; + } + foundURL = foundURL.replaceAll("\\s+", ""); + // Might have different id's in the database so remove them. + foundURL = foundURL.replaceAll("[0-9]", ""); + String testingURL = urlStr + "?id="; + return foundURL.equals(testingURL); + } } diff --git a/src/test/java/com/pluralsight/module1/Module1_Task2_IT.java b/src/test/java/com/pluralsight/module1/Module1_Task2_IT.java deleted file mode 100644 index 67f38d61..00000000 --- a/src/test/java/com/pluralsight/module1/Module1_Task2_IT.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.pluralsight; - -import static org.junit.Assert.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.sql.Connection; -import java.sql.PreparedStatement; - -import org.junit.BeforeClass; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import org.powermock.reflect.Whitebox; -import java.lang.reflect.Method; - -import java.io.*; - -public class Module1_Task2_IT { - - private ControllerServlet controllerServlet; - - // @Before - // public void setUp() throws Exception { - // controllerServlet = new ControllerServlet(); - // } - - // Verify the deleteBook() method exists in ControllerServlet - @Test - public void module1_task2() throws Exception { - Method method = null; - try { - method = Whitebox.getMethod(ControllerServlet.class, - "deleteBook", HttpServletRequest.class, HttpServletResponse.class); - } catch (Exception e) {} - - String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, method); - } -} diff --git a/src/test/java/com/pluralsight/module1/Module1_Task2_and_3_IT.java b/src/test/java/com/pluralsight/module1/Module1_Task2_and_3_IT.java new file mode 100644 index 00000000..52498550 --- /dev/null +++ b/src/test/java/com/pluralsight/module1/Module1_Task2_and_3_IT.java @@ -0,0 +1,83 @@ +package com.pluralsight.module1; + +import com.pluralsight.*; + +import static org.junit.Assert.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.sql.Connection; +import java.sql.PreparedStatement; + +import org.junit.BeforeClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.exceptions.*; + +import org.powermock.reflect.Whitebox; +import java.lang.reflect.Method; + +import java.io.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(ControllerServlet.class) +public class Module1_Task2_and_3_IT { + + private ControllerServlet controllerServlet; + private Method method = null; + + @Before + public void setUp() throws Exception { + try { + method = Whitebox.getMethod(ControllerServlet.class, "deleteBook", HttpServletRequest.class, + HttpServletResponse.class); + } catch (Exception e) { + } + } + + // Verify the deleteBook() method exists in ControllerServlet + @Test + public void _task2() throws Exception { + String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; + assertNotNull(errorMsg, method); + } + + @Test + public void _task3() throws Exception { + String tempID = "0"; + String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; + assertNotNull(errorMsg, method); + + ControllerServlet controllerServlet = PowerMockito.spy(new ControllerServlet()); + boolean called_deleteBook = false; + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + HttpServletResponse response = Mockito.mock(HttpServletResponse.class); + + try { + Mockito.when(request.getPathInfo()).thenReturn("/delete"); + // PowerMockito.doNothing().when(controllerServlet, "deleteBook", request, + // response); + Mockito.when(request.getParameter("id")).thenReturn(tempID); + } catch (MethodNotFoundException e) { + } + + try { + controllerServlet.doGet(request, response); + try { + PowerMockito.verifyPrivate(controllerServlet).invoke("deleteBook", request, response); + called_deleteBook = true; + } catch (Throwable e) { + } + } catch (Exception e) { + } + + errorMsg = "After action \"" + "/delete" + "\", did not call deleteBook()."; + assertTrue(errorMsg, called_deleteBook); + } +} diff --git a/src/test/java/com/pluralsight/module1/Module1_Task3_IT.java b/src/test/java/com/pluralsight/module1/Module1_Task3_IT.java deleted file mode 100644 index fd1217b1..00000000 --- a/src/test/java/com/pluralsight/module1/Module1_Task3_IT.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.pluralsight; - -import static org.junit.Assert.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.sql.Connection; -import java.sql.PreparedStatement; - -import org.junit.BeforeClass; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.exceptions.*; - -import java.io.*; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(ControllerServlet.class) -public class Module1_Task3_IT extends Mockito{ - static String tempID = "0"; - - // Verify the deleteBook() method exists in ControllerServlet - // Since it's private need to verify the lines of code get called - // through the /delete action in doGet() - @Test - public void module1_task3() throws Exception { - ControllerServlet controllerServlet = PowerMockito.spy(new ControllerServlet()); - boolean called_deleteBook = false; - HttpServletRequest request = mock(HttpServletRequest.class); - HttpServletResponse response = mock(HttpServletResponse.class); - - try { - when(request.getPathInfo()).thenReturn("/delete"); - //PowerMockito.doNothing().when(controllerServlet, "deleteBook", request, response); - when(request.getParameter("id")).thenReturn(tempID); - } catch (MethodNotFoundException e) {} - - // try { - // controllerServlet.doGet(request, response); - // try { - // PowerMockito.verifyPrivate(controllerServlet) - // .invoke("deleteBook", request, response); - // called_deleteBook = true; - // } catch (Throwable e) {} - // } catch (Exception e) {} - - String errorMsg = "After action \"" + "/delete" + - "\", did not call deleteBook()."; - assertTrue(errorMsg, called_deleteBook); - } -} diff --git a/src/test/java/com/pluralsight/module1/Module1_Task4_IT.java b/src/test/java/com/pluralsight/module1/Module1_Task4_IT.java index e07fb726..a4c7f9aa 100644 --- a/src/test/java/com/pluralsight/module1/Module1_Task4_IT.java +++ b/src/test/java/com/pluralsight/module1/Module1_Task4_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module1; + +import com.pluralsight.*; import static org.junit.Assert.*; import org.junit.Test; @@ -8,18 +10,18 @@ public class Module1_Task4_IT { - // Verify the deleteBook() method exists in BookDAO - @Test - public void module1_task4() throws Exception { - Method method = null; + // Verify the deleteBook() method exists in BookDAO + @Test + public void _task4() throws Exception { + Method method = null; - try { - method = BookDAO.class.getMethod("deleteBook", int.class); - } catch (NoSuchMethodException e) { - //e.printStackTrace(); - } + try { + method = BookDAO.class.getMethod("deleteBook", int.class); + } catch (NoSuchMethodException e) { + // e.printStackTrace(); + } - String message = "The method deleteBook() doesn't exist in BookDAO.java."; - assertNotNull(message, method); - } + String message = "The method deleteBook() doesn't exist in BookDAO.java."; + assertNotNull(message, method); + } } diff --git a/src/test/java/com/pluralsight/module1/Module1_Task5_IT.java b/src/test/java/com/pluralsight/module1/Module1_Task5_IT.java index 24d973ad..0488eec4 100644 --- a/src/test/java/com/pluralsight/module1/Module1_Task5_IT.java +++ b/src/test/java/com/pluralsight/module1/Module1_Task5_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module1; + +import com.pluralsight.*; import static org.junit.Assert.*; import java.sql.Connection; @@ -17,44 +19,44 @@ import java.io.*; - @RunWith(PowerMockRunner.class) -@PrepareForTest({DriverManager.class, PreparedStatement.class, BookDAO.class}) +@PrepareForTest({ DriverManager.class, PreparedStatement.class, BookDAO.class }) public class Module1_Task5_IT { - // Verify the deleteBook() method exists in BookDAO - @Test - public void module1_task5() throws Exception { - Method method = null; - String sql = "DELETE FROM book WHERE id = ?"; - Connection mockConnection = Mockito.mock(Connection.class); - PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class); - BookDAO bookDAO = new BookDAO(mockConnection); - BookDAO spyBookDAO = Mockito.spy(bookDAO); - boolean called_prepareStatement = false; - - - Mockito.when(mockConnection.prepareStatement(sql)).thenReturn(mockStatement); - - try { - method = BookDAO.class.getMethod("deleteBook", int.class); - } catch (NoSuchMethodException e) { - //e.printStackTrace(); - } - - String message = "The method deleteBook() doesn't exist in BookDAO.java."; - assertNotNull(message, method); - - try { - method.invoke(spyBookDAO, 0); - } catch (Exception e) {} - - try { - Mockito.verify(mockConnection,Mockito.atLeast(1)).prepareStatement(sql); - called_prepareStatement = true; - } catch (Throwable e) {} - - message = "The method deleteBook() doesn't call prepareStatement() correctly."; - assertTrue(message, called_prepareStatement); - } + // Verify the deleteBook() in BookDAO calls prepareStatement() + @Test + public void _task5() throws Exception { + Method method = null; + String sql = "DELETE FROM book WHERE id = ?"; + Connection mockConnection = Mockito.mock(Connection.class); + PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class); + BookDAO bookDAO = new BookDAO(mockConnection); + BookDAO spyBookDAO = Mockito.spy(bookDAO); + boolean called_prepareStatement = false; + + Mockito.when(mockConnection.prepareStatement(sql)).thenReturn(mockStatement); + + try { + method = BookDAO.class.getMethod("deleteBook", int.class); + } catch (NoSuchMethodException e) { + // e.printStackTrace(); + } + + String message = "The method deleteBook() doesn't exist in BookDAO.java."; + assertNotNull(message, method); + + try { + method.invoke(spyBookDAO, 0); + } catch (Exception e) { + } + + try { + Mockito.verify(mockConnection, Mockito.atLeast(1)).prepareStatement(sql); + called_prepareStatement = true; + } catch (Throwable e) { + } + + message = "The method deleteBook() doesn't call prepareStatement() correctly."; + assertTrue(message, called_prepareStatement); + } } diff --git a/src/test/java/com/pluralsight/module1/Module1_Task6_IT.java b/src/test/java/com/pluralsight/module1/Module1_Task6_IT.java index 488c858d..af24eb38 100644 --- a/src/test/java/com/pluralsight/module1/Module1_Task6_IT.java +++ b/src/test/java/com/pluralsight/module1/Module1_Task6_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module1; + +import com.pluralsight.*; import static org.junit.Assert.*; import java.sql.Connection; @@ -17,56 +19,57 @@ import java.io.*; - @RunWith(PowerMockRunner.class) -@PrepareForTest({DriverManager.class, PreparedStatement.class, BookDAO.class}) +@PrepareForTest({ DriverManager.class, PreparedStatement.class, BookDAO.class }) public class Module1_Task6_IT { - // Verify the deleteBook() method exists in BookDAO - @Test - public void module1_task6() throws Exception { - Method method = null; - String sql = "DELETE FROM book WHERE id = ?"; - Connection spyConnection = Mockito.mock(Connection.class); - PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class); - BookDAO bookDAO = new BookDAO(spyConnection); - BookDAO spyBookDAO = Mockito.spy(bookDAO); - boolean called_setInt = false; - boolean called_execute = false; - boolean called_prepareStatement = false; - boolean called_close = false; + // Verify the deleteBook() method exists in BookDAO + @Test + public void _task6() throws Exception { + Method method = null; + String sql = "DELETE FROM book WHERE id = ?"; + Connection spyConnection = Mockito.mock(Connection.class); + PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class); + BookDAO bookDAO = new BookDAO(spyConnection); + BookDAO spyBookDAO = Mockito.spy(bookDAO); + boolean called_setInt = false; + boolean called_execute = false; + boolean called_prepareStatement = false; + boolean called_close = false; - Mockito.when(spyConnection.prepareStatement(sql)).thenReturn(mockStatement); + Mockito.when(spyConnection.prepareStatement(sql)).thenReturn(mockStatement); - try { - method = BookDAO.class.getMethod("deleteBook", int.class); - } catch (NoSuchMethodException e) { - //e.printStackTrace(); - } + try { + method = BookDAO.class.getMethod("deleteBook", int.class); + } catch (NoSuchMethodException e) { + // e.printStackTrace(); + } - String message = "The method deleteBook() doesn't exist in BookDAO.java."; - assertNotNull(message, method); + String message = "The method deleteBook() doesn't exist in BookDAO.java."; + assertNotNull(message, method); - try { - method.invoke(spyBookDAO, 0); - } catch (Exception e) {} + try { + method.invoke(spyBookDAO, 0); + } catch (Exception e) { + } - try { - Mockito.verify(mockStatement, Mockito.atLeast(1)).setInt(Mockito.anyInt(), Mockito.anyInt()); - called_setInt = true; - Mockito.verify(mockStatement, Mockito.atLeast(1)).executeUpdate(); - called_execute = true; - Mockito.verify(mockStatement, Mockito.atLeast(1)).close(); - called_close = true; - } catch (Throwable e) {} + try { + Mockito.verify(mockStatement, Mockito.atLeast(1)).setInt(Mockito.anyInt(), Mockito.anyInt()); + called_setInt = true; + Mockito.verify(mockStatement, Mockito.atLeast(1)).executeUpdate(); + called_execute = true; + Mockito.verify(mockStatement, Mockito.atLeast(1)).close(); + called_close = true; + } catch (Throwable e) { + } - message = "The method deleteBook() doesn't call setInt()."; - assertTrue(message, called_setInt); + message = "The method deleteBook() doesn't call setInt()."; + assertTrue(message, called_setInt); - message = "The method deleteBook() doesn't call executeUpdate()."; - assertTrue(message, called_execute); + message = "The method deleteBook() doesn't call executeUpdate()."; + assertTrue(message, called_execute); - message = "The method deleteBook() doesn't call PreparedStatement close()."; - assertTrue(message, called_close); - } + message = "The method deleteBook() doesn't call PreparedStatement close()."; + assertTrue(message, called_close); + } } diff --git a/src/test/java/com/pluralsight/module1/Module1_Task7_and_8_IT.java b/src/test/java/com/pluralsight/module1/Module1_Task7_and_8_IT.java index 56c83fa4..e25a1cea 100644 --- a/src/test/java/com/pluralsight/module1/Module1_Task7_and_8_IT.java +++ b/src/test/java/com/pluralsight/module1/Module1_Task7_and_8_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module1; + +import com.pluralsight.*; import static org.junit.Assert.*; import javax.servlet.http.HttpServletRequest; @@ -25,7 +27,7 @@ import java.lang.reflect.Method; import java.io.*; -public class Module1_Task7_and_8_IT extends Mockito{ +public class Module1_Task7_and_8_IT extends Mockito { static StringWriter stringWriter = new StringWriter(); static String tempID = "0"; @@ -34,15 +36,15 @@ public class Module1_Task7_and_8_IT extends Mockito{ static boolean called_deleteBook = false; static HttpServletRequest request = mock(HttpServletRequest.class); static HttpServletResponse response = mock(HttpServletResponse.class); - static Method deleteMethod = null; - @Mock - private BookDAO mockBookDAO; + static Method deleteMethod = null; + @Mock + private BookDAO mockBookDAO; - @InjectMocks - private ControllerServlet controllerServlet; + @InjectMocks + private ControllerServlet controllerServlet; - @Before - public void setUp() throws Exception { + @Before + public void setUp() throws Exception { MockitoAnnotations.initMocks(this); request = mock(HttpServletRequest.class); @@ -52,55 +54,60 @@ public void setUp() throws Exception { when(request.getParameter("id")).thenReturn(tempID); try { - deleteMethod = Whitebox.getMethod(ControllerServlet.class, - "deleteBook", HttpServletRequest.class, HttpServletResponse.class); - } catch (Exception e) {} + deleteMethod = Whitebox.getMethod(ControllerServlet.class, "deleteBook", HttpServletRequest.class, + HttpServletResponse.class); + } catch (Exception e) { + } + + // String errorMsg = "private void deleteBook() does not exist in + // ControllerServlet"; + // assertNotNull(errorMsg, deleteMethod); + if (deleteMethod != null) { + try { + controllerServlet.doGet(request, response); + } catch (Exception e) { + } + } + } + // Verify deleteBook() in ControllerServlet is complete + @Test + public void _task7() throws Exception { String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; assertNotNull(errorMsg, deleteMethod); + MockingDetails mockingDetails = Mockito.mockingDetails(mockBookDAO); + + Collection invocations = mockingDetails.getInvocations(); + + List methodsCalled = new ArrayList<>(); + for (Invocation anInvocation : invocations) { + methodsCalled.add(anInvocation.getMethod().getName()); + } + errorMsg = "The ControllerServlet deleteBook() method was not called."; + assertTrue(errorMsg, methodsCalled.contains("deleteBook")); + try { - controllerServlet.doGet(request, response); - } catch (Exception e) {} - } - - // Verify deleteBook() in ControllerServlet is complete - @Test - public void module1_task7() throws Exception { - String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, deleteMethod); - - MockingDetails mockingDetails = Mockito.mockingDetails(mockBookDAO); - - Collection invocations = mockingDetails.getInvocations(); - - List methodsCalled = new ArrayList<>(); - for (Invocation anInvocation : invocations) { - methodsCalled.add(anInvocation.getMethod().getName()); - } - assertTrue(methodsCalled.contains("deleteBook")); - - try { - verify(request, atLeast(1)).getParameter("id"); - called_getParameter = true; - } catch (Throwable e) {} - - errorMsg = "In ControllerServlet deleteBook()," + - " did not call getParameter(\"id\")."; - assertTrue(errorMsg, called_getParameter); - } - - @Test - public void module1_task8() throws Exception { - String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, deleteMethod); - try { - verify(response, atLeast(1)).sendRedirect("list"); - called_sendRedirect = true; - } catch (Throwable e) {} + verify(request, atLeast(1)).getParameter("id"); + called_getParameter = true; + } catch (Throwable e) { + } + + errorMsg = "In ControllerServlet deleteBook()," + " did not call getParameter(\"id\")."; + assertTrue(errorMsg, called_getParameter); + } - errorMsg = "In ControllerServlet deleteBook()," + - " did not call sendRedirect(\"list\")."; - assertTrue(errorMsg, called_sendRedirect); + @Test + public void _task8() throws Exception { + String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; + assertNotNull(errorMsg, deleteMethod); + try { + verify(response, atLeast(1)).sendRedirect("list"); + called_sendRedirect = true; + } catch (Throwable e) { } + + errorMsg = "In ControllerServlet deleteBook()," + " did not call sendRedirect(\"list\")."; + assertTrue(errorMsg, called_sendRedirect); + } } diff --git a/src/test/java/com/pluralsight/module2/Module2_Task11_thru_14_IT.java b/src/test/java/com/pluralsight/module2/Module2_Task11_thru_14_IT.java index 8d3c3206..9b5f1d7f 100644 --- a/src/test/java/com/pluralsight/module2/Module2_Task11_thru_14_IT.java +++ b/src/test/java/com/pluralsight/module2/Module2_Task11_thru_14_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module2; + +import com.pluralsight.*; import org.junit.After; import org.junit.Before; @@ -22,113 +24,107 @@ public class Module2_Task11_thru_14_IT { private String BOOK_FORM_NAME = "book_form"; private String indexUrl; private WebClient webClient; - HtmlPage firstPage = null; + HtmlPage firstPage = null; HtmlPage nextPage = null; HtmlForm form = null; + String formErrorMsg = "We can’t find a
with name 'book_form' in BookForm.jsp"; - @Before - public void setUp() throws IOException { - indexUrl = "http://localhost:8080"; //System.getProperty("integration.base.url"); - webClient = new WebClient(); - // Open the admin page - firstPage = webClient.getPage(indexUrl + "/books/admin"); - clickLink("Edit"); - assertNotNull("Link Edit did not work.", nextPage); - // Get form - try { - form = nextPage.getFormByName(BOOK_FORM_NAME); - } catch (ElementNotFoundException e) {} - } - @After - public void tearDown() { - webClient.closeAllWindows(); - } - - // Verify they adapted the BookForm.jsp page for editing existing books - // and adding new book - // In this test check the form input fields have values filled in - @Test - public void module2_task11() { - assertNotNull("Link Edit did not work.", nextPage); - assertNotNull("Form is null.", form); - //Get id input field - try { - HtmlInput inputId = form.getInputByName("id"); + @Before + public void setUp() throws IOException { + indexUrl = "http://localhost:8080"; // System.getProperty("integration.base.url"); + webClient = new WebClient(); + // Open the admin page + firstPage = webClient.getPage(indexUrl + "/books/admin"); + clickLink("Edit"); + assertNotNull("Link Edit did not work.", nextPage); + // Get form + try { + form = nextPage.getFormByName(BOOK_FORM_NAME); + } catch (ElementNotFoundException e) { + } + } - // Check if hidden - String typeAttribute = inputId.getTypeAttribute(); - assertEquals("The id input needs type=\"hidden\".", "hidden", typeAttribute); + @After + public void tearDown() { + webClient.closeAllWindows(); + } - // Check value is an int - try { - Integer.parseInt(inputId.getValueAttribute()); - } catch (NumberFormatException e) { - assertTrue("The id input does not have an int for value.", false); - } - } catch (ElementNotFoundException e) { - assertTrue("The input field with name \"id\" does not exist.", false); - } - } + // Verify they adapted the BookForm.jsp page for editing existing books + // and adding new book + // In this test check the form input fields have values filled in + @Test + public void _task11() { + assertNotNull("Link Edit did not work.", nextPage); + assertNotNull(formErrorMsg, form); + // Get id input field + try { + HtmlInput inputId = form.getInputByName("id"); - @Test - public void module2_task12() { - assertNotNull("Link Edit did not work.", nextPage); - assertNotNull("Form is null.", form); - // Get title input field, check value + // Check if hidden + String typeAttribute = inputId.getTypeAttribute(); + assertEquals("The id input needs type=\"hidden\".", "hidden", typeAttribute); + + // Check value is an int try { - HtmlInput inputTitle = form.getInputByName("booktitle"); - String titleValue = inputTitle.getValueAttribute(); - assertTrue("Title field value is empty, value is \"" + titleValue + "\".", - titleValue.length() > 0); - }catch (ElementNotFoundException e) { - assertTrue("The input field with name \"booktitle\" does not exist.", false); + Integer.parseInt(inputId.getValueAttribute()); + } catch (NumberFormatException e) { + assertTrue("The id input does not have an int for value.", false); } + } catch (ElementNotFoundException e) { + assertTrue("The input field with name \"id\" does not exist.", false); } + } - @Test - public void module2_task13() { - assertNotNull("Link Edit did not work.", nextPage); - assertNotNull("Form is null.", form); + @Test + public void _task12() { + assertNotNull("Link Edit did not work.", nextPage); + assertNotNull(formErrorMsg, form); + // Get title input field, check value + try { + HtmlInput inputTitle = form.getInputByName("booktitle"); + String titleValue = inputTitle.getValueAttribute(); + assertTrue("Title field value is empty, value is \"" + titleValue + "\".", titleValue.length() > 0); + } catch (ElementNotFoundException e) { + assertTrue("The input field with name \"booktitle\" does not exist.", false); + } + } - // Get author input field, check value - try { - HtmlInput inputAuthor = form.getInputByName("bookauthor"); - String authorValue = inputAuthor.getValueAttribute(); - assertTrue("Author field value is empty, value is \"" + authorValue + "\".", - authorValue.length() > 0); - }catch (ElementNotFoundException e) { - assertTrue("The input field with name \"bookauthor\" does not exist.", false); - } + @Test + public void _task13() { + assertNotNull("Link Edit did not work.", nextPage); + assertNotNull(formErrorMsg, form); + + // Get author input field, check value + try { + HtmlInput inputAuthor = form.getInputByName("bookauthor"); + String authorValue = inputAuthor.getValueAttribute(); + assertTrue("Author field value is empty, value is \"" + authorValue + "\".", authorValue.length() > 0); + } catch (ElementNotFoundException e) { + assertTrue("The input field with name \"bookauthor\" does not exist.", false); } - @Test - public void module2_task14() { - assertNotNull("Link Edit did not work.", nextPage); - assertNotNull("Form is null.", form); + // Get price input field, check value + try { + HtmlInput inputPrice = form.getInputByName("bookprice"); + String priceValue = inputPrice.getValueAttribute(); + assertTrue("Price field value is empty, value is \"" + priceValue + "\".", priceValue.length() > 0); + } catch (ElementNotFoundException e) { + assertTrue("The input field with name \"bookprice\" does not exist.", false); + } + } - // Get price input field, check value - try { - HtmlInput inputPrice = form.getInputByName("bookprice"); - String priceValue = inputPrice.getValueAttribute(); - assertTrue("Price field value is empty, value is \"" + priceValue + "\".", - priceValue.length() > 0); - }catch (ElementNotFoundException e) { - assertTrue("The input field with name \"bookprice\" does not exist.", false); + private void clickLink(String urlStr) { + String foundURL = ""; + String desiredUrlText = urlStr.toLowerCase(); + try { + for (HtmlAnchor a : firstPage.getAnchors()) { + String href = a.getHrefAttribute(); + if (href.contains(desiredUrlText)) { + nextPage = a.click(); + break; + } } + } catch (Exception e) { } - - private void clickLink(String urlStr) { - String foundURL = ""; - String desiredUrlText = urlStr.toLowerCase(); - try { - for ( HtmlAnchor a : firstPage.getAnchors()) { - String href = a.getHrefAttribute(); - if (href.contains(desiredUrlText)) { - nextPage = a.click(); - break; - } - } - } - catch ( Exception e) {} - } + } } diff --git a/src/test/java/com/pluralsight/module2/Module2_Task1_IT.java b/src/test/java/com/pluralsight/module2/Module2_Task1_IT.java deleted file mode 100644 index f1c16909..00000000 --- a/src/test/java/com/pluralsight/module2/Module2_Task1_IT.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.pluralsight; - -import static org.junit.Assert.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.sql.Connection; -import java.sql.PreparedStatement; - -import org.junit.BeforeClass; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import org.powermock.reflect.Whitebox; -import java.lang.reflect.Method; - -import java.io.*; - -public class Module2_Task1_IT { - - private ControllerServlet controllerServlet; - - // @Before - // public void setUp() throws Exception { - // controllerServlet = new ControllerServlet(); - // } - - // Verify the showEditForm() method exists in ControllerServlet - @Test - public void module2_task1() throws Exception { - Method method = null; - try { - method = Whitebox.getMethod(ControllerServlet.class, - "showEditForm", HttpServletRequest.class, HttpServletResponse.class); - } catch (Exception e) {} - - String errorMsg = "private void showEditForm() does not exist in ControllerServlet"; - assertNotNull(errorMsg, method); - } -} diff --git a/src/test/java/com/pluralsight/module2/Module2_Task1_and_2_IT.java b/src/test/java/com/pluralsight/module2/Module2_Task1_and_2_IT.java new file mode 100644 index 00000000..885fcbe8 --- /dev/null +++ b/src/test/java/com/pluralsight/module2/Module2_Task1_and_2_IT.java @@ -0,0 +1,83 @@ +package com.pluralsight.module2; + +import com.pluralsight.*; + +import static org.junit.Assert.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.sql.Connection; +import java.sql.PreparedStatement; + +import org.junit.BeforeClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.exceptions.*; + +import org.powermock.reflect.Whitebox; +import java.lang.reflect.Method; + +import java.io.*; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(ControllerServlet.class) +public class Module2_Task1_and_2_IT extends Mockito { + + private ControllerServlet controllerServlet; + private Method method = null; + + @Before + public void setUp() throws Exception { + try { + method = Whitebox.getMethod(ControllerServlet.class, "showEditForm", HttpServletRequest.class, + HttpServletResponse.class); + } catch (Exception e) { + } + } + + // Verify the showEditForm() method exists in ControllerServlet + @Test + public void _task1() throws Exception { + String errorMsg = "private void showEditForm() does not exist in ControllerServlet"; + assertNotNull(errorMsg, method); + } + + @Test + public void _task2() throws Exception { + String errorMsg = "private void showEditForm() does not exist in ControllerServlet"; + assertNotNull(errorMsg, method); + + String tempID = "0"; + ControllerServlet controllerServlet = PowerMockito.spy(new ControllerServlet()); + boolean called_showEditForm = false; + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + + try { + when(request.getPathInfo()).thenReturn("/edit"); + // PowerMockito.doNothing().when(controllerServlet, "showEditForm", request, + // response); + when(request.getParameter("id")).thenReturn(tempID); + } catch (MethodNotFoundException e) { + } + + try { + controllerServlet.doGet(request, response); + try { + PowerMockito.verifyPrivate(controllerServlet).invoke("showEditForm", request, response); + called_showEditForm = true; + } catch (Throwable e) { + } + } catch (Exception e) { + } + + errorMsg = "After action \"" + "/edit" + "\", did not call showEditForm()."; + assertTrue(errorMsg, called_showEditForm); + } +} diff --git a/src/test/java/com/pluralsight/module2/Module2_Task2_IT.java b/src/test/java/com/pluralsight/module2/Module2_Task2_IT.java deleted file mode 100644 index 64edcbe8..00000000 --- a/src/test/java/com/pluralsight/module2/Module2_Task2_IT.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.pluralsight; - -import static org.junit.Assert.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.sql.Connection; -import java.sql.PreparedStatement; - -import org.junit.BeforeClass; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.exceptions.*; - -import java.io.*; - -@RunWith(PowerMockRunner.class) -@PrepareForTest(ControllerServlet.class) -public class Module2_Task2_IT extends Mockito{ - static String tempID = "0"; - - // Verify the showEditForm() method exists in ControllerServlet - // Since it's private need to verify the lines of code get called - // through the /delete action in doGet() - @Test - public void module1_task2() throws Exception { - ControllerServlet controllerServlet = PowerMockito.spy(new ControllerServlet()); - boolean called_showEditForm = false; - HttpServletRequest request = mock(HttpServletRequest.class); - HttpServletResponse response = mock(HttpServletResponse.class); - - try { - when(request.getPathInfo()).thenReturn("/edit"); - //PowerMockito.doNothing().when(controllerServlet, "showEditForm", request, response); - when(request.getParameter("id")).thenReturn(tempID); - } catch (MethodNotFoundException e) {} - - // try { - // controllerServlet.doGet(request, response); - // try { - // PowerMockito.verifyPrivate(controllerServlet) - // .invoke("showEditForm", request, response); - // called_showEditForm = true; - // } catch (Throwable e) {} - // } catch (Exception e) {} - - String errorMsg = "After action \"" + "/edit" + - "\", did not call showEditForm()."; - assertTrue(errorMsg, called_showEditForm); - } -} diff --git a/src/test/java/com/pluralsight/module2/Module2_Task3_thru_6_IT.java b/src/test/java/com/pluralsight/module2/Module2_Task3_thru_6_IT.java index 15668334..5a1fef0d 100644 --- a/src/test/java/com/pluralsight/module2/Module2_Task3_thru_6_IT.java +++ b/src/test/java/com/pluralsight/module2/Module2_Task3_thru_6_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module2; + +import com.pluralsight.*; import static org.junit.Assert.*; import javax.servlet.http.HttpServletRequest; @@ -19,25 +21,25 @@ import java.lang.reflect.Method; import java.io.*; -public class Module2_Task3_thru_6_IT extends Mockito{ +public class Module2_Task3_thru_6_IT extends Mockito { static StringWriter stringWriter = new StringWriter(); static String tempID = "1"; - static int tempIntID = 1; + static int tempIntID = 1; static HttpServletRequest request; static HttpServletResponse response; static RequestDispatcher mockRequestDispatcher; static Book mockBook; - @Mock - private BookDAO mockBookDAO; + @Mock + private BookDAO mockBookDAO; - @InjectMocks - private ControllerServlet controllerServlet; + @InjectMocks + private ControllerServlet controllerServlet; - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); request = mock(HttpServletRequest.class); response = mock(HttpServletResponse.class); @@ -47,81 +49,83 @@ public void setUp() throws Exception { when(request.getPathInfo()).thenReturn("/edit"); when(request.getParameter("id")).thenReturn(tempID); when(mockBookDAO.getBook(tempIntID)).thenReturn(mockBook); - when(request.getRequestDispatcher("/BookForm.jsp")) - .thenReturn(mockRequestDispatcher); + when(request.getRequestDispatcher("/BookForm.jsp")).thenReturn(mockRequestDispatcher); try { controllerServlet.doGet(request, response); - } catch (Exception e) {} - } - - // Verify showEditForm() is complete in ControllerServlet - // Since it's private need to verify the lines of code get called - // through the /edit action in doGet() - @Test - public void module2_task3() throws Exception { - boolean called_getParameter = false; - boolean called_getBook = false; - - try { - verify(request, atLeast(1)).getParameter("id"); - called_getParameter = true; - } catch (Throwable e) {} - - try { - verify(mockBookDAO).getBook(anyInt()); - called_getBook = true; - } catch (Throwable e) {} - - String errorMsg = "In ControllerServlet showEditForm()," + - " did not call getParameter(\"id\")."; - assertTrue(errorMsg, called_getParameter); - errorMsg = "In ControllerServlet showEditForm()," + - " did not call getBook(id)."; - assertTrue(errorMsg, called_getBook); - } - - @Test - public void module2_task4() throws Exception { - boolean called_getRequestDispatcher = false; - - try { - verify(request).getRequestDispatcher("/BookForm.jsp"); - called_getRequestDispatcher = true; - } catch (Throwable e) {} - - String errorMsg = "In ControllerServlet showEditForm()," + - " did not call request.getRequestDispatcher(\"BookForm.jsp\")."; - assertTrue(errorMsg, called_getRequestDispatcher); - } - - @Test - public void module2_task5() throws Exception { - boolean called_setAttribute = false; - - try { - verify(request).setAttribute("book", mockBook); - called_setAttribute = true; - } catch (Throwable e) {} - - String errorMsg = "In ControllerServlet showEditForm()," + - " did not call request.setAttribute(\"book\", bookObject);."; - assertTrue(errorMsg, called_setAttribute); - } - - @Test - public void module2_task6() throws Exception { - boolean called_forward = false; - - try { - verify(mockRequestDispatcher).forward(request, response); - called_forward = true; - } catch (Throwable e) {} - - String errorMsg = "In ControllerServlet showEditForm()," + - " did not call dispatcher.forward(request, response);."; - assertTrue(errorMsg, called_forward); - } + } catch (Exception e) { + } + } + + // Verify showEditForm() is complete in ControllerServlet + // Since it's private need to verify the lines of code get called + // through the /edit action in doGet() + @Test + public void _task3() throws Exception { + boolean called_getParameter = false; + boolean called_getBook = false; + + try { + verify(request, atLeast(1)).getParameter("id"); + called_getParameter = true; + } catch (Throwable e) { + } + + try { + verify(mockBookDAO).getBook(anyInt()); + called_getBook = true; + } catch (Throwable e) { + } + + String errorMsg = "In ControllerServlet showEditForm()," + " did not call getParameter(\"id\")."; + assertTrue(errorMsg, called_getParameter); + errorMsg = "In ControllerServlet showEditForm()," + " did not call getBook(id)."; + assertTrue(errorMsg, called_getBook); + } + + @Test + public void _task4() throws Exception { + boolean called_getRequestDispatcher = false; + + try { + verify(request).getRequestDispatcher("/BookForm.jsp"); + called_getRequestDispatcher = true; + } catch (Throwable e) { + } + + String errorMsg = "In ControllerServlet showEditForm()," + + " did not call request.getRequestDispatcher(\"BookForm.jsp\")."; + assertTrue(errorMsg, called_getRequestDispatcher); + } + + @Test + public void _task5() throws Exception { + boolean called_setAttribute = false; + + try { + verify(request).setAttribute("book", mockBook); + called_setAttribute = true; + } catch (Throwable e) { + } + + String errorMsg = "In ControllerServlet showEditForm()," + + " did not call request.setAttribute(\"book\", bookObject);."; + assertTrue(errorMsg, called_setAttribute); + } + @Test + public void _task6() throws Exception { + boolean called_forward = false; + + try { + verify(mockRequestDispatcher).forward(request, response); + called_forward = true; + } catch (Throwable e) { + } + + String errorMsg = "In ControllerServlet showEditForm()," + + " did not call dispatcher.forward(request, response);."; + assertTrue(errorMsg, called_forward); + } } diff --git a/src/test/java/com/pluralsight/module2/Module2_Task7_thru10_IT.java b/src/test/java/com/pluralsight/module2/Module2_Task7_thru10_IT.java index 5a3f1495..cea5195d 100644 --- a/src/test/java/com/pluralsight/module2/Module2_Task7_thru10_IT.java +++ b/src/test/java/com/pluralsight/module2/Module2_Task7_thru10_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module2; + +import com.pluralsight.*; import org.junit.After; import org.junit.Before; @@ -22,101 +24,108 @@ public class Module2_Task7_thru10_IT { private String BOOK_FORM_NAME = "book_form"; private String indexUrl; private WebClient webClient; - HtmlPage firstPage; + HtmlPage firstPage; HtmlPage editPage; HtmlPage newPage; - @Before - public void setUp() throws IOException { - indexUrl = "http://localhost:8080"; //System.getProperty("integration.base.url"); - webClient = new WebClient(); - // Open the admin page - firstPage = webClient.getPage(indexUrl + "/books/admin"); - - try { - for ( HtmlAnchor a : firstPage.getAnchors()) { - String href = a.getHrefAttribute(); - if (href.contains("edit")) { - editPage = a.click(); - } - else if (href.contains("new")) { - newPage = a.click(); - } - } - } - catch ( Exception e) {} - } - - @After - public void tearDown() { - webClient.closeAllWindows(); - } - - // Verify they adapted the BookForm.jsp page for editing existing books - // and adding new book - // In this test check the form action is conditional, and the form h2 - @Test - public void module2_task7() { - assertNotNull("Link, edit, did not work.", editPage); - checkForm("Edit"); - } - - @Test - public void module2_task8() { - assertNotNull("Link, new, did not work.", newPage); - checkForm("New"); - } - - @Test - public void module2_task9() { - h2_correct("Edit"); - } - - @Test - public void module2_task10() { - h2_correct("New"); - } - - public void h2_correct(String urlStr) { - // First check if an H2 exists with text "New Book Form" - boolean h2Text_correct = false; - DomNodeList< DomElement > list; - if (urlStr.equals("Edit")) list = editPage.getElementsByTagName( "h2" ); - else list = newPage.getElementsByTagName( "h2" ); - String h2Text = ""; - String desiredText = urlStr + " Book Form"; - desiredText = desiredText.replaceAll("\\s+",""); - for( DomElement domElement : list ) - { - h2Text = domElement.getTextContent(); - h2Text = h2Text.replaceAll("\\s+",""); - if (h2Text.equals(desiredText)) - h2Text_correct = true; - } - - assertTrue("h2 text = " + h2Text + " , desiredText = " +desiredText, h2Text_correct); - } - - public void checkForm(String urlStr) { - // Get form and check action - HtmlForm form = null; - String errorMsg = ""; - String desiredAction = ""; - try { - if (urlStr.equals("Edit")) { - form = editPage.getFormByName(BOOK_FORM_NAME); - errorMsg = "Form, book_form, action not \"update\"."; - desiredAction = "update"; - } - else { - form = newPage.getFormByName(BOOK_FORM_NAME); - errorMsg = "Form, book_form, action not \"insert\"."; - desiredAction = "insert"; + @Before + public void setUp() throws IOException { + indexUrl = "http://localhost:8080"; // System.getProperty("integration.base.url"); + webClient = new WebClient(); + // Open the admin page + firstPage = webClient.getPage(indexUrl + "/books/admin"); + + try { + for (HtmlAnchor a : firstPage.getAnchors()) { + String href = a.getHrefAttribute(); + if (href.contains("edit")) { + editPage = a.click(); + } else if (href.contains("new")) { + newPage = a.click(); } - } catch (ElementNotFoundException e) {} - - assertNotNull("Form is null.", form); - String action = form.getActionAttribute(); - assertEquals(errorMsg, desiredAction, action); + } + } catch (Exception e) { } + } + + @After + public void tearDown() { + webClient.closeAllWindows(); + } + + // Verify they adapted the BookForm.jsp page for editing existing books + // and adding new book + // In this test check the form action is conditional, and the form h2 + @Test + public void _task7() { + assertNotNull("Link, edit, did not work.", editPage); + checkForm("Edit"); + } + + @Test + public void _task8() { + assertNotNull("Link, edit, did not work.", editPage); + checkForm("Edit"); + assertNotNull("Link, new, did not work.", newPage); + checkForm("New"); + } + + @Test + public void _task9() { + assertNotNull("Link, edit, did not work.", editPage); + h2_correct("Edit"); + } + + @Test + public void _task10() { + assertNotNull("Link, edit, did not work.", editPage); + h2_correct("Edit"); + assertNotNull("Link, new, did not work.", newPage); + h2_correct("New"); + } + + public void h2_correct(String urlStr) { + // First check if an H2 exists with text "New Book Form" + boolean h2Text_correct = false; + DomNodeList list; + if (urlStr.equals("Edit")) + list = editPage.getElementsByTagName("h2"); + else + list = newPage.getElementsByTagName("h2"); + String h2Text = ""; + String desiredText = urlStr + " Book Form"; + desiredText = desiredText.replaceAll("\\s+", ""); + for (DomElement domElement : list) { + h2Text = domElement.getTextContent(); + h2Text = h2Text.replaceAll("\\s+", ""); + if (h2Text.equals(desiredText)) + h2Text_correct = true; + } + String errorMsg = "The h2 tag in BookForm contains " + h2Text + " but we expected it to contain " + desiredText; + assertTrue(errorMsg, h2Text_correct); + } + + public void checkForm(String urlStr) { + // Get form and check action + HtmlForm form = null; + String errorMsg = ""; + String desiredAction = ""; + try { + if (urlStr.equals("Edit")) { + form = editPage.getFormByName(BOOK_FORM_NAME); + errorMsg = "Form, book_form, action not \"update\"."; + desiredAction = "update"; + } else { + form = newPage.getFormByName(BOOK_FORM_NAME); + errorMsg = "Form, book_form, action not \"insert\"."; + desiredAction = "insert"; + } + } catch (ElementNotFoundException e) { + } + + String formErrorMsg = "We can’t find a with name 'book_form' in BookForm.jsp"; + assertNotNull(formErrorMsg, form); + String action = form.getActionAttribute(); + assertEquals(errorMsg, desiredAction, action); + } } diff --git a/src/test/java/com/pluralsight/module3/Module3_Task1_thru_5_IT.java b/src/test/java/com/pluralsight/module3/Module3_Task1_thru_5_IT.java index 5d8ee1ab..8ca35caf 100644 --- a/src/test/java/com/pluralsight/module3/Module3_Task1_thru_5_IT.java +++ b/src/test/java/com/pluralsight/module3/Module3_Task1_thru_5_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module3; + +import com.pluralsight.*; import static org.junit.Assert.*; import java.sql.Connection; @@ -18,106 +20,109 @@ import java.io.*; - @RunWith(PowerMockRunner.class) -@PrepareForTest({DriverManager.class, PreparedStatement.class, BookDAO.class}) +@PrepareForTest({ DriverManager.class, PreparedStatement.class, BookDAO.class }) public class Module3_Task1_thru_5_IT { - static Method method = null; - static String sql = "UPDATE book SET title = ?, author = ?, price = ?" + - " WHERE id = ?"; - Connection spyConnection; - PreparedStatement mockStatement; - static BookDAO bookDAO; - static BookDAO spyBookDAO; - static boolean called_prepareStatement = false; - static boolean called_setTitle = false; - static boolean called_setAuthor = false; - static boolean called_setPrice = false; - static boolean called_setId = false; - static boolean called_executeUpdate = false; - static boolean called_close = false; - static String message = ""; - @Before - public void setUp() { - spyConnection = Mockito.mock(Connection.class); - mockStatement = Mockito.mock(PreparedStatement.class); - bookDAO = new BookDAO(spyConnection); - spyBookDAO = Mockito.spy(bookDAO); - - Book tempBookObject = new Book(1, "1984", "George Orwell", 1.50f); - try { - Mockito.when(spyConnection.prepareStatement(sql)).thenReturn(mockStatement); - method = BookDAO.class.getMethod("updateBook", Book.class); - method.invoke(spyBookDAO, tempBookObject); - } catch (Exception e) { - //e.printStackTrace(); - } - } - - // Verify updateBook() method exists in BookDAO - @Test - public void module3_Task1() throws Exception { - message = "The method updateBook() doesn't exist in BookDAO.java."; - assertNotNull(message, method); - } - - @Test - public void module3_Task2() throws Exception { - try { - Mockito.verify(spyConnection).prepareStatement(sql); - called_prepareStatement = true; - } catch (Throwable e) {} - - message = "The method updateBook() doesn't call prepareStatement() correctly."; - assertTrue(message, called_prepareStatement); - } - - @Test - public void module3_Task3() throws Exception { - try { - Mockito.verify(mockStatement).setString(1, "1984"); - called_setTitle = true; - Mockito.verify(mockStatement).setString(2, "George Orwell"); - called_setAuthor = true; - } catch (Throwable e) {} - - message = "The method updateBook() doesn't call setString() for the title."; - assertTrue(message, called_setTitle); - - message = "The method updateBook() doesn't call setString() for the author."; - assertTrue(message, called_setAuthor); - } - - @Test - public void module3_Task4() throws Exception { - try { - Mockito.verify(mockStatement).setFloat(3, 1.50f); - called_setPrice = true; - Mockito.verify(mockStatement).setInt(4, 1); - called_setId = true; - } catch (Throwable e) {} - - message = "The method updateBook() doesn't call setFloat() for the price."; - assertTrue(message, called_setPrice); - - message = "The method updateBook() doesn't call setInt() for the id."; - assertTrue(message, called_setId); - } - - @Test - public void module3_Task5() throws Exception { - try { - Mockito.verify(mockStatement).executeUpdate(); - called_executeUpdate = true; - Mockito.verify(mockStatement).close(); - called_close = true; - } catch (Throwable e) {} - - message = "The method updateBook() doesn't call executeUpdate()."; - assertTrue(message, called_executeUpdate); - - message = "The method updateBook() doesn't call PreparedStatement close()."; - assertTrue(message, called_close); - } + static Method method = null; + static String sql = "UPDATE book SET title = ?, author = ?, price = ?" + " WHERE id = ?"; + Connection spyConnection; + PreparedStatement mockStatement; + static BookDAO bookDAO; + static BookDAO spyBookDAO; + static boolean called_prepareStatement = false; + static boolean called_setTitle = false; + static boolean called_setAuthor = false; + static boolean called_setPrice = false; + static boolean called_setId = false; + static boolean called_executeUpdate = false; + static boolean called_close = false; + static String message = ""; + + @Before + public void setUp() { + spyConnection = Mockito.mock(Connection.class); + mockStatement = Mockito.mock(PreparedStatement.class); + bookDAO = new BookDAO(spyConnection); + spyBookDAO = Mockito.spy(bookDAO); + + Book tempBookObject = new Book(1, "1984", "George Orwell", 1.50f); + try { + Mockito.when(spyConnection.prepareStatement(sql)).thenReturn(mockStatement); + method = BookDAO.class.getMethod("updateBook", Book.class); + method.invoke(spyBookDAO, tempBookObject); + } catch (Exception e) { + // e.printStackTrace(); + } + } + + // Verify updateBook() method exists in BookDAO + @Test + public void _task1() throws Exception { + message = "The method updateBook() doesn't exist in BookDAO.java."; + assertNotNull(message, method); + } + + @Test + public void _task2() throws Exception { + try { + Mockito.verify(spyConnection).prepareStatement(sql); + called_prepareStatement = true; + } catch (Throwable e) { + } + + message = "The method updateBook() doesn't call prepareStatement() correctly."; + assertTrue(message, called_prepareStatement); + } + + @Test + public void _task3() throws Exception { + try { + Mockito.verify(mockStatement).setString(1, "1984"); + called_setTitle = true; + Mockito.verify(mockStatement).setString(2, "George Orwell"); + called_setAuthor = true; + } catch (Throwable e) { + } + + message = "The method updateBook() doesn't call setString() for the title."; + assertTrue(message, called_setTitle); + + message = "The method updateBook() doesn't call setString() for the author."; + assertTrue(message, called_setAuthor); + } + + @Test + public void _task4() throws Exception { + try { + Mockito.verify(mockStatement).setFloat(3, 1.50f); + called_setPrice = true; + Mockito.verify(mockStatement).setInt(4, 1); + called_setId = true; + } catch (Throwable e) { + } + + message = "The method updateBook() doesn't call setFloat() for the price."; + assertTrue(message, called_setPrice); + + message = "The method updateBook() doesn't call setInt() for the id."; + assertTrue(message, called_setId); + } + + @Test + public void _task5() throws Exception { + try { + Mockito.verify(mockStatement).executeUpdate(); + called_executeUpdate = true; + Mockito.verify(mockStatement).close(); + called_close = true; + } catch (Throwable e) { + } + + message = "The method updateBook() doesn't call executeUpdate()."; + assertTrue(message, called_executeUpdate); + + message = "The method updateBook() doesn't call PreparedStatement close()."; + assertTrue(message, called_close); + } } diff --git a/src/test/java/com/pluralsight/module3/Module3_Task6_and_7_IT.java b/src/test/java/com/pluralsight/module3/Module3_Task6_and_7_IT.java index 70587cd9..6ab8652f 100644 --- a/src/test/java/com/pluralsight/module3/Module3_Task6_and_7_IT.java +++ b/src/test/java/com/pluralsight/module3/Module3_Task6_and_7_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module3; + +import com.pluralsight.*; import static org.junit.Assert.*; import javax.servlet.http.HttpServletRequest; @@ -29,54 +31,62 @@ @RunWith(PowerMockRunner.class) @PrepareForTest(ControllerServlet.class) -public class Module3_Task6_and_7_IT extends Mockito{ +public class Module3_Task6_and_7_IT extends Mockito { static String tempID = "0"; static boolean called_updateBook = false; static HttpServletRequest request; static HttpServletResponse response; + private Method method = null; private ControllerServlet controllerServlet; - @Before - public void setUp() throws Exception { - controllerServlet = PowerMockito.spy(new ControllerServlet()); - - request = mock(HttpServletRequest.class); - response = mock(HttpServletResponse.class); - try { - when(request.getPathInfo()).thenReturn("/update"); - //PowerMockito.doNothing().when(controllerServlet, "updateBook", request, response); - when(request.getParameter("id")).thenReturn(tempID); - } catch (MethodNotFoundException e) {} + @Before + public void setUp() throws Exception { try { - controllerServlet.doGet(request, response); - } catch (Exception e) {} - } - - // Verify updateBook() exists in ControllerServlet - @Test - public void module3_task6() throws Exception { - Method method = null; + method = Whitebox.getMethod(ControllerServlet.class, "updateBook", HttpServletRequest.class, + HttpServletResponse.class); + } catch (Exception e) { + } + + if (method != null) { + controllerServlet = PowerMockito.spy(new ControllerServlet()); + + request = mock(HttpServletRequest.class); + response = mock(HttpServletResponse.class); try { - method = Whitebox.getMethod(ControllerServlet.class, - "updateBook", HttpServletRequest.class, HttpServletResponse.class); - } catch (Exception e) {} + when(request.getPathInfo()).thenReturn("/update"); + // PowerMockito.doNothing().when(controllerServlet, "updateBook", request, + // response); + when(request.getParameter("id")).thenReturn(tempID); + } catch (MethodNotFoundException e) { + } + try { + controllerServlet.doGet(request, response); + } catch (Exception e) { + } + } + } + + // Verify updateBook() exists in ControllerServlet + @Test + public void _task6() throws Exception { + String errorMsg = "private void updateBook() does not exist in ControllerServlet"; + assertNotNull(errorMsg, method); + } - String errorMsg = "private void updateBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, method); + @Test + public void _task7() throws Exception { + String errorMsg = "private void updateBook() does not exist in ControllerServlet"; + assertNotNull(errorMsg, method); + + try { + PowerMockito.verifyPrivate(controllerServlet).invoke("updateBook", request, response); + called_updateBook = true; + } catch (Throwable e) { } - @Test - public void module3_task7() throws Exception { - // try { - // PowerMockito.verifyPrivate(controllerServlet) - // .invoke("updateBook", request, response); - // called_updateBook = true; - // } catch (Throwable e) {} - - String errorMsg = "After action \"" + "/update" + - "\", did not call updateBook()."; - assertTrue(errorMsg, called_updateBook); - } + errorMsg = "After action \"" + "/update" + "\", did not call updateBook()."; + assertTrue(errorMsg, called_updateBook); + } } diff --git a/src/test/java/com/pluralsight/module3/Module3_Task8_thru_11_IT.java b/src/test/java/com/pluralsight/module3/Module3_Task8_thru_11_IT.java index c98ff541..a1d92801 100644 --- a/src/test/java/com/pluralsight/module3/Module3_Task8_thru_11_IT.java +++ b/src/test/java/com/pluralsight/module3/Module3_Task8_thru_11_IT.java @@ -1,4 +1,6 @@ -package com.pluralsight; +package com.pluralsight.module3; + +import com.pluralsight.*; import static org.junit.Assert.*; import javax.servlet.http.HttpServletRequest; @@ -26,15 +28,15 @@ import java.io.*; -public class Module3_Task8_thru_11_IT extends Mockito{ +public class Module3_Task8_thru_11_IT extends Mockito { static StringWriter stringWriter = new StringWriter(); static String tempIDStr = "1"; - static int tempID = 1; - static String tempTitle = "1984"; - static String tempAuthor = "George Orwell"; - static String tempPriceStr = "1.50"; - static float tempPrice = 1.50f; + static int tempID = 1; + static String tempTitle = "1984"; + static String tempAuthor = "George Orwell"; + static String tempPriceStr = "1.50"; + static float tempPrice = 1.50f; static boolean called_getId = false; static boolean called_getTitle = false; @@ -48,14 +50,14 @@ public class Module3_Task8_thru_11_IT extends Mockito{ static Method updateMethod = null; @Mock - private BookDAO mockBookDAO; + private BookDAO mockBookDAO; - @InjectMocks - private ControllerServlet controllerServlet; + @InjectMocks + private ControllerServlet controllerServlet; - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); request = mock(HttpServletRequest.class); response = mock(HttpServletResponse.class); @@ -67,100 +69,101 @@ public void setUp() throws Exception { when(request.getParameter("bookauthor")).thenReturn(tempAuthor); when(request.getParameter("bookprice")).thenReturn(tempPriceStr); - try { - updateMethod = Whitebox.getMethod(ControllerServlet.class, - "updateBook", HttpServletRequest.class, HttpServletResponse.class); - } catch (Exception e) {} + updateMethod = Whitebox.getMethod(ControllerServlet.class, "updateBook", HttpServletRequest.class, + HttpServletResponse.class); + } catch (Exception e) { + } + + // String errorMsg = "private void updateBook() does not exist in + // ControllerServlet"; + // assertNotNull(errorMsg, updateMethod); + + if (updateMethod != null) { + try { + controllerServlet.doGet(request, response); + } catch (Exception e) { + } + } + } + @Test + public void _task8() throws Exception { String errorMsg = "private void updateBook() does not exist in ControllerServlet"; assertNotNull(errorMsg, updateMethod); try { - controllerServlet.doGet(request, response); - } catch (Exception e) {} - } + verify(request).getParameter("id"); + called_getId = true; + } catch (Throwable e) { + } - @Test - public void module3_task8() throws Exception { - String errorMsg = "private void updateBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, updateMethod); + errorMsg = "After action \"" + "/update" + "\", did not call getParameter(\"id\")."; + assertTrue(errorMsg, called_getId); + } - try { - verify(request).getParameter("id"); - called_getId = true; - } catch (Exception e) {} - - errorMsg = "After action \"" + "/update" + - "\", did not call getParameter(\"id\")."; - assertTrue(errorMsg, called_getId); - } + @Test + public void _task9() throws Exception { + String errorMsg = "private void updateBook() does not exist in ControllerServlet"; + assertNotNull(errorMsg, updateMethod); - @Test - public void module3_task9() throws Exception { - String errorMsg = "private void updateBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, updateMethod); + try { + verify(request).getParameter("booktitle"); + called_getTitle = true; + verify(request).getParameter("bookauthor"); + called_getAuthor = true; + verify(request).getParameter("bookprice"); + called_getPrice = true; + } catch (Throwable e) { + } + + errorMsg = "After action \"" + "/update" + "\", did not call getParameter(\"booktitle\")."; + assertTrue(errorMsg, called_getTitle); + errorMsg = "After action \"" + "/update" + "\", did not call getParameter(\"bookauthor\")."; + assertTrue(errorMsg, called_getAuthor); + errorMsg = "After action \"" + "/update" + "\", did not call getParameter(\"bookprice\")."; + assertTrue(errorMsg, called_getPrice); + } + + @Test + public void _task10() throws Exception { + String errorMsg = "private void updateBook() does not exist in ControllerServlet"; + assertNotNull(errorMsg, updateMethod); - try { - verify(request).getParameter("booktitle"); - called_getTitle = true; - verify(request).getParameter("bookauthor"); - called_getAuthor = true; - verify(request).getParameter("bookprice"); - called_getPrice = true; - } catch (Exception e) {} - - errorMsg = "After action \"" + "/update" + - "\", did not call getParameter(\"booktitle\")."; - assertTrue(errorMsg, called_getTitle); - errorMsg = "After action \"" + "/update" + - "\", did not call getParameter(\"bookauthor\")."; - assertTrue(errorMsg, called_getAuthor); - errorMsg = "After action \"" + "/update" + - "\", did not call getParameter(\"bookprice\")."; - assertTrue(errorMsg, called_getPrice); - } - - @Test - public void module3_task10() throws Exception { - String errorMsg = "private void updateBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, updateMethod); - - Method method = null; - try { - method = BookDAO.class.getMethod("updateBook", int.class); - } catch (NoSuchMethodException e) { - //e.printStackTrace(); - } + Method method = null; + try { + method = BookDAO.class.getMethod("updateBook", Book.class); + } catch (NoSuchMethodException e) { + // e.printStackTrace(); + } - errorMsg = "The method updateBook() doesn't exist in BookDAO.java."; - assertNotNull(errorMsg, method); + errorMsg = "The method updateBook() doesn't exist in BookDAO.java."; + assertNotNull(errorMsg, method); - MockingDetails mockingDetails = Mockito.mockingDetails(mockBookDAO); + MockingDetails mockingDetails = Mockito.mockingDetails(mockBookDAO); - Collection invocations = mockingDetails.getInvocations(); + Collection invocations = mockingDetails.getInvocations(); - List methodsCalled = new ArrayList<>(); - for (Invocation anInvocation : invocations) { - methodsCalled.add(anInvocation.getMethod().getName()); - } - errorMsg = "After action \"" + "/update" + - "\", did not udpateBook(newBookObject)."; - assertTrue(errorMsg, methodsCalled.contains("updateBook")); - } + List methodsCalled = new ArrayList<>(); + for (Invocation anInvocation : invocations) { + methodsCalled.add(anInvocation.getMethod().getName()); + } + errorMsg = "After action \"" + "/update" + "\", did not updateBook(newBookObject)."; + assertTrue(errorMsg, methodsCalled.contains("updateBook")); + } - @Test - public void module3_task11() throws Exception { - String errorMsg = "private void updateBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, updateMethod); + @Test + public void _task11() throws Exception { + String errorMsg = "private void updateBook() does not exist in ControllerServlet"; + assertNotNull(errorMsg, updateMethod); - try { - verify(response).sendRedirect("list"); - called_sendRedirect = true; - } catch (Exception e) {} - - errorMsg = "In ControllerServlet updateBook()," + - " did not call sendRedirect(\"list\")."; - assertTrue(errorMsg, called_sendRedirect); - } + try { + verify(response).sendRedirect("list"); + called_sendRedirect = true; + } catch (Throwable e) { + } + + errorMsg = "In ControllerServlet updateBook()," + " did not call sendRedirect(\"list\")."; + assertTrue(errorMsg, called_sendRedirect); + } }