|
| 1 | + |
| 2 | +import fi.helsinki.cs.tmc.edutestutils.Points; |
| 3 | +import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils; |
| 4 | +import fi.helsinki.cs.tmc.edutestutils.Reflex; |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.Random; |
| 8 | +import org.junit.*; |
| 9 | +import static org.junit.Assert.*; |
| 10 | + |
| 11 | +public class HealthStationTest { |
| 12 | + |
| 13 | + HealthStation station; |
| 14 | + Person peter; |
| 15 | + Random rand = new Random(); |
| 16 | + |
| 17 | + Reflex.ClassRef<Object> klass; |
| 18 | + String klassName = "HealthStation"; |
| 19 | + |
| 20 | + @Before |
| 21 | + public void findClass() { |
| 22 | + klass = Reflex.reflect(klassName); |
| 23 | + } |
| 24 | + |
| 25 | + @Before |
| 26 | + public void setUp() { |
| 27 | + station = new HealthStation(); |
| 28 | + peter = new Person("Peter", 33, 175, 78); |
| 29 | + } |
| 30 | + |
| 31 | + @Points("05-09.1") |
| 32 | + @Test |
| 33 | + public void noExtraVariables1() { |
| 34 | + sanityCheck(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + @Points("05-09.1") |
| 39 | + public void canWeigh() { |
| 40 | + assertEquals("check the code: station = HealthStation(); " |
| 41 | + + "p = new Person(\"Peter\", 33, 175, 78); " |
| 42 | + + "System.out.println( station.weigh(p) )", peter.getWeight(), station.weigh(peter)); |
| 43 | + |
| 44 | + for (int i = 0; i < 5; i++) { |
| 45 | + int weight = 60 + rand.nextInt(60); |
| 46 | + Person ethan = new Person("Ethan", 45, 181, weight); |
| 47 | + |
| 48 | + assertEquals("check the code: station = HealthStation(); " |
| 49 | + + "p = new Person(\"Ethan\", 45, 181, " + weight + "); " |
| 50 | + + "System.out.println( station.weigh(p) )", ethan.getWeight(), station.weigh(ethan)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Points("05-09.2") |
| 55 | + @Test |
| 56 | + public void noExtraVariables2() { |
| 57 | + sanityCheck(); |
| 58 | + } |
| 59 | + |
| 60 | + @Points("05-09.2") |
| 61 | + @Test |
| 62 | + public void aMethodCalledFeedExists() throws Throwable { |
| 63 | + String method = "feed"; |
| 64 | + |
| 65 | + Person p = new Person("Peter", 20, 175, 85); |
| 66 | + HealthStation s = new HealthStation(); |
| 67 | + |
| 68 | + assertTrue("implement a method public void " + method + "(Person p) for the class " + klassName, |
| 69 | + klass.method(s, method).returningVoid().taking(Person.class).isPublic()); |
| 70 | + |
| 71 | + String e = "\nThe code that caused the error " |
| 72 | + + "s = new HealthStation; p = new Person(\"Peter\", 20, 175, 85); s.weigh(p);"; |
| 73 | + |
| 74 | + klass.method(s, method).returningVoid().taking(Person.class).withNiceError(e).invoke(p); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + @Points("05-09.2") |
| 79 | + public void canFeed() { |
| 80 | + int expected = peter.getWeight() + 1; |
| 81 | + feed(station, peter); |
| 82 | + |
| 83 | + assertEquals("Feeding should increase the weight of the person by one kilo. Check the code: \n" |
| 84 | + + "station = HealthStation(); " |
| 85 | + + "p = new Person(\"Peter\", 33, 175, 78); " |
| 86 | + + "station.feed(p); " |
| 87 | + + "System.out.println( p.getPaino() )", |
| 88 | + expected, peter.getWeight()); |
| 89 | + |
| 90 | + assertEquals("Feeding should increase the weight of the person by one kilo. Check the code: \n" |
| 91 | + + "station = HealthStation(); " |
| 92 | + + "p = new Person(\"Peter\", 33, 175, 78); " |
| 93 | + + "station.feed(p); " |
| 94 | + + "System.out.println( station.weigh(p) )", |
| 95 | + expected, station.weigh(peter)); |
| 96 | + |
| 97 | + feed(station, peter); |
| 98 | + feed(station, peter); |
| 99 | + feed(station, peter); |
| 100 | + feed(station, peter); |
| 101 | + |
| 102 | + assertEquals("Feeding should increase the weight of the person by one kilo. Check the code: \n" |
| 103 | + + "station = HealthStation(); " |
| 104 | + + "peter = new Person(\"Peter\", 33, 175, 78); " |
| 105 | + + "station.feed(peter); " |
| 106 | + + "station.feed(peter); " |
| 107 | + + "station.feed(peter); " |
| 108 | + + "station.feed(peter); " |
| 109 | + + "station.feed(peter); " |
| 110 | + + "System.out.println( peter.getPaino() )", |
| 111 | + expected + 4, peter.getWeight()); |
| 112 | + } |
| 113 | + |
| 114 | + @Points("05-09.3") |
| 115 | + @Test |
| 116 | + public void noExtraVariables3() { |
| 117 | + sanityCheck(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + @Points("05-09.3") |
| 122 | + public void aMethodCalledWeighingsExists() throws Throwable { |
| 123 | + String method = "weighings"; |
| 124 | + |
| 125 | + HealthStation s = new HealthStation(); |
| 126 | + |
| 127 | + assertTrue("implement a method public int " + method + "() for the class " + klassName, |
| 128 | + klass.method(s, method).returning(int.class).takingNoParams().isPublic()); |
| 129 | + |
| 130 | + String e = "\nThe code that caused the error: " |
| 131 | + + "s = new HealthStation; s.weighings();"; |
| 132 | + |
| 133 | + klass.method(s, method).returning(int.class).takingNoParams().withNiceError(e).invoke(); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + @Points("05-09.3") |
| 138 | + public void numberOfWeighingsInMemory() { |
| 139 | + assertEquals("Does the method weighings() work as intended? Initially no one has been weighed! Try out the code " |
| 140 | + + "station = HealthStation(); " |
| 141 | + + "System.out.println( station.weighings() )", |
| 142 | + 0, weighings(station)); |
| 143 | + |
| 144 | + station.weigh(peter); |
| 145 | + assertEquals("Does the method weighings() work as intended? The method should tell how many times the method weigh() has been called " |
| 146 | + + "Try out the code\n" |
| 147 | + + "station = HealthStation(); " |
| 148 | + + "p1 = new Person(\"Peter\", 33, 175, 78); " |
| 149 | + + "station.weigh(p1);" |
| 150 | + + "System.out.println( station.weighings() )", |
| 151 | + 1, weighings(station)); |
| 152 | + |
| 153 | + Person ethan = new Person("Ethan", 0, 52, 4); |
| 154 | + station.weigh(ethan); |
| 155 | + assertEquals("Does the method weighings() work as intended? The method should tell how many times the method weigh() has been called " |
| 156 | + + "Try out the code\n" |
| 157 | + + "station = HealthStation(); " |
| 158 | + + "p1 = new Person(\"Peter\", 33, 175, 78); " |
| 159 | + + "p2 = new Person(\"Ethan\", 0, 52, 4); " |
| 160 | + + "station.weigh(p1);" |
| 161 | + + "station.weigh(p2);" |
| 162 | + + "System.out.println( station.weighings() )", |
| 163 | + 2, weighings(station)); |
| 164 | + |
| 165 | + station.weigh(ethan); |
| 166 | + station.weigh(peter); |
| 167 | + station.weigh(peter); |
| 168 | + assertEquals("Does the method weighings() work as intended? The method should tell how many times the method weigh() has been called " |
| 169 | + + "Try out the code\n" |
| 170 | + + "station = HealthStation(); " |
| 171 | + + "p1 = new Person(\"Peter\", 33, 175, 78); " |
| 172 | + + "p2 = new Person(\"Ethan\", 0, 52, 4); " |
| 173 | + + "station.weigh(p1);" |
| 174 | + + "station.weigh(p2);" |
| 175 | + + "station.weigh(p2);" |
| 176 | + + "station.weigh(p1);" |
| 177 | + + "station.weigh(p1);" |
| 178 | + + "System.out.println( station.weighings() )", |
| 179 | + 5, weighings(station)); |
| 180 | + } |
| 181 | + |
| 182 | + String nameOfTheClass = "HealthStation"; |
| 183 | + |
| 184 | + private void feed(Object station, Person hlo) { |
| 185 | + try { |
| 186 | + Method feed = ReflectionUtils.requireMethod(HealthStation.class, "feed", Person.class); |
| 187 | + ReflectionUtils.invokeMethod(void.class, feed, station, hlo); |
| 188 | + } catch (Throwable t) { |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private int weighings(Object station) { |
| 193 | + try { |
| 194 | + Method weighings = ReflectionUtils.requireMethod(HealthStation.class, "weighings"); |
| 195 | + return ReflectionUtils.invokeMethod(int.class, weighings, station); |
| 196 | + } catch (Throwable t) { |
| 197 | + } |
| 198 | + return -1; |
| 199 | + } |
| 200 | + |
| 201 | + private void sanityCheck() throws SecurityException { |
| 202 | + String nameOfTheClass = "HealthStation"; |
| 203 | + |
| 204 | + Field[] fields = ReflectionUtils.findClass(nameOfTheClass).getDeclaredFields(); |
| 205 | + |
| 206 | + for (Field field : fields) { |
| 207 | + assertFalse("you don't need \"static variables\", delete " + fieldName(field.toString()), field.toString().contains("static") && !field.toString().contains("final")); |
| 208 | + assertTrue("visibility of every object variable of the class must be private, change " + fieldName(field.toString()), field.toString().contains("private")); |
| 209 | + } |
| 210 | + |
| 211 | + if (fields.length > 1) { |
| 212 | + int var = 0; |
| 213 | + for (Field field : fields) { |
| 214 | + if (!field.toString().contains("final")) { |
| 215 | + var++; |
| 216 | + } |
| 217 | + } |
| 218 | + assertTrue("The class " + nameOfTheClass + " only need the object variable that remembers the number of weighings. Delete unnecessary variables.", var < 2); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + private String fieldName(String toString) { |
| 223 | + return toString.replace(nameOfTheClass + ".", ""); |
| 224 | + } |
| 225 | +} |
0 commit comments