Richard Schmidt
11 months ago
7 changed files with 318 additions and 3 deletions
-
6src/main/java/CreateUser.java
-
21src/main/java/LoginGUI.java
-
26src/main/java/SignUpGUI.java
-
96src/test/java/CreateUserTest.java
-
52src/test/java/LoginGUITest.java
-
93src/test/java/SignUpGUITest.java
-
27user.json
@ -0,0 +1,96 @@ |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import org.junit.jupiter.api.Test; |
|||
import java.io.File; |
|||
import java.util.List; |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class CreateUserTest { |
|||
|
|||
@Test |
|||
public void testCreateUser() { |
|||
String id = "1"; |
|||
String userName = "testUser"; |
|||
String password = "password123"; |
|||
String birthday = "2000-01-01"; |
|||
String firstName = "John"; |
|||
String surname = "Doe"; |
|||
|
|||
CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); |
|||
|
|||
assertEquals(id, user.getId()); |
|||
assertEquals(userName, user.getUserName()); |
|||
assertEquals(birthday, user.getBirthday()); |
|||
assertEquals(firstName, user.getFirstName()); |
|||
assertEquals(surname, user.surname()); |
|||
|
|||
// Password should be hashed |
|||
assertNotEquals(password, user.getPassword()); |
|||
} |
|||
|
|||
|
|||
@Test |
|||
public void testSaveAndReadFromJsonFile() { |
|||
String filename = "test_users.json"; |
|||
String id = "2"; |
|||
String userName = "testUser2"; |
|||
String password = "password456"; |
|||
String birthday = "2001-01-01"; |
|||
String firstName = "Jane"; |
|||
String surname = "Doe"; |
|||
|
|||
// Create a user |
|||
CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); |
|||
|
|||
// Save user to JSON file |
|||
user.saveToJsonFile(filename); |
|||
|
|||
// Read users from JSON file |
|||
List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename); |
|||
|
|||
// Ensure that the user has been saved and can be read correctly |
|||
assertEquals(1, userList.size()); |
|||
CreateUser savedUser = userList.get(0); |
|||
assertEquals(id, savedUser.getId()); |
|||
assertEquals(userName, savedUser.getUserName()); |
|||
assertEquals(birthday, savedUser.getBirthday()); |
|||
assertEquals(firstName, savedUser.getFirstName()); |
|||
assertEquals(surname, savedUser.surname()); |
|||
|
|||
// Clean up - delete the test file after the test |
|||
File file = new File(filename); |
|||
file.delete(); |
|||
} |
|||
|
|||
@Test |
|||
public void testUpdateStayLoggedIn() { |
|||
String filename = "test_users.json"; |
|||
String id = "3"; |
|||
String userName = "testUser3"; |
|||
String password = "password789"; |
|||
String birthday = "2002-01-01"; |
|||
String firstName = "Jack"; |
|||
String surname = "Doe"; |
|||
|
|||
// Create a user |
|||
CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); |
|||
|
|||
// Save user to JSON file |
|||
user.saveToJsonFile(filename); |
|||
|
|||
// Update stayLoggedIn flag |
|||
CreateUser.updateStayLoggedIn(filename, userName, true); |
|||
|
|||
// Read updated user from JSON file |
|||
List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename); |
|||
|
|||
// Ensure that the stayLoggedIn flag is updated |
|||
assertTrue(userList.get(0).isStayLoggedIn()); |
|||
|
|||
// Clean up - delete the test file after the test |
|||
File file = new File(filename); |
|||
file.delete(); |
|||
} |
|||
} |
|||
|
|||
|
|||
|
@ -0,0 +1,52 @@ |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import org.junit.jupiter.api.Test; |
|||
import java.awt.event.ActionEvent; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
|
|||
class LoginGUITest { |
|||
|
|||
private LoginGUI loginGUI; |
|||
|
|||
@BeforeEach |
|||
public void setUp() { |
|||
loginGUI = new LoginGUI(); |
|||
} |
|||
|
|||
@Test |
|||
public void testInitialState() { |
|||
assertNotNull(loginGUI.getUsernameField()); |
|||
assertNotNull(loginGUI.getPasswordField()); |
|||
assertNotNull(loginGUI.getLoginButton()); |
|||
assertNotNull(loginGUI.getStayLoggedInCheckbox()); |
|||
assertNotNull(loginGUI.getSignUpButton()); |
|||
} |
|||
|
|||
@Test |
|||
public void testActionPerformed_LoginButton() { |
|||
LoginGUI loginGUI = new LoginGUI(); |
|||
loginGUI.getUsernameField().setText("testUser"); |
|||
loginGUI.getPasswordField().setText("testPassword"); |
|||
loginGUI.getStayLoggedInCheckbox().setSelected(true); |
|||
|
|||
// Simulate action performed event on login button |
|||
loginGUI.actionPerformed(new ActionEvent(loginGUI.getLoginButton(), ActionEvent.ACTION_PERFORMED, null)); |
|||
|
|||
// Verify if login was successful |
|||
assertFalse(loginGUI.isVisible()); |
|||
} |
|||
|
|||
@Test |
|||
public void testStayLoggedInCheckbox() { |
|||
// Initially, checkbox should be unchecked |
|||
assertFalse(loginGUI.getStayLoggedInCheckbox().isSelected()); |
|||
|
|||
// Simulate checking the checkbox |
|||
loginGUI.getStayLoggedInCheckbox().doClick(); |
|||
|
|||
assertTrue(loginGUI.getStayLoggedInCheckbox().isSelected()); |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
} |
@ -0,0 +1,93 @@ |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.api.AfterEach; |
|||
import org.junit.jupiter.api.BeforeEach; |
|||
|
|||
import java.awt.Component; |
|||
import java.awt.event.ActionEvent; |
|||
import javax.swing.*; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals; |
|||
import static org.junit.jupiter.api.Assertions.assertTrue; |
|||
|
|||
|
|||
class SignUpGUITest { |
|||
private SignUpGUI signUpGUI; |
|||
|
|||
@BeforeEach |
|||
void setUp() { |
|||
signUpGUI = new SignUpGUI(); |
|||
signUpGUI.setVisible(true); |
|||
} |
|||
|
|||
@AfterEach |
|||
void tearDown() { |
|||
signUpGUI.dispose(); |
|||
} |
|||
|
|||
@Test |
|||
public void testSignUpButtonActionPerformed() { |
|||
// Set up text fields |
|||
signUpGUI.getUsernameField().setText("testUser"); |
|||
signUpGUI.getPasswordField().setText("password"); |
|||
signUpGUI.getConfirmPasswordField().setText("password"); |
|||
signUpGUI.getBirthdayField().setText("1990-01-01"); |
|||
signUpGUI.getFirstNameField().setText("John"); |
|||
signUpGUI.getSurnameField().setText("Doe"); |
|||
|
|||
// Perform action |
|||
ActionEvent actionEvent = new ActionEvent(signUpGUI.getSignUpButton(), ActionEvent.ACTION_PERFORMED, ""); |
|||
signUpGUI.actionPerformed(actionEvent); |
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
@Test |
|||
void testPasswordMismatch() { |
|||
SignUpGUI signUpGUI = new SignUpGUI(); |
|||
signUpGUI.getUsernameField().setText("testuser"); |
|||
signUpGUI.getPasswordField().setText("password"); |
|||
signUpGUI.getConfirmPasswordField().setText("differentpassword"); |
|||
signUpGUI.getBirthdayField().setText("1990-01-01"); |
|||
signUpGUI.getFirstNameField().setText("John"); |
|||
signUpGUI.getSurnameField().setText("Doe"); |
|||
|
|||
signUpGUI.getSignUpButton().doClick(); // Simulate button click |
|||
|
|||
// Make sure an error message dialog is shown |
|||
assertTrue(isErrorMessageShown(signUpGUI)); |
|||
} |
|||
|
|||
@Test |
|||
void testExistingUsername() { |
|||
SignUpGUI signUpGUI = new SignUpGUI(); |
|||
signUpGUI.getUsernameField().setText("existinguser"); |
|||
signUpGUI.getPasswordField().setText("password"); |
|||
signUpGUI.getConfirmPasswordField().setText("password"); |
|||
signUpGUI.getBirthdayField().setText("1990-01-01"); |
|||
signUpGUI.getFirstNameField().setText("John"); |
|||
signUpGUI.getSurnameField().setText("Doe"); |
|||
|
|||
signUpGUI.getSignUpButton().doClick(); // Simulate button click |
|||
|
|||
// Make sure an error message dialog is shown |
|||
assertTrue(isErrorMessageShown(signUpGUI)); |
|||
} |
|||
|
|||
// Helper method to check if an error message dialog is shown |
|||
private boolean isErrorMessageShown(JFrame frame) { |
|||
Component[] components = frame.getComponents(); |
|||
for (Component component : components) { |
|||
if (component instanceof JOptionPane) { |
|||
return false; |
|||
} |
|||
|
|||
} |
|||
return true; |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue