diff --git a/book_store.db b/book_store.db index 4a86d6e6..6022fd4d 100644 Binary files a/book_store.db and b/book_store.db differ diff --git a/pom.xml b/pom.xml index 1a90b05e..35f01a49 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,6 +96,7 @@ 8080 / + true 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..705aaa86 100644 --- a/src/main/java/com/pluralsight/BookDAO.java +++ b/src/main/java/com/pluralsight/BookDAO.java @@ -90,36 +90,34 @@ 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(); - // } - // } + + 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..98ee5512 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,15 @@ 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; + case "/delete": + deleteBook(request, response); + break; + case "/edit": + showEditForm(request, response); + break; + case "/update": + updateBook(request, response); + break; default: listBooks(request, response); break; @@ -102,22 +102,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 +114,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) */ @@ -154,5 +124,30 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) doGet(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 showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ + int id = Integer.parseInt(request.getParameter("id")); + Book existedBook = bookDAO.getBook(id); + RequestDispatcher dispatcher = request.getRequestDispatcher("/BookForm.jsp"); + request.setAttribute("book", existedBook); + dispatcher.forward(request, response); + } + + private void updateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ + int id = Integer.parseInt(request.getParameter("id")); + String title = request.getParameter("booktitle"); + String author = request.getParameter("bookauthor"); + Float price = Float.parseFloat(request.getParameter("bookprice")); + + Book newBook = new Book(id, title, author, price); + + bookDAO.updateBook(newBook); + response.sendRedirect("list"); + } } 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..ff1565e2 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 --%> + Edit + Delete diff --git a/src/main/webapp/BookForm.jsp b/src/main/webapp/BookForm.jsp index 353c2a51..03c1740c 100644 --- a/src/main/webapp/BookForm.jsp +++ b/src/main/webapp/BookForm.jsp @@ -11,39 +11,32 @@
- - <%-- +
- --%> + - <%-- --%> +

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

- <%-- - - --%>

-

- <%--

--%> + +

-

- <%--

--%> -

-

- <%--

--%> +

+

+

-
+
- + \ No newline at end of file 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" %> - - - - Codestin Search App - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - -
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..0144e088 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,5 @@ -package com.pluralsight; +package com.pluralsight.module1; +import com.pluralsight.*; import org.junit.After; import org.junit.Before; @@ -34,7 +35,7 @@ public void tearDown() { // Verify the edit and delete hrefs, in BookAdmin.jsp contain the id @Test - public void module1_task1() { + public void _task1() { url_contains_id("Delete"); url_contains_id("Edit"); } 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..422a4075 --- /dev/null +++ b/src/test/java/com/pluralsight/module1/Module1_Task2_and_3_IT.java @@ -0,0 +1,79 @@ +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..cc51128e 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,5 @@ -package com.pluralsight; +package com.pluralsight.module1; +import com.pluralsight.*; import static org.junit.Assert.*; import org.junit.Test; @@ -10,7 +11,7 @@ public class Module1_Task4_IT { // Verify the deleteBook() method exists in BookDAO @Test - public void module1_task4() throws Exception { + public void _task4() throws Exception { Method method = null; try { 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..e271b466 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,5 @@ -package com.pluralsight; +package com.pluralsight.module1; +import com.pluralsight.*; import static org.junit.Assert.*; import java.sql.Connection; @@ -22,9 +23,9 @@ @PrepareForTest({DriverManager.class, PreparedStatement.class, BookDAO.class}) public class Module1_Task5_IT { - // Verify the deleteBook() method exists in BookDAO + // Verify the deleteBook() in BookDAO calls prepareStatement() @Test - public void module1_task5() throws Exception { + public void _task5() throws Exception { Method method = null; String sql = "DELETE FROM book WHERE id = ?"; Connection mockConnection = Mockito.mock(Connection.class); 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..7cf42715 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,5 @@ -package com.pluralsight; +package com.pluralsight.module1; +import com.pluralsight.*; import static org.junit.Assert.*; import java.sql.Connection; @@ -24,7 +25,7 @@ public class Module1_Task6_IT { // Verify the deleteBook() method exists in BookDAO @Test - public void module1_task6() throws Exception { + public void _task6() throws Exception { Method method = null; String sql = "DELETE FROM book WHERE id = ?"; Connection spyConnection = Mockito.mock(Connection.class); 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..1461c702 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,5 @@ -package com.pluralsight; +package com.pluralsight.module1; +import com.pluralsight.*; import static org.junit.Assert.*; import javax.servlet.http.HttpServletRequest; @@ -56,17 +57,18 @@ public void setUp() throws Exception { "deleteBook", HttpServletRequest.class, HttpServletResponse.class); } catch (Exception e) {} - String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, deleteMethod); - - try { - controllerServlet.doGet(request, response); - } 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 module1_task7() throws Exception { + public void _task7() throws Exception { String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; assertNotNull(errorMsg, deleteMethod); @@ -78,7 +80,8 @@ public void module1_task7() throws Exception { for (Invocation anInvocation : invocations) { methodsCalled.add(anInvocation.getMethod().getName()); } - assertTrue(methodsCalled.contains("deleteBook")); + errorMsg = "The ControllerServlet deleteBook() method was not called."; + assertTrue(errorMsg, methodsCalled.contains("deleteBook")); try { verify(request, atLeast(1)).getParameter("id"); @@ -91,7 +94,7 @@ public void module1_task7() throws Exception { } @Test - public void module1_task8() throws Exception { + public void _task8() throws Exception { String errorMsg = "private void deleteBook() does not exist in ControllerServlet"; assertNotNull(errorMsg, deleteMethod); try { 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..23ee70d1 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,5 @@ -package com.pluralsight; +package com.pluralsight.module2; +import com.pluralsight.*; import org.junit.After; import org.junit.Before; @@ -25,6 +26,7 @@ public class Module2_Task11_thru_14_IT { 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 { @@ -48,9 +50,9 @@ public void tearDown() { // and adding new book // In this test check the form input fields have values filled in @Test - public void module2_task11() { + public void _task11() { assertNotNull("Link Edit did not work.", nextPage); - assertNotNull("Form is null.", form); + assertNotNull(formErrorMsg, form); //Get id input field try { HtmlInput inputId = form.getInputByName("id"); @@ -71,9 +73,9 @@ public void module2_task11() { } @Test - public void module2_task12() { + public void _task12() { assertNotNull("Link Edit did not work.", nextPage); - assertNotNull("Form is null.", form); + assertNotNull(formErrorMsg, form); // Get title input field, check value try { HtmlInput inputTitle = form.getInputByName("booktitle"); @@ -86,9 +88,9 @@ public void module2_task12() { } @Test - public void module2_task13() { + public void _task13() { assertNotNull("Link Edit did not work.", nextPage); - assertNotNull("Form is null.", form); + assertNotNull(formErrorMsg, form); // Get author input field, check value try { @@ -99,12 +101,6 @@ public void module2_task13() { }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 { 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..79366031 --- /dev/null +++ b/src/test/java/com/pluralsight/module2/Module2_Task1_and_2_IT.java @@ -0,0 +1,79 @@ +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..e2a42b83 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,5 @@ -package com.pluralsight; +package com.pluralsight.module2; +import com.pluralsight.*; import static org.junit.Assert.*; import javax.servlet.http.HttpServletRequest; @@ -19,6 +20,8 @@ import java.lang.reflect.Method; import java.io.*; + + public class Module2_Task3_thru_6_IT extends Mockito{ static StringWriter stringWriter = new StringWriter(); @@ -59,7 +62,7 @@ public void setUp() throws Exception { // 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 { + public void _task3() throws Exception { boolean called_getParameter = false; boolean called_getBook = false; @@ -82,7 +85,7 @@ public void module2_task3() throws Exception { } @Test - public void module2_task4() throws Exception { + public void _task4() throws Exception { boolean called_getRequestDispatcher = false; try { @@ -96,7 +99,7 @@ public void module2_task4() throws Exception { } @Test - public void module2_task5() throws Exception { + public void _task5() throws Exception { boolean called_setAttribute = false; try { @@ -110,7 +113,7 @@ public void module2_task5() throws Exception { } @Test - public void module2_task6() throws Exception { + public void _task6() throws Exception { boolean called_forward = false; try { 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..2bf7e288 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,5 @@ -package com.pluralsight; +package com.pluralsight.module2; +import com.pluralsight.*; import org.junit.After; import org.junit.Before; @@ -56,24 +57,27 @@ public void tearDown() { // and adding new book // In this test check the form action is conditional, and the form h2 @Test - public void module2_task7() { + public void _task7() { assertNotNull("Link, edit, did not work.", editPage); checkForm("Edit"); } @Test - public void module2_task8() { + public void _task8() { + assertNotNull("Link, edit, did not work.", editPage); + checkForm("Edit"); assertNotNull("Link, new, did not work.", newPage); checkForm("New"); } @Test - public void module2_task9() { + public void _task9() { h2_correct("Edit"); } @Test - public void module2_task10() { + public void _task10() { + h2_correct("Edit"); h2_correct("New"); } @@ -93,8 +97,9 @@ public void h2_correct(String urlStr) { if (h2Text.equals(desiredText)) h2Text_correct = true; } - - assertTrue("h2 text = " + h2Text + " , desiredText = " +desiredText, h2Text_correct); + 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) { @@ -115,7 +120,8 @@ public void checkForm(String urlStr) { } } catch (ElementNotFoundException e) {} - assertNotNull("Form is null.", form); + 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..d479698e 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,5 @@ -package com.pluralsight; +package com.pluralsight.module3; +import com.pluralsight.*; import static org.junit.Assert.*; import java.sql.Connection; @@ -57,13 +58,13 @@ public void setUp() { // Verify updateBook() method exists in BookDAO @Test - public void module3_Task1() throws Exception { + public void _task1() throws Exception { message = "The method updateBook() doesn't exist in BookDAO.java."; assertNotNull(message, method); } @Test - public void module3_Task2() throws Exception { + public void _task2() throws Exception { try { Mockito.verify(spyConnection).prepareStatement(sql); called_prepareStatement = true; @@ -74,7 +75,7 @@ public void module3_Task2() throws Exception { } @Test - public void module3_Task3() throws Exception { + public void _task3() throws Exception { try { Mockito.verify(mockStatement).setString(1, "1984"); called_setTitle = true; @@ -90,7 +91,7 @@ public void module3_Task3() throws Exception { } @Test - public void module3_Task4() throws Exception { + public void _task4() throws Exception { try { Mockito.verify(mockStatement).setFloat(3, 1.50f); called_setPrice = true; @@ -106,7 +107,7 @@ public void module3_Task4() throws Exception { } @Test - public void module3_Task5() throws Exception { + public void _task5() throws Exception { try { Mockito.verify(mockStatement).executeUpdate(); called_executeUpdate = true; 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..d3607fd5 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,5 @@ -package com.pluralsight; +package com.pluralsight.module3; +import com.pluralsight.*; import static org.junit.Assert.*; import javax.servlet.http.HttpServletRequest; @@ -36,46 +37,51 @@ public class Module3_Task6_and_7_IT extends Mockito{ 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) {} try { - controllerServlet.doGet(request, response); + method = Whitebox.getMethod(ControllerServlet.class, + "updateBook", HttpServletRequest.class, HttpServletResponse.class); } catch (Exception e) {} - } - // Verify updateBook() exists in ControllerServlet - @Test - public void module3_task6() throws Exception { - Method method = null; + 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); + 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); } @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" + + 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) {} + + 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..fe1a686e 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,5 @@ -package com.pluralsight; +package com.pluralsight.module3; +import com.pluralsight.*; import static org.junit.Assert.*; import javax.servlet.http.HttpServletRequest; @@ -73,23 +74,25 @@ public void setUp() throws Exception { "updateBook", HttpServletRequest.class, HttpServletResponse.class); } catch (Exception e) {} - String errorMsg = "private void updateBook() does not exist in ControllerServlet"; - assertNotNull(errorMsg, updateMethod); + // String errorMsg = "private void updateBook() does not exist in ControllerServlet"; + // assertNotNull(errorMsg, updateMethod); - try { - controllerServlet.doGet(request, response); - } catch (Exception e) {} + if (updateMethod != null) { + try { + controllerServlet.doGet(request, response); + } catch (Exception e) {} + } } @Test - public void module3_task8() throws Exception { + public void _task8() throws Exception { String errorMsg = "private void updateBook() does not exist in ControllerServlet"; assertNotNull(errorMsg, updateMethod); try { verify(request).getParameter("id"); called_getId = true; - } catch (Exception e) {} + } catch (Throwable e) {} errorMsg = "After action \"" + "/update" + "\", did not call getParameter(\"id\")."; @@ -97,7 +100,7 @@ public void module3_task8() throws Exception { } @Test - public void module3_task9() throws Exception { + public void _task9() throws Exception { String errorMsg = "private void updateBook() does not exist in ControllerServlet"; assertNotNull(errorMsg, updateMethod); @@ -108,7 +111,7 @@ public void module3_task9() throws Exception { called_getAuthor = true; verify(request).getParameter("bookprice"); called_getPrice = true; - } catch (Exception e) {} + } catch (Throwable e) {} errorMsg = "After action \"" + "/update" + "\", did not call getParameter(\"booktitle\")."; @@ -122,13 +125,13 @@ public void module3_task9() throws Exception { } @Test - public void module3_task10() throws Exception { + public void _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); + method = BookDAO.class.getMethod("updateBook", Book.class); } catch (NoSuchMethodException e) { //e.printStackTrace(); } @@ -145,19 +148,19 @@ public void module3_task10() throws Exception { methodsCalled.add(anInvocation.getMethod().getName()); } errorMsg = "After action \"" + "/update" + - "\", did not udpateBook(newBookObject)."; + "\", did not updateBook(newBookObject)."; assertTrue(errorMsg, methodsCalled.contains("updateBook")); } @Test - public void module3_task11() throws Exception { + 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) {} + } catch (Throwable e) {} errorMsg = "In ControllerServlet updateBook()," + " did not call sendRedirect(\"list\").";