Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package Shopping; import org.junit.Test; import static org.junit.Assert.*; import org.junit.Before; import Shopping.EmptyCollectionException; import Shopping.ElementNotFoundException; public class ShoppingListArrayListTest { Grocery item1, item2; ShoppingListArrayList instance; /** * Initialize

package Shopping; import org.junit.Test; import static org.junit.Assert.*; import org.junit.Before; import Shopping.EmptyCollectionException; import Shopping.ElementNotFoundException; public class ShoppingListArrayListTest { Grocery item1, item2; ShoppingListArrayList instance; /** * Initialize instance and entries */ @Before public void setupTestCases() { item1 = new Grocery("Harry Potter", "book", 3, 15.5f, 2); item2 = new Grocery("Hunger Game", "book", 3, 35.5f, 1); //Create aa shopping list instance instance = new ShoppingListArrayList(); } /** * TODO Test of add method, of class ShoppingArray for * a case that the given argument object is null. * Test whether the add method works well when the * given argument object is null */ @Test public void testAdd1() { // Test adding a null entry reference to the shpping list instance.add(null); // TODO to test whether the size of shopping cart is correct after inserting a null object. instance.add(item1); assertEquals("An item is added incorrectly", 1, instance.size()); // Test adding a null entry reference to the shpping list instance.add(null); // TODO to test whether the size of shopping cart is correct after inserting a null object. } @Test public void testAdd2() { instance.add(item1); int oldQuantity = item1.getQuantity(); // TODO test the add method for the case of adding a new item (item1) into list instance // Be sure that 1) size is increased by 1 and // 2) the first item in the list // is the same as in the reference variable, item1 assertEquals(1, instance.size()); try{ assertEquals(0, item1.compareTo(instance.find(0))); assertEquals(oldQuantity, item1.getQuantity()); assertEquals(oldQuantity, instance.find(0).getQuantity()); } catch (Exception ex){ ex.printStackTrace(); } oldQuantity = item2.getQuantity(); instance.add(item2); assertEquals(2, instance.size()); try{ assertEquals(0, item2.compareTo(instance.find(1))); assertEquals(oldQuantity, item2.getQuantity()); assertEquals(oldQuantity, instance.find(1).getQuantity()); } catch (Exception ex){ ex.printStackTrace(); } } */ @Test public void testAdd3() { //Create grocery objects and a shopping list instance Grocery item3 = new Grocery(item1); Grocery item4 = new Grocery(item1); item4.setQuantity(3); int expQuantity = item3.getQuantity() + item4.getQuantity(); instance.add(item3); instance.add(item4); assertEquals(1, instance.size()); try{ assertEquals(0, item3.compareTo(instance.find(0))); assertEquals(0, item4.compareTo(instance.find(0))); assertEquals(expQuantity, item3.getQuantity()); assertEquals(expQuantity, instance.find(0).getQuantity()); } catch (Exception ex){ ex.printStackTrace(); } } /** * TODO Test of remove method, of class ShoppingArrayList * in the case of removing an existing element. */ @Test public void testRemove1() { instance.add(item1); instance.add(item2); assertEquals(2, instance.size()); boolean isRemoved = instance.remove(item1); // TODO test the remove method for an existing entry // Be sure that // 1) the returned value from the remove method is true // if the to-be-removed item exists in the array list // 2) the shopping list is decreased by 1 // 3) the item being removed can not be found in the shopping list } /** * Test of remove method, of class ShoppingArrayList * in the case of removing an existing element. */ @Test public void testRemove2() { instance.add(item1); instance.add(item2); assertEquals(2, instance.size()); boolean isRemoved = instance.remove(item1); assertEquals(true, isRemoved); assertEquals(1, instance.size()); isRemoved = instance.remove(item2); assertEquals(true, isRemoved); assertEquals(0, instance.size()); } /** * TODO Test of remove method, of class ShoppingArrayList * in the case of removing a non-existing element. */ @Test public void testRemove3() { instance.add(item1); instance.add(item2); int oriSize = instance.size(); Grocery item3 = new Grocery("King of Ring", "book", 3, 35.5f, 1); boolean isRemoved = instance.remove(item3); // TODO test the remove method for a non-existing entry // Be sure that // 1) the returned value from the remove method is false // 2) the size of the shopping list is not changed } /** * Test of find method, of class ShoppingArrayList * in the case that the given index is out of bounds. */ @Test(expected=IndexOutOfBoundsException.class) public void testFind1() throws Exception { // Add item1 into the shopping list instance.add(item1); instance.add(item2); assertEquals(2, instance.size()); instance.find(5); } @Test(expected=IndexOutOfBoundsException.class) public void testFind2() throws Exception { // Add item1 into the shopping list instance.add(item1); instance.add(item2); assertEquals(2, instance.size()); instance.find(-5); } @Test(expected=EmptyCollectionException.class) public void testFind3() throws Exception { // Test the case of finding a grocery object when the shopping list is empty // Be sure that // An EmptyCollectionException instance is thrown in the case instance.find(0); } /** * Test of find method, of class ShoppingArrayList. */ @Test public void testFind4() throws Exception{ // Add item1 into the shopping list instance.add(item1); instance.add(item2); assertTrue(2 == instance.size()); Grocery item = instance.find(0); assertEquals(0, item1.compareTo(item)); } /** * Test of indexOf method, of class ShoppingArrayList * in the case that the search target item does not exist. */ @Test(expected=ElementNotFoundException.class) public void testIndexOf1() throws Exception { // Check the indexOf method when the shopping list is empty int index = instance.indexOf(item1); } /** * Test of indexOf method, of class ShoppingArrayList. */ @Test(expected=ElementNotFoundException.class) public void testIndexOf2() throws Exception { // Add grocery items into the shopping list instance.add(item1); instance.add(item2); Grocery item3 = new Grocery("Aladin", "book", 3, 15.5f, 2); instance.indexOf(item3); } @Test public void testIndexOf3() throws Exception { instance.add(item1); instance.add(item2); int index = instance.indexOf(item2); assertEquals(1, index); index = instance.indexOf(item1); assertEquals(0, index); } @Test(expected=ElementNotFoundException.class) public void testIndexOf4() throws Exception { instance.add(item1); instance.add(item2); Grocery obj = null; instance.indexOf(obj); } @Test public void testContains1() { boolean isTrue = instance.contains(item1); assertEquals(false, isTrue); } @Test public void testContains2() { instance.add(item1); instance.add(item2); boolean isTrue = instance.contains(item2); assertEquals(true, isTrue); } @Test public void testContains3() { instance.add(item1); instance.add(item2); Grocery item3 = new Grocery("Aladin", "book", 3, 15.5f, 2); boolean isTrue = instance.contains(item3); assertEquals(false, isTrue); } @Test public void testContains4() { instance.add(item1); instance.add(item2); Grocery obj = null; boolean isTrue = instance.contains(obj); assertEquals(false, isTrue); } @Test public void testSize() { assertEquals(0, instance.size()); instance.add(item1); // Test increment assertEquals(1, instance.size()); assertTrue(instance.remove(item1)); // Test decrement assertEquals(0, instance.size()); } @Test public void testIsEmpty() { // Test empty assertEquals(true, instance.isEmpty()); instance.add(item1); // Test not empty assertEquals(false, instance.isEmpty()); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions

Question

4. Describe the factors that influence self-disclosure

Answered: 1 week ago

Question

1. Explain key aspects of interpersonal relationships

Answered: 1 week ago