| 1 | package com.popx.presentazione; | |
| 2 | ||
| 3 | import com.google.gson.JsonObject; | |
| 4 | import com.popx.modello.ProdottoBean; | |
| 5 | import com.popx.persistenza.ProdottoDAOImpl; | |
| 6 | ||
| 7 | import javax.servlet.ServletException; | |
| 8 | import javax.servlet.annotation.MultipartConfig; | |
| 9 | import javax.servlet.annotation.WebServlet; | |
| 10 | import javax.servlet.http.*; | |
| 11 | import java.io.IOException; | |
| 12 | import java.io.InputStream; | |
| 13 | ||
| 14 | @WebServlet("/updateProductServlet") | |
| 15 | @MultipartConfig(maxFileSize = 2 * 1024 * 1024) | |
| 16 | public class UpdateProductServlet extends HttpServlet { | |
| 17 | ||
| 18 | private ProdottoDAOImpl prodottoDAO; | |
| 19 | ||
| 20 | // costruttore production | |
| 21 | public UpdateProductServlet() { | |
| 22 | this.prodottoDAO = new ProdottoDAOImpl(); | |
| 23 | } | |
| 24 | ||
| 25 | // costruttore test | |
| 26 | public UpdateProductServlet(ProdottoDAOImpl prodottoDAO) { | |
| 27 | this.prodottoDAO = prodottoDAO; | |
| 28 | } | |
| 29 | ||
| 30 | @Override | |
| 31 | public void doPost(HttpServletRequest request, HttpServletResponse response) | |
| 32 | throws ServletException, IOException { | |
| 33 | ||
| 34 |
1
1. doPost : removed call to javax/servlet/http/HttpServletResponse::setContentType → SURVIVED |
response.setContentType("application/json"); |
| 35 | JsonObject jsonResponse = new JsonObject(); | |
| 36 | ||
| 37 | try { | |
| 38 | String idProduct = request.getParameter("idProduct"); | |
| 39 | String name = request.getParameter("name"); | |
| 40 | String description = request.getParameter("description"); | |
| 41 | double price = Double.parseDouble(request.getParameter("price")); | |
| 42 | int qty = Integer.parseInt(request.getParameter("qty")); | |
| 43 | String brand = request.getParameter("brand"); | |
| 44 | String figure = request.getParameter("figure"); | |
| 45 | Part imgPart = request.getPart("img_src"); | |
| 46 | String currentImgSrc = request.getParameter("current_img_src"); | |
| 47 | ||
| 48 | ProdottoBean prodotto = | |
| 49 | new ProdottoBean(idProduct, name, description, price, qty, brand, null, figure); | |
| 50 | ||
| 51 | // gestione immagine | |
| 52 |
3
1. doPost : changed conditional boundary → SURVIVED 2. doPost : negated conditional → KILLED 3. doPost : negated conditional → KILLED |
if (imgPart != null && imgPart.getSize() > 0) { |
| 53 |
1
1. doPost : negated conditional → SURVIVED |
if (!imgPart.getContentType().startsWith("image/")) { |
| 54 |
1
1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED |
jsonResponse.addProperty("success", false); |
| 55 |
1
1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED |
jsonResponse.addProperty("message", "Il file caricato non è un'immagine valida."); |
| 56 |
1
1. doPost : removed call to java/io/PrintWriter::write → SURVIVED |
response.getWriter().write(jsonResponse.toString()); |
| 57 | return; | |
| 58 | } | |
| 59 | ||
| 60 | try (InputStream is = imgPart.getInputStream()) { | |
| 61 |
1
1. doPost : removed call to com/popx/modello/ProdottoBean::setImg → NO_COVERAGE |
prodotto.setImg(is.readAllBytes()); |
| 62 | } | |
| 63 |
2
1. doPost : negated conditional → SURVIVED 2. doPost : negated conditional → SURVIVED |
} else if (currentImgSrc != null && !currentImgSrc.isEmpty()) { |
| 64 | ProdottoBean existing = prodottoDAO.getProdottoById(idProduct); | |
| 65 |
1
1. doPost : negated conditional → SURVIVED |
if (existing != null) { |
| 66 |
1
1. doPost : removed call to com/popx/modello/ProdottoBean::setImg → SURVIVED |
prodotto.setImg(existing.getImg()); |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | boolean updated = prodottoDAO.updateProduct(prodotto); | |
| 71 | ||
| 72 |
1
1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED |
jsonResponse.addProperty("success", updated); |
| 73 |
1
1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED |
jsonResponse.addProperty( |
| 74 | "message", | |
| 75 |
1
1. doPost : negated conditional → SURVIVED |
updated ? "Prodotto aggiornato con successo." |
| 76 | : "Errore durante l'aggiornamento del prodotto." | |
| 77 | ); | |
| 78 | ||
| 79 | } catch (Exception e) { | |
| 80 |
1
1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED |
jsonResponse.addProperty("success", false); |
| 81 |
1
1. doPost : removed call to com/google/gson/JsonObject::addProperty → SURVIVED |
jsonResponse.addProperty("message", "Errore interno: " + e.getMessage()); |
| 82 | } | |
| 83 | ||
| 84 |
1
1. doPost : removed call to java/io/PrintWriter::write → SURVIVED |
response.getWriter().write(jsonResponse.toString()); |
| 85 | } | |
| 86 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 52 |
1.1 2.2 3.3 |
|
| 53 |
1.1 |
|
| 54 |
1.1 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 61 |
1.1 |
|
| 63 |
1.1 2.2 |
|
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 72 |
1.1 |
|
| 73 |
1.1 |
|
| 75 |
1.1 |
|
| 80 |
1.1 |
|
| 81 |
1.1 |
|
| 84 |
1.1 |