diff --git a/app/build.gradle b/app/build.gradle index 81ee478a283b79fcd552956eb94d4ff12a064b46..8ab73472b842d810d0234e2d1f858268ab4c7045 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/Auth/Authenticate.java b/app/src/main/java/Auth/Authenticate.java index f5640306663f79d639b2bbd26b77e523845dc9bf..45be53bc16a8b3c64031822887f078c9a84f8472 100644 --- a/app/src/main/java/Auth/Authenticate.java +++ b/app/src/main/java/Auth/Authenticate.java @@ -1,23 +1,15 @@ -/* - * 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)) { diff --git a/app/src/main/java/brickbreaker/GameMapBuilder.java b/app/src/main/java/brickbreaker/GameMapBuilder.java index 8d59cc6b32f21b739e436229343a33f02d2841e7..d669932d02b3b5f3884dbc8c7f68ea1e9fa1310e 100644 --- a/app/src/main/java/brickbreaker/GameMapBuilder.java +++ b/app/src/main/java/brickbreaker/GameMapBuilder.java @@ -36,7 +36,6 @@ public class GameMapBuilder { } - // this draws the bricks public void draw(Graphics2D g) { for (int i = 0; i < map.length; i++) { diff --git a/app/src/main/java/brickbreaker/Interfaces/AuthenticationService.java b/app/src/main/java/brickbreaker/Interfaces/AuthenticationService.java new file mode 100644 index 0000000000000000000000000000000000000000..d20bf6f618346d40d104651904fa84a2be5e7d5e --- /dev/null +++ b/app/src/main/java/brickbreaker/Interfaces/AuthenticationService.java @@ -0,0 +1,6 @@ + +package brickbreaker.Interfaces; + +public interface AuthenticationService { + boolean login(String email, String password); +} \ No newline at end of file diff --git a/app/src/main/java/brickbreaker/LoginFrame.java b/app/src/main/java/brickbreaker/LoginFrame.java index 69fe41bd364433372ab2b1a1081bdfe50c688126..cdb0db2b99ee743fe192f97fcb18752d14518fc1 100644 --- a/app/src/main/java/brickbreaker/LoginFrame.java +++ b/app/src/main/java/brickbreaker/LoginFrame.java @@ -1,33 +1,28 @@ 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,34 +131,33 @@ 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(""); - - String email = emailField.getText(); - String password = new String(passwordField.getPassword()); - boolean error = false; - // Simple validation - if (email.isEmpty()) { - emailErrorLabel.setText("Email is required!"); - error = true; - } else if(!isValidEmail(email)) { - emailErrorLabel.setText("Invalid email format!"); - error = true; - } + emailErrorLabel.setText(""); + passwordErrorLabel.setText(""); + + String email = emailField.getText(); + String password = new String(passwordField.getPassword()); + boolean error = false; + // Simple validation + if (email.isEmpty()) { + emailErrorLabel.setText("Email is required!"); + error = true; + } else if(!isValidEmail(email)) { + emailErrorLabel.setText("Invalid email format!"); + error = true; + } - if (password.isEmpty()) { - passwordErrorLabel.setText("Password is required!"); - error = true; - } - - if(error) return; - Authenticate auth = new Authenticate(); - - if (auth.login(email, password)) { + if (password.isEmpty()) { + passwordErrorLabel.setText("Password is required!"); + error = true; + } + + 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(); + } } diff --git a/app/src/main/java/brickbreaker/Menu.java b/app/src/main/java/brickbreaker/Menu.java index 826c8fc025434366c6f3ae41207245e352f17a53..1e19ab7aa0a1f95bb4d3b35d79bdf1b4fb43ac13 100644 --- a/app/src/main/java/brickbreaker/Menu.java +++ b/app/src/main/java/brickbreaker/Menu.java @@ -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(); } diff --git a/app/src/test/java/brickbreaker/GameMapBuilderTest.java b/app/src/test/java/brickbreaker/GameMapBuilderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f2f60f227f9de4eb22e27858739a4066e2dd0156 --- /dev/null +++ b/app/src/test/java/brickbreaker/GameMapBuilderTest.java @@ -0,0 +1,37 @@ + +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()); + } + +} diff --git a/app/src/test/java/brickbreaker/LoginTest.java b/app/src/test/java/brickbreaker/LoginTest.java new file mode 100644 index 0000000000000000000000000000000000000000..276ba61fcb2cd0c585f0e3606cc3a42a5d43f63d --- /dev/null +++ b/app/src/test/java/brickbreaker/LoginTest.java @@ -0,0 +1,28 @@ + +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")); + } + +}