From 023365e6539cec8310a11316cd2073e3e8fb3e0c Mon Sep 17 00:00:00 2001 From: azadehobenland Date: Fri, 6 Jan 2023 14:41:58 +0100 Subject: [PATCH] tests --- RobotAbgabe.iml | 13 ++++++++- src/RobotFactory.java | 21 -------------- src/{ => main/java/roboter}/C3PO.java | 5 ++-- src/{ => main/java/roboter}/Nexus6.java | 9 +++--- src/{ => main/java/roboter}/R2D2.java | 5 ++-- .../java}/roboter/Robot.java | 2 +- .../java}/roboter/RobotControl.java | 5 ++-- src/main/java/roboter/RobotFactory.java | 22 ++++++++++++++ .../java}/roboter/RobotInstructions.java | 8 ++--- src/main/java/roboter/Typ.java | 5 ++++ .../roboter/exceptions/RobotException.java | 2 +- .../RobotIllegalStateException.java | 6 ++-- .../exceptions/RobotMagicValueException.java | 2 +- src/test/java/roboter/ExceptionTest.java | 29 +++++++++++++++++++ src/test/java/roboter/FactoryTest.java | 29 +++++++++++++++++++ 15 files changed, 120 insertions(+), 43 deletions(-) delete mode 100644 src/RobotFactory.java rename src/{ => main/java/roboter}/C3PO.java (94%) rename src/{ => main/java/roboter}/Nexus6.java (82%) rename src/{ => main/java/roboter}/R2D2.java (94%) rename src/{tpe/exceptions => main/java}/roboter/Robot.java (87%) rename src/{tpe/exceptions => main/java}/roboter/RobotControl.java (96%) create mode 100644 src/main/java/roboter/RobotFactory.java rename src/{tpe/exceptions => main/java}/roboter/RobotInstructions.java (89%) create mode 100644 src/main/java/roboter/Typ.java rename src/{tpe/exceptions => main/java}/roboter/exceptions/RobotException.java (75%) rename src/{tpe/exceptions => main/java}/roboter/exceptions/RobotIllegalStateException.java (69%) rename src/{tpe/exceptions => main/java}/roboter/exceptions/RobotMagicValueException.java (50%) create mode 100644 src/test/java/roboter/ExceptionTest.java create mode 100644 src/test/java/roboter/FactoryTest.java diff --git a/RobotAbgabe.iml b/RobotAbgabe.iml index c90834f..850fc7d 100644 --- a/RobotAbgabe.iml +++ b/RobotAbgabe.iml @@ -3,9 +3,20 @@ - + + + + + + + + + + + + \ No newline at end of file diff --git a/src/RobotFactory.java b/src/RobotFactory.java deleted file mode 100644 index 052efd9..0000000 --- a/src/RobotFactory.java +++ /dev/null @@ -1,21 +0,0 @@ -import tpe.exceptions.roboter.Robot; - -public class RobotFactory { - private static int R2D2Id=0; - private static int C2POId=10000; - public static Robot getRobot(String typ){ - switch (typ){ - case "R2D2": - return new R2D2(R2D2Id++,"blah"); - - case "C3PO": - return new C3PO(C2POId++,"blah"); - - case "Nexus6": - return Nexus6.getInstance(); - - } - return null; - } - -} diff --git a/src/C3PO.java b/src/main/java/roboter/C3PO.java similarity index 94% rename from src/C3PO.java rename to src/main/java/roboter/C3PO.java index 54499b3..8927458 100644 --- a/src/C3PO.java +++ b/src/main/java/roboter/C3PO.java @@ -1,5 +1,6 @@ -import tpe.exceptions.roboter.Robot; -import tpe.exceptions.roboter.exceptions.RobotException; +package roboter; + +import roboter.exceptions.RobotException; import java.util.Arrays; import java.util.stream.Collectors; diff --git a/src/Nexus6.java b/src/main/java/roboter/Nexus6.java similarity index 82% rename from src/Nexus6.java rename to src/main/java/roboter/Nexus6.java index 66db374..dd1b97b 100644 --- a/src/Nexus6.java +++ b/src/main/java/roboter/Nexus6.java @@ -1,9 +1,8 @@ -import tpe.exceptions.roboter.Robot; -import tpe.exceptions.roboter.exceptions.RobotException; -import tpe.exceptions.roboter.exceptions.RobotIllegalStateException; +package roboter; -import java.util.Arrays; -import java.util.stream.Collectors; +import roboter.exceptions.RobotException; +import roboter.exceptions.RobotIllegalStateException; +import roboter.exceptions.RobotException; public class Nexus6 implements Robot { diff --git a/src/R2D2.java b/src/main/java/roboter/R2D2.java similarity index 94% rename from src/R2D2.java rename to src/main/java/roboter/R2D2.java index 104def9..41de9f8 100644 --- a/src/R2D2.java +++ b/src/main/java/roboter/R2D2.java @@ -1,5 +1,6 @@ -import tpe.exceptions.roboter.Robot; -import tpe.exceptions.roboter.exceptions.RobotException; +package roboter; + +import roboter.exceptions.RobotException; import java.util.Arrays; import java.util.stream.Collectors; diff --git a/src/tpe/exceptions/roboter/Robot.java b/src/main/java/roboter/Robot.java similarity index 87% rename from src/tpe/exceptions/roboter/Robot.java rename to src/main/java/roboter/Robot.java index 61069b4..57f3f28 100644 --- a/src/tpe/exceptions/roboter/Robot.java +++ b/src/main/java/roboter/Robot.java @@ -1,6 +1,6 @@ /* (c) 2012 Thomas Smits */ -package tpe.exceptions.roboter; +package roboter; /** * Interface für Roboter. diff --git a/src/tpe/exceptions/roboter/RobotControl.java b/src/main/java/roboter/RobotControl.java similarity index 96% rename from src/tpe/exceptions/roboter/RobotControl.java rename to src/main/java/roboter/RobotControl.java index 5e9ab20..651882d 100644 --- a/src/tpe/exceptions/roboter/RobotControl.java +++ b/src/main/java/roboter/RobotControl.java @@ -1,7 +1,8 @@ -package tpe.exceptions.roboter; +package roboter; -import tpe.exceptions.roboter.exceptions.RobotException; +import roboter.exceptions.RobotException; +import roboter.exceptions.RobotException; /** * Das Interface repräsentiert einen einfachen Roboter mit seinen Funktionen. diff --git a/src/main/java/roboter/RobotFactory.java b/src/main/java/roboter/RobotFactory.java new file mode 100644 index 0000000..5b2845c --- /dev/null +++ b/src/main/java/roboter/RobotFactory.java @@ -0,0 +1,22 @@ +package roboter; + +public class RobotFactory { + private static int R2D2Id=0; + private static int C2POId=10000; + + public static Robot getRobot(Typ typ,String name){ + switch (typ){ + case R2D2: + return new R2D2(R2D2Id++,name); + + case C3PO: + return new C3PO(C2POId++,name); + + case Nexus6: + return Nexus6.getInstance(); + + } + return null; + } + +} diff --git a/src/tpe/exceptions/roboter/RobotInstructions.java b/src/main/java/roboter/RobotInstructions.java similarity index 89% rename from src/tpe/exceptions/roboter/RobotInstructions.java rename to src/main/java/roboter/RobotInstructions.java index 4085249..374bec5 100644 --- a/src/tpe/exceptions/roboter/RobotInstructions.java +++ b/src/main/java/roboter/RobotInstructions.java @@ -1,9 +1,9 @@ -package tpe.exceptions.roboter; +package roboter; -import tpe.exceptions.roboter.exceptions.RobotException; -import tpe.exceptions.roboter.exceptions.RobotIllegalStateException; -import tpe.exceptions.roboter.exceptions.RobotMagicValueException; +import roboter.exceptions.RobotException; +import roboter.exceptions.RobotIllegalStateException; +import roboter.exceptions.RobotMagicValueException; /** * Das Interface repräsentiert den Befehlssatz eines einfachen Roboters. diff --git a/src/main/java/roboter/Typ.java b/src/main/java/roboter/Typ.java new file mode 100644 index 0000000..22df119 --- /dev/null +++ b/src/main/java/roboter/Typ.java @@ -0,0 +1,5 @@ +package roboter; + +public enum Typ { + R2D2,C3PO,Nexus6 +} diff --git a/src/tpe/exceptions/roboter/exceptions/RobotException.java b/src/main/java/roboter/exceptions/RobotException.java similarity index 75% rename from src/tpe/exceptions/roboter/exceptions/RobotException.java rename to src/main/java/roboter/exceptions/RobotException.java index df41be7..190cb72 100644 --- a/src/tpe/exceptions/roboter/exceptions/RobotException.java +++ b/src/main/java/roboter/exceptions/RobotException.java @@ -1,4 +1,4 @@ -package tpe.exceptions.roboter.exceptions; +package roboter.exceptions; public class RobotException extends Exception{ public RobotException(String errormessage) { diff --git a/src/tpe/exceptions/roboter/exceptions/RobotIllegalStateException.java b/src/main/java/roboter/exceptions/RobotIllegalStateException.java similarity index 69% rename from src/tpe/exceptions/roboter/exceptions/RobotIllegalStateException.java rename to src/main/java/roboter/exceptions/RobotIllegalStateException.java index f0e751c..9ab52ae 100644 --- a/src/tpe/exceptions/roboter/exceptions/RobotIllegalStateException.java +++ b/src/main/java/roboter/exceptions/RobotIllegalStateException.java @@ -1,7 +1,7 @@ -package tpe.exceptions.roboter.exceptions; - -import tpe.exceptions.roboter.Robot; +package roboter.exceptions; +import roboter.Robot; +import roboter.exceptions.*; public class RobotIllegalStateException extends RobotException { public RobotIllegalStateException(Robot roboter) { diff --git a/src/tpe/exceptions/roboter/exceptions/RobotMagicValueException.java b/src/main/java/roboter/exceptions/RobotMagicValueException.java similarity index 50% rename from src/tpe/exceptions/roboter/exceptions/RobotMagicValueException.java rename to src/main/java/roboter/exceptions/RobotMagicValueException.java index 922fdd0..6c47b7a 100644 --- a/src/tpe/exceptions/roboter/exceptions/RobotMagicValueException.java +++ b/src/main/java/roboter/exceptions/RobotMagicValueException.java @@ -1,4 +1,4 @@ -package tpe.exceptions.roboter.exceptions; +package roboter.exceptions; public class RobotMagicValueException { } diff --git a/src/test/java/roboter/ExceptionTest.java b/src/test/java/roboter/ExceptionTest.java new file mode 100644 index 0000000..4c1a8de --- /dev/null +++ b/src/test/java/roboter/ExceptionTest.java @@ -0,0 +1,29 @@ +package roboter; + +import org.junit.Assert; +import org.junit.Test; +import roboter.exceptions.RobotIllegalStateException; + +public class ExceptionTest { + @Test + public void testNexus6Speak(){ + Nexus6 robot = Nexus6.getInstance(); + Assert.assertThrows(RobotIllegalStateException.class,()->{ + robot.speak(new int[]{1,2,3,4}); + }); + Assert.assertThrows(RobotIllegalStateException.class,()->{ + robot.think(new int[]{1,2,3,4}); + }); + + + } + @Test + public void testNexus6Trigger(){ + Nexus6 robot = Nexus6.getInstance(); + boolean isPowerOn = robot.isPowerOn(); + robot.triggerPowerSwitch(); + Assert.assertEquals(isPowerOn, robot.isPowerOn()); + } + + +} diff --git a/src/test/java/roboter/FactoryTest.java b/src/test/java/roboter/FactoryTest.java new file mode 100644 index 0000000..cad4339 --- /dev/null +++ b/src/test/java/roboter/FactoryTest.java @@ -0,0 +1,29 @@ +package roboter; + +import org.junit.Assert; +import org.junit.Test; + +public class FactoryTest { + @Test + public void singletonNexus6(){ + Nexus6 roboter1 = Nexus6.getInstance(); + Nexus6 roboter2=Nexus6.getInstance(); + Assert.assertEquals(roboter1,roboter2); + } + @Test + public void factoryMethodTest(){ + Robot robot1=RobotFactory.getRobot(Typ.R2D2,"roboterX"); + Assert.assertTrue(robot1 instanceof R2D2); + Robot robot2=RobotFactory.getRobot(Typ.C3PO,"roboter2"); + Assert.assertTrue(robot2 instanceof C3PO); + + Assert.assertEquals(robot1.getName(),"roboterX"); + Assert.assertEquals(robot2.getName(),"roboter2"); + + + } + + + + +}