| 1 | package com.popx.persistenza; | |
| 2 | ||
| 3 | import com.popx.modello.AdminBean; | |
| 4 | ||
| 5 | import javax.naming.Context; | |
| 6 | import javax.naming.InitialContext; | |
| 7 | import javax.naming.NamingException; | |
| 8 | import javax.sql.DataSource; | |
| 9 | import java.sql.Connection; | |
| 10 | import java.sql.PreparedStatement; | |
| 11 | import java.sql.ResultSet; | |
| 12 | import java.sql.SQLException; | |
| 13 | ||
| 14 | /*@ public invariant ds != null; @*/ | |
| 15 | public class AdminDAOImpl implements UserDAO<AdminBean> { | |
| 16 | ||
| 17 | private static final DataSource ds; | |
| 18 | ||
| 19 | static { | |
| 20 | try { | |
| 21 | Context ctx = new InitialContext(); | |
| 22 | Context env = (Context) ctx.lookup("java:comp/env"); | |
| 23 | ds = (DataSource) env.lookup("jdbc/Popix"); | |
| 24 | } catch (NamingException e) { | |
| 25 | throw new RuntimeException(e); | |
| 26 | } | |
| 27 | } | |
| 28 | ||
| 29 | /*@ | |
| 30 | @ also | |
| 31 | @ public normal_behavior | |
| 32 | @ requires email != null && !email.isEmpty(); | |
| 33 | @ ensures \result == null | |
| 34 | @ || \result.getEmail().equals(email); | |
| 35 | @ signals (SQLException) true; | |
| 36 | @*/ | |
| 37 | @Override | |
| 38 | public AdminBean getUserByEmail(String email) throws SQLException { | |
| 39 | String query = "SELECT * FROM UtenteRegistrato u " + | |
| 40 | "JOIN Admin a ON u.email = a.utente_registrato_email " + | |
| 41 | "WHERE u.email = ?"; | |
| 42 | try (Connection conn = ds.getConnection(); | |
| 43 | PreparedStatement stmt = conn.prepareStatement(query)) { | |
| 44 |
1
1. getUserByEmail : removed call to java/sql/PreparedStatement::setString → NO_COVERAGE |
stmt.setString(1, email); |
| 45 | ResultSet rs = stmt.executeQuery(); | |
| 46 |
1
1. getUserByEmail : negated conditional → NO_COVERAGE |
if (rs.next()) { |
| 47 | return new AdminBean( | |
| 48 | rs.getString("username"), | |
| 49 | rs.getString("email"), | |
| 50 | rs.getString("password"), | |
| 51 | rs.getString("role") | |
| 52 | ); | |
| 53 | } | |
| 54 | } | |
| 55 | return null; | |
| 56 | } | |
| 57 | ||
| 58 | /*@ | |
| 59 | @ also | |
| 60 | @ public normal_behavior | |
| 61 | @ requires user != null; | |
| 62 | @ requires user.getEmail() != null && !user.getEmail().isEmpty(); | |
| 63 | @ ensures \result == true || \result == false; | |
| 64 | @ signals (SQLException) true; | |
| 65 | @*/ | |
| 66 | @Override | |
| 67 | public boolean saveUser(AdminBean user) throws SQLException { | |
| 68 | String userQuery = "INSERT INTO UtenteRegistrato (username, email, password, role) VALUES (?, ?, ?, ?)"; | |
| 69 | String adminQuery = "INSERT INTO Admin (utente_registrato_email) VALUES (?)"; | |
| 70 | ||
| 71 | try (Connection conn = ds.getConnection()) { | |
| 72 |
1
1. saveUser : removed call to java/sql/Connection::setAutoCommit → NO_COVERAGE |
conn.setAutoCommit(false); |
| 73 | try (PreparedStatement userStmt = conn.prepareStatement(userQuery); | |
| 74 | PreparedStatement adminStmt = conn.prepareStatement(adminQuery)) { | |
| 75 | ||
| 76 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → NO_COVERAGE |
userStmt.setString(1, user.getUsername()); |
| 77 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → NO_COVERAGE |
userStmt.setString(2, user.getEmail()); |
| 78 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → NO_COVERAGE |
userStmt.setString(3, com.popx.servizio.SecurityService.hashPassword(user.getPassword())); |
| 79 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → NO_COVERAGE |
userStmt.setString(4, "Admin"); |
| 80 | userStmt.executeUpdate(); | |
| 81 | ||
| 82 |
1
1. saveUser : removed call to java/sql/PreparedStatement::setString → NO_COVERAGE |
adminStmt.setString(1, user.getEmail()); |
| 83 | adminStmt.executeUpdate(); | |
| 84 | ||
| 85 |
1
1. saveUser : removed call to java/sql/Connection::commit → NO_COVERAGE |
conn.commit(); |
| 86 | return true; | |
| 87 | } catch (SQLException e) { | |
| 88 | conn.rollback(); | |
| 89 | throw e; | |
| 90 | } | |
| 91 | } | |
| 92 | } | |
| 93 | } | |
Mutations | ||
| 44 |
1.1 |
|
| 46 |
1.1 |
|
| 72 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 82 |
1.1 |
|
| 85 |
1.1 |