-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Repairman action test and Jacoco setup
- Loading branch information
Showing
3 changed files
with
282 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.junit.jupiter.api.DynamicTest.dynamicTest; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.DynamicTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestFactory; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.junit.jupiter.api.Nested; | ||
import org.mockito.MockedStatic; | ||
|
||
public class RepairmanActionsTest { | ||
Repairman rm; | ||
ArrayList<Element> elements; | ||
Pipe pipe; | ||
Pump pump; | ||
|
||
|
||
|
||
@BeforeEach | ||
void CreatesAPipeAndAPumpWithARepairmanOnAPipe(){ | ||
rm = new Repairman(); | ||
pump = new Pump(); | ||
pipe = new Pipe(); | ||
elements = new ArrayList<Element>(); | ||
elements.add(pipe); | ||
elements.add(pump); | ||
rm.setPosition(pipe); | ||
} | ||
|
||
static NonPipe[] nonPipes(){ | ||
return new NonPipe[]{new Pump(),new Cistern(),new WaterSource()}; | ||
} | ||
|
||
@ParameterizedTest(name = "Testing the {index}.NonPipe type to move onto from a Pipe") | ||
@MethodSource(value = "nonPipes") | ||
void givenRepairmanWhenMovesToAPermittedFieldThenMoved(NonPipe nonPipe){ | ||
elements.add(nonPipe); | ||
try (MockedStatic<Game> mockedStaticGame = mockStatic(Game.class)) { | ||
Game mockedGame = mock(Game.class); | ||
mockedStaticGame.when(Game::getInstance).thenReturn(mockedGame); | ||
when(mockedGame.getGameElements()).thenReturn(elements); | ||
|
||
AddEachotherToNeighbors(pipe, nonPipe); | ||
rm.move(0); | ||
assertNotEquals(pipe, rm.getPosition()); | ||
assertEquals(nonPipe, rm.getPosition()); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
void givenRepairmanWhenMovesToANotPermittedFieldThenStayed(){ | ||
try (MockedStatic<Game> mockedStaticGame = mockStatic(Game.class)) { | ||
Game mockedGame = mock(Game.class); | ||
mockedStaticGame.when(Game::getInstance).thenReturn(mockedGame); | ||
when(mockedGame.getGameElements()).thenReturn(elements); | ||
|
||
rm.move(0); | ||
assertEquals(pipe, rm.getPosition()); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
void givenRepairmanWhenRepairsABrokenPipeThenGetsFixed(){ | ||
pipe = new Pipe(true,false,0,0,0); | ||
rm.setPosition(pipe); | ||
assertTrue(pipe.getHoleOnPipe()); | ||
|
||
rm.RepairElement(); | ||
assertFalse(pipe.getHoleOnPipe()); | ||
|
||
} | ||
|
||
|
||
@Test | ||
void givenRepairmanWhenRepairsABrokenPumpThenGetsFixed(){ | ||
pump = new Pump(true,false,0); | ||
rm.setPosition(pump); | ||
assertTrue(pump.getBroken()); | ||
|
||
rm.RepairElement(); | ||
assertFalse(pump.getBroken()); | ||
} | ||
|
||
|
||
@TestFactory | ||
Stream<DynamicTest> givenRepairmanWithoutPumpWhenAsksForPumpAtElementsThenGetsOrNot(){ | ||
Object[][] data = new Object[][]{{new Pipe(),"pipe",false}, | ||
{new Pump(),"pump",false}, | ||
{new Cistern(),"cistern",true}, | ||
{new WaterSource(),"watersource",false}}; | ||
return Arrays.stream(data).map(entry ->{ | ||
Element element = (Element)entry[0]; | ||
String name = (String)entry[1]; | ||
Boolean expected = (Boolean)entry[2]; | ||
return dynamicTest("Repairman on a "+name+ " gets a pump => "+expected,() -> { | ||
rm = new Repairman(); | ||
rm.setPosition(element); | ||
rm.LiftPump(); | ||
if(expected) | ||
assertNotEquals(null, rm.getHoldingPump()); | ||
else | ||
assertEquals(null, rm.getHoldingPump()); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
@Test | ||
void givenRepairmanWithPumpWhenTriesToPlaceAPumpAtPipeThenItsPlaced(){ | ||
assertEquals(null,rm.getHoldingPump()); | ||
|
||
Pump pump2 = new Pump(); | ||
AddEachotherToNeighbors(pipe, pump2); | ||
AddEachotherToNeighbors(pipe, pump); | ||
|
||
rm.setHoldingPump(new Pump()); | ||
assertNotEquals(null,rm.getHoldingPump()); | ||
|
||
ElementButton elementButton =new ElementButton(pump); | ||
try (MockedStatic<Gui> mockedStaticGui = mockStatic(Gui.class)) { | ||
Gui mockedGui = mock(Gui.class); | ||
mockedStaticGui.when(Gui::getInstance).thenReturn(mockedGui); | ||
when(mockedGui.findElementButton(pump)).thenReturn(elementButton); | ||
when(mockedGui.findElementButton(pump2)).thenReturn(elementButton); | ||
when(mockedGui.findElementButton((Element)pipe)).thenReturn(elementButton); | ||
|
||
rm.PlacePump(); | ||
assertEquals(null,rm.getHoldingPump()); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
void givenRepairmanWithPumpWhenTriesToPlaceAPumpAtNonPipeThenNotPlaced(){ | ||
assertEquals(null,rm.getHoldingPump()); | ||
|
||
Pipe pipe2 = new Pipe(); | ||
AddEachotherToNeighbors(pipe, pump); | ||
AddEachotherToNeighbors(pipe2, pump); | ||
|
||
rm.setHoldingPump(new Pump()); | ||
assertNotEquals(null,rm.getHoldingPump()); | ||
|
||
rm.setPosition(pump); | ||
|
||
rm.PlacePump(); | ||
assertNotEquals(null,rm.getHoldingPump()); | ||
|
||
} | ||
|
||
|
||
@Test | ||
void givenRepairmanWithoutPipeWhenTriesToPickAPipeAtCisternThenItsPickedUp(){ | ||
Cistern cistern = new Cistern(); | ||
AddEachotherToNeighbors(pipe, cistern); | ||
rm.setPosition(cistern); | ||
assertEquals(null,rm.getHoldingPipe()); | ||
|
||
rm.LiftPipe(0); | ||
assertAll("End asserts", | ||
() ->assertNotEquals(null,rm.getHoldingPipe()), | ||
() ->assertEquals(pipe, rm.getHoldingPipe()) | ||
); | ||
} | ||
|
||
@Test | ||
void givenRepairmanWithoutPipeWhenTriesToPickAPipeDoesntExistsAtCisternThenNotPickedUp(){ | ||
Cistern cistern = new Cistern(); | ||
AddEachotherToNeighbors(pipe, cistern); | ||
rm.setPosition(cistern); | ||
assertEquals(null,rm.getHoldingPipe()); | ||
|
||
rm.LiftPipe(3); | ||
assertEquals(null,rm.getHoldingPipe()); | ||
} | ||
|
||
|
||
@Test | ||
void givenRepairmanWithoutPipeWhenTriesToPickThePipeAtPipeThenItsPickedUp(){ | ||
Cistern cistern = new Cistern(); | ||
AddEachotherToNeighbors(pipe, cistern); | ||
assertEquals(null,rm.getHoldingPipe()); | ||
|
||
rm.LiftPipe(0); | ||
assertAll("End asserts", | ||
() -> assertNotEquals(null,rm.getHoldingPipe()), | ||
() -> assertEquals(pipe, rm.getHoldingPipe()) | ||
); | ||
} | ||
|
||
|
||
@DisplayName("Grouped test for placing pipe") | ||
@Nested | ||
class PipePlacing{ | ||
|
||
@BeforeEach | ||
void setNeighborsAndPosition(){ | ||
AddEachotherToNeighbors(pipe, pump); | ||
rm.setPosition(pump); | ||
} | ||
|
||
@Disabled("The pump gets 2 more neighbors instead of 1 after connecting a pipe to it.") | ||
@Test | ||
void givenRepairmanWithPipeWhenTriesToPlaceThePipeAtNonPipeThenItsPlaced(){ | ||
rm.setHoldingPipe(new Pipe()); | ||
assertNotEquals(null,rm.getHoldingPipe()); | ||
|
||
assertEquals(1, pump.getNeighbors().size()); | ||
|
||
rm.PlacePipe(); | ||
assertAll("End asserts", | ||
() -> assertEquals(null,rm.getHoldingPipe()), | ||
() -> assertEquals(2, pump.getNeighbors().size()) | ||
); | ||
} | ||
|
||
@Test | ||
void givenRepairmanWithoutPipeWhenTriesToPlaceThePipeAtNonPipeThenNothing(){ | ||
assertEquals(null,rm.getHoldingPipe()); | ||
|
||
rm.PlacePipe(); | ||
assertAll("End asserts", | ||
() -> assertEquals(null,rm.getHoldingPipe()), | ||
() -> assertEquals(1, pump.getNeighbors().size()) | ||
); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
void AddEachotherToNeighbors(Pipe pipe,NonPipe nonPipe){ | ||
pipe.addNeighbor(nonPipe); | ||
nonPipe.addNeighbor(pipe); | ||
} | ||
|
||
|
||
|
||
} | ||
|