|
| 1 | + |
| 2 | +import fi.helsinki.cs.tmc.edutestutils.MockStdio; |
| 3 | +import fi.helsinki.cs.tmc.edutestutils.Points; |
| 4 | +import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils; |
| 5 | +import fi.helsinki.cs.tmc.edutestutils.Reflex; |
| 6 | +import java.lang.reflect.Field; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.logging.Level; |
| 10 | +import java.util.logging.Logger; |
| 11 | +import org.junit.*; |
| 12 | +import static org.junit.Assert.*; |
| 13 | + |
| 14 | +public class MenuTest { |
| 15 | + |
| 16 | + @Rule |
| 17 | + public MockStdio stdio = new MockStdio(); |
| 18 | + |
| 19 | + @Test |
| 20 | + @Points("06-01.1") |
| 21 | + public void methodAddMealExists() throws Throwable { |
| 22 | + String klassName = "Menu"; |
| 23 | + |
| 24 | + String method = "addMeal"; |
| 25 | + |
| 26 | + Reflex.ClassRef<Object> productClass = Reflex.reflect(klassName); |
| 27 | + Object object = productClass.constructor().takingNoParams().invoke(); |
| 28 | + |
| 29 | + assertTrue("implement a method public void " + method + "(String meal) for the class " + klassName, productClass.method(object, method) |
| 30 | + .returningVoid().taking(String.class).isPublic()); |
| 31 | + |
| 32 | + String v = "\nThe code that caused the error: rl = new Menu(); rl.addMeal(\"Bratwurst\");"; |
| 33 | + |
| 34 | + productClass.method(object, method) |
| 35 | + .returningVoid().taking(String.class).withNiceError(v).invoke("Bratwurst"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + @Points("06-01.1") |
| 40 | + public void addMealAddsANewMeal() { |
| 41 | + Field mealsField = null; |
| 42 | + try { |
| 43 | + mealsField = Menu.class.getDeclaredField("meals"); |
| 44 | + } catch (NoSuchFieldException ex) { |
| 45 | + fail("Make sure that the class Menu has the attribute private ArrayList<String> meals;"); |
| 46 | + } |
| 47 | + |
| 48 | + Menu menu = new Menu(); |
| 49 | + mealsField.setAccessible(true); |
| 50 | + |
| 51 | + Method m = ReflectionUtils.requireMethod(Menu.class, "addMeal", String.class); |
| 52 | + |
| 53 | + try { |
| 54 | + ReflectionUtils.invokeMethod(void.class, m, menu, "first"); |
| 55 | + } catch (Throwable ex) { |
| 56 | + fail("Make sure that the methof addMeal is of type void, i.e. doesn't return a value."); |
| 57 | + } |
| 58 | + try { |
| 59 | + ArrayList<String> meals = (ArrayList<String>) mealsField.get(menu); |
| 60 | + if (meals.size() != 1) { |
| 61 | + fail("Calling the addMeal method of Menu should add a meal to the meals list."); |
| 62 | + } |
| 63 | + try { |
| 64 | + ReflectionUtils.invokeMethod(void.class, m, menu, "second"); |
| 65 | + } catch (Throwable ex) { |
| 66 | + Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex); |
| 67 | + |
| 68 | + } |
| 69 | + if (meals.size() != 2) { |
| 70 | + fail("When two meals with different names are added, there should be two meals on the list."); |
| 71 | + } |
| 72 | + } catch (IllegalArgumentException ex) { |
| 73 | + Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex); |
| 74 | + } catch (IllegalAccessException ex) { |
| 75 | + Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @Points("06-01.1") |
| 81 | + public void mealsWithSameNameAreOnlyAddedOnce() { |
| 82 | + Field mealsField = null; |
| 83 | + try { |
| 84 | + mealsField = Menu.class.getDeclaredField("meals"); |
| 85 | + } catch (NoSuchFieldException ex) { |
| 86 | + fail("Make sure that the class Menu has the attribute private ArrayList<String> meals;"); |
| 87 | + } |
| 88 | + |
| 89 | + mealsField.setAccessible(true); |
| 90 | + |
| 91 | + Menu menu = new Menu(); |
| 92 | + Method m = ReflectionUtils.requireMethod(Menu.class, "addMeal", String.class); |
| 93 | + try { |
| 94 | + ReflectionUtils.invokeMethod(void.class, m, menu, "first"); |
| 95 | + ReflectionUtils.invokeMethod(void.class, m, menu, "first"); |
| 96 | + } catch (Throwable ex) { |
| 97 | + Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex); |
| 98 | + } |
| 99 | + |
| 100 | + ArrayList<String> meals; |
| 101 | + try { |
| 102 | + meals = (ArrayList<String>) mealsField.get(menu); |
| 103 | + if (meals.size() != 1) { |
| 104 | + fail("A particular food must only appear once on the menu."); |
| 105 | + } |
| 106 | + } catch (IllegalArgumentException ex) { |
| 107 | + Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex); |
| 108 | + } catch (IllegalAccessException ex) { |
| 109 | + Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex); |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + @Points("06-01.2") |
| 116 | + public void methodPrintMealsExists() throws Throwable { |
| 117 | + String klassName = "Menu"; |
| 118 | + |
| 119 | + String method = "printMeals"; |
| 120 | + |
| 121 | + Reflex.ClassRef<Object> productClass = Reflex.reflect(klassName); |
| 122 | + Object object = productClass.constructor().takingNoParams().invoke(); |
| 123 | + |
| 124 | + assertTrue("implement a method public void " + method + "() for the class " + klassName, productClass.method(object, method) |
| 125 | + .returningVoid().takingNoParams().isPublic()); |
| 126 | + |
| 127 | + String v = "\nThe code that caused the error: rl = new Menu(); rl.printMeals();"; |
| 128 | + |
| 129 | + productClass.method(object, method) |
| 130 | + .returningVoid().takingNoParams().withNiceError(v).invoke(); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + @Points("06-01.2") |
| 135 | + public void printMealsPrintsTheMenu() { |
| 136 | + String carrotSoup = "Le carrot soup"; |
| 137 | + String porkStew = "Pork stew"; |
| 138 | + |
| 139 | + Menu menu = new Menu(); |
| 140 | + |
| 141 | + Method addMeal = ReflectionUtils.requireMethod(Menu.class, "addMeal", String.class); |
| 142 | + |
| 143 | + try { |
| 144 | + ReflectionUtils.invokeMethod(void.class, addMeal, menu, carrotSoup); |
| 145 | + ReflectionUtils.invokeMethod(void.class, addMeal, menu, porkStew); |
| 146 | + } catch (Throwable ex) { |
| 147 | + fail("Make sure that adding a meal works for a new menu."); |
| 148 | + } |
| 149 | + |
| 150 | + Method m = ReflectionUtils.requireMethod(Menu.class, "printMeals"); |
| 151 | + try { |
| 152 | + ReflectionUtils.invokeMethod(void.class, m, menu); |
| 153 | + } catch (Throwable ex) { |
| 154 | + fail("Make sure that printing the meals works when there are more than one meal."); |
| 155 | + } |
| 156 | + |
| 157 | + String out = stdio.getSysOut(); |
| 158 | + assertTrue("Printing the meals should print all the added meals.", out.contains(carrotSoup) && out.contains(porkStew)); |
| 159 | + assertTrue("Each meal should be printed on its own line. Currently the output is:" + out, out.split("\n").length > 1); |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + @Points("06-01.2") |
| 165 | + public void methodClearMenuExists() throws Throwable { |
| 166 | + String klassName = "Menu"; |
| 167 | + |
| 168 | + String method = "clearMenu"; |
| 169 | + |
| 170 | + Reflex.ClassRef<Object> productClass = Reflex.reflect(klassName); |
| 171 | + Object object = productClass.constructor().takingNoParams().invoke(); |
| 172 | + |
| 173 | + assertTrue("implement a method public void " + method + "() for the class " + klassName, productClass.method(object, method) |
| 174 | + .returningVoid().takingNoParams().isPublic()); |
| 175 | + |
| 176 | + String v = "\nThe code that caused the error: rl = new Menu(); rl.clearMenu();"; |
| 177 | + |
| 178 | + productClass.method(object, method) |
| 179 | + .returningVoid().takingNoParams().withNiceError(v).invoke(); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + @Points("06-01.3") |
| 184 | + public void clearingTheMenuClearsTheMenu() { |
| 185 | + Field mealsField = null; |
| 186 | + try { |
| 187 | + mealsField = Menu.class.getDeclaredField("meals"); |
| 188 | + } catch (NoSuchFieldException ex) { |
| 189 | + fail("Make sure that the class Menu has the attribute private ArrayList<String> meals;"); |
| 190 | + } |
| 191 | + |
| 192 | + mealsField.setAccessible(true); |
| 193 | + |
| 194 | + Menu menu = new Menu(); |
| 195 | + Method addMeal = ReflectionUtils.requireMethod(Menu.class, "addMeal", String.class); |
| 196 | + |
| 197 | + try { |
| 198 | + ReflectionUtils.invokeMethod(void.class, addMeal, menu, "first"); |
| 199 | + ReflectionUtils.invokeMethod(void.class, addMeal, menu, "second"); |
| 200 | + } catch (Throwable ex) { |
| 201 | + fail("Make sure that the method addMeal is of type void, i.e. doesn't return a value."); |
| 202 | + } |
| 203 | + |
| 204 | + Method clear = ReflectionUtils.requireMethod(Menu.class, "clearMenu"); |
| 205 | + try { |
| 206 | + ReflectionUtils.invokeMethod(void.class, clear, menu); |
| 207 | + } catch (Throwable ex) { |
| 208 | + fail("Make sure the the method clearMenu is of type void, i.e. doesn't return a value. Also, make sure it works as intended."); |
| 209 | + } |
| 210 | + |
| 211 | + try { |
| 212 | + ArrayList<String> meals = (ArrayList<String>) mealsField.get(menu); |
| 213 | + if (meals == null) { |
| 214 | + fail("Do not empty the menu by setting the value of the meals variable to null."); |
| 215 | + } |
| 216 | + |
| 217 | + if (!meals.isEmpty()) { |
| 218 | + fail("The menu should be empty after calling the method clearMenu."); |
| 219 | + } |
| 220 | + } catch (IllegalArgumentException ex) { |
| 221 | + Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex); |
| 222 | + } catch (IllegalAccessException ex) { |
| 223 | + Logger.getLogger(MenuTest.class.getName()).log(Level.SEVERE, null, ex); |
| 224 | + } |
| 225 | + |
| 226 | + Method m = ReflectionUtils.requireMethod(Menu.class, "printMeals"); |
| 227 | + try { |
| 228 | + ReflectionUtils.invokeMethod(void.class, m, menu); |
| 229 | + } catch (Throwable ex) { |
| 230 | + fail("Make sure that printing the meals works when there are more than one meal."); |
| 231 | + } |
| 232 | + |
| 233 | + String out = stdio.getSysOut(); |
| 234 | + out = out.trim(); |
| 235 | + if (!out.isEmpty()) { |
| 236 | + fail("There should be no output when printing an empty menu."); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments