SearchBar.java

1
package control.search;
2
3
import javax.servlet.ServletException;
4
import javax.servlet.annotation.WebServlet;
5
import javax.servlet.http.HttpServlet;
6
import javax.servlet.http.HttpServletRequest;
7
import javax.servlet.http.HttpServletResponse;
8
import javax.sql.DataSource;
9
import java.io.IOException;
10
import java.sql.*;
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
import com.google.gson.Gson;
16
import model.DBConnection;
17
18
@WebServlet("/SearchBar")
19
public class SearchBar extends HttpServlet {
20
    private static final String TABLE_NAME = "Maglietta";
21
    private static DataSource ds;
22
    private static final String ERROR_PAGE = "/pages/errorpage.jsp";
23
24
    public SearchBar() {
25
        ds = DBConnection.getDataSource();
26
    }
27
28
    public SearchBar(DataSource ds) {
29
        SearchBar.ds = ds;
30
    }
31
32
    @Override
33
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
34
            throws ServletException, IOException {
35
36
        String search = req.getParameter("search");
37 1 1. doPost : negated conditional → KILLED
        if (search == null) {
38
            search = "";
39
        }
40
41
        String query = "SELECT * FROM " + TABLE_NAME +
42
                " WHERE Tipo <> 'Personalizzata' AND Tipo <> 'Eliminata' AND nome LIKE ?";
43
44
        try (Connection connection = ds.getConnection();
45
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
46
47 1 1. doPost : removed call to java/sql/PreparedStatement::setString → KILLED
            preparedStatement.setString(1, "%" + search + "%");
48
49
            ResultSet resultSet = preparedStatement.executeQuery();
50
            List<Map<String, Object>> results = new ArrayList<>();
51
52
            ResultSetMetaData metaData = resultSet.getMetaData();
53
            int colonne = metaData.getColumnCount();
54
55 1 1. doPost : negated conditional → TIMED_OUT
            while (resultSet.next()) {
56
                Map<String, Object> oggetto = new HashMap<>();
57 2 1. doPost : negated conditional → KILLED
2. doPost : changed conditional boundary → KILLED
                for (int i = 1; i <= colonne; i++) {
58
                    oggetto.put(metaData.getColumnName(i), resultSet.getObject(i));
59
                }
60
                results.add(oggetto);
61
            }
62
63
            String lista = new Gson().toJson(results);
64
65 1 1. doPost : removed call to control/search/SearchBar::writeJsonResponse → KILLED
            writeJsonResponse(resp, lista);
66
67
        } catch (SQLException | IOException e) {
68 1 1. doPost : removed call to javax/servlet/RequestDispatcher::forward → KILLED
            req.getRequestDispatcher(ERROR_PAGE).forward(req, resp);
69
        }
70
71
    }
72
73
    private void writeJsonResponse(HttpServletResponse resp,
74
                                   String json) throws IOException {
75 1 1. writeJsonResponse : removed call to javax/servlet/http/HttpServletResponse::setContentType → KILLED
        resp.setContentType("application/json");
76 1 1. writeJsonResponse : removed call to javax/servlet/http/HttpServletResponse::setCharacterEncoding → KILLED
        resp.setCharacterEncoding("UTF-8");
77 1 1. writeJsonResponse : removed call to java/io/PrintWriter::write → KILLED
        resp.getWriter().write(json);
78
    }
79
80
    @Override
81
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
82
            throws ServletException, IOException {
83 1 1. doGet : removed call to control/search/SearchBar::doPost → KILLED
        doPost(req, resp);
84
    }
85
}

Mutations

37

1.1
Location : doPost
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchNormale_listaVuota_dbOk()]
negated conditional → KILLED

47

1.1
Location : doPost
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchNormale_listaVuota_dbOk()]
removed call to java/sql/PreparedStatement::setString → KILLED

55

1.1
Location : doPost
Killed by : none
negated conditional → TIMED_OUT

57

1.1
Location : doPost
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchNormale_unaRiga_dbOk()]
negated conditional → KILLED

2.2
Location : doPost
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchNormale_unaRiga_dbOk()]
changed conditional boundary → KILLED

65

1.1
Location : doPost
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchVuota_listaVuota_dbOk()]
removed call to control/search/SearchBar::writeJsonResponse → KILLED

68

1.1
Location : doPost
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchNormale_dbException_forwardErrorPage()]
removed call to javax/servlet/RequestDispatcher::forward → KILLED

75

1.1
Location : writeJsonResponse
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchNormale_listaVuota_dbOk()]
removed call to javax/servlet/http/HttpServletResponse::setContentType → KILLED

76

1.1
Location : writeJsonResponse
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchNormale_listaVuota_dbOk()]
removed call to javax/servlet/http/HttpServletResponse::setCharacterEncoding → KILLED

77

1.1
Location : writeJsonResponse
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doPost_searchVuota_listaVuota_dbOk()]
removed call to java/io/PrintWriter::write → KILLED

83

1.1
Location : doGet
Killed by : control.search.SearchBarTest.[engine:junit-jupiter]/[class:control.search.SearchBarTest]/[method:doGet_delegaDoPost()]
removed call to control/search/SearchBar::doPost → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.0