Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dromano32 committed Feb 8, 2024
1 parent a64835a commit 5d16d16
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 71 deletions.
16 changes: 11 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,32 @@
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserDaoSQLiteImpl implements UserDao {
private Connection connection;

public UserDaoSQLiteImpl(String dbUrl) throws DatabaseConnectionException {
try {
connection = DriverManager.getConnection(dbUrl);
} catch (SQLException e) {
throw new DatabaseConnectionException("Connessione al database non riuscita");
throw new DatabaseConnectionException("Connessione al database non riuscita");
}
}

private PreparedStatement prepareStatement(String sql, String... parameters) throws SQLException {
PreparedStatement pstmt = connection.prepareStatement(sql);
for (int i = 0; i < parameters.length; i++) {
pstmt.setString(i + 1, parameters[i]);
}
return pstmt;
}

private void closePreparedStatement(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Expand All @@ -23,9 +40,7 @@ public User getUserByUsernameAndPassword(String username, String password) throw
String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt = prepareStatement(sql, username, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return new User(rs.getString("username"), rs.getString("password"));
Expand All @@ -35,59 +50,35 @@ public User getUserByUsernameAndPassword(String username, String password) throw
} catch (SQLException e) {
throw new UserException("Errore durante il recupero dell'utente.", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
closePreparedStatement(pstmt);
}
}


@Override
public void registerUser(User user) throws UserException {
String sql = "INSERT INTO users(username, password) VALUES(?, ?)";
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt = prepareStatement(sql, user.getUsername(), user.getPassword());
pstmt.executeUpdate();
} catch (SQLException e) {
throw new UserException("Errore durante la registrazione dell'utente.", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
closePreparedStatement(pstmt);
}
}


@Override
public void updateUser(User user) throws UserException {
String sql = "UPDATE users SET password = ? WHERE username = ?";
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, user.getPassword());
pstmt.setString(2, user.getUsername());
pstmt = prepareStatement(sql, user.getPassword(), user.getUsername());
pstmt.executeUpdate();
} catch (SQLException e) {
throw new UserException("Errore durante l'aggiornamento dell'utente.", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
closePreparedStatement(pstmt);
}
}

Expand All @@ -96,22 +87,14 @@ public void deleteUser(User user) throws UserException {
String sql = "DELETE FROM users WHERE username = ?";
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt = prepareStatement(sql, user.getUsername());
pstmt.executeUpdate();
} catch (SQLException e) {
throw new UserException("Errore durante l'eliminazione dell'utente.", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
closePreparedStatement(pstmt);
}
}


public void createUsersTable() throws UserException {
String sql = "CREATE TABLE IF NOT EXISTS users (\n"
Expand All @@ -120,21 +103,16 @@ public void createUsersTable() throws UserException {
+ ");";
PreparedStatement pstmt = null;
try {
pstmt = connection.prepareStatement(sql);
pstmt = prepareStatement(sql);
pstmt.execute();
} catch (SQLException e) {
throw new UserException("Errore durante la creazione della tabella users.", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
closePreparedStatement(pstmt);
}
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class DatabaseConnectionTest {

@Test
public void testGetConnection() {
Connection connection = DatabaseConnection.getConnection();
Assert.assertNotNull("Connection should not be null", connection);
assertNotNull(connection, "Connection should not be null");

try {
connection.close();
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/com/mvcguru/risiko/maven/eclipse/MainTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.mvcguru.risiko.maven.eclipse;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

/**
* Unit test for simple App.
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/com/mvcguru/risiko/maven/eclipse/UserTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.mvcguru.risiko.maven.eclipse;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class UserTest {

@Test
public void testUser() {
String username = "Bobby";
String password = "bobbybobby";
String username = "bobby";
String password = "bobbybobby00";

User user = new User(username, password);

Assert.assertEquals("Username should be equal to the one set", username, user.getUsername());
Assert.assertEquals("Password should be equal to the one set", password, user.getPassword());
assertEquals(username, user.getUsername(), "Username should be equal to the one set");
assertEquals(password, user.getPassword(), "Password should be equal to the one set");

String newUsername = "newTestUser";
String newPassword = "newTestPassword";

user.setUsername(newUsername);
user.setPassword(newPassword);

Assert.assertEquals("Username should be updated to the new one", newUsername, user.getUsername());
Assert.assertEquals("Password should be updated to the new one", newPassword, user.getPassword());
assertEquals(newUsername, user.getUsername(), "Username should be updated to the new one");
assertEquals(newPassword, user.getPassword(), "Password should be updated to the new one");
}
}

0 comments on commit 5d16d16

Please sign in to comment.