Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop qa enabled #1

Open
wants to merge 4 commits into
base: develop-qa-enabled
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/it/unibo/oop/lab/collections2/UserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
99 changes: 62 additions & 37 deletions src/it/unibo/oop/lab/exception1/BaseRobotTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,48 +14,67 @@
*/
public final class BaseRobotTest {

private static final int TEST_BATTERY_LEVEL = 20;

/**
* Simple test for testing a robot moving, wandering the available
* environment.
*
*/
@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());

}

/**
Expand All @@ -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);
}
}
9 changes: 1 addition & 8 deletions src/it/unibo/oop/lab/exception1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

* Refactor `moveUp` and `moveDown`
2 changes: 1 addition & 1 deletion src/it/unibo/oop/lab/exception2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
53 changes: 44 additions & 9 deletions src/it/unibo/oop/lab/exception2/TestStrictBankAccount.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
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.
*
* You can uncomment blocks of code in eclipse by using:
*
* 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.
* 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);
// }
}
}