Skip to content

Commit f95f76d

Browse files
authored
Add a Reflection proxy utility class (exercism#2053)
* Add a Reflexion proxy The proxy encapsulates the target class and helps checking if the methods were implemented correctly * Add changes from PR review * Group unused methods with region * Move unused constructor to "unused" region * Set constructor as protected * Fix code style constraints
1 parent acfdd2b commit f95f76d

File tree

3 files changed

+587
-1
lines changed

3 files changed

+587
-1
lines changed

exercises/concept/lasagna/src/test/java/LasagnaTest.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,65 @@
11
import org.junit.Test;
22
import org.junit.Ignore;
33

4+
import utils.Lasagna;
5+
46
import static org.assertj.core.api.Assertions.*;
57

68
public class LasagnaTest {
7-
9+
10+
@Test
11+
public void implemented_expected_minutes_in_oven() {
12+
assertThat(new Lasagna().hasMethod("expectedMinutesInOven"))
13+
.withFailMessage("Method expectedMinutesInOven must be created")
14+
.isTrue();
15+
assertThat(new Lasagna().isMethodPublic("expectedMinutesInOven"))
16+
.withFailMessage("Method expectedMinutesInOven must be public")
17+
.isTrue();
18+
assertThat(new Lasagna().isMethodReturnType(int.class, "expectedMinutesInOven"))
19+
.withFailMessage("Method expectedMinutesInOven must return an int")
20+
.isTrue();
21+
}
22+
23+
@Ignore("Remove to run test")
824
@Test
925
public void expected_minutes_in_oven() {
1026
assertThat(new Lasagna().expectedMinutesInOven()).isEqualTo(40);
1127
}
1228

29+
@Ignore("Remove to run test")
30+
@Test
31+
public void implemented_remaining_minutes_in_oven() {
32+
assertThat(new Lasagna().hasMethod("remainingMinutesInOven", int.class))
33+
.withFailMessage("Method remainingMinutesInOven must be created")
34+
.isTrue();
35+
assertThat(new Lasagna().isMethodPublic("remainingMinutesInOven", int.class))
36+
.withFailMessage("Method remainingMinutesInOven must be public")
37+
.isTrue();
38+
assertThat(new Lasagna().isMethodReturnType(int.class, "remainingMinutesInOven", int.class))
39+
.withFailMessage("Method remainingMinutesInOven must return an int")
40+
.isTrue();
41+
}
42+
1343
@Ignore("Remove to run test")
1444
@Test
1545
public void remaining_minutes_in_oven() {
1646
assertThat(new Lasagna().remainingMinutesInOven(25)).isEqualTo(15);
1747
}
1848

49+
@Ignore("Remove to run test")
50+
@Test
51+
public void implemented_preparation_time_in_minutes() {
52+
assertThat(new Lasagna().hasMethod("preparationTimeInMinutes", int.class))
53+
.withFailMessage("Method preparationTimeInMinutes must be created")
54+
.isTrue();
55+
assertThat(new Lasagna().isMethodPublic("preparationTimeInMinutes", int.class))
56+
.withFailMessage("Method preparationTimeInMinutes must be public")
57+
.isTrue();
58+
assertThat(new Lasagna().isMethodReturnType(int.class, "preparationTimeInMinutes", int.class))
59+
.withFailMessage("Method preparationTimeInMinutes must return an int")
60+
.isTrue();
61+
}
62+
1963
@Ignore("Remove to run test")
2064
@Test
2165
public void preparation_time_in_minutes_for_one_layer() {
@@ -28,6 +72,20 @@ public void preparation_time_in_minutes_for_multiple_layers() {
2872
assertThat(new Lasagna().preparationTimeInMinutes(4)).isEqualTo(8);
2973
}
3074

75+
@Ignore("Remove to run test")
76+
@Test
77+
public void implemented_total_time_in_minutes() {
78+
assertThat(new Lasagna().hasMethod("totalTimeInMinutes", int.class, int.class))
79+
.withFailMessage("Method totalTimeInMinutes must be created")
80+
.isTrue();
81+
assertThat(new Lasagna().isMethodPublic("totalTimeInMinutes", int.class, int.class))
82+
.withFailMessage("Method totalTimeInMinutes must be public")
83+
.isTrue();
84+
assertThat(new Lasagna().isMethodReturnType(int.class, "totalTimeInMinutes", int.class, int.class))
85+
.withFailMessage("Method totalTimeInMinutes must return an int")
86+
.isTrue();
87+
}
88+
3189
@Ignore("Remove to run test")
3290
@Test
3391
public void total_time_in_minutes_for_one_layer() {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package utils;
2+
3+
public class Lasagna extends ReflectionProxy {
4+
5+
@Override
6+
public String getTargetClassName() {
7+
return "Lasagna";
8+
}
9+
10+
public int expectedMinutesInOven() {
11+
try {
12+
return invokeMethod("expectedMinutesInOven", new Class[]{});
13+
} catch (Exception e) {
14+
throw new UnsupportedOperationException("Please implement the expectedMinutesInOven() method");
15+
}
16+
}
17+
18+
public int remainingMinutesInOven(int actualMinutes) {
19+
try {
20+
return invokeMethod("remainingMinutesInOven", new Class[]{int.class}, actualMinutes);
21+
} catch (Exception e) {
22+
throw new UnsupportedOperationException("Please implement the remainingMinutesInOven(int) method");
23+
}
24+
}
25+
26+
public int preparationTimeInMinutes(int amountLayers) {
27+
try {
28+
return invokeMethod("preparationTimeInMinutes", new Class[]{int.class}, amountLayers);
29+
} catch (Exception e) {
30+
throw new UnsupportedOperationException("Please implement the preparationTimeInMinutes(int) method");
31+
}
32+
}
33+
34+
public int totalTimeInMinutes(int amountLayers, int actualMinutes) {
35+
try {
36+
return invokeMethod("totalTimeInMinutes", new Class[]{int.class, int.class}, amountLayers, actualMinutes);
37+
} catch (Exception e) {
38+
throw new UnsupportedOperationException("Please implement the totalTimeInMinutes(int, int) method");
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)