Browse Source

Merge branch 'main' of https://gitlab.cs.hs-fulda.de/fdai7332/java-chat

remotes/origin/server
Paul Kattenborn 11 months ago
parent
commit
b6d11991a5
  1. 6
      src/main/java/CreateUser.java
  2. 21
      src/main/java/LoginGUI.java
  3. 26
      src/main/java/SignUpGUI.java
  4. 96
      src/test/java/CreateUserTest.java
  5. 52
      src/test/java/LoginGUITest.java
  6. 93
      src/test/java/SignUpGUITest.java
  7. 27
      user.json

6
src/main/java/CreateUser.java

@ -77,11 +77,11 @@ class CreateUser {
} }
public String surname() { public String surname() {
return firstName;
return surname;
} }
public void surname(String firstName) {
this.firstName = firstName;
public void setSurname(String surname) {
this.surname = surname;
} }
public boolean isStayLoggedIn() { public boolean isStayLoggedIn() {

21
src/main/java/LoginGUI.java

@ -14,7 +14,28 @@ public class LoginGUI extends JFrame implements ActionListener {
private JButton loginButton; private JButton loginButton;
private JCheckBox stayLoggedInCheckbox; private JCheckBox stayLoggedInCheckbox;
private JButton signUpButton; private JButton signUpButton;
public JTextField getUsernameField() {
return usernameField;
}
public JPasswordField getPasswordField() {
return passwordField;
}
public JButton getLoginButton() {
return loginButton;
}
public JCheckBox getStayLoggedInCheckbox() {
return stayLoggedInCheckbox;
}
public JButton getSignUpButton() {
return signUpButton;
}
public LoginGUI() { public LoginGUI() {
setTitle("Login"); setTitle("Login");
setSize(300, 220); setSize(300, 220);

26
src/main/java/SignUpGUI.java

@ -7,6 +7,32 @@ import java.util.UUID;
public class SignUpGUI extends JFrame implements ActionListener { public class SignUpGUI extends JFrame implements ActionListener {
private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField; private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField;
private JButton signUpButton; private JButton signUpButton;
public JTextField getUsernameField() {
return usernameField;
}
public JTextField getPasswordField() {
return passwordField;
}
public JTextField getConfirmPasswordField() {
return confirmPasswordField;
}
public JTextField getBirthdayField() {
return birthdayField;
}
public JTextField getFirstNameField() {
return firstNameField;
}
public JTextField getSurnameField() {
return surnameField;
}
public JButton getSignUpButton() {
return signUpButton;
}
public SignUpGUI() { public SignUpGUI() {
setTitle("Sign Up"); setTitle("Sign Up");

96
src/test/java/CreateUserTest.java

@ -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();
}
}

52
src/test/java/LoginGUITest.java

@ -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());
}
}

93
src/test/java/SignUpGUITest.java

@ -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;
}
}

27
user.json

@ -16,5 +16,32 @@
"firstName": "", "firstName": "",
"surname": "", "surname": "",
"stayLoggedIn": false "stayLoggedIn": false
},
{
"id": "ccfcc294-48ad-49db-996f-d7c99a93bac9",
"userName": "testuser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "2000-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
},
{
"id": "27ffc70f-dd76-4765-81e3-76fde9b618e5",
"userName": "testUser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "1990-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
},
{
"id": "731fda82-6b51-4f3f-ae97-c9b01c9b4a1a",
"userName": "existinguser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "1990-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
} }
] ]
Loading…
Cancel
Save