/** Example of using unit tests for this assignment. To run them on the command line, make * sure that the junit-cs211.jar is in the same directory. * * On Mac/Linux: * javac -cp .:junit-cs211.jar *.java # compile everything * java -cp .:junit-cs211.jar P2tester # run tests * * On windows replace colons with semicolons: (: with ;) * demo$ javac -cp .;junit-cs211.jar *.java # compile everything * demo$ java -cp .;junit-cs211.jar P2tester # run tests */ import org.junit.*; import static org.junit.Assert.*; import java.util.*; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class P2tester { public static void main(String args[]){ org.junit.runner.JUnitCore.main("P2tester"); } static ByteArrayOutputStream localOut, localErr; static PrintStream sOut, sErr; @BeforeClass public static void setup() throws Exception { sOut = System.out; sErr = System.err; } @Test public void menudish_constructors_exist() { MenuDish dish = new MenuDish(); dish = new MenuDish("test"); assertEquals("MenuDish(String) constructor fails to initialize dish name", "test", dish.getName()); dish = new MenuDish("test",5.0,5); assertEquals("MenuDish(String,double,int) constructor fails to initialize dish name", "test", dish.getName()); assertEquals("MenuDish(String,double,int) constructor fails to initialize dish price", 5.0, dish.getPrice(),0.0001); assertEquals("MenuDish(String,double,int) constructor fails to initialize dish category", 5, dish.getCategory()); } private String[] test_names = { "Chicken wings", "Shrimp", "Broiled spiced platypus eggs", "Miso soup", "Chicken noodle", "Avacado pumpkin puree", "Chicken caesar salad", "Garden salad", "Kelp & octopus tentacle salad", "Mushroom burger", "Veggie burger", "Overclocked-CPU grilled burger", "Fetuccini alfredo", "Broiled salmon", "Turducken", "Ice cream", "Tiramisu", "Bowl of sugar", "Wine", "Iced tea", "Miruvor" }; private String[] test_descs = { "Baked Buffalo chicken glazed with spicy sauce", "Chilled and shelled, served with seafood sauce", "A spicy delicacy from down under", "Hot miso mixed with tofu and wakame", "Tastes like home", "Better than it sounds!", "Romain lettuce, grilled chicken and croutons, topped with Caesar sauce", "Garden-grown fresh, lettuce, spinach and tomatoes", "A tasty salad featuring some of the mysteries of the sea", "Savory 1/4lb angus burger topped with portabella and melted provalone", "Vegetable patty topped with lettuce, tomatoes, onions and pickles", "We use only the hottest-running CPUs to bring you a truly well-done burger", "Fettucini noodles cooked with the chef's special cheese sauce", "Live caught from underwater Martian water reserves", "A traditional feast consisting of a deboned turkey stuffed with a duck stuffed with a chicken", "Chilled in real Antarctic ice, comes in an assortment of flavors", "A rich layered desert made of creamy custard and expresso-soaked ladyfingers", "A huge bowl of leftover Halloween candy", "1636 vintage", "Sweetened or unsweetened", "An elven liquor with magical healing powers" }; private double[] test_prices = { 8.00, 8.00, 15.00, 4.21, 4.12, 4.01, 6.00, 6.00, 192.21, 8.00, 8.50, 9.95, 16.70, 20.50, 22.00, 6.50, 7.50, 0.99, 19.99, 2.50, 29292.92 }; private int[] test_cats = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 }; private boolean[] test_healths = { false, false, false, true, false, true, false, true, true, false, true, false, false, false, false, false, false, false, false, false, true }; private int[] test_spices = { 2, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3 }; private String[] test_full = { " Chicken wings** ($8.00)\n Baked Buffalo chicken glazed with spicy sauce", " Shrimp ($8.00)\n Chilled and shelled, served with seafood sauce", " Broiled spiced platypus eggs*** ($15.00)\n A spicy delicacy from down under", " Miso soup+ ($4.21)\n Hot miso mixed with tofu and wakame", " Chicken noodle ($4.12)\n Tastes like home", " Avacado pumpkin puree+ ($4.01)\n Better than it sounds!", " Chicken caesar salad ($6.00)\n Romain lettuce, grilled chicken and croutons, topped with Caesar sauce", " Garden salad+ ($6.00)\n Garden-grown fresh, lettuce, spinach and tomatoes", " Kelp & octopus tentacle salad+ ($192.21)\n A tasty salad featuring some of the mysteries of the sea", " Mushroom burger ($8.00)\n Savory 1/4lb angus burger topped with portabella and melted provalone", " Veggie burger+ ($8.50)\n Vegetable patty topped with lettuce, tomatoes, onions and pickles", " Overclocked-CPU grilled burger* ($9.95)\n We use only the hottest-running CPUs to bring you a truly well-done burger", " Fetuccini alfredo ($16.70)\n Fettucini noodles cooked with the chef's special cheese sauce", " Broiled salmon ($20.50)\n Live caught from underwater Martian water reserves", " Turducken ($22.00)\n A traditional feast consisting of a deboned turkey stuffed with a duck stuffed with a chicken", " Ice cream ($6.50)\n Chilled in real Antarctic ice, comes in an assortment of flavors", " Tiramisu ($7.50)\n A rich layered desert made of creamy custard and expresso-soaked ladyfingers", " Bowl of sugar ($0.99)\n A huge bowl of leftover Halloween candy", " Wine ($19.99)\n 1636 vintage", " Iced tea ($2.50)\n Sweetened or unsweetened", " Miruvor+*** ($29292.92)\n An elven liquor with magical healing powers" }; @Test public void menudish_name_getset() { MenuDish dish = new MenuDish(); for (int i=0; i < test_names.length; i++) { dish.setName( test_names[i] ); String s = dish.getName(); String err = "expected: " + test_names[i] + "\n" + "actual: " + s + "\n"; assertEquals( err, test_names[i], s ); } } @Test public void menudish_description_getset() { MenuDish dish = new MenuDish(); for (int i=0; i < test_descs.length; i++) { dish.setDescription( test_descs[i] ); String s = dish.getDescription(); String err = "expected: " + test_descs[i] + "\n" + "actual: " + s + "\n"; assertEquals( err, test_descs[i], s ); } } @Test public void menudish_price_getset() { MenuDish dish = new MenuDish(); for (int i=0; i < test_prices.length; i++) { dish.setPrice( test_prices[i] ); double p = dish.getPrice(); String err = "expected: " + test_prices[i] + "\n" + "actual: " + p + "\n"; assertEquals( err, test_prices[i], p, 0.0001 ); } } @Test public void menudish_price_neg() { MenuDish dish = new MenuDish(); dish.setPrice(-10.0); double p = dish.getPrice(); assertEquals(0.0, p, 0.0001); } @Test public void menudish_spiciness_getset() { MenuDish dish = new MenuDish(); for (int i=0; i < test_spices.length; i++) { dish.setSpiciness( test_spices[i] ); int p = dish.getSpiciness(); String err = "expected: " + test_spices[i] + "\n" + "actual: " + p + "\n"; assertEquals( err, test_spices[i], p ); } } @Test public void menudish_spiciness_bounds() { MenuDish dish = new MenuDish(); dish.setSpiciness(-3); int s = dish.getSpiciness(); String err = "unfilterd negative: " + s + "\n"; assertEquals(err, 0, s); dish.setSpiciness(4); s = dish.getSpiciness(); err = "unfilterd positive: " + s + "\n"; assertEquals(err, 0, s); } @Test public void menudish_category_getset() { MenuDish dish = new MenuDish(); for (int i=0; i < test_cats.length; i++) { dish.setCategory( test_cats[i] ); int p = dish.getCategory(); String err = "expected: " + test_cats[i] + "\n" + "actual: " + p + "\n"; assertEquals( err, test_cats[i], p ); } } @Test public void menudish_category_neg() { MenuDish dish = new MenuDish(); dish.setCategory(-3); int c = dish.getCategory(); String err = "unfilterd negative: " + c + "\n"; assertEquals(err, 0, c); } @Test public void menudish_healthy_getset() { MenuDish dish = new MenuDish(); for (int i=0; i < test_healths.length; i++) { dish.setHealthy( test_healths[i] ); boolean p = dish.isHealthy(); String err = "expected: " + test_healths[i] + "\n" + "actual: " + p + "\n"; assertEquals( err, test_healths[i], p ); } } @Test public void menudish_assert_setday() { MenuDish dish = new MenuDish(); dish.setDay(0); dish.setPrice(10.00); dish.setDay(5); double p = dish.getPrice(); String err = "unexpected price change: 10.00 to " + p + "\n"; assertEquals(err, 10.00, p, 0.0001 ); } @Test public void menudish_assert_avail() { MenuDish dish = new MenuDish(); boolean a = dish.available(); assertTrue("availability should always be true", a); } private void setChain(MenuDish dish, int i) { dish.setName(test_names[i]).setDescription(test_descs[i]).setPrice(test_prices[i]).setCategory(test_cats[i]).setHealthy(test_healths[i]).setSpiciness(test_spices[i]); } private void checkDish(MenuDish dish, int i) { String err; String name = dish.getName(); err = "expected: " + test_names[i] + "\n" + "actual: " + name + "\n"; assertEquals( err, test_names[i], name ); String desc = dish.getDescription(); err = "expected: " + test_descs[i] + "\n" + "actual: " + desc + "\n"; assertEquals( err, test_descs[i], desc ); double price = dish.getPrice(); err = "expected: " + test_prices[i] + "\n" + "actual: " + price + "\n"; assertEquals( err, test_prices[i], price, 0.0001 ); int cat = dish.getCategory(); err = "expected: " + test_cats[i] + "\n" + "actual: " + cat + "\n"; assertEquals( err, test_cats[i], cat ); int spice = dish.getSpiciness(); err = "expected: " + test_spices[i] + "\n" + "actual: " + spice + "\n"; assertEquals( err, test_spices[i], spice ); boolean health = dish.isHealthy(); err = "expected: " + test_healths[i] + "\n" + "actual: " + health + "\n"; assertEquals( err, test_healths[i], health ); } @Test public void menudish_chain_test() { MenuDish dish = new MenuDish(); for (int i = 0; i < test_names.length; i++) { setChain(dish, i); checkDish(dish, i); } } @Test public void menudish_equals_test() { MenuDish dish1 = new MenuDish(); MenuDish dish2 = new MenuDish(); for (int i = 0; i < test_names.length; i++) { setChain(dish1, i); setChain(dish2, i); String err = "equals method failed to recognize two equivalent dishes"; assertTrue( err, dish1.equals(dish2) ); } } @Test public void menudish_tostring() { MenuDish dish = new MenuDish(); for (int i = 0; i < test_names.length; i++) { setChain(dish, i); String s = dish.toString(); String err = "expected:\n" + test_full[i] + "\n" + "actual:\n" + s + "\n"; assertTrue(err, test_full[i].equals(s)); } } @Test public void adjmenudish_constructors_exist() { AdjustableMenuDish dish = new AdjustableMenuDish(); dish = new AdjustableMenuDish("test"); assertEquals("MenuDish(String) constructor fails to initialize dish name", "test", dish.getName()); dish = new AdjustableMenuDish("test",5.0,5); assertEquals("AdjustableMenuDish(String,double,int) constructor fails to initialize dish name", "test", dish.getName()); assertEquals("AdjustableMenuDish(String,double,int) constructor fails to initialize dish price", 5.0, dish.getPrice(),0.0001); assertEquals("AdjustableMenuDish(String,double,int) constructor fails to initialize dish category", 5, dish.getCategory()); } @Test public void adjmenudish_inherits() { AdjustableMenuDish dish = new AdjustableMenuDish(); assertTrue("AdjustableMenuDish not an instance of MenuDish", (dish instanceof MenuDish)); } @Test public void adjmenudish_daily_prices() { AdjustableMenuDish dish = new AdjustableMenuDish(); for (int i = MenuDish.MONDAY; i <= MenuDish.SUNDAY; i++) { double p = 1.0+i; double a = dish.setDay(i).setPrice( p ).getPrice(); assertEquals(p, a, 0.0001); } // second pass for (int i = MenuDish.MONDAY; i <= MenuDish.SUNDAY; i++) { double p = 1.0+i; double a = dish.setDay(i).getPrice(); assertEquals(p, a, 0.0001); } } @Test public void adjmenudish_daily_avail() { AdjustableMenuDish dish = new AdjustableMenuDish(); for (int i = MenuDish.MONDAY; i <= MenuDish.SUNDAY; i++) { boolean p = (i%2 == 0); dish.setDay(i); boolean a = dish.setAvailability( p ).available(); assertTrue("Availability does not match on day "+i, p==a); } // second pass for (int i = MenuDish.MONDAY; i <= MenuDish.SUNDAY; i++) { boolean p = (i%2 == 0); boolean a = dish.setDay(i).available(); assertTrue("Availability does not match on day "+i, p==a); } } @Test public void restaurantmenu_constructors_exist() { RestaurantMenu menu = new RestaurantMenu(); menu = new RestaurantMenu(15); } private void buildMenu(RestaurantMenu menu) { for (int i=0; i < test_names.length; i++) { MenuDish dish = new MenuDish(); setChain(dish, i); menu.add(dish); } } @Test public void restaurantmenu_sizings_check() { RestaurantMenu menu = new RestaurantMenu(); for (int i = 0; i < 12; i++) { int j = i+1; if (i >= 10) j = 10; MenuDish dish = new MenuDish("test" + i, 0.0, 1); menu.add(dish); assertEquals( j, menu.getNumCategory(1) ); } } private int getTotal(RestaurantMenu menu) { int sum = 0; for (int i=0; i < 7; i++) sum += menu.getNumCategory(i); return sum; } @Test public void restaurantmenu_remove_check() { RestaurantMenu menu = new RestaurantMenu(); for (int i = 0; i < 10; i++) { MenuDish dish = new MenuDish(); setChain(dish, i); menu.add(dish); assertEquals( (i+1), getTotal(menu) ); } for (int i = 0; i < 10; i++) { MenuDish dish = new MenuDish(); setChain(dish, i+10); menu.remove(dish); assertEquals( 10, getTotal(menu) ); } for (int i = 0; i < 9; i++) { MenuDish dish = new MenuDish(); setChain(dish, i); menu.remove(dish); assertEquals( (9-i), getTotal(menu) ); } for (int i = 0; i < 9; i++) { MenuDish dish = new MenuDish(); setChain(dish, (i+10)); menu.add(dish); assertEquals( (i+2), getTotal(menu) ); } } @Test public void restaurantmenu_numcategories() { RestaurantMenu menu = new RestaurantMenu(25); buildMenu(menu); for (int i=0; i < 7; i++) { int c = menu.getNumCategory( i ); String err = "expected: 3\nactual: " + c + "\n"; assertEquals( err, 3, c ); } } private void setCapture() { localOut = new ByteArrayOutputStream(); localErr = new ByteArrayOutputStream(); System.setOut(new PrintStream( localOut ) ); System.setErr(new PrintStream( localErr ) ); } private String getCapture() { return localOut.toString().replaceAll("\\r?\\n", "\n"); } private void unsetCapture() { System.setOut( null ); System.setOut( sOut ); System.setErr( null ); System.setErr( sErr ); } @Test public void restaurantmenu_printmenu_0() { RestaurantMenu menu = new RestaurantMenu(25); buildMenu(menu); setCapture(); menu.printMenu(); String actual = getCapture(); unsetCapture(); String expect = "Appetizers\n\n" + test_full[0] + "\n" + test_full[1] + "\n" + test_full[2] + "\n\n" + "Soups\n\n" + test_full[3] + "\n" + test_full[4] + "\n" + test_full[5] + "\n\n" + "Salads\n\n" + test_full[6] + "\n" + test_full[7] + "\n" + test_full[8] + "\n\n" + "Burgers & Sandwiches\n\n" + test_full[9] + "\n" + test_full[10] + "\n" + test_full[11] + "\n\n" + "Entrees\n\n" + test_full[12] + "\n" + test_full[13] + "\n" + test_full[14] + "\n\n" + "Deserts\n\n" + test_full[15] + "\n" + test_full[16] + "\n" + test_full[17] + "\n\n" + "Beverages\n\n" + test_full[18] + "\n" + test_full[19] + "\n" + test_full[20] + "\n\n"; assertEquals(expect, actual); } @Test public void restaurantmenu_printmenu_1() { RestaurantMenu menu = new RestaurantMenu(25); buildMenu(menu); setCapture(); menu.printMenu(new int[]{6,5}); String actual = getCapture(); unsetCapture(); String expect = "Beverages\n\n" + test_full[18] + "\n" + test_full[19] + "\n" + test_full[20] + "\n\n" + "Deserts\n\n" + test_full[15] + "\n" + test_full[16] + "\n" + test_full[17] + "\n\n" ; assertEquals(expect, actual); } private void setAvail(AdjustableMenuDish dish, boolean avail) { for (int i = 0; i < 7; i++) { dish.setDay(i); dish.setAvailability(avail); } } @Test public void restaurantmenu_count_unavailable() { RestaurantMenu menu = new RestaurantMenu(); for (int i = 0; i < 10; i++) { AdjustableMenuDish dish = new AdjustableMenuDish("test" + i); dish.setCategory(1).setDescription("test"); setAvail( dish, (i%2 == 0) ); menu.add( dish ); } int c = menu.getNumCategory(1); assertEquals( 5, c ); } @Test public void restaurantmenu_check_hide_unavailable() { RestaurantMenu menu = new RestaurantMenu(); for (int i = 0; i < 10; i++) { AdjustableMenuDish dish = new AdjustableMenuDish("test" + i); dish.setCategory(1).setDescription("test"); setAvail( dish, (i%2 == 0) ); menu.add( dish ); } menu.setDay(0); setCapture(); menu.printMenu(); String actual = getCapture(); unsetCapture(); String expect = "Soups\n\n test0 ($0.00)\n test\n test2 ($0.00)\n test\n test4 ($0.00)\n test\n test6 ($0.00)\n test\n test8 ($0.00)\n test\n\n"; assertEquals(expect, actual); } @Test public void resizeable_constructors_exist() { ResizeableMenu menu = new ResizeableMenu(); } @Test public void resizeable_inherits() { ResizeableMenu menu = new ResizeableMenu(); String err = "ResizeableMenu not an instance of RestaurantDish"; assertTrue( err, (menu instanceof RestaurantMenu) ); } @Test public void resizable_sizings_check() { ResizeableMenu menu = new ResizeableMenu(); for (int i = 0; i < 45; i++) { MenuDish dish = new MenuDish("test" + i,0.0,1); menu.add(dish); assertEquals( (i+1), menu.getNumCategory(1) ); } } }