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 CreateAccountPageTest { private CreateAccountPage createAccountPage; private JTextField nomField; private JTextField emailField; private JComboBox roleComboBox; private JButton createAccountButton; private JButton retourLoginButton; @BeforeEach void setUp() throws NoSuchFieldException, IllegalAccessException { createAccountPage = new CreateAccountPage(); // Access private fields using reflection nomField = (JTextField) getField("nomField"); emailField = (JTextField) getField("emailField"); roleComboBox = (JComboBox) getField("roleComboBox"); createAccountButton = (JButton) getField("createAccountButton"); retourLoginButton = (JButton) getField("retourLoginButton"); } // Helper method to access private fields private Object getField(String fieldName) throws NoSuchFieldException, IllegalAccessException { Field field = CreateAccountPage.class.getDeclaredField(fieldName); field.setAccessible(true); return field.get(createAccountPage); } @Test void testCreateAccountPageComponents() { assertNotNull(createAccountPage); assertNotNull(nomField); assertNotNull(emailField); assertNotNull(roleComboBox); assertNotNull(createAccountButton); assertNotNull(retourLoginButton); // Check default values and UI setup assertEquals("", nomField.getText()); assertEquals("", emailField.getText()); assertEquals("benevole", roleComboBox.getSelectedItem()); } @Test void testCreateAccountWithValidData() throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { // Set field values nomField.setText("Test User"); emailField.setText("test@example.com"); roleComboBox.setSelectedItem("benevole"); // Access and invoke the createAccount() method using reflection Method createAccountMethod = CreateAccountPage.class.getDeclaredMethod("createAccount"); createAccountMethod.setAccessible(true); createAccountMethod.invoke(createAccountPage); // Check that the user was added to the database try (Connection connection = DatabaseConnection.getConnection()) { String sql = "SELECT * FROM utilisateur WHERE nom = ? AND email = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "Test User"); statement.setString(2, "test@example.com"); ResultSet resultSet = statement.executeQuery(); assertTrue(resultSet.next(), "L'utilisateur doit être ajouté à la base de données."); // Clean up the inserted test data String deleteSQL = "DELETE FROM utilisateur WHERE nom = ? AND email = ?"; PreparedStatement deleteStatement = connection.prepareStatement(deleteSQL); deleteStatement.setString(1, "Test User"); deleteStatement.setString(2, "test@example.com"); deleteStatement.executeUpdate(); } } @Test void testCreateAccountWithEmptyFields() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, SQLException { // Set empty fields to simulate missing input nomField.setText(""); emailField.setText(""); // Access and invoke the createAccount() method using reflection Method createAccountMethod = CreateAccountPage.class.getDeclaredMethod("createAccount"); createAccountMethod.setAccessible(true); createAccountMethod.invoke(createAccountPage); // Check if the error message dialog was displayed (mocked by asserting the lack of database change) try (Connection connection = DatabaseConnection.getConnection()) { String sql = "SELECT * FROM utilisateur WHERE nom = ? AND email = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, ""); statement.setString(2, ""); ResultSet resultSet = statement.executeQuery(); assertFalse(resultSet.next(), "Aucun utilisateur ne doit être ajouté lorsque les champs sont vides."); } } @Test void testRetourLoginButton() { // Simulate clicking the "Retour à la connexion" button retourLoginButton.doClick(); // Check that the current frame is disposed assertFalse(createAccountPage.isVisible(), "La page de création de compte devrait être fermée après avoir cliqué sur 'Retour à la connexion'."); } }