diff --git a/battery/pom.xml b/battery/pom.xml index 9a66e90321c7eea826414b9f9c0ddbf19d7a904c..6bbae2f91f0823dbefc7d940d71cac34bf2be45d 100644 --- a/battery/pom.xml +++ b/battery/pom.xml @@ -25,6 +25,12 @@ 4.13.1 test + + org.awaitility + awaitility + 4.3.0 + test + diff --git a/battery/src/main/java/fr/ufrst/m1info/gl/tpariney/Battery.java b/battery/src/main/java/fr/ufrst/m1info/gl/tpariney/Battery.java index a78334984b9aea658c59bc49325d289930b235e2..2b68abeb35818232aabc403d07acd760b73a7c99 100644 --- a/battery/src/main/java/fr/ufrst/m1info/gl/tpariney/Battery.java +++ b/battery/src/main/java/fr/ufrst/m1info/gl/tpariney/Battery.java @@ -6,7 +6,7 @@ import java.util.TimerTask; public class Battery { - private final long CHARGE_TOP = 1000; + private static final long CHARGE_TOP = 1000; private float chargeLevel; public Battery() { diff --git a/battery/src/test/java/fr/ufrst/m1info/gl/tpariney/BatteryUnitTest.java b/battery/src/test/java/fr/ufrst/m1info/gl/tpariney/BatteryUnitTest.java index dd6ddb1484f5e07b9b26c93705feb3556a4867d9..2464cd3654418e55c79b9347ae29dafe0c41fb1c 100644 --- a/battery/src/test/java/fr/ufrst/m1info/gl/tpariney/BatteryUnitTest.java +++ b/battery/src/test/java/fr/ufrst/m1info/gl/tpariney/BatteryUnitTest.java @@ -1,9 +1,12 @@ package fr.ufrst.m1info.gl.tpariney; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static org.awaitility.Awaitility.await; + public class BatteryUnitTest { @@ -15,16 +18,17 @@ public class BatteryUnitTest { Assert.assertEquals(111f, cell.getChargeLevel(),0); } - @Ignore @Test public void testSetup() throws InterruptedException { Battery cell = new Battery(); Assert.assertEquals(100f, cell.getChargeLevel(), 0); cell.setUp(); - Thread.sleep(1000); - Assert.assertEquals(111f, cell.getChargeLevel(), 0); - Thread.sleep(1000); - Assert.assertEquals(123.100006f, cell.getChargeLevel(), 0); + await().atMost(1100, TimeUnit.MILLISECONDS).until( + () -> 111f == cell.getChargeLevel() + ); + await().atMost(1100, TimeUnit.MILLISECONDS).until( + () -> 123.100006f == cell.getChargeLevel() + ); } @Test (expected = InsufficientChargeException.class) diff --git a/driver/src/main/java/fr/ufrst/m1info/gl/tpariney/App.java b/driver/src/main/java/fr/ufrst/m1info/gl/tpariney/App.java index e84cfe20ef3926747619c0ff575192d864e25c86..7d7aabdf8a8b2ab6778d1c1a2731fcbaf2b38d8b 100644 --- a/driver/src/main/java/fr/ufrst/m1info/gl/tpariney/App.java +++ b/driver/src/main/java/fr/ufrst/m1info/gl/tpariney/App.java @@ -1,24 +1,29 @@ package fr.ufrst.m1info.gl.tpariney; import java.util.Random; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Hello world! * */ public class App { - public static void main( String[] args ) - { + private static final Logger logger = Logger.getLogger("RobotDriver"); + + public static void main( String[] args ) throws InterruptedException { try { Robot robot = new Robot(); robot.land(new Coordinates(3, 0), new LandSensor(new Random())); - System.out.println("position du robot : (" + robot.getXposition() + - ", " + robot.getYposition() + ")"); + if (logger.isLoggable(Level.INFO)) { + logger.info(String.format("position du robot : (%d, %d)", robot.getXposition(), robot.getYposition())); + } robot.moveForward(); - System.out.println("position du robot : (" + robot.getXposition() + - ", " + robot.getYposition() + ")"); - } catch (UnlandedRobotException | InterruptedException e) { - e.printStackTrace(); + if (logger.isLoggable(Level.INFO)) { + logger.info(String.format("position du robot : (%d, %d)", robot.getXposition(), robot.getYposition())); + } + } catch (UnlandedRobotException e) { + logger.log(Level.SEVERE, e.getMessage(), e); } } } diff --git a/maptools/src/main/java/fr/ufrst/m1info/gl/tpariney/MapTools.java b/maptools/src/main/java/fr/ufrst/m1info/gl/tpariney/MapTools.java index 32a2b224ef3681252ae07a26327fc87aadd43daf..a57b97047331011e2fd673e08867b720571c1c7b 100644 --- a/maptools/src/main/java/fr/ufrst/m1info/gl/tpariney/MapTools.java +++ b/maptools/src/main/java/fr/ufrst/m1info/gl/tpariney/MapTools.java @@ -1,6 +1,9 @@ package fr.ufrst.m1info.gl.tpariney; public class MapTools { + private MapTools() { + throw new IllegalStateException("Utility class"); + } static Coordinates nextForwardPosition(Coordinates position, Direction direction) { if (direction == Direction.NORTH) diff --git a/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/CoordinatesUnitTest.java b/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/CoordinatesUnitTest.java new file mode 100644 index 0000000000000000000000000000000000000000..81beb707bc95a40e91bf0d3333160bac35c5c5cd --- /dev/null +++ b/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/CoordinatesUnitTest.java @@ -0,0 +1,13 @@ +package fr.ufrst.m1info.gl.tpariney; + +import org.junit.Assert; +import org.junit.Test; + +public class CoordinatesUnitTest { + @Test + public void testGetters() { + Coordinates coordinates = new Coordinates(1, 2); + Assert.assertEquals(1, coordinates.getX()); + Assert.assertEquals(2, coordinates.getY()); + } +} diff --git a/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/MapToolsUnitTests.java b/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/MapToolsUnitTests.java new file mode 100644 index 0000000000000000000000000000000000000000..4f68915e3da0f81ac2be7b509ca4cec2b97b92da --- /dev/null +++ b/maptools/src/test/java/fr/ufrst/m1info/gl/tpariney/MapToolsUnitTests.java @@ -0,0 +1,56 @@ +package fr.ufrst.m1info.gl.tpariney; + +import org.junit.Assert; +import org.junit.Test; + +public class MapToolsUnitTests { + @Test + public void nextForwardPosition() { + Coordinates position = new Coordinates(0, 0); + Coordinates north = MapTools.nextForwardPosition(position, Direction.NORTH); + Assert.assertEquals(0, north.getX()); + Assert.assertEquals(1, north.getY()); + Coordinates south = MapTools.nextForwardPosition(position, Direction.SOUTH); + Assert.assertEquals(0, south.getX()); + Assert.assertEquals(-1, south.getY()); + Coordinates east = MapTools.nextForwardPosition(position, Direction.EAST); + Assert.assertEquals(1, east.getX()); + Assert.assertEquals(0, east.getY()); + Coordinates west = MapTools.nextForwardPosition(position, Direction.WEST); + Assert.assertEquals(-1, west.getX()); + Assert.assertEquals(0, west.getY()); + } + + @Test + public void nextBackwardPosition() { + Coordinates position = new Coordinates(0, 0); + Coordinates north = MapTools.nextBackwardPosition(position, Direction.NORTH); + Assert.assertEquals(0, north.getX()); + Assert.assertEquals(-1, north.getY()); + Coordinates south = MapTools.nextBackwardPosition(position, Direction.SOUTH); + Assert.assertEquals(0, south.getX()); + Assert.assertEquals(1, south.getY()); + Coordinates east = MapTools.nextBackwardPosition(position, Direction.EAST); + Assert.assertEquals(-1, east.getX()); + Assert.assertEquals(0, east.getY()); + Coordinates west = MapTools.nextBackwardPosition(position, Direction.WEST); + Assert.assertEquals(1, west.getX()); + Assert.assertEquals(0, west.getY()); + } + + @Test + public void counterClockwise() { + Assert.assertEquals(Direction.WEST, MapTools.counterclockwise(Direction.NORTH)); + Assert.assertEquals(Direction.SOUTH, MapTools.counterclockwise(Direction.WEST)); + Assert.assertEquals(Direction.EAST, MapTools.counterclockwise(Direction.SOUTH)); + Assert.assertEquals(Direction.NORTH, MapTools.counterclockwise(Direction.EAST)); + } + + @Test + public void clockwise() { + Assert.assertEquals(Direction.EAST, MapTools.clockwise(Direction.NORTH)); + Assert.assertEquals(Direction.SOUTH, MapTools.clockwise(Direction.EAST)); + Assert.assertEquals(Direction.WEST, MapTools.clockwise(Direction.SOUTH)); + Assert.assertEquals(Direction.NORTH, MapTools.clockwise(Direction.WEST)); + } +} diff --git a/roadBook/src/main/java/fr/ufrst/m1info/gl/tpariney/RoadBook.java b/roadBook/src/main/java/fr/ufrst/m1info/gl/tpariney/RoadBook.java index 10b5dbff2564f10611e03cc08881f9f1b7e6256f..727be1eac3133a7be8acbe5dd0da117ea75b897b 100644 --- a/roadBook/src/main/java/fr/ufrst/m1info/gl/tpariney/RoadBook.java +++ b/roadBook/src/main/java/fr/ufrst/m1info/gl/tpariney/RoadBook.java @@ -6,16 +6,12 @@ import java.util.Iterator; import java.util.List; public class RoadBook { - private List instructions; private final Iterator instructionIterator; - public RoadBook(List instructions) { - this.instructions = instructions; instructionIterator = instructions.iterator(); } - public boolean hasInstruction() { return instructionIterator.hasNext(); } diff --git a/roadBook/src/main/java/fr/ufrst/m1info/gl/tpariney/RoadBookCalculator.java b/roadBook/src/main/java/fr/ufrst/m1info/gl/tpariney/RoadBookCalculator.java index 0370adae8aa3bdb0aaaeee41669549e14227f407..8cab53fb66947473701bb0f0cf651c118c886872 100644 --- a/roadBook/src/main/java/fr/ufrst/m1info/gl/tpariney/RoadBookCalculator.java +++ b/roadBook/src/main/java/fr/ufrst/m1info/gl/tpariney/RoadBookCalculator.java @@ -4,9 +4,12 @@ import java.util.ArrayList; import java.util.List; public class RoadBookCalculator { + private RoadBookCalculator() { + throw new IllegalStateException("Utility class"); + } - public static RoadBook calculateRoadBook(Direction direction, Coordinates position, Coordinates destination, ArrayList instructions) { - List directionList = new ArrayList(); + public static RoadBook calculateRoadBook(Direction direction, Coordinates position, Coordinates destination, List instructions) { + List directionList = new ArrayList<>(); if (destination.getX() < position.getX()) directionList.add(Direction.WEST); if (destination.getX() > position.getX()) directionList.add(Direction.EAST); if (destination.getY() < position.getY()) directionList.add(Direction.SOUTH); diff --git a/robot/pom.xml b/robot/pom.xml index 572739b1aef8a84a9bd0d49d7298380f618f0dc6..c5eccc5fccfcd65c06675fd1d0fdec306dd00b7a 100644 --- a/robot/pom.xml +++ b/robot/pom.xml @@ -44,12 +44,6 @@ 1.0-SNAPSHOT compile - - fr.ufrst.m1info.gl.tpariney - roadBook - 1.0-SNAPSHOT - compile - diff --git a/robot/src/main/java/fr/ufrst/m1info/gl/tpariney/LandSensor.java b/robot/src/main/java/fr/ufrst/m1info/gl/tpariney/LandSensor.java index da90d2790ee6cf75e946a19cd1e8ec9ed746bf9a..5bbd22c492050a7a091e1b5457fe45775302fe73 100644 --- a/robot/src/main/java/fr/ufrst/m1info/gl/tpariney/LandSensor.java +++ b/robot/src/main/java/fr/ufrst/m1info/gl/tpariney/LandSensor.java @@ -4,7 +4,7 @@ import java.util.Random; public class LandSensor { - private Random random; + private final Random random; public LandSensor(Random random) { this.random = random; @@ -16,6 +16,6 @@ public class LandSensor { } public static double distance(Coordinates coordinate1, Coordinates coordinate2) { - return Math.sqrt(Math.pow(coordinate1.getX()-coordinate2.getX(), 2) + Math.pow(coordinate1.getY()-coordinate2.getY(),2)); + return Math.sqrt(Math.pow((double)coordinate1.getX() - coordinate2.getX(), 2) + Math.pow((double)coordinate1.getY() - coordinate2.getY(), 2)); } } diff --git a/robot/src/main/java/fr/ufrst/m1info/gl/tpariney/Robot.java b/robot/src/main/java/fr/ufrst/m1info/gl/tpariney/Robot.java index 394629bf8795a89564af53a02f140d8d104bdad2..c6b65da6edcfe9437a903f6eda6d0efe5d5df4c4 100644 --- a/robot/src/main/java/fr/ufrst/m1info/gl/tpariney/Robot.java +++ b/robot/src/main/java/fr/ufrst/m1info/gl/tpariney/Robot.java @@ -110,6 +110,6 @@ public class Robot { public void computeRoadTo(Coordinates destination) throws UnlandedRobotException { if (!isLanded) throw new UnlandedRobotException(); - setRoadBook(calculateRoadBook(direction, position, destination, new ArrayList())); + setRoadBook(calculateRoadBook(direction, position, destination, new ArrayList<>())); } } diff --git a/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/LandSensorUnitTests.java b/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/LandSensorUnitTests.java new file mode 100644 index 0000000000000000000000000000000000000000..64e5ff4fa8def26c8631d3fe15d6a2ec333401be --- /dev/null +++ b/robot/src/test/java/fr/ufrst/m1info/gl/tpariney/LandSensorUnitTests.java @@ -0,0 +1,19 @@ +package fr.ufrst.m1info.gl.tpariney; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Random; + +public class LandSensorUnitTests { + @Test + public void distance() { + Coordinates pos = new Coordinates(0, 0); + Coordinates pos1 = new Coordinates(1, 0); + Coordinates pos2 = new Coordinates(0, 1); + Coordinates pos3 = new Coordinates(-1, -1); + Assert.assertEquals(1, LandSensor.distance(pos, pos1), 0); + Assert.assertEquals(1, LandSensor.distance(pos, pos2), 0); + Assert.assertEquals(Math.sqrt(2), LandSensor.distance(pos, pos3), 0); + } +}