├── book_store.db
├── .gitignore
├── src
├── main
│ ├── java
│ │ └── com
│ │ │ └── pluralsight
│ │ │ ├── NameHandler.java
│ │ │ ├── Book.java
│ │ │ ├── LoginServlet.java
│ │ │ ├── DBConnection.java
│ │ │ ├── BookDAO.java
│ │ │ └── ControllerServlet.java
│ └── webapp
│ │ ├── WEB-INF
│ │ └── web.xml
│ │ ├── css
│ │ └── style.css
│ │ ├── BookList.jsp
│ │ ├── ShoppingCart.jsp
│ │ ├── BookForm.jsp
│ │ └── BookAdmin.jsp
└── test
│ └── java
│ └── com
│ └── pluralsight
│ ├── module1
│ ├── Module1_Task4_IT.java
│ ├── Module1_Task2_IT.java
│ ├── Module1_Task5_IT.java
│ ├── Module1_Task3_IT.java
│ ├── Module1_Task1_IT.java
│ ├── Module1_Task6_IT.java
│ └── Module1_Task7_and_8_IT.java
│ ├── module2
│ ├── Module2_Task1_IT.java
│ ├── Module2_Task2_IT.java
│ ├── Module2_Task7_thru10_IT.java
│ ├── Module2_Task3_thru_6_IT.java
│ └── Module2_Task11_thru_14_IT.java
│ └── module3
│ ├── Module3_Task6_and_7_IT.java
│ ├── Module3_Task1_thru_5_IT.java
│ └── Module3_Task8_thru_11_IT.java
└── pom.xml
/book_store.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/renz45/Java-BookStore/HEAD/book_store.db
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Eclipse
2 | .classpath
3 | .project
4 | .settings/
5 |
6 | # Server stuff
7 | Servers/
8 |
9 | # Intellij
10 | .idea/
11 | *.iml
12 | *.iws
13 |
14 | # Mac
15 | .DS_Store
16 |
17 | # Maven
18 | log/
19 | target/
20 |
21 | # Default
22 | /build/
23 | /target/
24 | /.metadata/
25 | /.recommenders/
26 |
--------------------------------------------------------------------------------
/src/main/java/com/pluralsight/NameHandler.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | public class NameHandler {
4 |
5 | private String name;
6 |
7 | /** Creates a new instance of NameHandler */
8 | public NameHandler() {
9 | name = null;
10 | }
11 |
12 | public String getName() {
13 | return name;
14 | }
15 |
16 | public void setName(String name) {
17 | this.name = name;
18 | }
19 |
20 |
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module1/Module1_Task4_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import org.junit.Test;
5 |
6 | import java.lang.reflect.Method;
7 | import java.io.*;
8 |
9 | public class Module1_Task4_IT {
10 |
11 | // Verify the deleteBook() method exists in BookDAO
12 | @Test
13 | public void module1_task4() throws Exception {
14 | Method method = null;
15 |
16 | try {
17 | method = BookDAO.class.getMethod("deleteBook", int.class);
18 | } catch (NoSuchMethodException e) {
19 | //e.printStackTrace();
20 | }
21 |
22 | String message = "The method deleteBook() doesn't exist in BookDAO.java.";
23 | assertNotNull(message, method);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 | Archetype Created Web Application
9 |
10 |
11 | LoginServlet
12 | LoginServlet
13 |
14 | com.pluralsight.LoginServlet
15 |
16 |
17 | LoginServlet
18 | /LoginServlet
19 |
20 |
21 |
22 | ControllerServlet
23 | com.pluralsight.ControllerServlet
24 |
25 |
26 |
27 | ControllerServlet
28 | /books/*
29 |
30 |
31 |
--------------------------------------------------------------------------------
/src/main/webapp/css/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | font-family: "Open Sans", Open Sans, "Verdana", Verdana, Arial, sans;
3 | }
4 |
5 |
6 | .container {
7 | text-align: center;
8 | margin: 80px auto 0px;
9 |
10 | }
11 |
12 | .booktable table {
13 | width: 75%;
14 | margin: 0 auto;
15 | }
16 | table, td, th {
17 | border: 1px solid #ddd;
18 | text-align: left;
19 | }
20 | table {
21 | border-collapse: collapse;
22 | }
23 | th, td {
24 | padding: 15px;
25 | }
26 | form {
27 | margin: 80px;
28 | border: 1px solid #ddd;
29 | }
30 |
31 | ul {
32 | list-style-type: none;
33 | margin: 0;
34 | padding: 0;
35 | overflow: hidden;
36 | background-color: #333;
37 | position: fixed;
38 | top: 0;
39 | width: 100%;
40 | }
41 |
42 | li {
43 | float: left;
44 | }
45 |
46 | li a {
47 | display: block;
48 | color: white;
49 | text-align: center;
50 | padding: 14px 16px;
51 | text-decoration: none;
52 | }
53 |
54 | li a:hover:not(.active) {
55 | background-color: #111;
56 | }
57 |
58 | a {
59 | text-decoration: none;
60 | }
61 |
62 | .active {
63 | background-color: #cc0066;
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/com/pluralsight/Book.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | public class Book {
4 | int id;
5 | String title;
6 | String author;
7 | float price;
8 |
9 | public Book(String title, String author, float price) {
10 | this.title = title;
11 | this.author = author;
12 | this.price = price;
13 | }
14 |
15 | public Book(int id, String title, String author, float price) {
16 | this.id = id;
17 | this.title = title;
18 | this.author = author;
19 | this.price = price;
20 | }
21 |
22 | @Override
23 | public String toString() {
24 | return "(" + title + ", " + author + ", " + price + ")";
25 | }
26 |
27 | public int getId() {
28 | return id;
29 | }
30 | public void setId(int id) {
31 | this.id = id;
32 | }
33 | public String getTitle() {
34 | return title;
35 | }
36 | public void setTitle(String title) {
37 | this.title = title;
38 | }
39 | public String getAuthor() {
40 | return author;
41 | }
42 | public void setAuthor(String author) {
43 | this.author = author;
44 | }
45 | public float getPrice() {
46 | return price;
47 | }
48 | public void setPrice(float price) {
49 | this.price = price;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module1/Module1_Task2_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 |
7 | import java.sql.Connection;
8 | import java.sql.PreparedStatement;
9 |
10 | import org.junit.BeforeClass;
11 | import org.junit.Before;
12 | import org.junit.Test;
13 | import org.mockito.Mockito;
14 |
15 | import org.powermock.reflect.Whitebox;
16 | import java.lang.reflect.Method;
17 |
18 | import java.io.*;
19 |
20 | public class Module1_Task2_IT {
21 |
22 | private ControllerServlet controllerServlet;
23 |
24 | // @Before
25 | // public void setUp() throws Exception {
26 | // controllerServlet = new ControllerServlet();
27 | // }
28 |
29 | // Verify the deleteBook() method exists in ControllerServlet
30 | @Test
31 | public void module1_task2() throws Exception {
32 | Method method = null;
33 | try {
34 | method = Whitebox.getMethod(ControllerServlet.class,
35 | "deleteBook", HttpServletRequest.class, HttpServletResponse.class);
36 | } catch (Exception e) {}
37 |
38 | String errorMsg = "private void deleteBook() does not exist in ControllerServlet";
39 | assertNotNull(errorMsg, method);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module2/Module2_Task1_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 |
7 | import java.sql.Connection;
8 | import java.sql.PreparedStatement;
9 |
10 | import org.junit.BeforeClass;
11 | import org.junit.Before;
12 | import org.junit.Test;
13 | import org.mockito.Mockito;
14 |
15 | import org.powermock.reflect.Whitebox;
16 | import java.lang.reflect.Method;
17 |
18 | import java.io.*;
19 |
20 | public class Module2_Task1_IT {
21 |
22 | private ControllerServlet controllerServlet;
23 |
24 | // @Before
25 | // public void setUp() throws Exception {
26 | // controllerServlet = new ControllerServlet();
27 | // }
28 |
29 | // Verify the showEditForm() method exists in ControllerServlet
30 | @Test
31 | public void module2_task1() throws Exception {
32 | Method method = null;
33 | try {
34 | method = Whitebox.getMethod(ControllerServlet.class,
35 | "showEditForm", HttpServletRequest.class, HttpServletResponse.class);
36 | } catch (Exception e) {}
37 |
38 | String errorMsg = "private void showEditForm() does not exist in ControllerServlet";
39 | assertNotNull(errorMsg, method);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/com/pluralsight/LoginServlet.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import java.io.IOException;
4 | import java.io.PrintWriter;
5 |
6 | import javax.servlet.ServletException;
7 | import javax.servlet.http.HttpServlet;
8 | import javax.servlet.http.HttpServletRequest;
9 | import javax.servlet.http.HttpServletResponse;
10 |
11 | /**
12 | * Servlet implementation class LoginServlet
13 | */
14 | public class LoginServlet extends HttpServlet {
15 | private static final long serialVersionUID = 1L;
16 |
17 | /**
18 | * @see HttpServlet#HttpServlet()
19 | */
20 | public LoginServlet() {
21 | super();
22 | // TODO Auto-generated constructor stub
23 | }
24 |
25 | /**
26 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
27 | */
28 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
29 | // TODO Auto-generated method stub
30 | response.getWriter().append("Served at: ").append(request.getContextPath());
31 | }
32 |
33 | /**
34 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
35 | */
36 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
37 | // Get parameters
38 | String username = request.getParameter("username");
39 | String password = request.getParameter("password");
40 |
41 | PrintWriter writer = response.getWriter();
42 | writer.println("username = " + username + ", password = " + password);
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/webapp/BookList.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8"
2 | pageEncoding="UTF-8"%>
3 | <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
4 | <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
5 |
6 |
7 |
8 | Book Store
9 |
10 |
11 |
12 |
13 |
14 |
19 |
20 |
21 |
22 |
23 | List of Books
24 |
25 | | Title |
26 | Author |
27 | Price |
28 | |
29 |
30 |
31 |
32 |
33 | | ${ item.getTitle() } |
34 | ${ item.getAuthor() } |
35 | |
36 | Add to Cart |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/main/webapp/ShoppingCart.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8"
2 | pageEncoding="UTF-8"%>
3 | <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
4 | <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
5 |
6 |
7 |
8 | Book Store
9 |
10 |
11 |
12 |
13 |
14 |
19 |
20 |
21 |
22 |
23 | List of Books
24 |
25 | | Title |
26 | Author |
27 | Price |
28 | In Stock |
29 | Add Book |
30 |
31 |
32 |
33 |
34 | | ${ item.getTitle() } |
35 | ${ item.getAuthor() } |
36 | |
37 | 10 |
38 | Edit Delete |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/src/main/webapp/BookForm.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8"
2 | pageEncoding="UTF-8"%>
3 | <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
4 |
5 |
6 |
7 | Book Store
8 |
9 |
10 |
11 |
16 |
17 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/src/main/webapp/BookAdmin.jsp:
--------------------------------------------------------------------------------
1 | <%@ page language="java" contentType="text/html; charset=UTF-8"
2 | pageEncoding="UTF-8"%>
3 | <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
4 | <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
5 |
6 |
7 |
8 | Book Store
9 |
10 |
11 |
12 |
13 |
14 |
19 |
20 |
21 |
22 |
23 | List of Books
24 |
25 | | Title |
26 | Author |
27 | Price |
28 | In Stock |
29 | Add Book |
30 |
31 |
32 |
33 |
34 | | ${ item.getTitle() } |
35 | ${ item.getAuthor() } |
36 | |
37 | 10 |
38 | Edit
39 | Delete |
40 | <%-- Edit
41 | Delete | --%>
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module1/Module1_Task5_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import java.sql.Connection;
5 | import java.sql.DriverManager;
6 | import java.sql.PreparedStatement;
7 |
8 | import org.junit.BeforeClass;
9 | import org.junit.Test;
10 | import org.mockito.Mockito;
11 | import org.junit.runner.RunWith;
12 | import org.powermock.api.mockito.PowerMockito;
13 | import org.powermock.core.classloader.annotations.PrepareForTest;
14 | import org.powermock.modules.junit4.PowerMockRunner;
15 |
16 | import java.lang.reflect.Method;
17 |
18 | import java.io.*;
19 |
20 |
21 | @RunWith(PowerMockRunner.class)
22 | @PrepareForTest({DriverManager.class, PreparedStatement.class, BookDAO.class})
23 | public class Module1_Task5_IT {
24 |
25 | // Verify the deleteBook() method exists in BookDAO
26 | @Test
27 | public void module1_task5() throws Exception {
28 | Method method = null;
29 | String sql = "DELETE FROM book WHERE id = ?";
30 | Connection mockConnection = Mockito.mock(Connection.class);
31 | PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class);
32 | BookDAO bookDAO = new BookDAO(mockConnection);
33 | BookDAO spyBookDAO = Mockito.spy(bookDAO);
34 | boolean called_prepareStatement = false;
35 |
36 |
37 | Mockito.when(mockConnection.prepareStatement(sql)).thenReturn(mockStatement);
38 |
39 | try {
40 | method = BookDAO.class.getMethod("deleteBook", int.class);
41 | } catch (NoSuchMethodException e) {
42 | //e.printStackTrace();
43 | }
44 |
45 | String message = "The method deleteBook() doesn't exist in BookDAO.java.";
46 | assertNotNull(message, method);
47 |
48 | try {
49 | method.invoke(spyBookDAO, 0);
50 | } catch (Exception e) {}
51 |
52 | try {
53 | Mockito.verify(mockConnection,Mockito.atLeast(1)).prepareStatement(sql);
54 | called_prepareStatement = true;
55 | } catch (Throwable e) {}
56 |
57 | message = "The method deleteBook() doesn't call prepareStatement() correctly.";
58 | assertTrue(message, called_prepareStatement);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/com/pluralsight/DBConnection.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DatabaseMetaData;
5 | import java.sql.DriverManager;
6 | import java.sql.ResultSet;
7 | import java.sql.SQLException;
8 | import java.sql.Statement;
9 |
10 | public class DBConnection {
11 | private Connection jdbcConnection;
12 |
13 | public DBConnection() {
14 | connect();
15 | }
16 |
17 | public Connection getConnection() {
18 | return jdbcConnection;
19 | }
20 |
21 | public void connect() {
22 | try {
23 | Class.forName("org.sqlite.JDBC");
24 | jdbcConnection = DriverManager.getConnection("jdbc:sqlite:book_store.db");
25 | System.out.println("Opened database successfully");
26 |
27 | createTableIfNotExists();
28 | } catch ( Exception e ) {
29 | System.err.println( e.getClass().getName() + ": " + e.getMessage() );
30 | System.exit(0);
31 | }
32 | }
33 |
34 | private void createTableIfNotExists() {
35 | try {
36 | DatabaseMetaData meta = jdbcConnection.getMetaData();
37 | ResultSet res = meta.getTables(null, null, null, new String[] {"TABLE"});
38 | Statement stmt = jdbcConnection.createStatement();
39 | if (!res.next()) {
40 | // Create table
41 |
42 | String sql = "CREATE TABLE book " +
43 | "(id INTEGER PRIMARY KEY NOT NULL," +
44 | " title TEXT NOT NULL, " +
45 | " author TEXT NOT NULL, " +
46 | " price REAL)";
47 | stmt.executeUpdate(sql);
48 |
49 | sql = "INSERT INTO book (title, author, price) VALUES (\"1984\", \"George Orwell\", 1.00)";
50 | stmt.executeUpdate(sql);
51 |
52 | stmt.close();
53 | }
54 | } catch ( Exception e ) {
55 | System.err.println( e.getClass().getName() + ": " + e.getMessage() );
56 | System.exit(0);
57 | }
58 | }
59 |
60 |
61 | public void disconnect() {
62 | try {
63 | if (jdbcConnection != null && !jdbcConnection.isClosed()) {
64 | jdbcConnection.close();
65 | }
66 | } catch (SQLException e) {
67 | e.printStackTrace();
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module1/Module1_Task3_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 |
7 | import java.sql.Connection;
8 | import java.sql.PreparedStatement;
9 |
10 | import org.junit.BeforeClass;
11 | import org.junit.Before;
12 | import org.junit.Test;
13 | import org.mockito.Mockito;
14 |
15 | import org.junit.runner.RunWith;
16 | import org.powermock.api.mockito.PowerMockito;
17 | import org.powermock.core.classloader.annotations.PrepareForTest;
18 | import org.powermock.modules.junit4.PowerMockRunner;
19 | import org.powermock.reflect.exceptions.*;
20 |
21 | import java.io.*;
22 |
23 | @RunWith(PowerMockRunner.class)
24 | @PrepareForTest(ControllerServlet.class)
25 | public class Module1_Task3_IT extends Mockito{
26 | static String tempID = "0";
27 |
28 | // Verify the deleteBook() method exists in ControllerServlet
29 | // Since it's private need to verify the lines of code get called
30 | // through the /delete action in doGet()
31 | @Test
32 | public void module1_task3() throws Exception {
33 | ControllerServlet controllerServlet = PowerMockito.spy(new ControllerServlet());
34 | boolean called_deleteBook = false;
35 | HttpServletRequest request = mock(HttpServletRequest.class);
36 | HttpServletResponse response = mock(HttpServletResponse.class);
37 |
38 | try {
39 | when(request.getPathInfo()).thenReturn("/delete");
40 | //PowerMockito.doNothing().when(controllerServlet, "deleteBook", request, response);
41 | when(request.getParameter("id")).thenReturn(tempID);
42 | } catch (MethodNotFoundException e) {}
43 |
44 | // try {
45 | // controllerServlet.doGet(request, response);
46 | // try {
47 | // PowerMockito.verifyPrivate(controllerServlet)
48 | // .invoke("deleteBook", request, response);
49 | // called_deleteBook = true;
50 | // } catch (Throwable e) {}
51 | // } catch (Exception e) {}
52 |
53 | String errorMsg = "After action \"" + "/delete" +
54 | "\", did not call deleteBook().";
55 | assertTrue(errorMsg, called_deleteBook);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module2/Module2_Task2_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 |
7 | import java.sql.Connection;
8 | import java.sql.PreparedStatement;
9 |
10 | import org.junit.BeforeClass;
11 | import org.junit.Before;
12 | import org.junit.Test;
13 | import org.mockito.Mockito;
14 |
15 | import org.junit.runner.RunWith;
16 | import org.powermock.api.mockito.PowerMockito;
17 | import org.powermock.core.classloader.annotations.PrepareForTest;
18 | import org.powermock.modules.junit4.PowerMockRunner;
19 | import org.powermock.reflect.exceptions.*;
20 |
21 | import java.io.*;
22 |
23 | @RunWith(PowerMockRunner.class)
24 | @PrepareForTest(ControllerServlet.class)
25 | public class Module2_Task2_IT extends Mockito{
26 | static String tempID = "0";
27 |
28 | // Verify the showEditForm() method exists in ControllerServlet
29 | // Since it's private need to verify the lines of code get called
30 | // through the /delete action in doGet()
31 | @Test
32 | public void module1_task2() throws Exception {
33 | ControllerServlet controllerServlet = PowerMockito.spy(new ControllerServlet());
34 | boolean called_showEditForm = false;
35 | HttpServletRequest request = mock(HttpServletRequest.class);
36 | HttpServletResponse response = mock(HttpServletResponse.class);
37 |
38 | try {
39 | when(request.getPathInfo()).thenReturn("/edit");
40 | //PowerMockito.doNothing().when(controllerServlet, "showEditForm", request, response);
41 | when(request.getParameter("id")).thenReturn(tempID);
42 | } catch (MethodNotFoundException e) {}
43 |
44 | // try {
45 | // controllerServlet.doGet(request, response);
46 | // try {
47 | // PowerMockito.verifyPrivate(controllerServlet)
48 | // .invoke("showEditForm", request, response);
49 | // called_showEditForm = true;
50 | // } catch (Throwable e) {}
51 | // } catch (Exception e) {}
52 |
53 | String errorMsg = "After action \"" + "/edit" +
54 | "\", did not call showEditForm().";
55 | assertTrue(errorMsg, called_showEditForm);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module1/Module1_Task1_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import org.junit.After;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import com.gargoylesoftware.htmlunit.ElementNotFoundException;
8 | import com.gargoylesoftware.htmlunit.WebClient;
9 | import com.gargoylesoftware.htmlunit.WebResponse;
10 | import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
11 | import com.gargoylesoftware.htmlunit.html.HtmlPage;
12 |
13 | import static org.junit.Assert.*;
14 |
15 | import java.io.IOException;
16 |
17 | public class Module1_Task1_IT {
18 |
19 | private String indexUrl;
20 | private WebClient webClient;
21 | HtmlPage page;
22 |
23 | @Before
24 | public void setUp() throws IOException {
25 | indexUrl = "http://localhost:8080"; //System.getProperty("integration.base.url");
26 | webClient = new WebClient();
27 | // Open the admin page
28 | page = webClient.getPage(indexUrl + "/books/admin");
29 | }
30 | @After
31 | public void tearDown() {
32 | webClient.closeAllWindows();
33 | }
34 |
35 | // Verify the edit and delete hrefs, in BookAdmin.jsp contain the id
36 | @Test
37 | public void module1_task1() {
38 | url_contains_id("Delete");
39 | url_contains_id("Edit");
40 | }
41 |
42 | public void url_contains_id(String textStr) {
43 | // First check if an anchor with text "Edit" exists
44 | HtmlAnchor anchor = null;
45 | try {
46 | anchor = page.getAnchorByText(textStr);
47 | }
48 | catch ( ElementNotFoundException e) {}
49 |
50 | assertNotNull("An anchor with the text " + textStr + " does not exist.", anchor);
51 |
52 | boolean found = findURLWithID(textStr.toLowerCase());
53 | assertTrue("The " + textStr + " anchor's href does not contain the id.", found);
54 | }
55 |
56 | private boolean findURLWithID(String urlStr) {
57 | String foundURL = "";
58 | try {
59 | for ( HtmlAnchor a : page.getAnchors()) {
60 | String href = a.getHrefAttribute();
61 | if (href.contains(urlStr)) {
62 | foundURL = a.getHrefAttribute().toString();
63 | break;
64 | }
65 | }
66 | }
67 | catch ( ElementNotFoundException e) {
68 | return false;
69 | }
70 | foundURL = foundURL.replaceAll("\\s+","");
71 | // Might have different id's in the database so remove them.
72 | foundURL = foundURL.replaceAll("[0-9]","");
73 | String testingURL = urlStr+"?id=";
74 | return foundURL.equals(testingURL);
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module1/Module1_Task6_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import java.sql.Connection;
5 | import java.sql.DriverManager;
6 | import java.sql.PreparedStatement;
7 |
8 | import org.junit.BeforeClass;
9 | import org.junit.Test;
10 | import org.mockito.Mockito;
11 | import org.junit.runner.RunWith;
12 | import org.powermock.api.mockito.PowerMockito;
13 | import org.powermock.core.classloader.annotations.PrepareForTest;
14 | import org.powermock.modules.junit4.PowerMockRunner;
15 |
16 | import java.lang.reflect.Method;
17 |
18 | import java.io.*;
19 |
20 |
21 | @RunWith(PowerMockRunner.class)
22 | @PrepareForTest({DriverManager.class, PreparedStatement.class, BookDAO.class})
23 | public class Module1_Task6_IT {
24 |
25 | // Verify the deleteBook() method exists in BookDAO
26 | @Test
27 | public void module1_task6() throws Exception {
28 | Method method = null;
29 | String sql = "DELETE FROM book WHERE id = ?";
30 | Connection spyConnection = Mockito.mock(Connection.class);
31 | PreparedStatement mockStatement = Mockito.mock(PreparedStatement.class);
32 | BookDAO bookDAO = new BookDAO(spyConnection);
33 | BookDAO spyBookDAO = Mockito.spy(bookDAO);
34 | boolean called_setInt = false;
35 | boolean called_execute = false;
36 | boolean called_prepareStatement = false;
37 | boolean called_close = false;
38 |
39 | Mockito.when(spyConnection.prepareStatement(sql)).thenReturn(mockStatement);
40 |
41 | try {
42 | method = BookDAO.class.getMethod("deleteBook", int.class);
43 | } catch (NoSuchMethodException e) {
44 | //e.printStackTrace();
45 | }
46 |
47 | String message = "The method deleteBook() doesn't exist in BookDAO.java.";
48 | assertNotNull(message, method);
49 |
50 | try {
51 | method.invoke(spyBookDAO, 0);
52 | } catch (Exception e) {}
53 |
54 | try {
55 | Mockito.verify(mockStatement, Mockito.atLeast(1)).setInt(Mockito.anyInt(), Mockito.anyInt());
56 | called_setInt = true;
57 | Mockito.verify(mockStatement, Mockito.atLeast(1)).executeUpdate();
58 | called_execute = true;
59 | Mockito.verify(mockStatement, Mockito.atLeast(1)).close();
60 | called_close = true;
61 | } catch (Throwable e) {}
62 |
63 | message = "The method deleteBook() doesn't call setInt().";
64 | assertTrue(message, called_setInt);
65 |
66 | message = "The method deleteBook() doesn't call executeUpdate().";
67 | assertTrue(message, called_execute);
68 |
69 | message = "The method deleteBook() doesn't call PreparedStatement close().";
70 | assertTrue(message, called_close);
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module3/Module3_Task6_and_7_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 |
7 | import java.sql.Connection;
8 | import java.sql.PreparedStatement;
9 |
10 | import org.junit.BeforeClass;
11 | import org.junit.Before;
12 | import org.junit.Test;
13 | import org.mockito.Mockito;
14 |
15 | import org.mockito.InjectMocks;
16 | import org.mockito.Mock;
17 | import org.mockito.MockitoAnnotations;
18 |
19 | import org.powermock.reflect.Whitebox;
20 | import java.lang.reflect.Method;
21 |
22 | import org.junit.runner.RunWith;
23 | import org.powermock.api.mockito.PowerMockito;
24 | import org.powermock.core.classloader.annotations.PrepareForTest;
25 | import org.powermock.modules.junit4.PowerMockRunner;
26 | import org.powermock.reflect.exceptions.*;
27 |
28 | import java.io.*;
29 |
30 | @RunWith(PowerMockRunner.class)
31 | @PrepareForTest(ControllerServlet.class)
32 | public class Module3_Task6_and_7_IT extends Mockito{
33 | static String tempID = "0";
34 | static boolean called_updateBook = false;
35 |
36 | static HttpServletRequest request;
37 | static HttpServletResponse response;
38 |
39 | private ControllerServlet controllerServlet;
40 |
41 | @Before
42 | public void setUp() throws Exception {
43 | controllerServlet = PowerMockito.spy(new ControllerServlet());
44 |
45 | request = mock(HttpServletRequest.class);
46 | response = mock(HttpServletResponse.class);
47 | try {
48 | when(request.getPathInfo()).thenReturn("/update");
49 | //PowerMockito.doNothing().when(controllerServlet, "updateBook", request, response);
50 | when(request.getParameter("id")).thenReturn(tempID);
51 | } catch (MethodNotFoundException e) {}
52 | try {
53 | controllerServlet.doGet(request, response);
54 | } catch (Exception e) {}
55 | }
56 |
57 | // Verify updateBook() exists in ControllerServlet
58 | @Test
59 | public void module3_task6() throws Exception {
60 | Method method = null;
61 | try {
62 | method = Whitebox.getMethod(ControllerServlet.class,
63 | "updateBook", HttpServletRequest.class, HttpServletResponse.class);
64 | } catch (Exception e) {}
65 |
66 | String errorMsg = "private void updateBook() does not exist in ControllerServlet";
67 | assertNotNull(errorMsg, method);
68 | }
69 |
70 | @Test
71 | public void module3_task7() throws Exception {
72 | // try {
73 | // PowerMockito.verifyPrivate(controllerServlet)
74 | // .invoke("updateBook", request, response);
75 | // called_updateBook = true;
76 | // } catch (Throwable e) {}
77 |
78 | String errorMsg = "After action \"" + "/update" +
79 | "\", did not call updateBook().";
80 | assertTrue(errorMsg, called_updateBook);
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module1/Module1_Task7_and_8_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 |
7 | import java.util.List;
8 | import java.util.ArrayList;
9 | import java.util.Collection;
10 |
11 | import java.sql.Connection;
12 | import java.sql.PreparedStatement;
13 |
14 | import org.junit.BeforeClass;
15 | import org.junit.Before;
16 | import org.junit.Test;
17 | import org.mockito.Mockito;
18 | import org.mockito.InjectMocks;
19 | import org.mockito.Mock;
20 | import org.mockito.MockitoAnnotations;
21 | import org.mockito.MockingDetails;
22 | import org.mockito.invocation.Invocation;
23 | import org.powermock.reflect.Whitebox;
24 |
25 | import java.lang.reflect.Method;
26 | import java.io.*;
27 |
28 | public class Module1_Task7_and_8_IT extends Mockito{
29 |
30 | static StringWriter stringWriter = new StringWriter();
31 | static String tempID = "0";
32 | static boolean called_getParameter = false;
33 | static boolean called_sendRedirect = false;
34 | static boolean called_deleteBook = false;
35 | static HttpServletRequest request = mock(HttpServletRequest.class);
36 | static HttpServletResponse response = mock(HttpServletResponse.class);
37 | static Method deleteMethod = null;
38 | @Mock
39 | private BookDAO mockBookDAO;
40 |
41 | @InjectMocks
42 | private ControllerServlet controllerServlet;
43 |
44 | @Before
45 | public void setUp() throws Exception {
46 | MockitoAnnotations.initMocks(this);
47 |
48 | request = mock(HttpServletRequest.class);
49 | response = mock(HttpServletResponse.class);
50 |
51 | when(request.getPathInfo()).thenReturn("/delete");
52 | when(request.getParameter("id")).thenReturn(tempID);
53 |
54 | try {
55 | deleteMethod = Whitebox.getMethod(ControllerServlet.class,
56 | "deleteBook", HttpServletRequest.class, HttpServletResponse.class);
57 | } catch (Exception e) {}
58 |
59 | String errorMsg = "private void deleteBook() does not exist in ControllerServlet";
60 | assertNotNull(errorMsg, deleteMethod);
61 |
62 | try {
63 | controllerServlet.doGet(request, response);
64 | } catch (Exception e) {}
65 | }
66 |
67 | // Verify deleteBook() in ControllerServlet is complete
68 | @Test
69 | public void module1_task7() throws Exception {
70 | String errorMsg = "private void deleteBook() does not exist in ControllerServlet";
71 | assertNotNull(errorMsg, deleteMethod);
72 |
73 | MockingDetails mockingDetails = Mockito.mockingDetails(mockBookDAO);
74 |
75 | Collection invocations = mockingDetails.getInvocations();
76 |
77 | List methodsCalled = new ArrayList<>();
78 | for (Invocation anInvocation : invocations) {
79 | methodsCalled.add(anInvocation.getMethod().getName());
80 | }
81 | assertTrue(methodsCalled.contains("deleteBook"));
82 |
83 | try {
84 | verify(request, atLeast(1)).getParameter("id");
85 | called_getParameter = true;
86 | } catch (Throwable e) {}
87 |
88 | errorMsg = "In ControllerServlet deleteBook()," +
89 | " did not call getParameter(\"id\").";
90 | assertTrue(errorMsg, called_getParameter);
91 | }
92 |
93 | @Test
94 | public void module1_task8() throws Exception {
95 | String errorMsg = "private void deleteBook() does not exist in ControllerServlet";
96 | assertNotNull(errorMsg, deleteMethod);
97 | try {
98 | verify(response, atLeast(1)).sendRedirect("list");
99 | called_sendRedirect = true;
100 | } catch (Throwable e) {}
101 |
102 | errorMsg = "In ControllerServlet deleteBook()," +
103 | " did not call sendRedirect(\"list\").";
104 | assertTrue(errorMsg, called_sendRedirect);
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module2/Module2_Task7_thru10_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import org.junit.After;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import com.gargoylesoftware.htmlunit.ElementNotFoundException;
8 | import com.gargoylesoftware.htmlunit.WebClient;
9 | import com.gargoylesoftware.htmlunit.WebResponse;
10 | import com.gargoylesoftware.htmlunit.html.DomElement;
11 | import com.gargoylesoftware.htmlunit.html.DomNodeList;
12 | import com.gargoylesoftware.htmlunit.html.HtmlPage;
13 | import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
14 | import com.gargoylesoftware.htmlunit.html.HtmlForm;
15 | import com.gargoylesoftware.htmlunit.html.HtmlInput;
16 |
17 | import static org.junit.Assert.*;
18 |
19 | import java.io.IOException;
20 |
21 | public class Module2_Task7_thru10_IT {
22 | private String BOOK_FORM_NAME = "book_form";
23 | private String indexUrl;
24 | private WebClient webClient;
25 | HtmlPage firstPage;
26 | HtmlPage editPage;
27 | HtmlPage newPage;
28 |
29 | @Before
30 | public void setUp() throws IOException {
31 | indexUrl = "http://localhost:8080"; //System.getProperty("integration.base.url");
32 | webClient = new WebClient();
33 | // Open the admin page
34 | firstPage = webClient.getPage(indexUrl + "/books/admin");
35 |
36 | try {
37 | for ( HtmlAnchor a : firstPage.getAnchors()) {
38 | String href = a.getHrefAttribute();
39 | if (href.contains("edit")) {
40 | editPage = a.click();
41 | }
42 | else if (href.contains("new")) {
43 | newPage = a.click();
44 | }
45 | }
46 | }
47 | catch ( Exception e) {}
48 | }
49 |
50 | @After
51 | public void tearDown() {
52 | webClient.closeAllWindows();
53 | }
54 |
55 | // Verify they adapted the BookForm.jsp page for editing existing books
56 | // and adding new book
57 | // In this test check the form action is conditional, and the form h2
58 | @Test
59 | public void module2_task7() {
60 | assertNotNull("Link, edit, did not work.", editPage);
61 | checkForm("Edit");
62 | }
63 |
64 | @Test
65 | public void module2_task8() {
66 | assertNotNull("Link, new, did not work.", newPage);
67 | checkForm("New");
68 | }
69 |
70 | @Test
71 | public void module2_task9() {
72 | h2_correct("Edit");
73 | }
74 |
75 | @Test
76 | public void module2_task10() {
77 | h2_correct("New");
78 | }
79 |
80 | public void h2_correct(String urlStr) {
81 | // First check if an H2 exists with text "New Book Form"
82 | boolean h2Text_correct = false;
83 | DomNodeList< DomElement > list;
84 | if (urlStr.equals("Edit")) list = editPage.getElementsByTagName( "h2" );
85 | else list = newPage.getElementsByTagName( "h2" );
86 | String h2Text = "";
87 | String desiredText = urlStr + " Book Form";
88 | desiredText = desiredText.replaceAll("\\s+","");
89 | for( DomElement domElement : list )
90 | {
91 | h2Text = domElement.getTextContent();
92 | h2Text = h2Text.replaceAll("\\s+","");
93 | if (h2Text.equals(desiredText))
94 | h2Text_correct = true;
95 | }
96 |
97 | assertTrue("h2 text = " + h2Text + " , desiredText = " +desiredText, h2Text_correct);
98 | }
99 |
100 | public void checkForm(String urlStr) {
101 | // Get form and check action
102 | HtmlForm form = null;
103 | String errorMsg = "";
104 | String desiredAction = "";
105 | try {
106 | if (urlStr.equals("Edit")) {
107 | form = editPage.getFormByName(BOOK_FORM_NAME);
108 | errorMsg = "Form, book_form, action not \"update\".";
109 | desiredAction = "update";
110 | }
111 | else {
112 | form = newPage.getFormByName(BOOK_FORM_NAME);
113 | errorMsg = "Form, book_form, action not \"insert\".";
114 | desiredAction = "insert";
115 | }
116 | } catch (ElementNotFoundException e) {}
117 |
118 | assertNotNull("Form is null.", form);
119 | String action = form.getActionAttribute();
120 | assertEquals(errorMsg, desiredAction, action);
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/src/main/java/com/pluralsight/BookDAO.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DatabaseMetaData;
5 | import java.sql.DriverManager;
6 | import java.sql.PreparedStatement;
7 | import java.sql.ResultSet;
8 | import java.sql.SQLException;
9 | import java.sql.Statement;
10 |
11 | import java.util.ArrayList;
12 |
13 | public class BookDAO {
14 | private Connection jdbcConnection;
15 | public BookDAO(Connection connection)
16 | {
17 | jdbcConnection = connection;
18 | }
19 |
20 | public Book getBook(int id) {
21 | Book book = null;
22 | String sql = "SELECT * FROM book WHERE id = ?";
23 |
24 | try {
25 | PreparedStatement statement = jdbcConnection.prepareStatement(sql);
26 | statement.setInt(1, id);
27 |
28 | ResultSet resultSet = statement.executeQuery();
29 |
30 | if (resultSet.next()) {
31 | String title = resultSet.getString("title");
32 | String author = resultSet.getString("author");
33 | float price = resultSet.getFloat("price");
34 |
35 | book = new Book(id, title, author, price);
36 | }
37 |
38 | resultSet.close();
39 | statement.close();
40 | } catch (SQLException e) {
41 | e.printStackTrace();
42 | }
43 |
44 | return book;
45 | }
46 |
47 | public ArrayList listAllBooks() {
48 | ArrayList listBook = new ArrayList<>();
49 |
50 | String sql = "SELECT * FROM book";
51 |
52 | try {
53 | Statement statement = jdbcConnection.createStatement();
54 |
55 | ResultSet resultSet = statement.executeQuery(sql);
56 |
57 | while (resultSet.next()) {
58 | int id = resultSet.getInt("id");
59 | String title = resultSet.getString("title");
60 | String author = resultSet.getString("author");
61 | float price = resultSet.getFloat("price");
62 |
63 | Book book = new Book(id, title, author, price);
64 | listBook.add(book);
65 | }
66 |
67 | resultSet.close();
68 | statement.close();
69 | } catch (SQLException e) {
70 | e.printStackTrace();
71 | }
72 | return listBook;
73 | }
74 |
75 | public boolean insertBook(Book book) {
76 | String sql = "INSERT INTO book (title, author, price) VALUES (?, ?, ?)";
77 |
78 | try {
79 | PreparedStatement statement = jdbcConnection.prepareStatement(sql);
80 | statement.setString(1, book.getTitle());
81 | statement.setString(2, book.getAuthor());
82 | statement.setFloat(3, book.getPrice());
83 |
84 | boolean rowInserted = statement.executeUpdate() > 0;
85 | statement.close();
86 | return rowInserted;
87 | } catch (SQLException e) {
88 | e.printStackTrace();
89 | }
90 |
91 | return false;
92 | }
93 |
94 | // public void deleteBook(int id) {
95 | // String sql = "DELETE FROM book WHERE id = ?";
96 | //
97 | // try {
98 | // PreparedStatement statement = jdbcConnection.prepareStatement(sql);
99 | // statement.setInt(1, id);
100 | // statement.executeUpdate();
101 | //
102 | // statement.close();
103 | // } catch (SQLException e) {
104 | // e.printStackTrace();
105 | // }
106 | // }
107 | //
108 | // public void updateBook(Book book) {
109 | // String sql = "UPDATE book SET title = ?, author = ?, price = ?" +
110 | // " WHERE id = ?";
111 | //
112 | // try {
113 | // PreparedStatement statement = jdbcConnection.prepareStatement(sql);
114 | // statement.setString(1, book.getTitle());
115 | // statement.setString(2, book.getAuthor());
116 | // statement.setFloat(3, book.getPrice());
117 | // statement.setInt(4, book.getId());
118 | //
119 | // statement.executeUpdate();
120 | // statement.close();
121 | // } catch(SQLException e) {
122 | // e.printStackTrace();
123 | // }
124 | // }
125 | }
126 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.pluralsight
5 | bookstore
6 | war
7 | 1.0-SNAPSHOT
8 | bookstore Maven Webapp
9 | http://maven.apache.org
10 |
11 |
12 | UTF-8
13 | UTF-8
14 | 1.8
15 | 1.8
16 | false
17 |
18 |
19 |
20 |
21 | javax.servlet
22 | javax.servlet-api
23 | 3.1.0
24 | provided
25 |
26 |
27 |
28 | javax.servlet
29 | jstl
30 | 1.2
31 |
32 |
33 |
34 | javax.inject
35 | javax.inject
36 | 1
37 |
38 |
39 |
40 |
41 |
42 | org.xerial
43 | sqlite-jdbc
44 | 3.21.0.1
45 |
46 |
47 |
48 |
49 |
50 | junit
51 | junit
52 | 4.12
53 | test
54 |
55 |
56 | net.sourceforge.htmlunit
57 | htmlunit
58 | 2.15
59 | test
60 |
61 |
62 | org.mockito
63 | mockito-core
64 | 2.18.0
65 |
66 |
67 |
68 | org.powermock
69 | powermock-api-mockito2
70 | 2.0.0-beta.5
71 |
72 |
73 | org.powermock
74 | powermock-module-junit4
75 | 2.0.0-beta.5
76 |
77 |
78 |
79 |
80 |
81 | bookstore
82 |
83 |
84 | org.apache.tomcat.maven
85 | tomcat7-maven-plugin
86 | 2.2
87 |
88 | 8080
89 | /
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 | integration
98 |
99 |
100 |
101 |
102 | org.apache.maven.plugins
103 | maven-failsafe-plugin
104 | 2.18.1
105 |
106 |
107 |
108 | integration-test
109 | verify
110 |
111 |
112 |
113 |
114 | http://localhost:8080
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module2/Module2_Task3_thru_6_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 | import javax.servlet.RequestDispatcher;
7 |
8 | import java.sql.Connection;
9 | import java.sql.PreparedStatement;
10 |
11 | import org.junit.BeforeClass;
12 | import org.junit.Before;
13 | import org.junit.Test;
14 | import org.mockito.Mockito;
15 | import org.mockito.InjectMocks;
16 | import org.mockito.Mock;
17 | import org.mockito.MockitoAnnotations;
18 |
19 | import java.lang.reflect.Method;
20 | import java.io.*;
21 |
22 | public class Module2_Task3_thru_6_IT extends Mockito{
23 |
24 | static StringWriter stringWriter = new StringWriter();
25 | static String tempID = "1";
26 | static int tempIntID = 1;
27 | static HttpServletRequest request;
28 | static HttpServletResponse response;
29 | static RequestDispatcher mockRequestDispatcher;
30 | static Book mockBook;
31 |
32 | @Mock
33 | private BookDAO mockBookDAO;
34 |
35 | @InjectMocks
36 | private ControllerServlet controllerServlet;
37 |
38 | @Before
39 | public void setUp() throws Exception {
40 | MockitoAnnotations.initMocks(this);
41 |
42 | request = mock(HttpServletRequest.class);
43 | response = mock(HttpServletResponse.class);
44 | mockRequestDispatcher = mock(RequestDispatcher.class);
45 | mockBook = mock(Book.class);
46 |
47 | when(request.getPathInfo()).thenReturn("/edit");
48 | when(request.getParameter("id")).thenReturn(tempID);
49 | when(mockBookDAO.getBook(tempIntID)).thenReturn(mockBook);
50 | when(request.getRequestDispatcher("/BookForm.jsp"))
51 | .thenReturn(mockRequestDispatcher);
52 |
53 | try {
54 | controllerServlet.doGet(request, response);
55 | } catch (Exception e) {}
56 | }
57 |
58 | // Verify showEditForm() is complete in ControllerServlet
59 | // Since it's private need to verify the lines of code get called
60 | // through the /edit action in doGet()
61 | @Test
62 | public void module2_task3() throws Exception {
63 | boolean called_getParameter = false;
64 | boolean called_getBook = false;
65 |
66 | try {
67 | verify(request, atLeast(1)).getParameter("id");
68 | called_getParameter = true;
69 | } catch (Throwable e) {}
70 |
71 | try {
72 | verify(mockBookDAO).getBook(anyInt());
73 | called_getBook = true;
74 | } catch (Throwable e) {}
75 |
76 | String errorMsg = "In ControllerServlet showEditForm()," +
77 | " did not call getParameter(\"id\").";
78 | assertTrue(errorMsg, called_getParameter);
79 | errorMsg = "In ControllerServlet showEditForm()," +
80 | " did not call getBook(id).";
81 | assertTrue(errorMsg, called_getBook);
82 | }
83 |
84 | @Test
85 | public void module2_task4() throws Exception {
86 | boolean called_getRequestDispatcher = false;
87 |
88 | try {
89 | verify(request).getRequestDispatcher("/BookForm.jsp");
90 | called_getRequestDispatcher = true;
91 | } catch (Throwable e) {}
92 |
93 | String errorMsg = "In ControllerServlet showEditForm()," +
94 | " did not call request.getRequestDispatcher(\"BookForm.jsp\").";
95 | assertTrue(errorMsg, called_getRequestDispatcher);
96 | }
97 |
98 | @Test
99 | public void module2_task5() throws Exception {
100 | boolean called_setAttribute = false;
101 |
102 | try {
103 | verify(request).setAttribute("book", mockBook);
104 | called_setAttribute = true;
105 | } catch (Throwable e) {}
106 |
107 | String errorMsg = "In ControllerServlet showEditForm()," +
108 | " did not call request.setAttribute(\"book\", bookObject);.";
109 | assertTrue(errorMsg, called_setAttribute);
110 | }
111 |
112 | @Test
113 | public void module2_task6() throws Exception {
114 | boolean called_forward = false;
115 |
116 | try {
117 | verify(mockRequestDispatcher).forward(request, response);
118 | called_forward = true;
119 | } catch (Throwable e) {}
120 |
121 | String errorMsg = "In ControllerServlet showEditForm()," +
122 | " did not call dispatcher.forward(request, response);.";
123 | assertTrue(errorMsg, called_forward);
124 | }
125 |
126 |
127 | }
128 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module3/Module3_Task1_thru_5_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import java.sql.Connection;
5 | import java.sql.DriverManager;
6 | import java.sql.PreparedStatement;
7 |
8 | import org.junit.BeforeClass;
9 | import org.junit.Test;
10 | import org.junit.Before;
11 | import org.mockito.Mockito;
12 | import org.junit.runner.RunWith;
13 | import org.powermock.api.mockito.PowerMockito;
14 | import org.powermock.core.classloader.annotations.PrepareForTest;
15 | import org.powermock.modules.junit4.PowerMockRunner;
16 |
17 | import java.lang.reflect.Method;
18 |
19 | import java.io.*;
20 |
21 |
22 | @RunWith(PowerMockRunner.class)
23 | @PrepareForTest({DriverManager.class, PreparedStatement.class, BookDAO.class})
24 | public class Module3_Task1_thru_5_IT {
25 |
26 | static Method method = null;
27 | static String sql = "UPDATE book SET title = ?, author = ?, price = ?" +
28 | " WHERE id = ?";
29 | Connection spyConnection;
30 | PreparedStatement mockStatement;
31 | static BookDAO bookDAO;
32 | static BookDAO spyBookDAO;
33 | static boolean called_prepareStatement = false;
34 | static boolean called_setTitle = false;
35 | static boolean called_setAuthor = false;
36 | static boolean called_setPrice = false;
37 | static boolean called_setId = false;
38 | static boolean called_executeUpdate = false;
39 | static boolean called_close = false;
40 | static String message = "";
41 | @Before
42 | public void setUp() {
43 | spyConnection = Mockito.mock(Connection.class);
44 | mockStatement = Mockito.mock(PreparedStatement.class);
45 | bookDAO = new BookDAO(spyConnection);
46 | spyBookDAO = Mockito.spy(bookDAO);
47 |
48 | Book tempBookObject = new Book(1, "1984", "George Orwell", 1.50f);
49 | try {
50 | Mockito.when(spyConnection.prepareStatement(sql)).thenReturn(mockStatement);
51 | method = BookDAO.class.getMethod("updateBook", Book.class);
52 | method.invoke(spyBookDAO, tempBookObject);
53 | } catch (Exception e) {
54 | //e.printStackTrace();
55 | }
56 | }
57 |
58 | // Verify updateBook() method exists in BookDAO
59 | @Test
60 | public void module3_Task1() throws Exception {
61 | message = "The method updateBook() doesn't exist in BookDAO.java.";
62 | assertNotNull(message, method);
63 | }
64 |
65 | @Test
66 | public void module3_Task2() throws Exception {
67 | try {
68 | Mockito.verify(spyConnection).prepareStatement(sql);
69 | called_prepareStatement = true;
70 | } catch (Throwable e) {}
71 |
72 | message = "The method updateBook() doesn't call prepareStatement() correctly.";
73 | assertTrue(message, called_prepareStatement);
74 | }
75 |
76 | @Test
77 | public void module3_Task3() throws Exception {
78 | try {
79 | Mockito.verify(mockStatement).setString(1, "1984");
80 | called_setTitle = true;
81 | Mockito.verify(mockStatement).setString(2, "George Orwell");
82 | called_setAuthor = true;
83 | } catch (Throwable e) {}
84 |
85 | message = "The method updateBook() doesn't call setString() for the title.";
86 | assertTrue(message, called_setTitle);
87 |
88 | message = "The method updateBook() doesn't call setString() for the author.";
89 | assertTrue(message, called_setAuthor);
90 | }
91 |
92 | @Test
93 | public void module3_Task4() throws Exception {
94 | try {
95 | Mockito.verify(mockStatement).setFloat(3, 1.50f);
96 | called_setPrice = true;
97 | Mockito.verify(mockStatement).setInt(4, 1);
98 | called_setId = true;
99 | } catch (Throwable e) {}
100 |
101 | message = "The method updateBook() doesn't call setFloat() for the price.";
102 | assertTrue(message, called_setPrice);
103 |
104 | message = "The method updateBook() doesn't call setInt() for the id.";
105 | assertTrue(message, called_setId);
106 | }
107 |
108 | @Test
109 | public void module3_Task5() throws Exception {
110 | try {
111 | Mockito.verify(mockStatement).executeUpdate();
112 | called_executeUpdate = true;
113 | Mockito.verify(mockStatement).close();
114 | called_close = true;
115 | } catch (Throwable e) {}
116 |
117 | message = "The method updateBook() doesn't call executeUpdate().";
118 | assertTrue(message, called_executeUpdate);
119 |
120 | message = "The method updateBook() doesn't call PreparedStatement close().";
121 | assertTrue(message, called_close);
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module2/Module2_Task11_thru_14_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import org.junit.After;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 |
7 | import com.gargoylesoftware.htmlunit.ElementNotFoundException;
8 | import com.gargoylesoftware.htmlunit.WebClient;
9 | import com.gargoylesoftware.htmlunit.WebResponse;
10 | import com.gargoylesoftware.htmlunit.html.DomElement;
11 | import com.gargoylesoftware.htmlunit.html.DomNodeList;
12 | import com.gargoylesoftware.htmlunit.html.HtmlPage;
13 | import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
14 | import com.gargoylesoftware.htmlunit.html.HtmlForm;
15 | import com.gargoylesoftware.htmlunit.html.HtmlInput;
16 |
17 | import static org.junit.Assert.*;
18 |
19 | import java.io.IOException;
20 |
21 | public class Module2_Task11_thru_14_IT {
22 | private String BOOK_FORM_NAME = "book_form";
23 | private String indexUrl;
24 | private WebClient webClient;
25 | HtmlPage firstPage = null;
26 | HtmlPage nextPage = null;
27 | HtmlForm form = null;
28 |
29 | @Before
30 | public void setUp() throws IOException {
31 | indexUrl = "http://localhost:8080"; //System.getProperty("integration.base.url");
32 | webClient = new WebClient();
33 | // Open the admin page
34 | firstPage = webClient.getPage(indexUrl + "/books/admin");
35 | clickLink("Edit");
36 | assertNotNull("Link Edit did not work.", nextPage);
37 | // Get form
38 | try {
39 | form = nextPage.getFormByName(BOOK_FORM_NAME);
40 | } catch (ElementNotFoundException e) {}
41 | }
42 | @After
43 | public void tearDown() {
44 | webClient.closeAllWindows();
45 | }
46 |
47 | // Verify they adapted the BookForm.jsp page for editing existing books
48 | // and adding new book
49 | // In this test check the form input fields have values filled in
50 | @Test
51 | public void module2_task11() {
52 | assertNotNull("Link Edit did not work.", nextPage);
53 | assertNotNull("Form is null.", form);
54 | //Get id input field
55 | try {
56 | HtmlInput inputId = form.getInputByName("id");
57 |
58 | // Check if hidden
59 | String typeAttribute = inputId.getTypeAttribute();
60 | assertEquals("The id input needs type=\"hidden\".", "hidden", typeAttribute);
61 |
62 | // Check value is an int
63 | try {
64 | Integer.parseInt(inputId.getValueAttribute());
65 | } catch (NumberFormatException e) {
66 | assertTrue("The id input does not have an int for value.", false);
67 | }
68 | } catch (ElementNotFoundException e) {
69 | assertTrue("The input field with name \"id\" does not exist.", false);
70 | }
71 | }
72 |
73 | @Test
74 | public void module2_task12() {
75 | assertNotNull("Link Edit did not work.", nextPage);
76 | assertNotNull("Form is null.", form);
77 | // Get title input field, check value
78 | try {
79 | HtmlInput inputTitle = form.getInputByName("booktitle");
80 | String titleValue = inputTitle.getValueAttribute();
81 | assertTrue("Title field value is empty, value is \"" + titleValue + "\".",
82 | titleValue.length() > 0);
83 | }catch (ElementNotFoundException e) {
84 | assertTrue("The input field with name \"booktitle\" does not exist.", false);
85 | }
86 | }
87 |
88 | @Test
89 | public void module2_task13() {
90 | assertNotNull("Link Edit did not work.", nextPage);
91 | assertNotNull("Form is null.", form);
92 |
93 | // Get author input field, check value
94 | try {
95 | HtmlInput inputAuthor = form.getInputByName("bookauthor");
96 | String authorValue = inputAuthor.getValueAttribute();
97 | assertTrue("Author field value is empty, value is \"" + authorValue + "\".",
98 | authorValue.length() > 0);
99 | }catch (ElementNotFoundException e) {
100 | assertTrue("The input field with name \"bookauthor\" does not exist.", false);
101 | }
102 | }
103 |
104 | @Test
105 | public void module2_task14() {
106 | assertNotNull("Link Edit did not work.", nextPage);
107 | assertNotNull("Form is null.", form);
108 |
109 | // Get price input field, check value
110 | try {
111 | HtmlInput inputPrice = form.getInputByName("bookprice");
112 | String priceValue = inputPrice.getValueAttribute();
113 | assertTrue("Price field value is empty, value is \"" + priceValue + "\".",
114 | priceValue.length() > 0);
115 | }catch (ElementNotFoundException e) {
116 | assertTrue("The input field with name \"bookprice\" does not exist.", false);
117 | }
118 | }
119 |
120 | private void clickLink(String urlStr) {
121 | String foundURL = "";
122 | String desiredUrlText = urlStr.toLowerCase();
123 | try {
124 | for ( HtmlAnchor a : firstPage.getAnchors()) {
125 | String href = a.getHrefAttribute();
126 | if (href.contains(desiredUrlText)) {
127 | nextPage = a.click();
128 | break;
129 | }
130 | }
131 | }
132 | catch ( Exception e) {}
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/src/main/java/com/pluralsight/ControllerServlet.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import java.io.IOException;
4 | import java.io.PrintWriter;
5 | import java.sql.SQLException;
6 | import java.util.ArrayList;
7 | import java.sql.Connection;
8 | import java.sql.DatabaseMetaData;
9 | import java.sql.DriverManager;
10 |
11 | import javax.servlet.RequestDispatcher;
12 | import javax.servlet.ServletException;
13 | import javax.servlet.http.HttpServlet;
14 | import javax.servlet.http.HttpServletRequest;
15 | import javax.servlet.http.HttpServletResponse;
16 | import javax.inject.Inject;
17 | /**
18 | * Servlet implementation class HelloWorld
19 | */
20 |
21 | public class ControllerServlet extends HttpServlet {
22 | private static final long serialVersionUID = 1L;
23 | private DBConnection dbConnection;
24 |
25 | @Inject
26 | private BookDAO bookDAO;
27 | /**
28 | * @see HttpServlet#HttpServlet()
29 | */
30 |
31 | public void init() {
32 | dbConnection = new DBConnection();
33 | bookDAO = new BookDAO(dbConnection.getConnection());
34 | }
35 |
36 | public void destroy() {
37 | dbConnection.disconnect();
38 | }
39 |
40 | public ControllerServlet() {
41 | super();
42 | }
43 |
44 | /**
45 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
46 | */
47 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
48 | throws ServletException, IOException {
49 | String action = request.getPathInfo();
50 |
51 | try {
52 | switch(action) {
53 | case "/admin":
54 | showBookAdmin(request, response);
55 | break;
56 | case "/new":
57 | showNewForm(request, response);
58 | break;
59 | case "/insert":
60 | insertBook(request, response);
61 | break;
62 | // case "/edit":
63 | // showEditForm(request, response);
64 | // break;
65 | // case "/delete":
66 | // deleteBook(request, response);
67 | // break;
68 | // case "/update":
69 | // updateBook(request, response);
70 | // break;
71 | default:
72 | listBooks(request, response);
73 | break;
74 | }
75 | } catch (Exception e) {
76 | // TODO Auto-generated catch block
77 | e.printStackTrace();
78 | }
79 | }
80 |
81 | private void showBookAdmin(HttpServletRequest request, HttpServletResponse response)
82 | throws ClassNotFoundException, SQLException, ServletException, IOException {
83 | ArrayList books_list = bookDAO.listAllBooks();
84 |
85 | request.setAttribute("books", books_list);
86 | RequestDispatcher dispatcher = request.getRequestDispatcher("/BookAdmin.jsp");
87 | dispatcher.forward(request, response);
88 | }
89 |
90 | private void listBooks(HttpServletRequest request, HttpServletResponse response)
91 | throws ClassNotFoundException, SQLException, ServletException, IOException {
92 | ArrayList books_list = bookDAO.listAllBooks();
93 |
94 | request.setAttribute("books", books_list);
95 | RequestDispatcher dispatcher = request.getRequestDispatcher("/BookList.jsp");
96 | dispatcher.forward(request, response);
97 | }
98 |
99 | private void showNewForm(HttpServletRequest request, HttpServletResponse response)
100 | throws ServletException, IOException {
101 | RequestDispatcher dispatcher = request.getRequestDispatcher("/BookForm.jsp");
102 | dispatcher.forward(request, response);
103 | }
104 |
105 | // private void showEditForm(HttpServletRequest request, HttpServletResponse response)
106 | // throws ServletException, IOException {
107 | // int id = Integer.parseInt(request.getParameter("id"));
108 | // Book existingBook = bookDAO.getBook(id);
109 | // RequestDispatcher dispatcher = request.getRequestDispatcher("/BookForm.jsp");
110 | // request.setAttribute("book", existingBook);
111 | // dispatcher.forward(request, response);
112 | // }
113 |
114 | // private void deleteBook(HttpServletRequest request, HttpServletResponse response)
115 | // throws ServletException, IOException {
116 | // int id = Integer.parseInt(request.getParameter("id"));
117 | // bookDAO.deleteBook(id);
118 | // response.sendRedirect("list");
119 | // }
120 |
121 | private void insertBook(HttpServletRequest request, HttpServletResponse response)
122 | throws ServletException, IOException, ClassNotFoundException, SQLException {
123 | String title = request.getParameter("booktitle");
124 | String author = request.getParameter("bookauthor");
125 | String priceString = request.getParameter("bookprice");
126 |
127 | Book newBook = new Book(title, author, Float.parseFloat(priceString));
128 |
129 | bookDAO.insertBook(newBook);
130 | response.sendRedirect("list");
131 | }
132 |
133 | // private void updateBook(HttpServletRequest request, HttpServletResponse response)
134 | // throws ServletException, IOException, ClassNotFoundException, SQLException {
135 | // String idStr = request.getParameter("id");
136 | // int id = Integer.parseInt(idStr);
137 | // String title = request.getParameter("booktitle");
138 | // String author = request.getParameter("bookauthor");
139 | // String priceString = request.getParameter("bookprice");
140 | //
141 | // Book newBook = new Book(id, title, author, Float.parseFloat(priceString));
142 | //
143 | // bookDAO.updateBook(newBook);
144 | // response.sendRedirect("list");
145 | // }
146 |
147 | /**
148 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
149 | */
150 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
151 | // TODO Auto-generated method stub
152 | PrintWriter out = response.getWriter();
153 | out.println("This is the doPost() method!");
154 | doGet(request, response);
155 |
156 | }
157 |
158 | }
159 |
--------------------------------------------------------------------------------
/src/test/java/com/pluralsight/module3/Module3_Task8_thru_11_IT.java:
--------------------------------------------------------------------------------
1 | package com.pluralsight;
2 |
3 | import static org.junit.Assert.*;
4 | import javax.servlet.http.HttpServletRequest;
5 | import javax.servlet.http.HttpServletResponse;
6 |
7 | import java.util.List;
8 | import java.util.ArrayList;
9 | import java.util.Collection;
10 | import java.lang.reflect.Method;
11 |
12 | import java.sql.Connection;
13 | import java.sql.PreparedStatement;
14 |
15 | import org.junit.BeforeClass;
16 | import org.junit.Before;
17 | import org.junit.Test;
18 | import org.mockito.Mockito;
19 |
20 | import org.mockito.InjectMocks;
21 | import org.mockito.Mock;
22 | import org.mockito.MockitoAnnotations;
23 | import org.mockito.MockingDetails;
24 | import org.mockito.invocation.Invocation;
25 | import org.powermock.reflect.Whitebox;
26 |
27 | import java.io.*;
28 |
29 | public class Module3_Task8_thru_11_IT extends Mockito{
30 |
31 | static StringWriter stringWriter = new StringWriter();
32 | static String tempIDStr = "1";
33 | static int tempID = 1;
34 | static String tempTitle = "1984";
35 | static String tempAuthor = "George Orwell";
36 | static String tempPriceStr = "1.50";
37 | static float tempPrice = 1.50f;
38 |
39 | static boolean called_getId = false;
40 | static boolean called_getTitle = false;
41 | static boolean called_getAuthor = false;
42 | static boolean called_getPrice = false;
43 | static boolean called_updateBook = false;
44 | static boolean called_sendRedirect = false;
45 | static HttpServletRequest request;
46 | static HttpServletResponse response;
47 | static Book tempBook;
48 | static Method updateMethod = null;
49 |
50 | @Mock
51 | private BookDAO mockBookDAO;
52 |
53 | @InjectMocks
54 | private ControllerServlet controllerServlet;
55 |
56 | @Before
57 | public void setUp() throws Exception {
58 | MockitoAnnotations.initMocks(this);
59 |
60 | request = mock(HttpServletRequest.class);
61 | response = mock(HttpServletResponse.class);
62 | tempBook = new Book(tempID, tempTitle, tempAuthor, tempPrice);
63 |
64 | when(request.getPathInfo()).thenReturn("/update");
65 | when(request.getParameter("id")).thenReturn(tempIDStr);
66 | when(request.getParameter("booktitle")).thenReturn(tempTitle);
67 | when(request.getParameter("bookauthor")).thenReturn(tempAuthor);
68 | when(request.getParameter("bookprice")).thenReturn(tempPriceStr);
69 |
70 |
71 | try {
72 | updateMethod = Whitebox.getMethod(ControllerServlet.class,
73 | "updateBook", HttpServletRequest.class, HttpServletResponse.class);
74 | } catch (Exception e) {}
75 |
76 | String errorMsg = "private void updateBook() does not exist in ControllerServlet";
77 | assertNotNull(errorMsg, updateMethod);
78 |
79 | try {
80 | controllerServlet.doGet(request, response);
81 | } catch (Exception e) {}
82 | }
83 |
84 | @Test
85 | public void module3_task8() throws Exception {
86 | String errorMsg = "private void updateBook() does not exist in ControllerServlet";
87 | assertNotNull(errorMsg, updateMethod);
88 |
89 | try {
90 | verify(request).getParameter("id");
91 | called_getId = true;
92 | } catch (Exception e) {}
93 |
94 | errorMsg = "After action \"" + "/update" +
95 | "\", did not call getParameter(\"id\").";
96 | assertTrue(errorMsg, called_getId);
97 | }
98 |
99 | @Test
100 | public void module3_task9() throws Exception {
101 | String errorMsg = "private void updateBook() does not exist in ControllerServlet";
102 | assertNotNull(errorMsg, updateMethod);
103 |
104 | try {
105 | verify(request).getParameter("booktitle");
106 | called_getTitle = true;
107 | verify(request).getParameter("bookauthor");
108 | called_getAuthor = true;
109 | verify(request).getParameter("bookprice");
110 | called_getPrice = true;
111 | } catch (Exception e) {}
112 |
113 | errorMsg = "After action \"" + "/update" +
114 | "\", did not call getParameter(\"booktitle\").";
115 | assertTrue(errorMsg, called_getTitle);
116 | errorMsg = "After action \"" + "/update" +
117 | "\", did not call getParameter(\"bookauthor\").";
118 | assertTrue(errorMsg, called_getAuthor);
119 | errorMsg = "After action \"" + "/update" +
120 | "\", did not call getParameter(\"bookprice\").";
121 | assertTrue(errorMsg, called_getPrice);
122 | }
123 |
124 | @Test
125 | public void module3_task10() throws Exception {
126 | String errorMsg = "private void updateBook() does not exist in ControllerServlet";
127 | assertNotNull(errorMsg, updateMethod);
128 |
129 | Method method = null;
130 | try {
131 | method = BookDAO.class.getMethod("updateBook", int.class);
132 | } catch (NoSuchMethodException e) {
133 | //e.printStackTrace();
134 | }
135 |
136 | errorMsg = "The method updateBook() doesn't exist in BookDAO.java.";
137 | assertNotNull(errorMsg, method);
138 |
139 | MockingDetails mockingDetails = Mockito.mockingDetails(mockBookDAO);
140 |
141 | Collection invocations = mockingDetails.getInvocations();
142 |
143 | List methodsCalled = new ArrayList<>();
144 | for (Invocation anInvocation : invocations) {
145 | methodsCalled.add(anInvocation.getMethod().getName());
146 | }
147 | errorMsg = "After action \"" + "/update" +
148 | "\", did not udpateBook(newBookObject).";
149 | assertTrue(errorMsg, methodsCalled.contains("updateBook"));
150 | }
151 |
152 | @Test
153 | public void module3_task11() throws Exception {
154 | String errorMsg = "private void updateBook() does not exist in ControllerServlet";
155 | assertNotNull(errorMsg, updateMethod);
156 |
157 | try {
158 | verify(response).sendRedirect("list");
159 | called_sendRedirect = true;
160 | } catch (Exception e) {}
161 |
162 | errorMsg = "In ControllerServlet updateBook()," +
163 | " did not call sendRedirect(\"list\").";
164 | assertTrue(errorMsg, called_sendRedirect);
165 | }
166 | }
167 |
--------------------------------------------------------------------------------