GetProductsServlet.java

1
package com.popx.presentazione;
2
3
import com.popx.modello.ProdottoBean;
4
import com.popx.persistenza.ProdottoDAO;
5
import com.popx.persistenza.ProdottoDAOImpl;
6
7
import javax.servlet.*;
8
import javax.servlet.http.*;
9
import javax.servlet.annotation.*;
10
import java.io.IOException;
11
import java.util.List;
12
13
@WebServlet(name = "GetProductsServlet", urlPatterns = {"/getProductsServlet"})
14
public class GetProductsServlet extends HttpServlet {
15
16
    private static final int PRODUCTS_PER_PAGE = 6;
17
    private final ProdottoDAO prodottoDAO;
18
19
    // �� costruttore production
20
    public GetProductsServlet() {
21
        this.prodottoDAO = new ProdottoDAOImpl();
22
    }
23
24
    // �� costruttore test
25
    public GetProductsServlet(ProdottoDAO prodottoDAO) {
26
        this.prodottoDAO = prodottoDAO;
27
    }
28
29
    @Override
30
    public void doGet(HttpServletRequest request, HttpServletResponse response)
31
            throws ServletException, IOException {
32
33
        String category = request.getParameter("category");
34
        String price = request.getParameter("price");
35
        String pageParam = request.getParameter("page");
36
37
        int currentPage = 1;
38
        try {
39 1 1. doGet : negated conditional → SURVIVED
            if (pageParam != null) {
40
                currentPage = Math.max(1, Integer.parseInt(pageParam));
41
            }
42
        } catch (NumberFormatException ignored) {
43
            currentPage = 1;
44
        }
45
46
        try {
47
            List<ProdottoBean> prodotti;
48
49 2 1. doGet : negated conditional → KILLED
2. doGet : negated conditional → KILLED
            if (category != null && !category.isEmpty()) {
50 2 1. doGet : negated conditional → KILLED
2. doGet : negated conditional → KILLED
                if (price != null && !price.isEmpty()) {
51
                    boolean ascending = "low".equalsIgnoreCase(price);
52
                    prodotti = prodottoDAO.getProdottiByBrandAndPrice(category, ascending);
53
                } else {
54
                    prodotti = prodottoDAO.getProdottiByBrand(category);
55
                }
56 2 1. doGet : negated conditional → NO_COVERAGE
2. doGet : negated conditional → KILLED
            } else if (price != null && !price.isEmpty()) {
57
                boolean ascending = "low".equalsIgnoreCase(price);
58
                prodotti = prodottoDAO.getProdottiSortedByPrice(ascending);
59
            } else {
60
                prodotti = prodottoDAO.getAllProducts();
61
            }
62
63
            int totalProducts = prodotti.size();
64 1 1. doGet : Replaced double division with multiplication → KILLED
            int totalPages = (int) Math.ceil((double) totalProducts / PRODUCTS_PER_PAGE);
65 1 1. doGet : negated conditional → SURVIVED
            if (totalPages == 0) totalPages = 1;
66
67
            currentPage = Math.min(currentPage, totalPages);
68
69 2 1. doGet : Replaced integer multiplication with division → SURVIVED
2. doGet : Replaced integer subtraction with addition → KILLED
            int start = (currentPage - 1) * PRODUCTS_PER_PAGE;
70 1 1. doGet : Replaced integer addition with subtraction → KILLED
            int end = Math.min(start + PRODUCTS_PER_PAGE, totalProducts);
71
72
            List<ProdottoBean> productsForPage = prodotti.subList(start, end);
73
74 1 1. doGet : removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED
            request.setAttribute("products", productsForPage);
75 1 1. doGet : removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED
            request.setAttribute("currentPage", currentPage);
76 1 1. doGet : removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED
            request.setAttribute("totalPages", totalPages);
77
78 1 1. doGet : removed call to javax/servlet/RequestDispatcher::forward → KILLED
            request.getRequestDispatcher("/jsp/Catalog.jsp").forward(request, response);
79
80
        } catch (Exception e) {
81 1 1. doGet : removed call to javax/servlet/http/HttpServletResponse::sendError → KILLED
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
82
                    "Errore durante il recupero dei prodotti.");
83
        }
84
    }
85
}

Mutations

39

1.1
Location : doGet
Killed by : none
negated conditional → SURVIVED

49

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:categoryFilter_applied()]
negated conditional → KILLED

2.2
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:invalidPage_defaultsToFirst()]
negated conditional → KILLED

50

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:categoryAndPriceFilter_appliedAscending()]
negated conditional → KILLED

2.2
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:categoryFilter_applied()]
negated conditional → KILLED

56

1.1
Location : doGet
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:invalidPage_defaultsToFirst()]
negated conditional → KILLED

64

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:noFilters_returnsAllProducts_firstPage()]
Replaced double division with multiplication → KILLED

65

1.1
Location : doGet
Killed by : none
negated conditional → SURVIVED

69

1.1
Location : doGet
Killed by : none
Replaced integer multiplication with division → SURVIVED

2.2
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:invalidPage_defaultsToFirst()]
Replaced integer subtraction with addition → KILLED

70

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:invalidPage_defaultsToFirst()]
Replaced integer addition with subtraction → KILLED

74

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:noFilters_returnsAllProducts_firstPage()]
removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED

75

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:invalidPage_defaultsToFirst()]
removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED

76

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:noFilters_returnsAllProducts_firstPage()]
removed call to javax/servlet/http/HttpServletRequest::setAttribute → KILLED

78

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:noFilters_returnsAllProducts_firstPage()]
removed call to javax/servlet/RequestDispatcher::forward → KILLED

81

1.1
Location : doGet
Killed by : com.popx.integration.presentazione.GetProductsServletTest.[engine:junit-jupiter]/[class:com.popx.integration.presentazione.GetProductsServletTest]/[method:daoThrowsException_returns500()]
removed call to javax/servlet/http/HttpServletResponse::sendError → KILLED

Active mutators

Tests examined


Report generated by PIT 1.15.2