From 5fcb343245a98d903b2105079665a028d60e0c45 Mon Sep 17 00:00:00 2001 From: sziki12 Date: Fri, 17 May 2024 22:21:22 +0200 Subject: [PATCH 1/6] Test Base Defined Created common init for tests and Map and WaterFlow test skeleton, progress in implementation --- src/test/java/MapAndWaterFlowTest.java | 137 +++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/test/java/MapAndWaterFlowTest.java diff --git a/src/test/java/MapAndWaterFlowTest.java b/src/test/java/MapAndWaterFlowTest.java new file mode 100644 index 0000000..dc73d84 --- /dev/null +++ b/src/test/java/MapAndWaterFlowTest.java @@ -0,0 +1,137 @@ +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.awt.image.AreaAveragingScaleFilter; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class MapAndWaterFlowTest { + + Game game = Game.getInstance(); + Cistern c; + WaterSource w; + Pump p1; + Pump p2; + Pipe pi1; + Pipe pi2; + Pipe pi3; + Pipe pi4; + @BeforeEach + public void MapInit() + { + c = new Cistern(); + w = new WaterSource(); + + p1 = new Pump(); + p2 = new Pump(); + + pi1 = new Pipe(); + pi2 = new Pipe(); + pi3 = new Pipe(); + pi4 = new Pipe(); + + addNeighbor(w, pi1); + addNeighbor(pi1, p1); + addNeighbor(p1, pi2); + p1.adjust(0,1); + addNeighbor(pi2, c); + + addNeighbor(w, pi3); + addNeighbor(pi3, p2); + addNeighbor(p2, pi4); + p2.adjust(0,1); + addNeighbor(pi4, c); + + pi4.damage(); + + ArrayList elements = new ArrayList<>(List.of(w, c, p1, p2, pi1, pi2, pi3, pi4)); + ArrayList saboteurPointSources= new ArrayList<>(List.of( p1, p2, pi1, pi2, pi3, pi4)); + ArrayList pumps = new ArrayList<>(List.of( p1, p2)); + + game.load(elements,saboteurPointSources,new ArrayList<>(List.of(c)),new ArrayList<>(),new ArrayList<>(),0,0,1,2,2,pumps); + + } + + @Test + @DisplayName("Map Initialization Test") + public void MapInitTest() + { + assertAll("Pipe Neighbors Test", + () -> assertTrue(w.getNeighbors().contains(pi1)), + () -> assertTrue(w.getNeighbors().contains(pi3)), + + () -> assertTrue(pi1.getNeighbors().contains(w)), + () -> assertTrue(pi1.getNeighbors().contains(p1)), + + () -> assertTrue(pi2.getNeighbors().contains(p1)), + () -> assertTrue(pi2.getNeighbors().contains(c)), + + () -> assertTrue(pi3.getNeighbors().contains(w)), + () -> assertTrue(pi3.getNeighbors().contains(p2)), + + () -> assertTrue(pi4.getNeighbors().contains(p2)), + () -> assertTrue(pi4.getNeighbors().contains(c)), + + () -> assertTrue(c.getNeighbors().contains(pi2)), + () -> assertTrue(c.getNeighbors().contains(pi4)) + ); + + //Pumps are Not broken at first + assertFalse(p1.getBroken()); + assertFalse(p2.getBroken()); + + //If we break one it should be broken + p2.breakPump(); + assertTrue(p2.getBroken()); + + //Only pi4 should be broken + assertFalse(pi1.getHoleOnPipe()); + assertFalse(pi2.getHoleOnPipe()); + assertFalse(pi3.getHoleOnPipe()); + assertTrue(pi4.getHoleOnPipe()); + } + + @Test + @DisplayName("Water Flow Test") + public void WaterFlowTest() + { + assertAll("All Pipe and Pump is Empty", + () -> assertFalse(pi1.getContainingWater()), + () -> assertFalse(pi2.getContainingWater()), + () -> assertFalse(pi3.getContainingWater()), + () -> assertFalse(pi4.getContainingWater()), + () -> assertFalse(p1.getContainingWater()), + () -> assertFalse(p2.getContainingWater()) + ); + + game.SimulateWaterflow(); + assertAll("", + () -> assertTrue(pi1.getContainingWater()), + () -> assertTrue(pi2.getContainingWater()),//false + () -> assertTrue(pi3.getContainingWater()), + + + () -> assertTrue(p1.getContainingWater()), + () -> assertTrue(p2.getContainingWater()), + + () -> assertFalse(pi4.getContainingWater()) + ); + + } + + private void addNeighbor(NonPipe np,Pipe pi) + { + np.addNeighbor(pi); + pi.addNeighbor(np); + } + + private void addNeighbor(Pipe pi,NonPipe np) + { + np.addNeighbor(pi); + pi.addNeighbor(np); + } +} From bc2c0dd71672cbaaf250f84109fba5acd1423de6 Mon Sep 17 00:00:00 2001 From: sziki12 Date: Sat, 18 May 2024 00:05:26 +0200 Subject: [PATCH 2/6] Water flow test extension --- src/test/java/MapAndWaterFlowTest.java | 39 +++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/test/java/MapAndWaterFlowTest.java b/src/test/java/MapAndWaterFlowTest.java index dc73d84..30c507b 100644 --- a/src/test/java/MapAndWaterFlowTest.java +++ b/src/test/java/MapAndWaterFlowTest.java @@ -1,9 +1,7 @@ -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.awt.image.AreaAveragingScaleFilter; import java.util.ArrayList; import java.util.List; @@ -96,10 +94,10 @@ public void MapInitTest() } @Test - @DisplayName("Water Flow Test") - public void WaterFlowTest() + @DisplayName("Full Water Flow Test") + public void FullWaterFlowTest() { - assertAll("All Pipe and Pump is Empty", + assertAll("Stage 1, All Pipe and Pump is Empty", () -> assertFalse(pi1.getContainingWater()), () -> assertFalse(pi2.getContainingWater()), () -> assertFalse(pi3.getContainingWater()), @@ -107,20 +105,41 @@ public void WaterFlowTest() () -> assertFalse(p1.getContainingWater()), () -> assertFalse(p2.getContainingWater()) ); - game.SimulateWaterflow(); - assertAll("", + assertAll("Stage 2", () -> assertTrue(pi1.getContainingWater()), - () -> assertTrue(pi2.getContainingWater()),//false () -> assertTrue(pi3.getContainingWater()), + //True, because on P1 turn W fills pi1 and pi3, and from pi3 water can be extracted + () -> assertTrue(p2.getContainingWater()), + () -> assertFalse(p1.getContainingWater()), + () -> assertFalse(pi2.getContainingWater()), + () -> assertFalse(pi4.getContainingWater()) + ); + game.SimulateWaterflow(); + assertAll("Stage 3", + () -> assertTrue(pi1.getContainingWater()), + () -> assertTrue(pi3.getContainingWater()), () -> assertTrue(p1.getContainingWater()), () -> assertTrue(p2.getContainingWater()), + //True, because water can be pumped through a leaking pipe + () -> assertTrue(pi4.getContainingWater()), - () -> assertFalse(pi4.getContainingWater()) - ); + //Water haven't reached pi2 yet + () -> assertFalse(pi2.getContainingWater()) + ); + game.SimulateWaterflow(); + assertAll("Stage 4, System filled with water", + () -> assertTrue(pi1.getContainingWater()), + () -> assertTrue(pi3.getContainingWater()), + () -> assertTrue(p1.getContainingWater()), + () -> assertTrue(p2.getContainingWater()), + () -> assertTrue(pi2.getContainingWater()), + //True, because water can be pumped through a leaking pipe + () -> assertTrue(pi4.getContainingWater()) + ); } private void addNeighbor(NonPipe np,Pipe pi) From e6368f99b19d73f8afeb485920a14d1ea8b227ca Mon Sep 17 00:00:00 2001 From: sziki12 Date: Sat, 18 May 2024 18:17:14 +0200 Subject: [PATCH 3/6] Tests Completed Waterflow, Point Calculation and Map Setup tests --- src/test/java/MapAndWaterFlowTest.java | 156 ------------------------- src/test/java/MapTest.java | 98 ++++++++++++++++ src/test/java/MapTestBase.java | 106 +++++++++++++++++ src/test/java/PointsTest.java | 39 +++++++ src/test/java/WaterflowTest.java | 58 +++++++++ 5 files changed, 301 insertions(+), 156 deletions(-) delete mode 100644 src/test/java/MapAndWaterFlowTest.java create mode 100644 src/test/java/MapTest.java create mode 100644 src/test/java/MapTestBase.java create mode 100644 src/test/java/PointsTest.java create mode 100644 src/test/java/WaterflowTest.java diff --git a/src/test/java/MapAndWaterFlowTest.java b/src/test/java/MapAndWaterFlowTest.java deleted file mode 100644 index 30c507b..0000000 --- a/src/test/java/MapAndWaterFlowTest.java +++ /dev/null @@ -1,156 +0,0 @@ -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; - -public class MapAndWaterFlowTest { - - Game game = Game.getInstance(); - Cistern c; - WaterSource w; - Pump p1; - Pump p2; - Pipe pi1; - Pipe pi2; - Pipe pi3; - Pipe pi4; - @BeforeEach - public void MapInit() - { - c = new Cistern(); - w = new WaterSource(); - - p1 = new Pump(); - p2 = new Pump(); - - pi1 = new Pipe(); - pi2 = new Pipe(); - pi3 = new Pipe(); - pi4 = new Pipe(); - - addNeighbor(w, pi1); - addNeighbor(pi1, p1); - addNeighbor(p1, pi2); - p1.adjust(0,1); - addNeighbor(pi2, c); - - addNeighbor(w, pi3); - addNeighbor(pi3, p2); - addNeighbor(p2, pi4); - p2.adjust(0,1); - addNeighbor(pi4, c); - - pi4.damage(); - - ArrayList elements = new ArrayList<>(List.of(w, c, p1, p2, pi1, pi2, pi3, pi4)); - ArrayList saboteurPointSources= new ArrayList<>(List.of( p1, p2, pi1, pi2, pi3, pi4)); - ArrayList pumps = new ArrayList<>(List.of( p1, p2)); - - game.load(elements,saboteurPointSources,new ArrayList<>(List.of(c)),new ArrayList<>(),new ArrayList<>(),0,0,1,2,2,pumps); - - } - - @Test - @DisplayName("Map Initialization Test") - public void MapInitTest() - { - assertAll("Pipe Neighbors Test", - () -> assertTrue(w.getNeighbors().contains(pi1)), - () -> assertTrue(w.getNeighbors().contains(pi3)), - - () -> assertTrue(pi1.getNeighbors().contains(w)), - () -> assertTrue(pi1.getNeighbors().contains(p1)), - - () -> assertTrue(pi2.getNeighbors().contains(p1)), - () -> assertTrue(pi2.getNeighbors().contains(c)), - - () -> assertTrue(pi3.getNeighbors().contains(w)), - () -> assertTrue(pi3.getNeighbors().contains(p2)), - - () -> assertTrue(pi4.getNeighbors().contains(p2)), - () -> assertTrue(pi4.getNeighbors().contains(c)), - - () -> assertTrue(c.getNeighbors().contains(pi2)), - () -> assertTrue(c.getNeighbors().contains(pi4)) - ); - - //Pumps are Not broken at first - assertFalse(p1.getBroken()); - assertFalse(p2.getBroken()); - - //If we break one it should be broken - p2.breakPump(); - assertTrue(p2.getBroken()); - - //Only pi4 should be broken - assertFalse(pi1.getHoleOnPipe()); - assertFalse(pi2.getHoleOnPipe()); - assertFalse(pi3.getHoleOnPipe()); - assertTrue(pi4.getHoleOnPipe()); - } - - @Test - @DisplayName("Full Water Flow Test") - public void FullWaterFlowTest() - { - assertAll("Stage 1, All Pipe and Pump is Empty", - () -> assertFalse(pi1.getContainingWater()), - () -> assertFalse(pi2.getContainingWater()), - () -> assertFalse(pi3.getContainingWater()), - () -> assertFalse(pi4.getContainingWater()), - () -> assertFalse(p1.getContainingWater()), - () -> assertFalse(p2.getContainingWater()) - ); - game.SimulateWaterflow(); - assertAll("Stage 2", - () -> assertTrue(pi1.getContainingWater()), - () -> assertTrue(pi3.getContainingWater()), - //True, because on P1 turn W fills pi1 and pi3, and from pi3 water can be extracted - () -> assertTrue(p2.getContainingWater()), - - () -> assertFalse(p1.getContainingWater()), - () -> assertFalse(pi2.getContainingWater()), - () -> assertFalse(pi4.getContainingWater()) - ); - - game.SimulateWaterflow(); - assertAll("Stage 3", - () -> assertTrue(pi1.getContainingWater()), - () -> assertTrue(pi3.getContainingWater()), - () -> assertTrue(p1.getContainingWater()), - () -> assertTrue(p2.getContainingWater()), - //True, because water can be pumped through a leaking pipe - () -> assertTrue(pi4.getContainingWater()), - - //Water haven't reached pi2 yet - () -> assertFalse(pi2.getContainingWater()) - - ); - game.SimulateWaterflow(); - assertAll("Stage 4, System filled with water", - () -> assertTrue(pi1.getContainingWater()), - () -> assertTrue(pi3.getContainingWater()), - () -> assertTrue(p1.getContainingWater()), - () -> assertTrue(p2.getContainingWater()), - () -> assertTrue(pi2.getContainingWater()), - //True, because water can be pumped through a leaking pipe - () -> assertTrue(pi4.getContainingWater()) - ); - } - - private void addNeighbor(NonPipe np,Pipe pi) - { - np.addNeighbor(pi); - pi.addNeighbor(np); - } - - private void addNeighbor(Pipe pi,NonPipe np) - { - np.addNeighbor(pi); - pi.addNeighbor(np); - } -} diff --git a/src/test/java/MapTest.java b/src/test/java/MapTest.java new file mode 100644 index 0000000..6948ad4 --- /dev/null +++ b/src/test/java/MapTest.java @@ -0,0 +1,98 @@ +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertFalse; + +public class MapTest extends MapTestBase +{ + @Test + @DisplayName("Map Initialization Test") + public void MapInitTest() + { + TestNeighbors(); + + TestPumpBreak(); + + TestHole(); + + TestSliminess(); + + TestStickiness(); + } + + private void TestNeighbors() + { + assertAll("Pipe Neighbors Test", + () -> assertTrue(w.getNeighbors().contains(pi1)), + () -> assertTrue(w.getNeighbors().contains(pi3)), + + () -> assertTrue(pi1.getNeighbors().contains(w)), + () -> assertTrue(pi1.getNeighbors().contains(p1)), + + () -> assertTrue(pi2.getNeighbors().contains(p1)), + () -> assertTrue(pi2.getNeighbors().contains(c)), + + () -> assertTrue(pi3.getNeighbors().contains(w)), + () -> assertTrue(pi3.getNeighbors().contains(p2)), + + () -> assertTrue(pi4.getNeighbors().contains(p2)), + () -> assertTrue(pi4.getNeighbors().contains(c)), + + () -> assertTrue(c.getNeighbors().contains(pi2)), + () -> assertTrue(c.getNeighbors().contains(pi4)) + ); + } + + private void TestStickiness() + { + assertEquals(0, pi1.getSticky()); + assertEquals(0, pi2.getSticky()); + assertEquals(0, pi3.getSticky()); + assertEquals(0, pi4.getSticky()); + + pi3.stick(); + + assertEquals(0, pi1.getSticky()); + assertEquals(0, pi2.getSticky()); + assertEquals(0, pi4.getSticky()); + assertNotEquals(0, pi3.getSticky()); + } + + private void TestHole() + { + //Only pi4 should be broken + assertFalse(pi1.getHoleOnPipe()); + assertFalse(pi2.getHoleOnPipe()); + assertFalse(pi3.getHoleOnPipe()); + assertTrue(pi4.getHoleOnPipe()); + } + + private void TestSliminess() + { + assertEquals(0, pi1.getSlimey()); + assertEquals(0, pi2.getSlimey()); + assertEquals(0, pi3.getSlimey()); + assertEquals(0, pi4.getSlimey()); + + pi2.slime(); + + assertEquals(0, pi1.getSlimey()); + assertEquals(0, pi3.getSlimey()); + assertEquals(0, pi4.getSlimey()); + assertNotEquals(0, pi2.getSlimey()); + } + + private void TestPumpBreak() + { + //Pumps are Not broken at first + assertFalse(p1.getBroken()); + assertFalse(p2.getBroken()); + + //If we break one it should be broken + p2.breakPump(); + + assertTrue(p2.getBroken()); + assertFalse(p1.getBroken()); + } +} diff --git a/src/test/java/MapTestBase.java b/src/test/java/MapTestBase.java new file mode 100644 index 0000000..7fb2551 --- /dev/null +++ b/src/test/java/MapTestBase.java @@ -0,0 +1,106 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.List; + + +abstract class MapTestBase{ + + protected Game game = Game.getInstance(); + protected Cistern c; + protected WaterSource w; + protected Pump p1; + protected Pump p2; + protected Pipe pi1; + protected Pipe pi2; + protected Pipe pi3; + protected Pipe pi4; + protected int repairmanPoints = 0; + protected int saboteurPoints = 0; + + protected ArrayList elements; + protected ArrayList saboteurPointSources; + protected ArrayList pumps; + @BeforeEach + public void MapInit() + { + c = new Cistern(); + c.setName("c"); + + w = new WaterSource(); + w.setName("w"); + + p1 = new Pump(); + p1.setName("p1"); + + p2 = new Pump(); + p2.setName("p2"); + + pi1 = new Pipe(); + pi1.setName("pi1"); + pi2 = new Pipe(); + pi2.setName("pi2"); + pi3 = new Pipe(); + pi3.setName("pi3"); + pi4 = new Pipe(); + pi4.setName("pi4"); + + addNeighbor(w, pi1); + addNeighbor(pi1, p1); + addNeighbor(p1, pi2); + p1.adjust(0,1); + addNeighbor(pi2, c); + + addNeighbor(w, pi3); + addNeighbor(pi3, p2); + addNeighbor(p2, pi4); + p2.adjust(0,1); + addNeighbor(pi4, c); + + pi4.damage(); + + elements = new ArrayList<>(List.of(w, c, p1, p2, pi1, pi2, pi3, pi4)); + saboteurPointSources= new ArrayList<>(List.of( p1, p2, pi1, pi2, pi3, pi4)); + pumps = new ArrayList<>(List.of( p1, p2)); + + game.load(elements,saboteurPointSources,new ArrayList<>(List.of(c)),new ArrayList<>(),new ArrayList<>(),0,0,1,2,2,pumps); + } + + protected void addNeighbor(NonPipe np,Pipe pi) + { + np.addNeighbor(pi); + pi.addNeighbor(np); + } + + protected void addNeighbor(Pipe pi,NonPipe np) + { + np.addNeighbor(pi); + pi.addNeighbor(np); + } + + protected void SimulateWaterFlowMock() + { + game.SimulateWaterflow(); + CalculatePointsMock(List.of(c),saboteurPointSources); + } + + protected void CalculatePointsMock(List cisterns, List saboteurPointSources) + { + int repairmanWater = 0; + int saboteurWater = 0; + for (Cistern cistern : cisterns) { + int ciWater = cistern.measureAndResetWaterFlown(); + repairmanWater += ciWater; + } + + for (SaboteurPointSource saboteurPointSource : saboteurPointSources) { + int sWater = saboteurPointSource.measureAndResetLeakedWaterAmount(); + saboteurWater += sWater; + } + repairmanPoints+=repairmanWater; + saboteurPoints+=saboteurWater; + } +} diff --git a/src/test/java/PointsTest.java b/src/test/java/PointsTest.java new file mode 100644 index 0000000..378d1a5 --- /dev/null +++ b/src/test/java/PointsTest.java @@ -0,0 +1,39 @@ +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.util.List; + +public class PointsTest extends MapTestBase +{ + @Test + @DisplayName("Calculate Points After Waterflow Test") + public void CalculatePointsTest() + { + assertAll("Stage 1, All Pipe and Pump is Empty", + () -> assertFalse(pi1.getContainingWater()), + () -> assertFalse(pi2.getContainingWater()), + () -> assertFalse(pi3.getContainingWater()), + () -> assertFalse(pi4.getContainingWater()), + () -> assertFalse(p1.getContainingWater()), + () -> assertFalse(p2.getContainingWater()) + ); + //All Point is 0 before start + CalculatePointsMock(List.of(c),saboteurPointSources); + assertEquals(0,repairmanPoints); + assertEquals(0,saboteurPoints); + + //3 iterations of Water Flow Simulation, to water reach the leaking pipe + SimulateWaterFlowMock(); + SimulateWaterFlowMock(); + SimulateWaterFlowMock(); + + assertEquals(0,repairmanPoints); + assertEquals(1,saboteurPoints); + + //Water reaches the Cistern + SimulateWaterFlowMock(); + assertEquals(1,repairmanPoints); + assertEquals(2,saboteurPoints); + } +} diff --git a/src/test/java/WaterflowTest.java b/src/test/java/WaterflowTest.java new file mode 100644 index 0000000..1cb0f14 --- /dev/null +++ b/src/test/java/WaterflowTest.java @@ -0,0 +1,58 @@ +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class WaterflowTest extends MapTestBase +{ + @Test + @DisplayName("Only Water Flow Test") + public void FullWaterFlowTest() + { + assertAll("Stage 1, All Pipe and Pump is Empty", + () -> assertFalse(pi1.getContainingWater()), + () -> assertFalse(pi2.getContainingWater()), + () -> assertFalse(pi3.getContainingWater()), + () -> assertFalse(pi4.getContainingWater()), + () -> assertFalse(p1.getContainingWater()), + () -> assertFalse(p2.getContainingWater()) + ); + SimulateWaterFlowMock(); + assertAll("Stage 2", + () -> assertTrue(pi1.getContainingWater()), + () -> assertTrue(pi3.getContainingWater()), + //True, because on P1 turn W fills pi1 and pi3, and from pi3 water can be extracted + () -> assertTrue(p2.getContainingWater()), + + () -> assertFalse(p1.getContainingWater()), + () -> assertFalse(pi2.getContainingWater()), + () -> assertFalse(pi4.getContainingWater()) + ); + + SimulateWaterFlowMock(); + assertAll("Stage 3", + () -> assertTrue(pi1.getContainingWater()), + () -> assertTrue(pi3.getContainingWater()), + () -> assertTrue(p1.getContainingWater()), + () -> assertTrue(p2.getContainingWater()), + //True, because water can be pumped through a leaking pipe + () -> assertTrue(pi4.getContainingWater()), + + //Water haven't reached pi2 yet + () -> assertFalse(pi2.getContainingWater()) + + ); + + SimulateWaterFlowMock(); + assertAll("Stage 4, System filled with water", + () -> assertTrue(pi1.getContainingWater()), + () -> assertTrue(pi3.getContainingWater()), + () -> assertTrue(p1.getContainingWater()), + () -> assertTrue(p2.getContainingWater()), + () -> assertTrue(pi2.getContainingWater()), + //True, because water can be pumped through a leaking pipe + () -> assertTrue(pi4.getContainingWater()) + ); + } +} From eb9a6f0eb3ce8f9cf97b862d376d5abceadeda03 Mon Sep 17 00:00:00 2001 From: sziki12 Date: Sat, 18 May 2024 18:20:07 +0200 Subject: [PATCH 4/6] Pipe Bug Fix Pipe was leaking water without water in it. --- src/main/java/Pipe.java | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/Pipe.java b/src/main/java/Pipe.java index b7ca566..b29d2c5 100644 --- a/src/main/java/Pipe.java +++ b/src/main/java/Pipe.java @@ -27,7 +27,7 @@ public class Pipe extends Element implements SaboteurPointSource { /** * Beállítja a betöltés során a Pipe változóit. - * + * * @param hole holeOnPipe * @param leakedWater leakedWaterAmount * @param _slimey _slimey @@ -45,7 +45,7 @@ public class Pipe extends Element implements SaboteurPointSource { /** * Ot kell meghivni ha ra szeretnek lepni a csore. - * + * * @param c - a karakterunk * @return boolean - a ralepes sikeressege */ @@ -87,7 +87,7 @@ public boolean accept(Character c) { /** * Lelepes egy csorol - * + * * @param c a karakter * @return Sikeres volt-e a lelepes */ @@ -110,7 +110,7 @@ public boolean remove(Character c) { */ @Override public void step() { - if (holeOnPipe) { + if (holeOnPipe&&containingWater) { containingWater = false; leakedWaterAmount++; } @@ -155,7 +155,7 @@ public void damage() { /** * Letrehoz egy uj csovet, majd koze es a meglevo cso koze lehelyezi a pumpat. - * + * * @param holdingPump - a lehelyezni kivant pumpa * @return Pipe - az ujonnan letrehozott cso */ @@ -192,7 +192,7 @@ public Pipe placePump(Pump holdingPump) { /** * Olyan cso felemelesenel hasznaljuk, amelyiknek az egyik fele nincs sehova * bekotve. - * + * * @param dir - nem hasznaljuk ebben a megvalositsban * @return Pipe - a cso, amit felemelunk */ @@ -215,7 +215,7 @@ public Pipe lift(int dir) { /** * Uj szomszed hozzacsatlakoztatasa a csohoz. - * + * * @param n - a csatlakoztatni kivant szomszed */ public void addNeighbor(NonPipe n) { @@ -226,7 +226,7 @@ public void addNeighbor(NonPipe n) { /** * Ezen keresztul lehet rola szomszedot lecsatlakoztatni. - * + * * @param n - a lecsatlakoztatni kivant szomszed */ public void removeNeighbor(NonPipe n) { @@ -235,7 +235,7 @@ public void removeNeighbor(NonPipe n) { /** * Visszadja a kifolyott viz mennyiseget, majd nullara allitja - * + * * @return int - a kifolyott viz mennyisege */ @Override @@ -247,7 +247,7 @@ public int measureAndResetLeakedWaterAmount() { /** * Kiszivja az adott csobol a vizet - * + * * @return boolean - volt-e benne viz */ public boolean waterExtraction() { @@ -262,7 +262,7 @@ public boolean waterExtraction() { /** * Vizet probal a csobe tenni - * + * * @return boolean - sikerult-e bele vizet tenni */ public boolean giveWater() { @@ -292,7 +292,7 @@ public void slime() { /** * Visszaadja a szomszedait - * + * * @return List - a szomszedok */ public List getNeighbors() { From 7c2b205b881b802b13adf5a633d6e420afb42cdc Mon Sep 17 00:00:00 2001 From: sziki12 Date: Sat, 18 May 2024 18:57:13 +0200 Subject: [PATCH 5/6] GUI for Unit Test --- .github/workflows/maven.yml | 56 ++++++++++++++++++++----------------- execute-on-vnc.sh | 32 +++++++++++++++++++++ 2 files changed, 62 insertions(+), 26 deletions(-) create mode 100644 execute-on-vnc.sh diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index de32502..5468b41 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -20,29 +20,33 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - cache: maven - - name: Build with Maven - run: mvn -B package --file pom.xml - - name: Cache SonarCloud packages - uses: actions/cache@v3 - with: - path: ~/.sonar/cache - key: ${{ runner.os }}-sonar - restore-keys: ${{ runner.os }}-sonar - - name: Cache Maven packages - uses: actions/cache@v3 - with: - path: ~/.m2 - key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} - restore-keys: ${{ runner.os }}-m2 - - name: Build and analyze - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BME-MIT-IET_iet-hf-2024-iet-arcok + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + cache: maven + - name: Install TightVnc for UI tests + run: sudo apt-get install tightvncserver + - name: Add permission to run vnc script + run: chmod +x ./execute-on-vnc.sh + - name: Build with Maven + run: ./execute-on-vnc.sh mvn -B package --file pom.xml + - name: Cache SonarCloud packages + uses: actions/cache@v3 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + - name: Cache Maven packages + uses: actions/cache@v3 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Build and analyze + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: ./execute-on-vnc.sh mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BME-MIT-IET_iet-hf-2024-iet-arcok \ No newline at end of file diff --git a/execute-on-vnc.sh b/execute-on-vnc.sh new file mode 100644 index 0000000..5a2b699 --- /dev/null +++ b/execute-on-vnc.sh @@ -0,0 +1,32 @@ +#/bin/bash +NEW_DISPLAY=42 +DONE="no" + +umask 0077 +mkdir -p "$HOME/.vnc" +chmod go-rwx "$HOME/.vnc" +vncpasswd -f <<<"$password" >"$HOME/.vnc/passwd" + +while [ "$DONE" == "no" ] +do + out=$(xdpyinfo -display :${NEW_DISPLAY} 2>&1) + if [[ "$out" == name* ]] || [[ "$out" == Invalid* ]] + then + # command succeeded; or failed with access error; display exists + (( NEW_DISPLAY+=1 )) + else + # display doesn't exist + DONE="yes" + fi +done + +echo "Using first available display :${NEW_DISPLAY}" + +OLD_DISPLAY=${DISPLAY} +vncserver ":${NEW_DISPLAY}" -localhost -geometry 1600x1200 -depth 16 +export DISPLAY=:${NEW_DISPLAY} + +"$@" + +export DISPLAY=${OLD_DISPLAY} +vncserver -kill ":${NEW_DISPLAY}" \ No newline at end of file From 9dfcecbce3ab2b1b635b6ec329c14854181277a3 Mon Sep 17 00:00:00 2001 From: sziki12 Date: Tue, 21 May 2024 16:03:20 +0200 Subject: [PATCH 6/6] Function Call Test and Pump fix Waterflow function test by invocation count for different elements of the map. Fix for Pump step, now it correctly calls the step of the neighbour pipes. --- src/main/java/Pump.java | 6 ++ src/test/java/WaterflowFunctionCallTest.java | 80 ++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/test/java/WaterflowFunctionCallTest.java diff --git a/src/main/java/Pump.java b/src/main/java/Pump.java index 041c49f..fd1542b 100644 --- a/src/main/java/Pump.java +++ b/src/main/java/Pump.java @@ -136,6 +136,12 @@ public void step(){ containingWater=true; } } + //Release water form leaking pipe neighbors + for(Element neighbor : neighbors) + { + if(outputPipe!=neighbor) + neighbor.step(); + } for(Element neighbor : inputPipe.getNeighbors()) { if(this!=neighbor) diff --git a/src/test/java/WaterflowFunctionCallTest.java b/src/test/java/WaterflowFunctionCallTest.java new file mode 100644 index 0000000..5523287 --- /dev/null +++ b/src/test/java/WaterflowFunctionCallTest.java @@ -0,0 +1,80 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.List; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class WaterflowFunctionCallTest extends MapTestBase{ + + @Override + @BeforeEach + public void MapInit() { + c = Mockito.spy(new Cistern()); + c.setName("Spy c"); + + w = Mockito.spy(new WaterSource()); + w.setName("Spy w"); + + p1 = Mockito.spy(new Pump()); + p1.setName("Spy p1"); + + p2 = Mockito.spy(new Pump()); + p2.setName("Spy p2"); + + pi1 = Mockito.spy(new Pipe()); + pi1.setName("Spy pi1"); + pi2 = Mockito.spy(new Pipe()); + pi2.setName("Spy pi2"); + pi3 = Mockito.spy(new Pipe()); + pi3.setName("Spy pi3"); + pi4 = Mockito.spy(new Pipe()); + pi4.setName("Spy pi4"); + + addNeighbor(w, pi1); + addNeighbor(pi1, p1); + addNeighbor(p1, pi2); + p1.adjust(0,1); + addNeighbor(pi2, c); + + addNeighbor(w, pi3); + addNeighbor(pi3, p2); + addNeighbor(p2, pi4); + p2.adjust(0,1); + addNeighbor(pi4, c); + + pi4.damage(); + + elements = new ArrayList<>(List.of(w, c, p1, p2, pi1, pi2, pi3, pi4)); + saboteurPointSources= new ArrayList<>(List.of( p1, p2, pi1, pi2, pi3, pi4)); + pumps = new ArrayList<>(List.of( p1, p2)); + + game.load(elements,saboteurPointSources,new ArrayList<>(List.of(c)),new ArrayList<>(),new ArrayList<>(),0,0,1,2,2,pumps); + } + + @Test + @DisplayName("Water Flow Function Call Test") + public void WaterflowInvocationTest() + { + SimulateWaterFlowMock(); + //The Cistern is the beginning of water simulation + verify(c,times(1)).step(); + + //WaterSource called 2 times, because 2 path of water can reach it + verify(w,times(2)).step(); + + verify(p1,times(1)).step(); + verify(p2,times(1)).step(); + + verify(pi1,times(1)).step(); + verify(pi2,times(1)).step(); + verify(pi3,times(1)).step(); + //Damaging the pipe also calls step, beside the water flow simulation + verify(pi4,times(2)).step(); + + } +}