Skip to content

Commit

Permalink
Merge pull request #74 from LowTL/JUnit-Testing
Browse files Browse the repository at this point in the history
JUnit testing
  • Loading branch information
LowTL committed Mar 29, 2024
2 parents 39d9ede + de045a8 commit 51168ca
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/main/java/command/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package command;

import exceptions.CommandFormatException;
import item.Item;
import itemlist.Itemlist;
import parser.CommandType;
import storage.Storage;

public class DeleteCommand extends Command {

protected String itemName;

public DeleteCommand(String itemName) {
this.itemName = itemName;
this.itemName = itemName.toLowerCase(); //for checking later
}

public void execute() throws CommandFormatException {
public void execute() {
int index = -1;
for (Item item : Itemlist.getItems()) {
if (item.getItemName().toLowerCase().equals(itemName)) {
index = Itemlist.getItems().indexOf(item);
break;
try {
for (Item item : Itemlist.getItems()) {
if (item.getItemName().toLowerCase().equals(itemName)) {
index = Itemlist.getItems().indexOf(item);
break;
}
}
}
if (index == -1) {
//throw exception;
throw new CommandFormatException(CommandType.DEL);
} else {
Itemlist.deleteItem(index);
System.out.println(itemName + " has been successfully deleted.");
Storage.overwriteFile(Itemlist.getItems());
assert(!Itemlist.getItem(index).getItemName().equals(itemName));
if (index == -1) {
//throw exception;
System.out.println("Item does not exist.");
} else {
Itemlist.deleteItem(index);
System.out.println(itemName + " has been successfully deleted.");
Storage.overwriteFile(Itemlist.getItems());
assert(!Itemlist.getItem(index).getItemName().equals(itemName));

}
} catch (IndexOutOfBoundsException e) {
System.out.println("Itemlist is empty.");
}
}
}
24 changes: 24 additions & 0 deletions src/test/java/command/AddCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package command;

import exceptions.CommandFormatException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.fail;

public class AddCommandTest {

@Test
public void addItemTest() throws CommandFormatException {
try {
Command addCommandTest1 = new AddCommand("testItem", 1, "EA",
"NA", 1, 10);
Command addCommandTest2 = new AddCommand("testItem", 7, "EA",
"NA", 1, 10);
addCommandTest1.execute();
addCommandTest2.execute();
} catch (CommandFormatException e) {
fail("Unable to add item.");
}
}

}
27 changes: 27 additions & 0 deletions src/test/java/command/DeleteCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package command;

import exceptions.CommandFormatException;
import itemlist.Itemlist;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;

public class DeleteCommandTest {
Command addCommandTest1 = new AddCommand("testItem", 1, "EA",
"NA", 1, 10);
Command deleteCommand = new DeleteCommand("testItem");

@Test
public void delCommandTest_success() throws CommandFormatException {
try {
addCommandTest1.execute();
deleteCommand.execute();
assertFalse(Itemlist.itemIsExist("testItem"));
deleteCommand.execute();
} catch (CommandFormatException e) {
fail("Unable to delete.");
}

}
}
23 changes: 23 additions & 0 deletions src/test/java/command/FindCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package command;

import exceptions.CommandFormatException;
import itemlist.Itemlist;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;

public class FindCommandTest {

@Test
public void findCommandTest() throws CommandFormatException {
Command addCommandTest1 = new AddCommand("testItem", 1, "EA",
"NA", 1, 10);
Command findCommand = new FindCommand("testItem");
Command findCommand2 = new FindCommand("failFindCommand");

addCommandTest1.execute();
findCommand.execute();
findCommand2.execute();
assertFalse(Itemlist.itemIsExist("failFindCommand"));
}
}

0 comments on commit 51168ca

Please sign in to comment.