OrdineDAO.java

1
package model.ordine;
2
3
import model.DAOInterface;
4
import model.DBConnection;
5
6
import javax.sql.DataSource;
7
import java.sql.*;
8
import java.time.LocalDate;
9
import java.util.ArrayList;
10
import java.util.Arrays;
11
import java.util.Collection;
12
import java.util.List;
13
14
public class OrdineDAO implements DAOInterface<OrdineBean, Integer> {
15
    private static final String TABLE_NAME = "Ordine";
16
    private static final List<String> ORDERS =
17
            new ArrayList<>(Arrays.asList("username", "dataOrdine"));
18
    private static DataSource ds;
19
20
    public OrdineDAO() {
21
        ds = DBConnection.getDataSource();
22
    }
23
24
    public OrdineDAO(DataSource dataSource) {
25
        ds = dataSource;
26
    }
27
28
    @Override
29
    public OrdineBean doRetrieveByKey(Integer code) throws SQLException {
30
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE ID = ?";
31
32
        try (Connection connection = ds.getConnection();
33
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
34
35 1 1. doRetrieveByKey : removed call to java/sql/PreparedStatement::setInt → KILLED
            preparedStatement.setInt(1, code);
36
37
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
38
39
                OrdineBean ordineBean = null;
40
41 1 1. doRetrieveByKey : negated conditional → KILLED
                if (resultSet.next()) {
42
                    ordineBean = new OrdineBean();
43 1 1. doRetrieveByKey : removed call to model/ordine/OrdineDAO::setOrders → KILLED
                    setOrders(resultSet, ordineBean);
44
                }
45
46 1 1. doRetrieveByKey : replaced return value with null for model/ordine/OrdineDAO::doRetrieveByKey → KILLED
                return ordineBean;
47
            }
48
        }
49
    }
50
51
    public Collection<OrdineBean> doRetrieveByKey(String code) throws SQLException {
52
        Collection<OrdineBean> ordini = new ArrayList<>();
53
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE username = ?";
54
55
        try (Connection connection = ds.getConnection();
56
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
57
58 1 1. doRetrieveByKey : removed call to java/sql/PreparedStatement::setString → KILLED
            preparedStatement.setString(1, code);
59
60
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
61
62 1 1. doRetrieveByKey : negated conditional → KILLED
                while (resultSet.next()) {
63
                    OrdineBean ordineBean = new OrdineBean();
64 1 1. doRetrieveByKey : removed call to model/ordine/OrdineDAO::setOrders → KILLED
                    setOrders(resultSet, ordineBean);
65
                    ordini.add(ordineBean);
66
                }
67
            }
68
        }
69
70 1 1. doRetrieveByKey : replaced return value with Collections.emptyList for model/ordine/OrdineDAO::doRetrieveByKey → KILLED
        return ordini;
71
    }
72
73
    @Override
74
    public Collection<OrdineBean> doRetriveAll(String order) throws SQLException {
75
        Collection<OrdineBean> ordini = new ArrayList<>();
76
        StringBuilder query = new StringBuilder("SELECT * FROM " + TABLE_NAME);
77
78
        for (String s : ORDERS) {
79 1 1. doRetriveAll : negated conditional → KILLED
            if (s.equals(order)) {
80
                query.append(" ORDER BY ").append(s);
81
                break;
82
            }
83
        }
84
85
        try (Connection connection = ds.getConnection();
86
             PreparedStatement preparedStatement = connection.prepareStatement(query.toString());
87
             ResultSet resultSet = preparedStatement.executeQuery()) {
88
89 1 1. doRetriveAll : negated conditional → KILLED
            while (resultSet.next()) {
90
                OrdineBean ordineBean = new OrdineBean();
91 1 1. doRetriveAll : removed call to model/ordine/OrdineDAO::setOrders → KILLED
                setOrders(resultSet, ordineBean);
92
                ordini.add(ordineBean);
93
            }
94
        }
95
96 1 1. doRetriveAll : replaced return value with Collections.emptyList for model/ordine/OrdineDAO::doRetriveAll → KILLED
        return ordini;
97
    }
98
99
    @Override
100
    public void doSave(OrdineBean ordineBean) throws SQLException {
101
        String query =
102
                "INSERT INTO " + TABLE_NAME +
103
                " (username, prezzoTotale, dataConsegna, dataOrdine, nomeConsegna, cognomeConsegna, cap, via, citta) " +
104
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
105
106
        try (Connection connection = ds.getConnection();
107
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
108
109 1 1. doSave : removed call to model/ordine/OrdineDAO::setOrdineStatement → KILLED
            setOrdineStatement(ordineBean, preparedStatement);
110
            preparedStatement.executeUpdate();
111
        }
112
    }
113
114
    @Override
115
    public void doUpdate(OrdineBean product) {
116
        // Update operation is not supported for Ordine entities
117
    }
118
119
    @Override
120
    public boolean doDelete(Integer code) {
121 1 1. doDelete : replaced boolean return with true for model/ordine/OrdineDAO::doDelete → NO_COVERAGE
        return false;
122
    }
123
124
    public int getMaxID() throws SQLException {
125
        String sessionCacheQuery = "SET @@SESSION.information_schema_stats_expiry = 0;";
126
        String query =
127
                "SELECT AUTO_INCREMENT FROM information_schema.tables " +
128
                "WHERE table_name = ? AND table_schema = 'whiTee'";
129
130
        try (Connection connection = ds.getConnection();
131
             Statement cacheStmt = connection.createStatement();
132
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
133
134
            cacheStmt.execute(sessionCacheQuery);
135 1 1. getMaxID : removed call to java/sql/PreparedStatement::setString → KILLED
            preparedStatement.setString(1, TABLE_NAME);
136
137
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
138
139 1 1. getMaxID : negated conditional → KILLED
                if (!resultSet.next()) {
140
                    throw new SQLException("AUTO_INCREMENT non trovato per tabella: " + TABLE_NAME);
141
                }
142
143 1 1. getMaxID : replaced int return with 0 for model/ordine/OrdineDAO::getMaxID → KILLED
                return resultSet.getInt("AUTO_INCREMENT");
144
            }
145
        }
146
    }
147
148
    private void setOrders(ResultSet resultSet, OrdineBean ordineBean) throws SQLException {
149 1 1. setOrders : removed call to model/ordine/OrdineBean::setID → KILLED
        ordineBean.setID(resultSet.getInt("ID"));
150 1 1. setOrders : removed call to model/ordine/OrdineBean::setUsername → KILLED
        ordineBean.setUsername(resultSet.getString("username"));
151 1 1. setOrders : removed call to model/ordine/OrdineBean::setPrezzoTotale → KILLED
        ordineBean.setPrezzoTotale(resultSet.getFloat("prezzoTotale"));
152 1 1. setOrders : removed call to model/ordine/OrdineBean::setDataConsegna → KILLED
        ordineBean.setDataConsegna(resultSet.getDate("dataConsegna").toLocalDate());
153 1 1. setOrders : removed call to model/ordine/OrdineBean::setDataOrdine → KILLED
        ordineBean.setDataOrdine(resultSet.getDate("dataOrdine").toLocalDate());
154 1 1. setOrders : removed call to model/ordine/OrdineBean::setNomeConsegna → KILLED
        ordineBean.setNomeConsegna(resultSet.getString("nomeConsegna"));
155 1 1. setOrders : removed call to model/ordine/OrdineBean::setCognomeConsegna → KILLED
        ordineBean.setCognomeConsegna(resultSet.getString("cognomeConsegna"));
156 1 1. setOrders : removed call to model/ordine/OrdineBean::setCap → KILLED
        ordineBean.setCap(resultSet.getString("cap"));
157 1 1. setOrders : removed call to model/ordine/OrdineBean::setVia → KILLED
        ordineBean.setVia(resultSet.getString("via"));
158 1 1. setOrders : removed call to model/ordine/OrdineBean::setCitta → KILLED
        ordineBean.setCitta(resultSet.getString("citta"));
159
    }
160
161
    private void setOrdineStatement(OrdineBean ordineBean, PreparedStatement preparedStatement) throws SQLException {
162 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(1, ordineBean.getUsername());
163 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setFloat → KILLED
        preparedStatement.setFloat(2, ordineBean.getPrezzoTotale());
164 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setDate → KILLED
        preparedStatement.setDate(3, Date.valueOf(LocalDate.now().plusDays(15)));
165 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setDate → KILLED
        preparedStatement.setDate(4, Date.valueOf(LocalDate.now()));
166 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(5, ordineBean.getNomeConsegna());
167 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(6, ordineBean.getCognomeConsegna());
168 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(7, ordineBean.getCap());
169 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(8, ordineBean.getVia());
170 1 1. setOrdineStatement : removed call to java/sql/PreparedStatement::setString → KILLED
        preparedStatement.setString(9, ordineBean.getCitta());
171
    }
172
}

Mutations

35

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

41

1.1
Location : doRetrieveByKey
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_rsVuoto()]
negated conditional → KILLED

43

1.1
Location : doRetrieveByKey
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineDAO::setOrders → KILLED

46

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

58

1.1
Location : doRetrieveByKey
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByUsername_vuoto()]
removed call to java/sql/PreparedStatement::setString → KILLED

62

1.1
Location : doRetrieveByKey
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByUsername_vuoto()]
negated conditional → KILLED

64

1.1
Location : doRetrieveByKey
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByUsername_unaRiga()]
removed call to model/ordine/OrdineDAO::setOrders → KILLED

70

1.1
Location : doRetrieveByKey
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByUsername_unaRiga()]
replaced return value with Collections.emptyList for model/ordine/OrdineDAO::doRetrieveByKey → KILLED

79

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

89

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

91

1.1
Location : doRetriveAll
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveAll_orderValido()]
removed call to model/ordine/OrdineDAO::setOrders → KILLED

96

1.1
Location : doRetriveAll
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveAll_orderValido()]
replaced return value with Collections.emptyList for model/ordine/OrdineDAO::doRetriveAll → KILLED

109

1.1
Location : doSave
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to model/ordine/OrdineDAO::setOrdineStatement → KILLED

121

1.1
Location : doDelete
Killed by : none
replaced boolean return with true for model/ordine/OrdineDAO::doDelete → NO_COVERAGE

135

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

139

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

143

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

149

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setID → KILLED

150

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setUsername → KILLED

151

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setPrezzoTotale → KILLED

152

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setDataConsegna → KILLED

153

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setDataOrdine → KILLED

154

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setNomeConsegna → KILLED

155

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setCognomeConsegna → KILLED

156

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setCap → KILLED

157

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setVia → KILLED

158

1.1
Location : setOrders
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doRetrieveByKey_idValido()]
removed call to model/ordine/OrdineBean::setCitta → KILLED

162

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

163

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setFloat → KILLED

164

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setDate → KILLED

165

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setDate → KILLED

166

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

167

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

168

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

169

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

170

1.1
Location : setOrdineStatement
Killed by : model.ordine.OrdineDAOTest.[engine:junit-jupiter]/[class:model.ordine.OrdineDAOTest]/[method:doSave_ok()]
removed call to java/sql/PreparedStatement::setString → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.0