Skip to content

Commit

Permalink
test for create form
Browse files Browse the repository at this point in the history
  • Loading branch information
alina-z7 committed Dec 5, 2024
1 parent c4a2a9f commit ff5c130
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 5 deletions.
21 changes: 21 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
47 changes: 43 additions & 4 deletions src/main/project/pages/CreateForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 0 additions & 1 deletion src/main/project/pages/HealthClubHomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down
141 changes: 141 additions & 0 deletions src/test/TestCreateForm.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
2 changes: 2 additions & 0 deletions target/checkstyle-cachefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Thu Dec 05 15:45:49 CST 2024
configuration*?=E3A0464A86BDB5AD0445F393F4DB4760762F386E
Loading

0 comments on commit ff5c130

Please sign in to comment.