Skip to content
Snippets Groups Projects
Commit 8a60006d authored by abhatta5's avatar abhatta5
Browse files

Login Tests

parent 641c1816
No related branches found
No related tags found
No related merge requests found
......@@ -23,8 +23,9 @@ dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
implementation 'mysql:mysql-connector-java:8.0.28'
// Mockito
testImplementation 'org.mockito:mockito-core:3.10.0'
testImplementation 'org.easytesting:fest-assert-core:2.0M10'
}
......
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package Auth;
import brickbreaker.App;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import brickbreaker.Interfaces.AuthenticationService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class Authenticate {
public class Authenticate implements AuthenticationService {
public boolean login(String email, String password) {
try (Connection connection = DriverManager.getConnection(App.DB_URL, App.DB_USER, App.DB_PASSWORD)) {
......
......@@ -36,7 +36,6 @@ public class GameMapBuilder {
}
// this draws the bricks
public void draw(Graphics2D g) {
for (int i = 0; i < map.length; i++) {
......
package brickbreaker.Interfaces;
public interface AuthenticationService {
boolean login(String email, String password);
}
\ No newline at end of file
package brickbreaker;
import Auth.Authenticate;
import brickbreaker.Interfaces.AuthenticationService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoginFrame extends JPanel {
private JTextField emailField;
private JPasswordField passwordField;
private JButton submitButton;
private JButton backButton;
private JLabel headerLabel, usernameLabel, passwordLabel;
private JFrame parentFrame;
private Color buttonColor = new Color(36, 172, 226);
private Color hoverButtonColor = new Color(26, 152, 206);
private Font textFont = new Font("MV Boli", Font.PLAIN, 14);
private Font labelFont = new Font("MV Boli", Font.BOLD, 16);
private Font headerFont = new Font("MV Boli", Font.BOLD, 24);
private JLabel emailErrorLabel = new JLabel("");
private JLabel passwordErrorLabel = new JLabel("");
private Authenticate fileAuth = new Authenticate();
public JTextField emailField;
public JPasswordField passwordField;
public JButton submitButton;
public JButton backButton;
public JLabel headerLabel, usernameLabel, passwordLabel;
public JFrame parentFrame;
public Color buttonColor = new Color(36, 172, 226);
public Color hoverButtonColor = new Color(26, 152, 206);
public Font textFont = new Font("MV Boli", Font.PLAIN, 14);
public Font labelFont = new Font("MV Boli", Font.BOLD, 16);
public Font headerFont = new Font("MV Boli", Font.BOLD, 24);
public JLabel emailErrorLabel = new JLabel("");
public JLabel passwordErrorLabel = new JLabel("");
public AuthenticationService auth;
public LoginFrame(JFrame parentFrame) {
this.parentFrame = parentFrame;
initComponents();
......@@ -136,7 +131,7 @@ public class LoginFrame extends JPanel {
setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
}
private void handleLogin() {
public void handleLogin() {
// Reset error labels
emailErrorLabel.setText("");
passwordErrorLabel.setText("");
......@@ -158,12 +153,11 @@ public class LoginFrame extends JPanel {
error = true;
}
if(error) return;
Authenticate auth = new Authenticate();
if (auth.login(email, password)) {
if (!error && auth.login(email, password)) {
App.scoreSystem.setHighScore();
parentFrame.remove(this);
if(App.gamePanel == null)
App.createGame();
App.gamePanel.show();
} else {
JOptionPane.showMessageDialog(this, "Invalid username or password!", "Error", JOptionPane.ERROR_MESSAGE);
......@@ -181,5 +175,25 @@ public class LoginFrame extends JPanel {
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
public void setAuthenticateInstance(AuthenticationService auth) {
this.auth = auth;
}
public void setEmailFieldText(String text) {
emailField.setText(text);
}
public void setPasswordFieldText(String text) {
passwordField.setText(text);
}
public String getEmailErrorLabelText() {
return emailErrorLabel.getText();
}
public String getPasswordErrorLabelText() {
return passwordErrorLabel.getText();
}
}
......@@ -4,6 +4,8 @@
*/
package brickbreaker;
import Auth.Authenticate;
import brickbreaker.Interfaces.AuthenticationService;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
......@@ -215,13 +217,13 @@ public class Menu extends JPanel {
}
private void navigateToLogin() {
// Remove current panel
frame.remove(this);
LoginFrame loginPage = new LoginFrame(frame);
frame.add(loginPage);
// Refresh and repaint
AuthenticationService auth = new Authenticate();
loginPage.setAuthenticateInstance(auth);
frame.revalidate();
frame.repaint();
}
......
package brickbreaker;
import brickbreaker.GameComponents.Brick;
import brickbreaker.Interfaces.BrickFactory;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class GameMapBuilderTest {
private BrickFactory brickFactory;
private GameMapBuilder gameMapBuilder;
@BeforeEach
public void setUp() {
// Initialize the BrickFactory mock
brickFactory = mock(BrickFactory.class);
gameMapBuilder = new GameMapBuilder(3, 4, brickFactory);
}
@Test
public void testMapInitialization() {
// Define your test case
Brick[][] map = gameMapBuilder.map;
// Ensure that the map dimensions match the constructor parameters
assertEquals(3, map.length);
assertEquals(4, map[0].length);
// Verify that the brickFactory.createBrick method was called appropriately
verify(brickFactory, atLeastOnce()).createBrick(anyInt(), anyInt(), anyInt(), anyInt(), anyBoolean());
}
}
package brickbreaker;
import static org.junit.jupiter.api.Assertions.*;
import javax.swing.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class LoginTest {
private LoginFrame loginFrame;
private JFrame parentFrame;
@BeforeEach
public void setUp() {
parentFrame = new JFrame();
loginFrame = new LoginFrame(parentFrame);
}
@Test
public void testIsValidEmail() {
assertTrue(LoginFrame.isValidEmail("valid@example.com"));
assertFalse(LoginFrame.isValidEmail("invalid-email"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment