Skip to content

Commit

Permalink
feat: prepare exercises for lab08
Browse files Browse the repository at this point in the history
  • Loading branch information
anitvam committed Nov 12, 2023
1 parent dbceff8 commit bc16428
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import static it.unibo.bank.impl.SimpleBankAccount.*;
import static it.unibo.bank.impl.SimpleBankAccount.ATM_TRANSACTION_FEE;
import static it.unibo.bank.impl.StrictBankAccount.TRANSACTION_FEE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class TestStrictBankAccount {

Expand All @@ -23,47 +21,31 @@ public class TestStrictBankAccount {

@BeforeEach
public void setUp() {
mRossi = new AccountHolder("Mario", "Rossi", 1);
bankAccount = new StrictBankAccount(mRossi, 0.0);
fail();
}

// 2. Test the initial state of the StrictBankAccount
@Test
public void testInitialization() {
assertEquals(0.0, bankAccount.getBalance());
assertEquals(0, bankAccount.getTransactionsCount());
assertEquals(mRossi, bankAccount.getAccountHolder());
fail();
}


// 3. Perform a deposit of 100€, compute the management fees, and check that the balance is correctly reduced.
@Test
public void testManagementFees() {
bankAccount.deposit(mRossi.getUserID(), INITIAL_AMOUNT);
assertEquals(INITIAL_AMOUNT, bankAccount.getBalance());
bankAccount.chargeManagementFees(mRossi.getUserID());
assertEquals(INITIAL_AMOUNT - TRANSACTION_FEE - MANAGEMENT_FEE, bankAccount.getBalance());
fail();
}

// 4. Test the withdraw of a negative value
@Test
public void testNegativeWithdraw() {
try {
bankAccount.withdraw(mRossi.getUserID(), -INITIAL_AMOUNT);
} catch (IllegalArgumentException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 1);
}
fail();
}

// 4. Test withdrawing more money than it is in the account
// 5. Test withdrawing more money than it is in the account
@Test
public void testWithdrawingTooMuch() {
try {
bankAccount.withdraw(mRossi.getUserID(), INITIAL_AMOUNT);
} catch (IllegalArgumentException e) {
assertNotNull(e.getMessage());
assertTrue(e.getMessage().length() > 1);
}
fail();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,139 +1,5 @@
package it.unibo.deathnote;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import it.unibo.deathnote.api.DeathNote;
import it.unibo.deathnote.impl.DeathNoteImplementation;

import static it.unibo.deathnote.api.DeathNote.RULES;
import static java.lang.Thread.sleep;
import static org.junit.jupiter.api.Assertions.*;

class TestDeathNote {

private DeathNote deathNote;
private static String DANILO_PIANINI = "Danilo Pianini";
private static String LIGHT_YAGAMI = "Light Yagami";

@BeforeEach
void init() {
deathNote = new DeathNoteImplementation();
}

/**
* Tests that rule number 0 and negative rules do not exist.
*/
@Test
void testIllegalRule() {
for (final var index: List.of(-1, 0, RULES.size() + 1)) {
assertThrows(
new IllegalArgumentThrower() {
@Override
public void run() {
deathNote.getRule(index);
}
}
);
}
}

/**
* Checks that no rule is empty or null.
*/
@Test
void testRules() {
for (int i = 1; i <= RULES.size(); i++) {
final var rule = deathNote.getRule(i);
assertNotNull(rule);
assertFalse(rule.isBlank());
}
}

/**
* Checks that the human whose name is written in this DeathNote will die.
*/
@Test
void testActualDeath() {
assertFalse(deathNote.isNameWritten(DANILO_PIANINI));
deathNote.writeName(DANILO_PIANINI);
assertTrue(deathNote.isNameWritten(DANILO_PIANINI));
assertFalse(deathNote.isNameWritten(LIGHT_YAGAMI));
assertFalse(deathNote.isNameWritten(""));
}

/**
* Checks that only if the cause of death is written within the next 40 milliseconds
* of writing the person's name, it will happen.
*/
@Test
void testDeathCause() throws InterruptedException {
assertThrows(
new IllegalStateThrower() {
@Override
public void run() {
deathNote.writeDeathCause("spontaneous combustion");
}
}
);
deathNote.writeName(LIGHT_YAGAMI);
assertEquals("heart attack", deathNote.getDeathCause(LIGHT_YAGAMI));
deathNote.writeName(DANILO_PIANINI);
assertTrue(deathNote.writeDeathCause("karting accident"));
// Assuming the method can be executed in less than 40ms
assertEquals("karting accident", deathNote.getDeathCause(DANILO_PIANINI));
// Wait for more than 40 ms
sleep(100);
assertFalse(deathNote.writeDeathCause("Spontaneous human combustion"));
assertEquals("karting accident", deathNote.getDeathCause(DANILO_PIANINI));
}

/**
* Checks that only if the cause of death is written within the next 6 seconds and
* 40 milliseconds of writing the death's details, it will happen.
*/
@Test
void testDeathDetails() throws InterruptedException {
assertThrows(
new IllegalStateThrower() {
@Override
public void run() {
deathNote.writeDetails(LIGHT_YAGAMI);
}
}
);
deathNote.writeName(LIGHT_YAGAMI);
assertEquals("", deathNote.getDeathDetails(LIGHT_YAGAMI));
assertTrue(deathNote.writeDetails("ran for too long"));
// Assuming the method can be executed in less than 6040ms
assertEquals("ran for too long", deathNote.getDeathDetails(LIGHT_YAGAMI));
// Wait for more than 6040 ms
deathNote.writeName(DANILO_PIANINI);
sleep(6100);
assertFalse(deathNote.writeDetails("wrote many tests before dying"));
assertEquals("", deathNote.getDeathDetails(DANILO_PIANINI));
}

static void assertThrows(final RuntimeExceptionThrower exceptionThrower) {
try {
exceptionThrower.run();
fail();
} catch (IllegalStateException | IllegalArgumentException e) {
assertTrue(
exceptionThrower instanceof IllegalArgumentThrower && e instanceof IllegalArgumentException
|| exceptionThrower instanceof IllegalStateThrower && e instanceof IllegalStateException
);
assertNotNull(e.getMessage());
assertFalse(e.getMessage().isBlank());
}
}

private interface RuntimeExceptionThrower {
void run();
}

private interface IllegalStateThrower extends RuntimeExceptionThrower { }

private interface IllegalArgumentThrower extends RuntimeExceptionThrower { }
}

0 comments on commit bc16428

Please sign in to comment.