From 293143274aab2e669eb732691d99cb50a94a0db6 Mon Sep 17 00:00:00 2001 From: zlohbierdcul <82916072+zlohbierdcul@users.noreply.github.com> Date: Mon, 9 Jan 2023 22:09:11 +0100 Subject: [PATCH] small bug fix --- src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java | 3 +++ src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java | 3 +++ .../informatik/name/domain/starwars/StarWarsRobot.java | 3 +++ .../informatik/name/domain/starwars/SpeakTest.java | 6 ++++-- .../informatik/name/domain/starwars/ThinkTest.java | 4 ++-- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java index 6738601..1abf81a 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java @@ -34,6 +34,9 @@ public class C3PO extends StarWarsRobot { //A: RobotMagicValueException is a subclass of RobotException. So if a RobotException is thrown in a method, // the Method signature is already covered for any subclass of RobotException that is thrown inside the method public int[] think(int[] numbers) throws RobotException { + if (!this.isPowerOn()) { + throw new RobotIllegalStateException("The robot is not on!", this); + } int[] arrToSort = numbers.clone(); for (int k : arrToSort) { if (k == MAGIC_NUMBER) { diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java index 6d83930..15d2777 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java @@ -31,6 +31,9 @@ public class R2D2 extends StarWarsRobot { */ @Override public int[] think(int[] numbers) throws RobotException { + if (!this.isPowerOn()) { + throw new RobotIllegalStateException("The robot is not on!", this); + } int[] arrayToSort = numbers.clone(); for (int i = 0; i < arrayToSort.length; i++) { if (arrayToSort[i] == MAGIC_NUMBER) { diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java index 7b43d1b..37ac599 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java @@ -33,6 +33,9 @@ public abstract class StarWarsRobot extends RobotBluePrint { */ @Override public String speak(int[] numbers) throws RobotException { + if (!this.isPowerOn()) { + throw new RobotIllegalStateException("The robot is not on!", this); + } if (Arrays.stream(numbers).anyMatch(i -> i == MAGIC_NUMBER)) { throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this); } diff --git a/tests/de/hsmannheim/informatik/name/domain/starwars/SpeakTest.java b/tests/de/hsmannheim/informatik/name/domain/starwars/SpeakTest.java index 682e210..c3bae70 100644 --- a/tests/de/hsmannheim/informatik/name/domain/starwars/SpeakTest.java +++ b/tests/de/hsmannheim/informatik/name/domain/starwars/SpeakTest.java @@ -10,11 +10,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; class SpeakTest { + final R2D2 r2d2 = new R2D2(9999, "test"); + final C3PO c3po = new C3PO(19999, "test"); - private final R2D2 r2d2 = new R2D2(9999, "test"); - private final C3PO c3po = new C3PO(19999, "test"); SpeakTest() throws RobotIllegalStateException { + r2d2.triggerPowerSwitch(); + c3po.triggerPowerSwitch(); } @Test diff --git a/tests/de/hsmannheim/informatik/name/domain/starwars/ThinkTest.java b/tests/de/hsmannheim/informatik/name/domain/starwars/ThinkTest.java index 7fc0f2b..5b67cf0 100644 --- a/tests/de/hsmannheim/informatik/name/domain/starwars/ThinkTest.java +++ b/tests/de/hsmannheim/informatik/name/domain/starwars/ThinkTest.java @@ -14,11 +14,12 @@ class ThinkTest { private final C3PO c3po = new C3PO(19999, "test"); ThinkTest() throws RobotIllegalStateException { + r2d2.triggerPowerSwitch(); + c3po.triggerPowerSwitch(); } @Test void thinkTestStandard() throws RobotException { - assertArrayEquals(new int[]{0, 1, 2, 3, 4, 5}, r2d2.think(new int[]{3, 0, 1, 5, 2, 4})); } @@ -43,7 +44,6 @@ class ThinkTest { } @Test void thinkTestStandardC3PO() throws RobotException { - assertArrayEquals(new int[]{5, 4, 3, 2, 1, 0}, c3po.think(new int[]{3, 0, 1, 5, 2, 4})); }