added speak and id test coverage for C3PO, added name handling for C3PO

main
roman 2022-12-28 15:37:55 +01:00
parent 5b81587492
commit d28c82abdc
11 changed files with 208 additions and 146 deletions

View File

@ -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.RobotException;
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
import de.hsmannheim.informatik.name.domain.requirements.Robot; import de.hsmannheim.informatik.name.domain.requirements.Robot;
/** /**
@ -71,8 +70,8 @@ public abstract class RobotBluePrint implements Robot {
} }
@Override @Override
public abstract String speak(int[] zahlen) throws RobotException, RobotMagicValueException; public abstract String speak(int[] zahlen) throws RobotException;
@Override @Override
public abstract int[] think(int[] zahlen) throws RobotException, RobotMagicValueException; public abstract int[] think(int[] zahlen) throws RobotException;
} }

View File

@ -24,7 +24,7 @@ public class RobotFactory {
* Factory Method to get a {@link Robot} * Factory Method to get a {@link Robot}
* *
* @param type {@link RobotType Type} of the robot * @param type {@link RobotType Type} of the robot
* @param serialNumber serialnumber in form of an {@code int} * @param name {@link String Name} of the robot
* @return a {@link Robot robot instance} of specified type, if no type matches {@code null} * @return a {@link Robot robot instance} of specified type, if no type matches {@code null}
* @throws RobotIllegalStateException the robot initializes in an illegal state * @throws RobotIllegalStateException the robot initializes in an illegal state
*/ */
@ -34,31 +34,31 @@ public class RobotFactory {
switch (type) { switch (type) {
case C3P0 -> { case C3P0 -> {
int serialNumberC3P0; int serialNumberC3P0;
do{ do {
serialNumberC3P0 = r.nextInt(10000)+10000; serialNumberC3P0 = r.nextInt(10000) + 10000;
if(serialNumbersC3P0.size() == 10000){ if (serialNumbersC3P0.size() == 10000) {
return null; return null;
} }
if(!serialNumbersC3P0.contains(serialNumberC3P0)){ if (!serialNumbersC3P0.contains(serialNumberC3P0)) {
serialNumbersC3P0.add(serialNumberC3P0); serialNumbersC3P0.add(serialNumberC3P0);
usedSerialNumber = false; usedSerialNumber = false;
} }
}while(usedSerialNumber); } while (usedSerialNumber);
return new C3PO(serialNumberC3P0); return new C3PO(serialNumberC3P0, name);
} }
case R2D2 -> { case R2D2 -> {
int serialNumberR2D2; int serialNumberR2D2;
do{ do {
String temp = String.format("%05d", r.nextInt(10000)); String temp = String.format("%05d", r.nextInt(10000));
serialNumberR2D2 = Integer.parseInt(temp); serialNumberR2D2 = Integer.parseInt(temp);
if(serialNumbersR2D2.size() == 10000){ if (serialNumbersR2D2.size() == 10000) {
return null; return null;
} }
if(!serialNumbersR2D2.contains(serialNumberR2D2)){ if (!serialNumbersR2D2.contains(serialNumberR2D2)) {
serialNumbersR2D2.add(serialNumberR2D2); serialNumbersR2D2.add(serialNumberR2D2);
usedSerialNumber = false; usedSerialNumber = false;
} }
}while(usedSerialNumber); } while (usedSerialNumber);
return new R2D2(serialNumberR2D2, name); return new R2D2(serialNumberR2D2, name);
} }
default -> { default -> {

View File

@ -34,7 +34,7 @@ public interface RobotInstructions {
* @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, * @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 * Sortiert ein Array von Zahlen. Die Reihenfolge hängt von dem Typ des
@ -45,5 +45,5 @@ public interface RobotInstructions {
* @throws RobotException wenn der Roboter in einem ungültigen Zustand ist, * @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;
} }

View File

@ -13,8 +13,8 @@ import de.hsmannheim.informatik.name.domain.exceptions.RobotMagicValueException;
* @see StarWarsRobot * @see StarWarsRobot
*/ */
public class C3PO extends StarWarsRobot { public class C3PO extends StarWarsRobot {
public C3PO(int id) throws RobotIllegalStateException { public C3PO(int id, String name) throws RobotIllegalStateException {
super(';', id, "C3PO"); super(';', id, name);
} }
@Override @Override
@ -27,14 +27,16 @@ public class C3PO extends StarWarsRobot {
* *
* @param numbers integer numbers for sorting * @param numbers integer numbers for sorting
* @return arrToSort integer array with numbers sorted in descending order * @return arrToSort integer array with numbers sorted in descending order
* @throws RobotException * @throws RobotException if
*/ */
@Override @Override
//TODO why is RobotMagicValueException grayed out? //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(); int[] arrToSort = numbers.clone();
for (int i = 0; i < arrToSort.length; i++) { for (int k : arrToSort) {
if (arrToSort[i] == MAGIC_NUMBER) { if (k == MAGIC_NUMBER) {
throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this); throw new RobotMagicValueException(String.format("Appearance of Magic Number: %d", MAGIC_NUMBER), this);
} }
} }

View File

@ -27,11 +27,10 @@ public class R2D2 extends StarWarsRobot {
* *
* @param numbers integer numbers for sorting * @param numbers integer numbers for sorting
* @return arrayToSort integer array with numbers sorted in ascending order * @return arrayToSort integer array with numbers sorted in ascending order
* @throws RobotException * @throws RobotException if any kind of error occurs
* @throws RobotMagicValueException
*/ */
@Override @Override
public int[] think(int[] numbers) throws RobotException, RobotMagicValueException { public int[] think(int[] numbers) throws RobotException {
int[] arrayToSort = numbers.clone(); int[] arrayToSort = numbers.clone();
for (int i = 0; i < arrayToSort.length; i++) { for (int i = 0; i < arrayToSort.length; i++) {
if (arrayToSort[i] == MAGIC_NUMBER) { if (arrayToSort[i] == MAGIC_NUMBER) {

View File

@ -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));
// }
//}

View File

@ -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}));
// }
//}

View File

@ -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}));
// }
//
//
//
//}

View File

@ -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"));
}
}

View File

@ -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}));
}
}

View File

@ -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}));
}
}