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 class Main {
public static void main(String[] args) throws RobotIllegalStateException { public static void main(String[] args) throws RobotIllegalStateException {
Random r = new Random();
RobotFactory rf = new RobotFactory(); RobotFactory rf = new RobotFactory();
RobotType R2D2 = RobotType.R2D2; RobotType R2D2 = RobotType.R2D2;
RobotType C3P0 = RobotType.C3P0; RobotType C3P0 = RobotType.C3P0;
for (int i = 0; i < 10000; i++) { 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"); System.out.println("finished R2D2");
for (int i = 0; i < 10000; i++) { 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 C3P0");
System.out.println("finished"); System.out.println("finished");
} }
} }

View File

@ -36,6 +36,9 @@ public class RobotFactory {
int serialNumberC3P0; int serialNumberC3P0;
do{ do{
serialNumberC3P0 = r.nextInt(10000)+10000; serialNumberC3P0 = r.nextInt(10000)+10000;
if(serialNumbersC3P0.size() == 10000){
return null;
}
if(!serialNumbersC3P0.contains(serialNumberC3P0)){ if(!serialNumbersC3P0.contains(serialNumberC3P0)){
serialNumbersC3P0.add(serialNumberC3P0); serialNumbersC3P0.add(serialNumberC3P0);
usedSerialNumber = false; usedSerialNumber = false;
@ -48,6 +51,9 @@ public class RobotFactory {
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){
return null;
}
if(!serialNumbersR2D2.contains(serialNumberR2D2)){ if(!serialNumbersR2D2.contains(serialNumberR2D2)){
serialNumbersR2D2.add(serialNumberR2D2); serialNumbersR2D2.add(serialNumberR2D2);
usedSerialNumber = false; usedSerialNumber = false;

View File

@ -1,28 +1,28 @@
import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException; //import de.hsmannheim.informatik.name.domain.exceptions.RobotIllegalStateException;
import de.hsmannheim.informatik.name.domain.starwars.R2D2; //import de.hsmannheim.informatik.name.domain.starwars.R2D2;
import org.junit.jupiter.api.Test; //import org.junit.jupiter.api.Test;
//
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; //import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows; //import static org.junit.jupiter.api.Assertions.assertThrows;
//
public class R2D2IDTest { //public class R2D2IDTest {
//
@Test // @Test
public void NegativeTest(){ // public void NegativeTest(){
assertThrows(RobotIllegalStateException.class, () -> new R2D2(-1)); // assertThrows(RobotIllegalStateException.class, () -> new R2D2(-1));
assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MIN_VALUE)); // assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MIN_VALUE));
} // }
//
@Test // @Test
public void InIDRangeTest() { // public void InIDRangeTest() {
assertDoesNotThrow(() -> new R2D2(0)); // assertDoesNotThrow(() -> new R2D2(0));
assertDoesNotThrow(() -> new R2D2(1)); // assertDoesNotThrow(() -> new R2D2(1));
assertDoesNotThrow(() -> new R2D2(9999)); // assertDoesNotThrow(() -> new R2D2(9999));
} // }
//
@Test // @Test
public void OuterIDRangeTest() { // public void OuterIDRangeTest() {
assertThrows(RobotIllegalStateException.class, () -> new R2D2(10000)); // assertThrows(RobotIllegalStateException.class, () -> new R2D2(10000));
assertThrows(RobotIllegalStateException.class, () -> new R2D2(Integer.MAX_VALUE)); // 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.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.exceptions.RobotMagicValueException;
import de.hsmannheim.informatik.name.domain.starwars.R2D2; //import de.hsmannheim.informatik.name.domain.starwars.R2D2;
import org.junit.jupiter.api.Test; //import org.junit.jupiter.api.Test;
//
import static org.junit.jupiter.api.Assertions.*; //import static org.junit.jupiter.api.Assertions.*;
//
//
class R2D2SpeakTest { //class R2D2SpeakTest {
//
private final R2D2 r2d2 = new R2D2(9999); // private final R2D2 r2d2 = new R2D2(9999);
//
R2D2SpeakTest() throws RobotIllegalStateException { // R2D2SpeakTest() throws RobotIllegalStateException {
} // }
//
@Test // @Test
void speakTestStandard() throws RobotException, RobotMagicValueException { // 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})); // 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 // @Test
void speakTestEmpty() throws RobotException, RobotMagicValueException { // void speakTestEmpty() throws RobotException, RobotMagicValueException {
assertEquals("", r2d2.speak(new int[]{})); // assertEquals("", r2d2.speak(new int[]{}));
} // }
//
@Test // @Test
void speakTestZeros() throws RobotException, RobotMagicValueException { // void speakTestZeros() throws RobotException, RobotMagicValueException {
assertEquals("0, 0, 0, 0", r2d2.speak(new int[4])); // assertEquals("0, 0, 0, 0", r2d2.speak(new int[4]));
} // }
//
@Test // @Test
void speakTestOneElement() throws RobotException, RobotMagicValueException { // void speakTestOneElement() throws RobotException, RobotMagicValueException {
assertEquals("1", r2d2.speak(new int[]{1})); // assertEquals("1", r2d2.speak(new int[]{1}));
} // }
//
@Test // @Test
void speakTestUnitElements() throws RobotException, RobotMagicValueException { // void speakTestUnitElements() throws RobotException, RobotMagicValueException {
assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1})); // assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1}));
} // }
//
@Test // @Test
void speakTestMagicNumberException() throws RobotException { // void speakTestMagicNumberException() throws RobotException {
assertThrows(RobotMagicValueException.class, () -> r2d2.speak(new int[]{42})); // 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.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.exceptions.RobotMagicValueException;
import de.hsmannheim.informatik.name.domain.starwars.R2D2; //import de.hsmannheim.informatik.name.domain.starwars.R2D2;
import org.junit.jupiter.api.Test; //import org.junit.jupiter.api.Test;
//
import static org.junit.jupiter.api.Assertions.*; //import static org.junit.jupiter.api.Assertions.*;
//
class R2D2ThinkTest { //class R2D2ThinkTest {
//
private final R2D2 r2d2 = new R2D2(9999); // private final R2D2 r2d2 = new R2D2(9999);
//
R2D2ThinkTest() throws RobotIllegalStateException { // R2D2ThinkTest() throws RobotIllegalStateException {
} // }
//
@Test // @Test
void thinkTestStandard() throws RobotException, RobotMagicValueException { // void thinkTestStandard() throws RobotException, RobotMagicValueException {
//
assertArrayEquals(new int[]{0,1,2,3,4,5}, r2d2.think(new int[]{3,0,1,5,2,4})); // assertArrayEquals(new int[]{0,1,2,3,4,5}, r2d2.think(new int[]{3,0,1,5,2,4}));
} // }
//
@Test // @Test
void thinkTestNegative() throws RobotException, RobotMagicValueException { // 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})); // assertArrayEquals(new int[]{-3,-2,-1,0,1,2,3}, r2d2.think(new int[]{0,-3,2,-1,1,3,-2}));
} // }
//
@Test // @Test
void thinkTestZeros() throws RobotException, RobotMagicValueException { // void thinkTestZeros() throws RobotException, RobotMagicValueException {
assertArrayEquals(new int[]{0,0,0,0}, r2d2.think(new int[]{0,0,0,0})); // assertArrayEquals(new int[]{0,0,0,0}, r2d2.think(new int[]{0,0,0,0}));
} // }
//
@Test // @Test
void thinkTestEmpty() throws RobotException, RobotMagicValueException { // void thinkTestEmpty() throws RobotException, RobotMagicValueException {
assertArrayEquals(new int[]{}, r2d2.think(new int[]{})); // assertArrayEquals(new int[]{}, r2d2.think(new int[]{}));
} // }
//
@Test // @Test
void thinkTestMagicNumberException() { // void thinkTestMagicNumberException() {
assertThrows(RobotMagicValueException.class,() -> r2d2.think(new int[]{42})); // 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"));
}
}