added factory unit test

main
Stefan 2022-12-28 03:21:50 +01:00
parent 8705cf23ea
commit 5b81587492
6 changed files with 161 additions and 121 deletions

View File

@ -5,18 +5,22 @@ import java.util.Random;
public class Main {
public static void main(String[] args) throws RobotIllegalStateException {
Random r = new Random();
RobotFactory rf = new RobotFactory();
RobotType R2D2 = RobotType.R2D2;
RobotType C3P0 = RobotType.C3P0;
for (int i = 0; i < 10000; i++) {
rf.getRobot(R2D2, "TestR2D2");
if(rf.getRobot(R2D2, "TestR2D2") == null){
System.out.println("Error");
}
}
System.out.println("finished R2D2");
for (int i = 0; i < 10000; i++) {
rf.getRobot(C3P0, "TestC3P0");
if(rf.getRobot(C3P0, "TestC3P0") == null){
System.out.println("Error");
}
}
System.out.println("finished C3P0");
System.out.println("finished");
}
}

View File

@ -36,6 +36,9 @@ public class RobotFactory {
int serialNumberC3P0;
do{
serialNumberC3P0 = r.nextInt(10000)+10000;
if(serialNumbersC3P0.size() == 10000){
return null;
}
if(!serialNumbersC3P0.contains(serialNumberC3P0)){
serialNumbersC3P0.add(serialNumberC3P0);
usedSerialNumber = false;
@ -48,6 +51,9 @@ public class RobotFactory {
do{
String temp = String.format("%05d", r.nextInt(10000));
serialNumberR2D2 = Integer.parseInt(temp);
if(serialNumbersR2D2.size() == 10000){
return null;
}
if(!serialNumbersR2D2.contains(serialNumberR2D2)){
serialNumbersR2D2.add(serialNumberR2D2);
usedSerialNumber = false;

View File

@ -1,28 +1,28 @@
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));
}
}
//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 +1,46 @@
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}));
}
}
//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 +1,44 @@
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}));
}
}
//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,30 @@
package de.hsmannheim.informatik.name.domain;
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
import de.hsmannheim.informatik.name.domain.starwars.C3PO;
import de.hsmannheim.informatik.name.domain.starwars.R2D2;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class RobotFactoryTest {
@Test
public void toMuchR2D2s() throws RobotIllegalStateException {
RobotFactory rf = new RobotFactory();
RobotType R2D2 = RobotType.R2D2;
for (int i = 0; i < 9999; i++) {
rf.getRobot(R2D2, "TestR2D2");
}
assertInstanceOf(R2D2.class, rf.getRobot(R2D2, "Test"));
assertNull( rf.getRobot(R2D2, "TestR2D2"));
}
@Test
public void toMuchC3P0() throws RobotIllegalStateException {
RobotFactory rf = new RobotFactory();
RobotType R2D2 = RobotType.C3P0;
for (int i = 0; i < 9999; i++) {
rf.getRobot(RobotType.C3P0, "TestC3P0");
}
assertInstanceOf(C3PO.class, rf.getRobot(R2D2, "Test"));
assertNull( rf.getRobot(R2D2, "TestC3P0"));
}
}