From ff5c1308719afe141753c3235624872f30eccefb Mon Sep 17 00:00:00 2001 From: Alina Zacaria Date: Thu, 5 Dec 2024 16:28:45 -0600 Subject: [PATCH] test for create form --- .idea/inspectionProfiles/Project_Default.xml | 21 +++ pom.xml | 13 ++ src/main/project/pages/CreateForm.java | 47 ++++- .../project/pages/HealthClubHomePage.java | 1 - src/test/TestCreateForm.java | 141 ++++++++++++++ target/checkstyle-cachefile | 2 + target/checkstyle-checker.xml | 177 ++++++++++++++++++ target/checkstyle-result.xml | 3 + target/maven-archiver/pom.properties | 3 + 9 files changed, 403 insertions(+), 5 deletions(-) create mode 100644 target/checkstyle-cachefile create mode 100644 target/checkstyle-checker.xml create mode 100644 target/checkstyle-result.xml create mode 100644 target/maven-archiver/pom.properties diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 675fde4..f3da700 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -2,5 +2,26 @@ \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8cbfcd2..b2a42f0 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,19 @@ javafaker 1.0.2 + + junit + junit + 4.13.1 + compile + + + + org.mockito + mockito-core + 3.12.4 + compile + diff --git a/src/main/project/pages/CreateForm.java b/src/main/project/pages/CreateForm.java index 303a200..6022dd7 100644 --- a/src/main/project/pages/CreateForm.java +++ b/src/main/project/pages/CreateForm.java @@ -110,18 +110,57 @@ public int checkIfMemberExists(Member member, Connection conn) { public Long createUniqueID(Connection conn) { long id = 10000000000L; - try{ - while(true){ + try { + while (true) { String query = "SELECT * FROM members WHERE id_number=?"; PreparedStatement preparedStatement = conn.prepareStatement(query); preparedStatement.setLong(1, id); ResultSet rs = preparedStatement.executeQuery(); - if(!rs.next()) break; - else id = (long)(Math.random() * (99999999999L - 10000000000L)) + 10000000000L; + if (!rs.next()) break; + else id = (long) (Math.random() * (99999999999L - 10000000000L)) + 10000000000L; } } catch (Exception e) { e.printStackTrace(); } return id; } + + + +// Implement the getter methods + public JTextField getFirstNameField() { + return tfFirstName; + } + + public JTextField getLastNameField() { + return tfLastName; + } + + public JTextField getEmailField() { + return tfEmail; + } + + public JTextField getPhoneNumberField() { + return tfPhoneNumber; + } + + public JTextField getBdMonthField() { + return bdMonth; + } + + public JTextField getBdDayField() { + return bdDay; + } + + public JTextField getBdYearField() { + return bdYear; + } + + public JTextField getMembershipDurationField() { + return MembershipDuration; + } + + public JButton getCreateButton() { + return createButton; + } } diff --git a/src/main/project/pages/HealthClubHomePage.java b/src/main/project/pages/HealthClubHomePage.java index 2c8aefd..f4063ae 100644 --- a/src/main/project/pages/HealthClubHomePage.java +++ b/src/main/project/pages/HealthClubHomePage.java @@ -3,7 +3,6 @@ import main.project.modules.Guest; import main.project.modules.Member; import main.project.modules.Status; -import modules.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; diff --git a/src/test/TestCreateForm.java b/src/test/TestCreateForm.java index e69de29..89c17b6 100644 --- a/src/test/TestCreateForm.java +++ b/src/test/TestCreateForm.java @@ -0,0 +1,141 @@ +package test; + +import com.github.javafaker.Faker; +import main.project.modules.Member; +import main.project.pages.CreateForm; +import org.junit.Before; +import org.junit.Test; +import javax.swing.*; +import java.sql.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +public class TestCreateForm { + + private CreateForm createForm; + private Member memberMock; + private Connection connMock; + private Faker faker; + + private JTextField tfFirstName; + private JTextField tfLastName; + private JTextField tfEmail; + private JTextField tfPhoneNumber; + private JTextField tfBdMonth; + private JTextField tfBdDay; + private JTextField tfBdYear; + private JTextField tfMembershipDuration; + private JButton createButton; + + @Before + public void setUp() { + faker = new Faker(); + + tfFirstName = mock(JTextField.class); + tfLastName = mock(JTextField.class); + tfEmail = mock(JTextField.class); + tfPhoneNumber = mock(JTextField.class); + tfBdMonth = mock(JTextField.class); + tfBdDay = mock(JTextField.class); + tfBdYear = mock(JTextField.class); + tfMembershipDuration = mock(JTextField.class); + createButton = mock(JButton.class); + createForm = mock(CreateForm.class); + doReturn(tfFirstName).when(createForm).getFirstNameField(); + doReturn(tfLastName).when(createForm).getLastNameField(); + doReturn(tfEmail).when(createForm).getEmailField(); + doReturn(tfPhoneNumber).when(createForm).getPhoneNumberField(); + doReturn(tfBdMonth).when(createForm).getBdMonthField(); + doReturn(tfBdDay).when(createForm).getBdDayField(); + doReturn(tfBdYear).when(createForm).getBdYearField(); + doReturn(tfMembershipDuration).when(createForm).getMembershipDurationField(); + doReturn(createButton).when(createForm).getCreateButton(); + + connMock = mock(Connection.class); + } + + @Test + public void testCreateMember_Success() throws Exception { + String firstName = faker.name().firstName(); + String lastName = faker.name().lastName(); + String email = faker.internet().emailAddress(); + long phoneNumber = 4151234567L; + int membershipDuration = faker.number().numberBetween(6, 24); + + when(tfFirstName.getText()).thenReturn(firstName); + when(tfLastName.getText()).thenReturn(lastName); + when(tfEmail.getText()).thenReturn(email); + when(tfPhoneNumber.getText()).thenReturn(String.valueOf(phoneNumber)); + when(tfBdMonth.getText()).thenReturn("12"); + when(tfBdDay.getText()).thenReturn("15"); + when(tfBdYear.getText()).thenReturn("1990"); + when(tfMembershipDuration.getText()).thenReturn(String.valueOf(membershipDuration)); + + + Member member = new Member(firstName, lastName, email, faker.date().birthday().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate(), phoneNumber); + when(connMock.prepareStatement(anyString())).thenReturn(mock(PreparedStatement.class)); + + assertTrue(createForm.checkIfMemberExists(member, connMock) == 0); + } + + @Test + public void testCreateMember_EmailAlreadyExists() throws Exception { + String firstName = faker.name().firstName(); + String lastName = faker.name().lastName(); + String email = faker.internet().emailAddress(); + long phoneNumber = 6265552349L; + int membershipDuration = faker.number().numberBetween(6, 24); + + Member member = new Member(firstName, lastName, email, faker.date().birthday().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate(), phoneNumber); + when(createForm.checkIfMemberExists(any(), eq(connMock))).thenReturn(1); + + long res = createForm.createMember(member, membershipDuration); + assertEquals(0, res); + } + + @Test + public void testCreateMember_PhoneAlreadyExists() throws Exception { + String firstName = faker.name().firstName(); + String lastName = faker.name().lastName(); + String email = faker.internet().emailAddress(); + long phoneNumber = 8325678901L; + int membershipDuration = faker.number().numberBetween(6, 24); + + Member member = new Member(firstName, lastName, email, faker.date().birthday().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate(), phoneNumber); + when(createForm.checkIfMemberExists(any(), eq(connMock))).thenReturn(2); // 2 means phone number exists + + long res = createForm.createMember(member, membershipDuration); + assertEquals(0, res); + } + + @Test + public void testCreateMember_GenerateMetrics() throws Exception { + String firstName = faker.name().firstName(); + String lastName = faker.name().lastName(); + String email = faker.internet().emailAddress(); + long phoneNumber = 3214509876L; + int membershipDuration = faker.number().numberBetween(6, 24); + + Member member = new Member(firstName, lastName, email, faker.date().birthday().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate(), phoneNumber); + + long startTime = System.nanoTime(); + + when(createForm.createMember(any(), anyInt())).thenReturn(12345L); + long memberId = createForm.createMember(member, membershipDuration); + + long endTime = System.nanoTime(); + long execTime = endTime - startTime; + + Runtime runtime = Runtime.getRuntime(); + long memBefore = runtime.totalMemory() - runtime.freeMemory(); + + long memAfter = runtime.totalMemory() - runtime.freeMemory(); + long memUsage = memAfter - memBefore; + + assertEquals(12345L, memberId); + System.out.println("Execution Time: "+execTime+" ns"); + System.out.println("Memory Usage: "+memUsage+ " bytes"); + assertTrue("Execution time is too long", execTime < 1000000000L); // 1 second max + verify(createForm, times(1)).createMember(any(), anyInt()); + } +} diff --git a/target/checkstyle-cachefile b/target/checkstyle-cachefile new file mode 100644 index 0000000..7291acf --- /dev/null +++ b/target/checkstyle-cachefile @@ -0,0 +1,2 @@ +#Thu Dec 05 15:45:49 CST 2024 +configuration*?=E3A0464A86BDB5AD0445F393F4DB4760762F386E diff --git a/target/checkstyle-checker.xml b/target/checkstyle-checker.xml new file mode 100644 index 0000000..35826d1 --- /dev/null +++ b/target/checkstyle-checker.xml @@ -0,0 +1,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/target/checkstyle-result.xml b/target/checkstyle-result.xml new file mode 100644 index 0000000..7e142e4 --- /dev/null +++ b/target/checkstyle-result.xml @@ -0,0 +1,3 @@ + + + diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..e8b507f --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,3 @@ +artifactId=loc-counting +groupId=com.example +version=1.0-SNAPSHOT