Skip to content

Commit

Permalink
Merge pull request #264 from muhdbhz/bugfix-listinglabels
Browse files Browse the repository at this point in the history
ListingCard: Fix Ui to accomodate malicious input + update tests
  • Loading branch information
muhdbhz authored Nov 6, 2024
2 parents e4f2d97 + 6151b0c commit 9335e1f
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 27 deletions.
60 changes: 54 additions & 6 deletions src/main/java/seedu/address/ui/ListingCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ public ListingCard(Listing listing, int displayedIndex) {
}

private void initializeName() {
name.setText(listing.getName().fullName);
String actualName = listing.getName().fullName;

// Check if the listing name length is greater than 45
if (actualName.length() > 45) {
// Truncate the listing name to 45 characters and add "..."
name.setText(actualName.substring(0, 45) + "...");
} else {
name.setText(actualName);
}
}

private void initializeUnderline() {
Expand All @@ -66,11 +74,27 @@ private void initializeUnderline() {
}

private void initializePrice() {
price.setText(String.format("$%s", listing.getPrice().toString()));
String actualPrice = listing.getPrice().toString();

// Check if the price length is greater than 15
if (actualPrice.length() > 15) {
// Truncate the price to 15 characters and add "..."
actualPrice = actualPrice.substring(0, 15) + "...";
}

price.setText(String.format("$%s", actualPrice));
}

private void initializeArea() {
area.setText(String.format("%s m²", listing.getArea().toString()));
String actualArea = listing.getArea().toString();

// Check if the area length is greater than 10
if (actualArea.length() > 10) {
// Truncate the area to 10 characters and add "..."
actualArea = actualArea.substring(0, 10) + "...";
}

area.setText(String.format("%s m²", actualArea));
}

private void initializeRegion() {
Expand All @@ -82,11 +106,27 @@ private void initializeRegion() {
}

private void initializeAddress() {
address.setText(listing.getAddress().toString());
String actualAddress = listing.getAddress().toString();

// Check if the address length is greater than 55
if (actualAddress.length() > 55) {
// Truncate the address to 55 characters and add "..."
address.setText(actualAddress.substring(0, 55) + "...");
} else {
address.setText(actualAddress);
}
}

private void initializeSeller() {
seller.setText(listing.getSeller().getName().fullName);
String actualSeller = listing.getSeller().getName().fullName;

// Check if the seller name length is greater than 55
if (actualSeller.length() > 55) {
// Truncate the seller name to 55 characters and add "..."
actualSeller = actualSeller.substring(0, 55) + "...";
}

seller.setText(actualSeller);
}

private void initializeBuyers() {
Expand All @@ -96,7 +136,15 @@ private void initializeBuyers() {
listing.getBuyers().stream()
.sorted(Comparator.comparing(buyer -> buyer.getName().fullName))
.forEach(buyer -> {
Label buyerLabel = new Label(buyer.getName().fullName);
String actualName = buyer.getName().fullName;

// Check if the buyer name length is greater than 55
if (actualName.length() > 55) {
// Truncate the buyer name to 55 characters and add "..."
actualName = actualName.substring(0, 55) + "...";
}

Label buyerLabel = new Label(actualName);
buyers.getChildren().add(buyerLabel);
});
}
Expand Down
100 changes: 79 additions & 21 deletions src/test/java/seedu/address/ui/ListingCardUiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.math.BigDecimal;
import java.util.HashSet;
Expand Down Expand Up @@ -73,26 +72,6 @@ public void start(Stage stage) {
stage.show();
}

@Test
void listingCard_uiComponentsAreVisible() {
assertNotNull(listingCard.getName());
assertNotNull(listingCard.getPrice());
assertNotNull(listingCard.getArea());
assertNotNull(listingCard.getRegion());
assertNotNull(listingCard.getAddress());
assertNotNull(listingCard.getSeller());
assertNotNull(listingCard.getBuyers());

assertTrue(listingCard.getName().isVisible());
assertTrue(listingCard.getPrice().isVisible());
assertTrue(listingCard.getArea().isVisible());
assertTrue(listingCard.getRegion().isVisible());
assertTrue(listingCard.getAddress().isVisible());
assertTrue(listingCard.getSeller().isVisible());
assertTrue(listingCard.getBuyers().isVisible());
}


@Test
void listingCard_displayCorrectDetails() {
// Assert that the listing object is not null
Expand Down Expand Up @@ -120,6 +99,36 @@ void listingCard_displayCorrectDetails() {

}

@Test
void listingCard_displayTruncatedCorrectDetails() {
ListingCard listingCard = new ListingCard(createSampleTruncatedListing(), 1);

// Assert that the listing object is not null
assertNotNull(listingCard.listing);

// Check if the displayed name, price, area, region, address, and seller labels are correct
// after truncation
assertEquals("1. ", listingCard.getId().getText());
assertEquals("Sample Listingggggggggggggggggggggggggggggggg...",
listingCard.getName().getText());
assertEquals("$500000000000000...", listingCard.getPrice().getText());
assertEquals("1000000000... m²", listingCard.getArea().getText());
assertEquals("NORTH", listingCard.getRegion().getText());
assertEquals("123 Main Sttttttttttttttttttttttttttttttttttttttttttttt...",
listingCard.getAddress().getText());
assertEquals("John Sellerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr...",
listingCard.getSeller().getText());


// Check if the number of buyers is correctly displayed
FlowPane buyersFlowPane = listingCard.getBuyers();
assertEquals(1, buyersFlowPane.getChildren().size());

// Check if the buyer name is accurate (after truncation)
Label buyerOne = (Label) buyersFlowPane.getChildren().get(0);
assertEquals("John Buyerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr...", buyerOne.getText());
}

@Test
void listingCard_handlesDifferentIndexes() {
// Test ListingCard with various indexes
Expand Down Expand Up @@ -149,6 +158,25 @@ private Listing createSampleListing() {
);
}

/**
* Helper method to create a sample Listing object for testing (with truncation).
*/
private Listing createSampleTruncatedListing() {
Set<Person> buyersSet = new HashSet<>();
buyersSet.add(createSampleTruncatedBuyerOne());

return new Listing(
new Name("Sample Listinggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"),
new Address("123 Main Sttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"),
new Price("50000000000000000000000000000000000",
new BigDecimal("50000000000000000000000000000000000")),
new Area("100000000000000000000000000000000000000000000000000000"),
Region.NORTH,
createSampleTruncatedSeller(),
buyersSet
);
}

/**
* Helper method to create a sample Seller object for testing.
*/
Expand All @@ -165,6 +193,21 @@ private Person createSampleSeller() {
);
}

/**
* Helper method to create a sample Seller object for testing (with truncation).
*/
private Person createSampleTruncatedSeller() {
Set<Tag> tagSet = new HashSet<>();

return new Seller(
new Name("John Sellerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"),
new Phone("98765432"),
new Email("[email protected]"),
tagSet,
new Appointment(new Date("02-01-23"), new From("1100"), new To("1200"))
);
}

/**
* Helper method to create a sample Buyer object for testing.
*/
Expand All @@ -182,6 +225,21 @@ private Person createSampleBuyerOne() {
);
}

/**
* Helper method to create a sample Buyer object for testing (with truncation).
*/
private Person createSampleTruncatedBuyerOne() {
Set<Tag> tagSet = new HashSet<>();

return new Buyer(
new Name("John Buyerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"),
new Phone("91234567"),
new Email("[email protected]"),
tagSet,
new Appointment(new Date("01-01-23"), new From("10:00"), new To("11:00"))
);
}

/**
* Helper method to create a sample Buyer object for testing.
*/
Expand Down

0 comments on commit 9335e1f

Please sign in to comment.