From 7190a6a127cad6e3d665d31b68011408f0452af8 Mon Sep 17 00:00:00 2001 From: KDippold <2023962@stud.hs-mannheim.de> Date: Sun, 8 Jan 2023 16:51:04 +0100 Subject: [PATCH] Exceptions geben den Namen des Roboters wieder, SystemTest erweitert --- Roboterfabrik/src/facade/SystemTest.java | 16 +++++++++++++--- .../roboter/exceptions/RobotException.java | 8 +++++++- .../exceptions/RobotIllegalStateException.java | 8 +------- .../exceptions/RobotMagicValueException.java | 7 +------ Roboterfabrik/src/ui/Factory.java | 3 ++- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Roboterfabrik/src/facade/SystemTest.java b/Roboterfabrik/src/facade/SystemTest.java index 426ad73..7f67602 100644 --- a/Roboterfabrik/src/facade/SystemTest.java +++ b/Roboterfabrik/src/facade/SystemTest.java @@ -1,10 +1,17 @@ package facade; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; + import tpe.exceptions.roboter.exceptions.RobotException; +import tpe.exceptions.roboter.exceptions.RobotIllegalStateException; class SystemTest { @@ -37,6 +44,8 @@ class SystemTest { assertFalse(fs.zustandRoboter(id)); fs.schalterBetätigen(id); assertTrue(fs.zustandRoboter(id)); + fs.schalterBetätigen(id); + assertFalse(fs.zustandRoboter(id)); } @Test @@ -56,10 +65,11 @@ class SystemTest { void sprechenTest() throws RobotException { int id = fs.roboterAnlegen("Test", 1); int id2 = fs.roboterAnlegen("Test2", 2); - + int[] zahlen = { 5, 9, 1, 6 }; + Throwable exception = assertThrows(RobotIllegalStateException.class, () -> fs.sprechenAufruf(id, zahlen)); + assertEquals("Roboter ausgeschaltet! Bitte einschalten.", exception.getMessage()); fs.schalterBetätigen(id); fs.schalterBetätigen(id2); - int[] zahlen = { 5, 9, 1, 6 }; assertEquals("1, 5, 6, 9, ", fs.sprechenAufruf(id, zahlen)); assertEquals("9; 6; 5; 1; ", fs.sprechenAufruf(id2, zahlen)); diff --git a/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotException.java b/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotException.java index c1b17bd..d5f1162 100644 --- a/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotException.java +++ b/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotException.java @@ -6,8 +6,14 @@ public class RobotException extends Exception { * */ private static final long serialVersionUID = 1L; + String name; - public RobotException(String message) { + public RobotException(String message, String name) { super(message); + this.name = name; + } + + public String getRobotName() { + return this.name; } } diff --git a/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotIllegalStateException.java b/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotIllegalStateException.java index 41ccd5f..26aac48 100644 --- a/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotIllegalStateException.java +++ b/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotIllegalStateException.java @@ -5,14 +5,8 @@ public class RobotIllegalStateException extends RobotException { * */ private static final long serialVersionUID = 1L; - String name; public RobotIllegalStateException(String message, String name) { - super(message); - this.name = name; - } - - public String getRobotName() { - return this.name; + super(message, name); } } diff --git a/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotMagicValueException.java b/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotMagicValueException.java index c51f9ba..2ced3d6 100644 --- a/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotMagicValueException.java +++ b/Roboterfabrik/src/tpe/exceptions/roboter/exceptions/RobotMagicValueException.java @@ -6,14 +6,9 @@ public class RobotMagicValueException extends RobotException { * */ private static final long serialVersionUID = 1L; - String name; public RobotMagicValueException(String message, String name) { - super(message); - this.name = name; + super(message, name); } - public String getRobotName() { - return this.name; - } } diff --git a/Roboterfabrik/src/ui/Factory.java b/Roboterfabrik/src/ui/Factory.java index 3e6d3a9..3cb3af2 100644 --- a/Roboterfabrik/src/ui/Factory.java +++ b/Roboterfabrik/src/ui/Factory.java @@ -258,7 +258,8 @@ public class Factory { * @param id vom Roboter */ private void letzteFehlermeldung(int id) { - System.out.println(factorysystem.fehlerAuslesen(id)); + RobotException fehler = factorysystem.fehlerAuslesen(id); + System.out.println(fehler.getRobotName() + ": " + fehler.getMessage()); }