From 8952a535a13e6cf6ad164ab0c93f47450b396af0 Mon Sep 17 00:00:00 2001 From: Andrea Placuzzi Date: Fri, 30 Oct 2020 17:55:45 +0100 Subject: [PATCH 1/5] remove PMD --- .project | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.project b/.project index 7437d2aa..9adb639d 100644 --- a/.project +++ b/.project @@ -10,14 +10,8 @@ - - net.sourceforge.pmd.eclipse.plugin.pmdBuilder - - - org.eclipse.jdt.core.javanature - net.sourceforge.pmd.eclipse.plugin.pmdNature From 00a4afdd8c70593eb036967019bd96232f2b7711 Mon Sep 17 00:00:00 2001 From: Andrea Placuzzi Date: Fri, 30 Oct 2020 15:45:22 +0100 Subject: [PATCH 2/5] use raw type --- src/it/unibo/oop/lab/collections2/UserImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/it/unibo/oop/lab/collections2/UserImpl.java b/src/it/unibo/oop/lab/collections2/UserImpl.java index bbd3e769..80474313 100644 --- a/src/it/unibo/oop/lab/collections2/UserImpl.java +++ b/src/it/unibo/oop/lab/collections2/UserImpl.java @@ -10,7 +10,7 @@ public class UserImpl implements User { private final String firstName; private final String lastName; - private final Integer age; + private final int age; private final String username; /* * to be "lazily" initialized. From c1553969853a3ab69db0fe104ab9497707b4d37a Mon Sep 17 00:00:00 2001 From: Andrea Placuzzi Date: Fri, 30 Oct 2020 17:52:43 +0100 Subject: [PATCH 3/5] provide test class already implemented --- src/it/unibo/oop/lab/exception2/README.md | 2 +- .../lab/exception2/TestStrictBankAccount.java | 54 +++++++++++++++---- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/it/unibo/oop/lab/exception2/README.md b/src/it/unibo/oop/lab/exception2/README.md index eeb7268d..adf88b27 100644 --- a/src/it/unibo/oop/lab/exception2/README.md +++ b/src/it/unibo/oop/lab/exception2/README.md @@ -10,4 +10,4 @@ Modify `StrictBankAccountImpl` in such a way that: * A `NotEnoughFoundsException` is thrown if there is not enough money for a draw operation to complete * A `TransactionsOverQuotaException` is thrown if the count of ATM transactions gets over the maximum allowed -Complete the class `TestStrictBankAccount` using JUnit +Execute the class `TestStrictBankAccount` to verify that the exceptions are correctly implemented and thrown. \ No newline at end of file diff --git a/src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java b/src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java index 8f4fd71f..6dd8202b 100644 --- a/src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java +++ b/src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java @@ -1,27 +1,63 @@ package it.unibo.oop.lab.exception2; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + import org.junit.Test; /** * JUnit test to test - * {@link StrictBankAccount}. - * + * {@link it.unibo.oop.lab.exception2.StrictBankAccount}. */ public class TestStrictBankAccount { + private static final int MAX_ATM_TRANSACTIONS = 10; + private static final int INITIAL_AMOUNT = 10_000; + private static final int TOO_MUCH = 50_000; + /** - * Used to test Exceptions on {@link StrictBankAccount}. + * Used to test Exceptions on + * {@link it.unibo.oop.lab.exception2.StrictBankAccount}. */ @Test public void testBankOperations() { /* - * 1) Creare due StrictBankAccountImpl assegnati a due AccountHolder a - * scelta, con ammontare iniziale pari a 10000 e nMaxATMTransactions=10 + * TODO: uncomment this test. * - * 2) Effetture un numero di operazioni a piacere per verificare che le - * eccezioni create vengano lanciate soltanto quando opportuno, cioè in - * presenza di un id utente errato, oppure al superamento del numero di - * operazioni ATM gratuite. + * You can uncomment blocks of code in eclipse by using: + * + * Control + Shift + 7 (With an Italian keyboard...) */ +// final AccountHolder usr1 = new AccountHolder("Mario", "Rossi", 1); +// final AccountHolder usr2 = new AccountHolder("Luigi", "Bianchi", 2); +// final StrictBankAccount account1 = new StrictBankAccount(usr1.getUserID(), INITIAL_AMOUNT, MAX_ATM_TRANSACTIONS); +// final StrictBankAccount account2 = new StrictBankAccount(usr2.getUserID(), INITIAL_AMOUNT, MAX_ATM_TRANSACTIONS); +// try { +// account1.deposit(4, 100); +// fail(); +// } catch (WrongAccountHolderException e) { +// assertNotNull(e); +// } +// for (int i = 0; i < MAX_ATM_TRANSACTIONS; i++) { +// try { +// account2.depositFromATM(usr2.getUserID(), 1); +// } catch (TransactionsOverQuotaException | WrongAccountHolderException e) { +// fail("Not exceeded yet max no. transactions!"); +// } +// } +// try { +// account2.depositFromATM(usr2.getUserID(), 1); +// fail("Should raise the exception signaling that we exceeded max no. transactions!"); +// } catch (TransactionsOverQuotaException | WrongAccountHolderException e) { +// assertNotNull(e); +// } +// try { +// account1.withdraw(usr1.getUserID(), TOO_MUCH); +// } catch (WrongAccountHolderException e) { +// fail(); +// } catch (NotEnoughFoundsException e) { +// assertNotNull(e); +// } } + } From f278c4dd612627be2282843f65bd9fe47c5ba217 Mon Sep 17 00:00:00 2001 From: Andrea Placuzzi Date: Fri, 30 Oct 2020 16:12:02 +0100 Subject: [PATCH 4/5] provide test class already implementated --- .../oop/lab/exception1/BaseRobotTest.java | 99 ++++++++++++------- src/it/unibo/oop/lab/exception1/README.md | 9 +- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/src/it/unibo/oop/lab/exception1/BaseRobotTest.java b/src/it/unibo/oop/lab/exception1/BaseRobotTest.java index ebebc395..4f0a35b4 100644 --- a/src/it/unibo/oop/lab/exception1/BaseRobotTest.java +++ b/src/it/unibo/oop/lab/exception1/BaseRobotTest.java @@ -1,8 +1,10 @@ package it.unibo.oop.lab.exception1; +import static it.unibo.oop.lab.exception1.RobotEnvironment.WORLD_X_UPPER_LIMIT; +import static it.unibo.oop.lab.exception1.RobotEnvironment.WORLD_Y_UPPER_LIMIT; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; import org.junit.Test; @@ -12,6 +14,8 @@ */ public final class BaseRobotTest { + private static final int TEST_BATTERY_LEVEL = 20; + /** * Simple test for testing a robot moving, wandering the available * environment. @@ -19,41 +23,58 @@ public final class BaseRobotTest { */ @Test public void testRobotMovementBase() { - /* - * FIRST OF ALL, take a look to "TestWithExceptions". Read the source and the - * comments very carefully. - */ /* * 1) Create a Robot with battery level 100 */ final Robot r1 = new Robot("SimpleRobot", 100); - // checking if robot is in position x=0; y=0 + /* + * checking if robot in in position x=0; y=0 + */ assertEquals("[CHECKING ROBOT INIT POS X]", 0, r1.getEnvironment().getCurrPosX()); assertEquals("[CHECKING ROBOT INIT POS Y]", 0, r1.getEnvironment().getCurrPosY()); /* * 2) Move the robot right until it touches the world limit */ - for (int i = 0; i < RobotEnvironment.WORLD_X_UPPER_LIMIT; i++) { - // check if position if coherent - assertTrue("[CHECKING MOVING RIGHT]", r1.moveRight()); + try { + for (int i = 0; i < WORLD_X_UPPER_LIMIT; i++) { + r1.moveRight(); + } + /* + * this is causing an exception to be raised + */ + r1.moveRight(); + /* + * this must not be reached; + */ + fail("I should not get such far!"); + } catch (PositionOutOfBoundException e) { + assertTrue(e.getMessage().contains("pos(" + (WORLD_X_UPPER_LIMIT + 1) + ", 0)")); + } catch (RuntimeException e) { + /* + * use a generic exception in a catch clause is always bad idea and you have NEVER to do that. + * Here we do that only to be sure that you throw the right exception. + */ + fail("A wrong exception was raised!"); } - // reached the right limit of the world - assertFalse("[CHECKING MOVING RIGHT]", r1.moveRight()); - // checking positions x=50; y=0 - assertEquals("[MOVING RIGHT ROBOT POS X]", RobotEnvironment.WORLD_X_UPPER_LIMIT, r1.getEnvironment().getCurrPosX()); - assertEquals("[MOVING RIGHT ROBOT POS Y]", 0, r1.getEnvironment().getCurrPosY()); /* * 2) Move to the top until it reaches the upper right conrner of the world */ - for (int i = 0; i < RobotEnvironment.WORLD_Y_UPPER_LIMIT; i++) { - // check if position if coherent - assertTrue("[CHECKING MOVING UP]", r1.moveUp()); + try { + for (int i = 0; i < WORLD_Y_UPPER_LIMIT; i++) { + // check if position if coherent + r1.moveUp(); + } + r1.moveUp(); + } catch (PositionOutOfBoundException e) { + assertTrue(e.getMessage().contains("pos(" + WORLD_X_UPPER_LIMIT + ", " + (WORLD_Y_UPPER_LIMIT + 1) + ")")); + } catch (RuntimeException e) { + /* + * use a generic exception in a catch clause is always bad idea and you have NEVER to do that. + * Here we do that only to be sure that you throw the right exception. + */ + fail("A wrong exception was raised!"); } - // reached the upper limit of the world - assertFalse("[CHECKING MOVING UP]", r1.moveUp()); - // checking positions x=50; y=80 - assertEquals("[MOVING RIGHT ROBOT POS X]", RobotEnvironment.WORLD_X_UPPER_LIMIT, r1.getEnvironment().getCurrPosX()); - assertEquals("[MOVING RIGHT ROBOT POS Y]", RobotEnvironment.WORLD_Y_UPPER_LIMIT, r1.getEnvironment().getCurrPosY()); + } /** @@ -62,26 +83,30 @@ public void testRobotMovementBase() { */ @Test public void testRobotBatteryBase() { - final Robot r2 = new Robot("SimpleRobot2", 20); + final Robot r2 = new Robot("SimpleRobot2", TEST_BATTERY_LEVEL); /* * Repeatedly move the robot up and down until the battery is completely * exhausted. */ - while (r2.getBatteryLevel() > 0) { - r2.moveUp(); + try { + while (r2.getBatteryLevel() > 0) { + r2.moveUp(); + r2.moveDown(); + } r2.moveDown(); + fail("You're not supposed to get that far with no battery!"); + } catch (PositionOutOfBoundException e) { + fail("I expected battery to fail!"); + } catch (RuntimeException e) { + /* + * use a generic exception in a catch clause is always bad idea and you have NEVER to do that. + * Here we do that only to be sure that you throw the right exception. + * The method getSimpleName is an advanced aspect of java that you'll see later. + * It return the runtime class name of the object + */ + if (!e.getClass().getSimpleName().equals("NotEnoughBatteryException")) { + fail("A wrong exception was raised!"); + } } - // verify battery level: - // expected, actual, delta (accepted error as we deal with decimal - // values: in this case we accept NO ERROR, which is generally bad) - assertEquals(0d, r2.getBatteryLevel(), 0); - // verify position: same as start position - assertEquals("[CHECKING ROBOT INIT POS Y]", 0, r2.getEnvironment().getCurrPosY()); - // out of world: returns false - assertFalse("[CHECKING MOVING UP]", r2.moveUp()); - // recharge battery - r2.recharge(); - // verify battery level - assertEquals(100, r2.getBatteryLevel(), 0); } } diff --git a/src/it/unibo/oop/lab/exception1/README.md b/src/it/unibo/oop/lab/exception1/README.md index d9fce79c..89a75ab6 100644 --- a/src/it/unibo/oop/lab/exception1/README.md +++ b/src/it/unibo/oop/lab/exception1/README.md @@ -34,11 +34,6 @@ Modify `Robot` to make it work with `RobotEnvironment` * `moveToPosition` must be changed to account for the changes in `RobotEnvironment.move()` * `moveUp` and `moveDown` need a refactoring as well -### `BaseRobotTest` - -* Modify `BaseRobotTest.testRobotMovementBase` in order to verify that the exceptions are correctly thrown -* **HINT**: Use `Assert.fail()` in the try block, and `Assert.assertNotNull()` to verify that the exception message is present - ## Design a new exception Design and realize `NotEnoughBatteryException`, to be thrown when the robot is asked to move (whatever the direction) in case the battery is not sufficient @@ -48,6 +43,4 @@ Design and realize `NotEnoughBatteryException`, to be thrown when the robot is a * Extend one among: `Exception`, `RuntimeException`, or `IllegalStateException`. Prepare yourself to explain the professor the reason behind your choice. * Change the behavior of `moveToPosition` (it must return `void`) * Throw an exception if there is not enough battery) -* Refactor `moveUp` and `moveDown` -* Modify `BaseRobotTest.testRobotBatteryBase()` to verify that the exception is correctly implemented and thrown - \ No newline at end of file +* Refactor `moveUp` and `moveDown` \ No newline at end of file From e4144a947266c8d3357f92a196d54f08f5659813 Mon Sep 17 00:00:00 2001 From: Andrea Placuzzi Date: Fri, 30 Oct 2020 18:07:56 +0100 Subject: [PATCH 5/5] remove blank line --- src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java b/src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java index 6dd8202b..9937992e 100644 --- a/src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java +++ b/src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java @@ -59,5 +59,4 @@ public void testBankOperations() { // assertNotNull(e); // } } - }