Actualiser src/test/java/controller/SoumettreDemandeTest.java

This commit is contained in:
Abderrahman El-Ouali 2024-11-15 23:15:44 +01:00
parent 0f410a0e2f
commit 2e41f1185e

View file

@ -1,28 +1,126 @@
package controller;
import database.DatabaseConnection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.*;
class SoumettreDemandeTest {
class LoginPageTest {
private SoumettreDemande soumettreDemande;
private LoginPage loginPage;
private JTextField emailField;
private JButton loginButton;
private JButton createAccountButton;
@BeforeEach
void setUp() {
soumettreDemande = new SoumettreDemande(2); // Utilisateur ID fictif
void setUp() throws NoSuchFieldException, IllegalAccessException {
loginPage = new LoginPage();
// Access private fields using reflection
emailField = (JTextField) getField("emailField");
loginButton = (JButton) getField("loginButton");
createAccountButton = (JButton) getField("createAccountButton");
}
// Helper method to access private fields
private Object getField(String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field field = LoginPage.class.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(loginPage);
}
@Test
void testSoumettreDemandeNotNull() {
// Vérifier si la page de soumission de demande est bien créée
assertNotNull(soumettreDemande);
void testLoginPageComponents() {
assertNotNull(loginPage);
assertNotNull(emailField);
assertNotNull(loginButton);
assertNotNull(createAccountButton);
// Check default values and UI setup
assertEquals("", emailField.getText());
}
@Test
void testDescriptionFieldIsEmptyInitially() {
// Tester si le champ de description est vide au départ
// assertEquals("", soumettreDemande.getDescriptionField().getText());
void testLoginWithValidUser() throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// Insert a test user in the database
try (Connection connection = DatabaseConnection.getConnection()) {
String insertSQL = "INSERT INTO utilisateur (email, role) VALUES (?, ?)";
PreparedStatement insertStatement = connection.prepareStatement(insertSQL, PreparedStatement.RETURN_GENERATED_KEYS);
insertStatement.setString(1, "test@example.com");
insertStatement.setString(2, "benevole");
insertStatement.executeUpdate();
// Get generated user ID for cleanup later
ResultSet generatedKeys = insertStatement.getGeneratedKeys();
int testUserId = -1;
if (generatedKeys.next()) {
testUserId = generatedKeys.getInt(1);
}
// Set email field to match the test user
emailField.setText("test@example.com");
// Access and invoke the loginUser() method using reflection
Method loginUserMethod = LoginPage.class.getDeclaredMethod("loginUser");
loginUserMethod.setAccessible(true);
loginUserMethod.invoke(loginPage);
// Verify redirection to MenuBenevole
assertFalse(loginPage.isVisible(), "LoginPage should close upon successful login.");
// Clean up the inserted test data
String deleteSQL = "DELETE FROM utilisateur WHERE id = ?";
PreparedStatement deleteStatement = connection.prepareStatement(deleteSQL);
deleteStatement.setInt(1, testUserId);
deleteStatement.executeUpdate();
}
}
@Test
void testLoginWithInvalidEmail() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// Set an email that does not exist in the database
emailField.setText("nonexistent@example.com");
// Access and invoke the loginUser() method using reflection
Method loginUserMethod = LoginPage.class.getDeclaredMethod("loginUser");
loginUserMethod.setAccessible(true);
loginUserMethod.invoke(loginPage);
// Check for a dialog box error message (mocked by checking that page remains open)
assertTrue(loginPage.isVisible(), "LoginPage should remain open if login fails.");
}
@Test
void testLoginWithEmptyEmailField() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// Leave email field empty
emailField.setText("");
// Access and invoke the loginUser() method using reflection
Method loginUserMethod = LoginPage.class.getDeclaredMethod("loginUser");
loginUserMethod.setAccessible(true);
loginUserMethod.invoke(loginPage);
// Check for a dialog box error message (mocked by checking that page remains open)
assertTrue(loginPage.isVisible(), "LoginPage should remain open if email field is empty.");
}
@Test
void testCreateAccountButtonAction() {
// Simulate clicking the "Créer un compte" button
createAccountButton.doClick();
// Check that the current frame is disposed
assertFalse(loginPage.isVisible(), "LoginPage should close after clicking 'Créer un compte'.");
}
}