From d28c82abdcc448d6fb4160ecad4ff7c07e1774b3 Mon Sep 17 00:00:00 2001 From: roman Date: Wed, 28 Dec 2022 15:37:55 +0100 Subject: [PATCH] added speak and id test coverage for C3PO, added name handling for C3PO --- .../name/domain/RobotBluePrint.java | 5 +- .../informatik/name/domain/RobotFactory.java | 24 +++--- .../requirements/RobotInstructions.java | 8 +- .../informatik/name/domain/starwars/C3PO.java | 14 ++-- .../informatik/name/domain/starwars/R2D2.java | 5 +- tests/R2D2IDTest.java | 28 ------- tests/R2D2SpeakTest.java | 46 ----------- tests/R2D2ThinkTest.java | 44 ----------- .../name/domain/starwars/IDTest.java | 56 +++++++++++++ .../name/domain/starwars/SpeakTest.java | 79 +++++++++++++++++++ .../name/domain/starwars/ThinkTest.java | 45 +++++++++++ 11 files changed, 208 insertions(+), 146 deletions(-) delete mode 100644 tests/R2D2IDTest.java delete mode 100644 tests/R2D2SpeakTest.java delete mode 100644 tests/R2D2ThinkTest.java create mode 100644 tests/de/hsmannheim/informatik/name/domain/starwars/IDTest.java create mode 100644 tests/de/hsmannheim/informatik/name/domain/starwars/SpeakTest.java create mode 100644 tests/de/hsmannheim/informatik/name/domain/starwars/ThinkTest.java diff --git a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java index d83c048..3bb56cf 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotBluePrint.java @@ -2,7 +2,6 @@ package de.hsmannheim.informatik.name.domain; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; -import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; import de.hsmannheim.informatik.name.domain.requirements.Robot; /** @@ -71,8 +70,8 @@ public abstract class RobotBluePrint implements Robot { } @Override - public abstract String speak(int[] zahlen) throws RobotException, RobotMagicValueException; + public abstract String speak(int[] zahlen) throws RobotException; @Override - public abstract int[] think(int[] zahlen) throws RobotException, RobotMagicValueException; + public abstract int[] think(int[] zahlen) throws RobotException; } diff --git a/src/de/hsmannheim/informatik/name/domain/RobotFactory.java b/src/de/hsmannheim/informatik/name/domain/RobotFactory.java index 57003c5..b163968 100644 --- a/src/de/hsmannheim/informatik/name/domain/RobotFactory.java +++ b/src/de/hsmannheim/informatik/name/domain/RobotFactory.java @@ -23,8 +23,8 @@ public class RobotFactory { /** * Factory Method to get a {@link Robot} * - * @param type {@link RobotType Type} of the robot - * @param serialNumber serialnumber in form of an {@code int} + * @param type {@link RobotType Type} of the robot + * @param name {@link String Name} of the robot * @return a {@link Robot robot instance} of specified type, if no type matches {@code null} * @throws RobotIllegalStateException the robot initializes in an illegal state */ @@ -34,31 +34,31 @@ public class RobotFactory { switch (type) { case C3P0 -> { int serialNumberC3P0; - do{ - serialNumberC3P0 = r.nextInt(10000)+10000; - if(serialNumbersC3P0.size() == 10000){ + do { + serialNumberC3P0 = r.nextInt(10000) + 10000; + if (serialNumbersC3P0.size() == 10000) { return null; } - if(!serialNumbersC3P0.contains(serialNumberC3P0)){ + if (!serialNumbersC3P0.contains(serialNumberC3P0)) { serialNumbersC3P0.add(serialNumberC3P0); usedSerialNumber = false; } - }while(usedSerialNumber); - return new C3PO(serialNumberC3P0); + } while (usedSerialNumber); + return new C3PO(serialNumberC3P0, name); } case R2D2 -> { int serialNumberR2D2; - do{ + do { String temp = String.format("%05d", r.nextInt(10000)); serialNumberR2D2 = Integer.parseInt(temp); - if(serialNumbersR2D2.size() == 10000){ + if (serialNumbersR2D2.size() == 10000) { return null; } - if(!serialNumbersR2D2.contains(serialNumberR2D2)){ + if (!serialNumbersR2D2.contains(serialNumberR2D2)) { serialNumbersR2D2.add(serialNumberR2D2); usedSerialNumber = false; } - }while(usedSerialNumber); + } while (usedSerialNumber); return new R2D2(serialNumberR2D2, name); } default -> { diff --git a/src/de/hsmannheim/informatik/name/domain/requirements/RobotInstructions.java b/src/de/hsmannheim/informatik/name/domain/requirements/RobotInstructions.java index 4a2dd66..8e3f4cc 100644 --- a/src/de/hsmannheim/informatik/name/domain/requirements/RobotInstructions.java +++ b/src/de/hsmannheim/informatik/name/domain/requirements/RobotInstructions.java @@ -32,9 +32,9 @@ public interface RobotInstructions { * @param zahlen Zahlen, die ausgegeben werden sollen. * @return Zahlen als String * @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, - * oder das Array nicht seinen Vorstellungen entspricht. + * oder das Array nicht seinen Vorstellungen entspricht. */ - public String speak(int[] zahlen) throws RobotException, RobotMagicValueException; + public String speak(int[] zahlen) throws RobotException; /** * Sortiert ein Array von Zahlen. Die Reihenfolge hängt von dem Typ des @@ -43,7 +43,7 @@ public interface RobotInstructions { * @param zahlen Zahlen, die sortiert werden sollen. * @return Sortierte Zahlen * @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, - * oder das Array nicht seinen Vorstellungen entspricht. + * oder das Array nicht seinen Vorstellungen entspricht. */ - public int[] think(int[] zahlen) throws RobotException, RobotMagicValueException; + public int[] think(int[] zahlen) throws RobotException; } diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java index cd99b3a..6738601 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java @@ -13,8 +13,8 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; * @see StarWarsRobot */ public class C3PO extends StarWarsRobot { - public C3PO(int id) throws RobotIllegalStateException { - super(';', id, "C3PO"); + public C3PO(int id, String name) throws RobotIllegalStateException { + super(';', id, name); } @Override @@ -27,14 +27,16 @@ public class C3PO extends StarWarsRobot { * * @param numbers integer numbers for sorting * @return arrToSort integer array with numbers sorted in descending order - * @throws RobotException + * @throws RobotException if */ @Override //TODO why is RobotMagicValueException grayed out? - public int[] think(int[] numbers) throws RobotException, RobotMagicValueException { + //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 { int[] arrToSort = numbers.clone(); - for (int i = 0; i < arrToSort.length; i++) { - if (arrToSort[i] == MAGIC_NUMBER) { + for (int k : arrToSort) { + if (k == MAGIC_NUMBER) { throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this); } } diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java index ab1589d..6d83930 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java @@ -27,11 +27,10 @@ public class R2D2 extends StarWarsRobot { * * @param numbers integer numbers for sorting * @return arrayToSort integer array with numbers sorted in ascending order - * @throws RobotException - * @throws RobotMagicValueException + * @throws RobotException if any kind of error occurs */ @Override - public int[] think(int[] numbers) throws RobotException, RobotMagicValueException { + public int[] think(int[] numbers) throws RobotException { int[] arrayToSort = numbers.clone(); for (int i = 0; i < arrayToSort.length; i++) { if (arrayToSort[i] == MAGIC_NUMBER) { diff --git a/tests/R2D2IDTest.java b/tests/R2D2IDTest.java deleted file mode 100644 index 01ffc25..0000000 --- a/tests/R2D2IDTest.java +++ /dev/null @@ -1,28 +0,0 @@ -//import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; -//import de.hsmannheim.informatik.name.domain.starwars.R2D2; -//import org.junit.jupiter.api.Test; -// -//import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -//import static org.junit.jupiter.api.Assertions.assertThrows; -// -//public class R2D2IDTest { -// -// @Test -// public void NegativeTest(){ -// assertThrows(RobotIllegalStateException.class, () -> new R2D2(-1)); -// assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MIN_VALUE)); -// } -// -// @Test -// public void InIDRangeTest() { -// assertDoesNotThrow(() -> new R2D2(0)); -// assertDoesNotThrow(() -> new R2D2(1)); -// assertDoesNotThrow(() -> new R2D2(9999)); -// } -// -// @Test -// public void OuterIDRangeTest() { -// assertThrows(RobotIllegalStateException.class, () -> new R2D2(10000)); -// assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MAX_VALUE)); -// } -//} diff --git a/tests/R2D2SpeakTest.java b/tests/R2D2SpeakTest.java deleted file mode 100644 index e0a8a3f..0000000 --- a/tests/R2D2SpeakTest.java +++ /dev/null @@ -1,46 +0,0 @@ -//import de.hsmannheim.informatik.name.domain.exceptions.RobotException; -//import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; -//import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; -//import de.hsmannheim.informatik.name.domain.starwars.R2D2; -//import org.junit.jupiter.api.Test; -// -//import static org.junit.jupiter.api.Assertions.*; -// -// -//class R2D2SpeakTest { -// -// private final R2D2 r2d2 = new R2D2(9999); -// -// R2D2SpeakTest() throws RobotIllegalStateException { -// } -// -// @Test -// void speakTestStandard() throws RobotException, RobotMagicValueException { -// assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); -// } -// -// @Test -// void speakTestEmpty() throws RobotException, RobotMagicValueException { -// assertEquals("", r2d2.speak(new int[]{})); -// } -// -// @Test -// void speakTestZeros() throws RobotException, RobotMagicValueException { -// assertEquals("0, 0, 0, 0", r2d2.speak(new int[4])); -// } -// -// @Test -// void speakTestOneElement() throws RobotException, RobotMagicValueException { -// assertEquals("1", r2d2.speak(new int[]{1})); -// } -// -// @Test -// void speakTestUnitElements() throws RobotException, RobotMagicValueException { -// assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1})); -// } -// -// @Test -// void speakTestMagicNumberException() throws RobotException { -// assertThrows(RobotMagicValueException.class, () -> r2d2.speak(new int[]{42})); -// } -//} \ No newline at end of file diff --git a/tests/R2D2ThinkTest.java b/tests/R2D2ThinkTest.java deleted file mode 100644 index c3c0304..0000000 --- a/tests/R2D2ThinkTest.java +++ /dev/null @@ -1,44 +0,0 @@ -//import de.hsmannheim.informatik.name.domain.exceptions.RobotException; -//import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; -//import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; -//import de.hsmannheim.informatik.name.domain.starwars.R2D2; -//import org.junit.jupiter.api.Test; -// -//import static org.junit.jupiter.api.Assertions.*; -// -//class R2D2ThinkTest { -// -// private final R2D2 r2d2 = new R2D2(9999); -// -// R2D2ThinkTest() throws RobotIllegalStateException { -// } -// -// @Test -// void thinkTestStandard() throws RobotException, RobotMagicValueException { -// -// assertArrayEquals(new int[]{0,1,2,3,4,5}, r2d2.think(new int[]{3,0,1,5,2,4})); -// } -// -// @Test -// void thinkTestNegative() throws RobotException, RobotMagicValueException { -// assertArrayEquals(new int[]{-3,-2,-1,0,1,2,3}, r2d2.think(new int[]{0,-3,2,-1,1,3,-2})); -// } -// -// @Test -// void thinkTestZeros() throws RobotException, RobotMagicValueException { -// assertArrayEquals(new int[]{0,0,0,0}, r2d2.think(new int[]{0,0,0,0})); -// } -// -// @Test -// void thinkTestEmpty() throws RobotException, RobotMagicValueException { -// assertArrayEquals(new int[]{}, r2d2.think(new int[]{})); -// } -// -// @Test -// void thinkTestMagicNumberException() { -// assertThrows(RobotMagicValueException.class,() -> r2d2.think(new int[]{42})); -// } -// -// -// -//} diff --git a/tests/de/hsmannheim/informatik/name/domain/starwars/IDTest.java b/tests/de/hsmannheim/informatik/name/domain/starwars/IDTest.java new file mode 100644 index 0000000..85b4f57 --- /dev/null +++ b/tests/de/hsmannheim/informatik/name/domain/starwars/IDTest.java @@ -0,0 +1,56 @@ +package de.hsmannheim.informatik.name.domain.starwars; + +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class IDTest { + + @Test + public void R2D2NegativeTest() { + assertThrows(RobotIllegalStateException.class, () -> new R2D2(-1, "test")); + assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MIN_VALUE, "test")); + + assertThrows(RobotIllegalStateException.class, () -> new C3PO(-1, "test")); + assertThrows(RobotIllegalStateException.class, () -> new C3PO(Integer.MIN_VALUE, "test")); + } + + @Test + public void C3PONegativeTest() { + assertThrows(RobotIllegalStateException.class, () -> new C3PO(-1, "test")); + assertThrows(RobotIllegalStateException.class, () -> new C3PO(Integer.MIN_VALUE, "test")); + } + + @Test + public void R2D2InIDRangeTest() { + assertDoesNotThrow(() -> new R2D2(0, "test")); + assertDoesNotThrow(() -> new R2D2(1, "test")); + assertDoesNotThrow(() -> new R2D2(5000, "test")); + assertDoesNotThrow(() -> new R2D2(9999, "test")); + + } + + @Test + public void C3POInIDRangeTest() { + assertDoesNotThrow(() -> new C3PO(10000, "test")); + assertDoesNotThrow(() -> new C3PO(10001, "test")); + assertDoesNotThrow(() -> new C3PO(15000, "test")); + assertDoesNotThrow(() -> new C3PO(19999, "test")); + } + + @Test + public void R2D2OuterIDRangeTest() { + assertThrows(RobotIllegalStateException.class, () -> new R2D2(-1, "test")); + assertThrows(RobotIllegalStateException.class, () -> new R2D2(10000, "test")); + assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MAX_VALUE, "test")); + } + + @Test + public void C3POOuterIDRangeTest() { + assertThrows(RobotIllegalStateException.class, () -> new C3PO(0, "test")); + assertThrows(RobotIllegalStateException.class, () -> new C3PO(9999, "test")); + assertThrows(RobotIllegalStateException.class, () -> new C3PO(Integer.MAX_VALUE, "test")); + } +} diff --git a/tests/de/hsmannheim/informatik/name/domain/starwars/SpeakTest.java b/tests/de/hsmannheim/informatik/name/domain/starwars/SpeakTest.java new file mode 100644 index 0000000..682e210 --- /dev/null +++ b/tests/de/hsmannheim/informatik/name/domain/starwars/SpeakTest.java @@ -0,0 +1,79 @@ +package de.hsmannheim.informatik.name.domain.starwars; + +import de.hsmannheim.informatik.name.domain.exceptions.RobotException; +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; +import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + + +class SpeakTest { + + private final R2D2 r2d2 = new R2D2(9999, "test"); + private final C3PO c3po = new C3PO(19999, "test"); + + SpeakTest() throws RobotIllegalStateException { + } + + @Test + void R2D2SpeakTestStandard() throws RobotException { + assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); + } + + @Test + void R2D2SpeakTestEmpty() throws RobotException { + assertEquals("", r2d2.speak(new int[]{})); + } + + @Test + void R2D2SpeakTestZeros() throws RobotException { + assertEquals("0, 0, 0, 0", r2d2.speak(new int[4])); + } + + @Test + void R2D2SpeakTestOneElement() throws RobotException { + assertEquals("1", r2d2.speak(new int[]{1})); + } + + @Test + void R2D2SpeakTestUnitElements() throws RobotException { + assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1})); + } + + @Test + void R2D2SpeakTestMagicNumberException() { + assertThrows(RobotMagicValueException.class, () -> r2d2.speak(new int[]{42})); + } + + @Test + void C3POSpeakTestStandard() throws RobotException { + assertEquals("1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12", c3po.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); + } + + @Test + void C3POSpeakTestEmpty() throws RobotException { + assertEquals("", c3po.speak(new int[]{})); + } + + @Test + void C3POSpeakTestZeros() throws RobotException { + assertEquals("0; 0; 0; 0", c3po.speak(new int[4])); + } + + @Test + void C3POSpeakTestOneElement() throws RobotException { + assertEquals("1", c3po.speak(new int[]{1})); + } + + @Test + void C3POSpeakTestUnitElements() throws RobotException { + assertEquals("-1; 0; 1", c3po.speak(new int[]{-1, 0, 1})); + } + + @Test + void C3POSpeakTestMagicNumberException() { + assertThrows(RobotMagicValueException.class, () -> c3po.speak(new int[]{42})); + } +} \ No newline at end of file diff --git a/tests/de/hsmannheim/informatik/name/domain/starwars/ThinkTest.java b/tests/de/hsmannheim/informatik/name/domain/starwars/ThinkTest.java new file mode 100644 index 0000000..4d461cb --- /dev/null +++ b/tests/de/hsmannheim/informatik/name/domain/starwars/ThinkTest.java @@ -0,0 +1,45 @@ +package de.hsmannheim.informatik.name.domain.starwars; + +import de.hsmannheim.informatik.name.domain.exceptions.RobotException; +import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; +import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class ThinkTest { + + private final R2D2 r2d2 = new R2D2(9999, "test"); + + ThinkTest() throws RobotIllegalStateException { + } + + @Test + void thinkTestStandard() throws RobotException { + + assertArrayEquals(new int[]{0, 1, 2, 3, 4, 5}, r2d2.think(new int[]{3, 0, 1, 5, 2, 4})); + } + + @Test + void thinkTestNegative() throws RobotException { + assertArrayEquals(new int[]{-3, -2, -1, 0, 1, 2, 3}, r2d2.think(new int[]{0, -3, 2, -1, 1, 3, -2})); + } + + @Test + void thinkTestZeros() throws RobotException { + assertArrayEquals(new int[]{0, 0, 0, 0}, r2d2.think(new int[]{0, 0, 0, 0})); + } + + @Test + void thinkTestEmpty() throws RobotException { + assertArrayEquals(new int[]{}, r2d2.think(new int[]{})); + } + + @Test + void thinkTestMagicNumberException() { + assertThrows(RobotMagicValueException.class, () -> r2d2.think(new int[]{42})); + } + + +}