forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from LowTL/JUnit-Testing
JUnit testing
- Loading branch information
Showing
4 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |