MagliettaDAO.java

1
package model.maglietta;
2
3
import model.DAOInterface;
4
import model.DBConnection;
5
import javax.sql.DataSource;
6
import java.sql.*;
7
import java.util.*;
8
9
public class MagliettaDAO implements DAOInterface<MagliettaBean, Integer> {
10
    private static final String TABLE_NAME = "Maglietta";
11
    private final DataSource ds;
12
    private static final List<String> ORDERS =
13
            new ArrayList<>(Arrays.asList("nome", "prezzo", "colore", "tipo"));
14
15
    public MagliettaDAO() {
16
        ds = DBConnection.getDataSource();
17
    }
18
19
    public MagliettaDAO(DataSource ds) {
20
        this.ds = ds;
21
    }
22
23
    public synchronized Collection<MagliettaBean> doRetrieveByTipo(String tipo) throws SQLException {
24
        Collection<MagliettaBean> maglietteTipo = new ArrayList<>();
25
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE tipo = ?";
26
27
        try (Connection connection = ds.getConnection();
28
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
29
30 1 1. doRetrieveByTipo : removed call to java/sql/PreparedStatement::setString → KILLED
            preparedStatement.setString(1, tipo);
31
32
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
33 1 1. doRetrieveByTipo : negated conditional → TIMED_OUT
                while (resultSet.next()) {
34
                    MagliettaBean magliettaBean = new MagliettaBean();
35 1 1. doRetrieveByTipo : removed call to model/maglietta/MagliettaDAO::setMaglietta → KILLED
                    setMaglietta(resultSet, magliettaBean);
36
                    maglietteTipo.add(magliettaBean);
37
                }
38
            }
39
        }
40
41 1 1. doRetrieveByTipo : replaced return value with Collections.emptyList for model/maglietta/MagliettaDAO::doRetrieveByTipo → KILLED
        return maglietteTipo;
42
    }
43
44
    // Restituisce un oggetto maglietta con delle caratteristiche (SQL SELECT)
45
    @Override
46
    public synchronized MagliettaBean doRetrieveByKey(Integer code) throws SQLException {
47
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE ID = ?";
48
49
        try (Connection connection = ds.getConnection();
50
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
51
52 1 1. doRetrieveByKey : removed call to java/sql/PreparedStatement::setInt → KILLED
            preparedStatement.setInt(1, code);
53
54
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
55
56 1 1. doRetrieveByKey : negated conditional → KILLED
                if (!resultSet.next()) {
57
                    throw new SQLException("Maglietta non trovata con ID: " + code);
58
                }
59
60
                MagliettaBean magliettaBean = new MagliettaBean();
61 1 1. doRetrieveByKey : removed call to model/maglietta/MagliettaDAO::setMaglietta → KILLED
                setMaglietta(resultSet, magliettaBean);
62 1 1. doRetrieveByKey : replaced return value with null for model/maglietta/MagliettaDAO::doRetrieveByKey → KILLED
                return magliettaBean;
63
            }
64
        }
65
    }
66
67
    // Restituisce una collezione di magliette che soddisfano una condizione (SQL ORDER BY)
68
    @Override
69
    public Collection<MagliettaBean> doRetriveAll(String order) throws SQLException {
70
        Collection<MagliettaBean> magliette = new ArrayList<>();
71
72
        StringBuilder query = new StringBuilder(
73
                "SELECT * FROM " + TABLE_NAME + " WHERE Tipo <> 'Personalizzata' AND Tipo <> 'Eliminata'"
74
        );
75
76
        for (String s : ORDERS) {
77 1 1. doRetriveAll : negated conditional → KILLED
            if (s.equals(order)) {
78
                query.append(" ORDER BY ").append(s);
79
                break;
80
            }
81
        }
82
83
        try (Connection connection = ds.getConnection();
84
             PreparedStatement preparedStatement = connection.prepareStatement(query.toString());
85
             ResultSet resultSet = preparedStatement.executeQuery()) {
86
87 1 1. doRetriveAll : negated conditional → KILLED
            while (resultSet.next()) {
88
                MagliettaBean magliettaBean = new MagliettaBean();
89 1 1. doRetriveAll : removed call to model/maglietta/MagliettaDAO::setMaglietta → KILLED
                setMaglietta(resultSet, magliettaBean);
90
                magliette.add(magliettaBean);
91
            }
92
        }
93
94 1 1. doRetriveAll : replaced return value with Collections.emptyList for model/maglietta/MagliettaDAO::doRetriveAll → KILLED
        return magliette;
95
    }
96
97
    // Salva i dati dell'oggetto maglietta nel database (SQL Insert)
98
    @Override
99
    public void doSave(MagliettaBean maglietta) throws SQLException {
100
        String query = "INSERT INTO " + TABLE_NAME +
101
                " (nome, prezzo, IVA, colore, tipo, grafica, descrizione) VALUES (?, ?, ?, ?, ?, ?, ?)";
102
103
        try (Connection connection = ds.getConnection();
104
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
105
106 1 1. doSave : removed call to model/maglietta/MagliettaDAO::setMagliettaStatement → KILLED
            setMagliettaStatement(maglietta, preparedStatement);
107
            preparedStatement.executeUpdate();
108
        }
109
    }
110
111
    // Aggiorna i dati dell'oggetto maglietta nel database (SQL UPDATE)
112
    @Override
113
    public void doUpdate(MagliettaBean maglietta) throws SQLException {
114
        String query = "UPDATE " + TABLE_NAME +
115
                " SET nome = ?, prezzo = ?, IVA = ?, colore = ?, tipo = ?, grafica = ?, descrizione = ? " +
116
                "WHERE ID = ?";
117
118
        try (Connection connection = ds.getConnection();
119
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
120
121 1 1. doUpdate : removed call to model/maglietta/MagliettaDAO::setMagliettaStatement → KILLED
            setMagliettaStatement(maglietta, preparedStatement);
122 1 1. doUpdate : removed call to java/sql/PreparedStatement::setInt → KILLED
            preparedStatement.setInt(8, maglietta.getID());
123
            preparedStatement.executeUpdate();
124
        }
125
    }
126
127
    // Cancella i dati dell'oggetto maglietta dal database (SQL DELETE)
128
    @Override
129
    public boolean doDelete(Integer code) throws SQLException {
130
        String query = "DELETE FROM " + TABLE_NAME + " WHERE ID = ?";
131
        int result;
132
133
        try (Connection connection = ds.getConnection();
134
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
135
136 1 1. doDelete : removed call to java/sql/PreparedStatement::setInt → KILLED
            preparedStatement.setInt(1, code);
137
            result = preparedStatement.executeUpdate();
138
        }
139
140 2 1. doDelete : negated conditional → KILLED
2. doDelete : replaced boolean return with true for model/maglietta/MagliettaDAO::doDelete → KILLED
        return result != 0;
141
    }
142
143
    public boolean deleteMaglietta(Integer code) throws SQLException {
144
        String query = "UPDATE " + TABLE_NAME + " SET Tipo = 'Eliminata' WHERE ID = ?";
145
        int result;
146
147
        try (Connection connection = ds.getConnection();
148
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
149
150 1 1. deleteMaglietta : removed call to java/sql/PreparedStatement::setInt → KILLED
            preparedStatement.setInt(1, code);
151
            result = preparedStatement.executeUpdate();
152
        }
153
154 2 1. deleteMaglietta : replaced boolean return with true for model/maglietta/MagliettaDAO::deleteMaglietta → KILLED
2. deleteMaglietta : negated conditional → KILLED
        return result != 0;
155
    }
156
157
    public int getMaxID() throws SQLException {
158
        String sessionCacheQuery = "SET @@SESSION.information_schema_stats_expiry = 0;";
159
        String query = "SELECT AUTO_INCREMENT " +
160
                "FROM information_schema.tables " +
161
                "WHERE table_name = ? AND table_schema = 'whiTee'";
162
163
        try (Connection connection = ds.getConnection();
164
             Statement cacheStmt = connection.createStatement();
165
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
166
167
            cacheStmt.execute(sessionCacheQuery);
168
169 1 1. getMaxID : removed call to java/sql/PreparedStatement::setString → KILLED
            preparedStatement.setString(1, TABLE_NAME);
170
171
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
172 1 1. getMaxID : negated conditional → KILLED
                if (!resultSet.next()) {
173
                    throw new SQLException("AUTO_INCREMENT non trovato per tabella: " + TABLE_NAME);
174
                }
175 1 1. getMaxID : replaced int return with 0 for model/maglietta/MagliettaDAO::getMaxID → KILLED
                return resultSet.getInt("AUTO_INCREMENT");
176
            }
177
        }
178
    }
179
180
    private void setMaglietta(ResultSet resultSet, MagliettaBean magliettaBean) throws SQLException {
181 1 1. setMaglietta : removed call to model/maglietta/MagliettaBean::setID → KILLED
        magliettaBean.setID(resultSet.getInt("ID"));
182 1 1. setMaglietta : removed call to model/maglietta/MagliettaBean::setNome → KILLED
        magliettaBean.setNome(resultSet.getString("nome"));
183 1 1. setMaglietta : removed call to model/maglietta/MagliettaBean::setPrezzo → KILLED
        magliettaBean.setPrezzo(resultSet.getFloat("prezzo"));
184 1 1. setMaglietta : removed call to model/maglietta/MagliettaBean::setIVA → KILLED
        magliettaBean.setIVA(resultSet.getInt("IVA"));
185 1 1. setMaglietta : removed call to model/maglietta/MagliettaBean::setColore → KILLED
        magliettaBean.setColore(resultSet.getString("colore"));
186 1 1. setMaglietta : removed call to model/maglietta/MagliettaBean::setTipo → KILLED
        magliettaBean.setTipo(resultSet.getString("tipo"));
187 1 1. setMaglietta : removed call to model/maglietta/MagliettaBean::setGrafica → KILLED
        magliettaBean.setGrafica(resultSet.getString("grafica"));
188 1 1. setMaglietta : removed call to model/maglietta/MagliettaBean::setDescrizione → KILLED
        magliettaBean.setDescrizione(resultSet.getString("descrizione"));
189
    }
190
191
    private void setMagliettaStatement(MagliettaBean maglietta, PreparedStatement preparedStatement) throws SQLException {
192 1 1. setMagliettaStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(1, maglietta.getNome());
193 1 1. setMagliettaStatement : removed call to java/sql/PreparedStatement::setFloat → KILLED
        preparedStatement.setFloat(2, maglietta.getPrezzo());
194 1 1. setMagliettaStatement : removed call to java/sql/PreparedStatement::setInt → KILLED
        preparedStatement.setInt(3, maglietta.getIVA());
195 1 1. setMagliettaStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(4, maglietta.getColore());
196 1 1. setMagliettaStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(5, maglietta.getTipo());
197 1 1. setMagliettaStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(6, maglietta.getGrafica());
198 1 1. setMagliettaStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(7, maglietta.getDescrizione());
199
    }
200
}

Mutations

30

1.1
Location : doRetrieveByTipo
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByTipo_vuoto()]
removed call to java/sql/PreparedStatement::setString → KILLED

33

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

35

1.1
Location : doRetrieveByTipo
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByTipo_unaRiga()]
removed call to model/maglietta/MagliettaDAO::setMaglietta → KILLED

41

1.1
Location : doRetrieveByTipo
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByTipo_unaRiga()]
replaced return value with Collections.emptyList for model/maglietta/MagliettaDAO::doRetrieveByTipo → KILLED

52

1.1
Location : doRetrieveByKey
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_nextFalse()]
removed call to java/sql/PreparedStatement::setInt → KILLED

56

1.1
Location : doRetrieveByKey
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
negated conditional → KILLED

61

1.1
Location : doRetrieveByKey
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/maglietta/MagliettaDAO::setMaglietta → KILLED

62

1.1
Location : doRetrieveByKey
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
replaced return value with null for model/maglietta/MagliettaDAO::doRetrieveByKey → KILLED

77

1.1
Location : doRetriveAll
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveAll_orderValido()]
negated conditional → KILLED

87

1.1
Location : doRetriveAll
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveAll_orderInvalido()]
negated conditional → KILLED

89

1.1
Location : doRetriveAll
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveAll_orderValido()]
removed call to model/maglietta/MagliettaDAO::setMaglietta → KILLED

94

1.1
Location : doRetriveAll
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveAll_orderInvalido()]
replaced return value with Collections.emptyList for model/maglietta/MagliettaDAO::doRetriveAll → KILLED

106

1.1
Location : doSave
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doSave_ok()]
removed call to model/maglietta/MagliettaDAO::setMagliettaStatement → KILLED

121

1.1
Location : doUpdate
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to model/maglietta/MagliettaDAO::setMagliettaStatement → KILLED

122

1.1
Location : doUpdate
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to java/sql/PreparedStatement::setInt → KILLED

136

1.1
Location : doDelete
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doDelete_false()]
removed call to java/sql/PreparedStatement::setInt → KILLED

140

1.1
Location : doDelete
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doDelete_false()]
negated conditional → KILLED

2.2
Location : doDelete
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doDelete_false()]
replaced boolean return with true for model/maglietta/MagliettaDAO::doDelete → KILLED

150

1.1
Location : deleteMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:deleteMaglietta_false()]
removed call to java/sql/PreparedStatement::setInt → KILLED

154

1.1
Location : deleteMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:deleteMaglietta_false()]
replaced boolean return with true for model/maglietta/MagliettaDAO::deleteMaglietta → KILLED

2.2
Location : deleteMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:deleteMaglietta_false()]
negated conditional → KILLED

169

1.1
Location : getMaxID
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:getMaxID_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

172

1.1
Location : getMaxID
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:getMaxID_rsVuoto()]
negated conditional → KILLED

175

1.1
Location : getMaxID
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:getMaxID_ok()]
replaced int return with 0 for model/maglietta/MagliettaDAO::getMaxID → KILLED

181

1.1
Location : setMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/maglietta/MagliettaBean::setID → KILLED

182

1.1
Location : setMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveAll_orderValido()]
removed call to model/maglietta/MagliettaBean::setNome → KILLED

183

1.1
Location : setMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/maglietta/MagliettaBean::setPrezzo → KILLED

184

1.1
Location : setMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/maglietta/MagliettaBean::setIVA → KILLED

185

1.1
Location : setMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/maglietta/MagliettaBean::setColore → KILLED

186

1.1
Location : setMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/maglietta/MagliettaBean::setTipo → KILLED

187

1.1
Location : setMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/maglietta/MagliettaBean::setGrafica → KILLED

188

1.1
Location : setMaglietta
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/maglietta/MagliettaBean::setDescrizione → KILLED

192

1.1
Location : setMagliettaStatement
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

193

1.1
Location : setMagliettaStatement
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to java/sql/PreparedStatement::setFloat → KILLED

194

1.1
Location : setMagliettaStatement
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to java/sql/PreparedStatement::setInt → KILLED

195

1.1
Location : setMagliettaStatement
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

196

1.1
Location : setMagliettaStatement
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

197

1.1
Location : setMagliettaStatement
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

198

1.1
Location : setMagliettaStatement
Killed by : model.maglietta.MagliettaDAOTest.[engine:junit-jupiter]/[class:model.maglietta.MagliettaDAOTest]/[method:doUpdate_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.0