From bc16428173f8d7727a39b09c9595cefbe284c943 Mon Sep 17 00:00:00 2001 From: Martina Baiardi Date: Sun, 12 Nov 2023 11:49:17 +0100 Subject: [PATCH] feat: prepare exercises for lab08 --- .../bank/impl/TestStrictBankAccount.java | 32 +--- .../impl/DeathNoteImplementation.java | 152 ------------------ .../it/unibo/deathnote/TestDeathNote.java | 134 --------------- 3 files changed, 7 insertions(+), 311 deletions(-) delete mode 100644 java/testing/tdd-deathnote/src/main/java/it/unibo/deathnote/impl/DeathNoteImplementation.java diff --git a/java/testing/junit/src/test/java/it/unibo/bank/impl/TestStrictBankAccount.java b/java/testing/junit/src/test/java/it/unibo/bank/impl/TestStrictBankAccount.java index 1c424be4..80826b83 100644 --- a/java/testing/junit/src/test/java/it/unibo/bank/impl/TestStrictBankAccount.java +++ b/java/testing/junit/src/test/java/it/unibo/bank/impl/TestStrictBankAccount.java @@ -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 { @@ -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(); } } diff --git a/java/testing/tdd-deathnote/src/main/java/it/unibo/deathnote/impl/DeathNoteImplementation.java b/java/testing/tdd-deathnote/src/main/java/it/unibo/deathnote/impl/DeathNoteImplementation.java deleted file mode 100644 index 14e65159..00000000 --- a/java/testing/tdd-deathnote/src/main/java/it/unibo/deathnote/impl/DeathNoteImplementation.java +++ /dev/null @@ -1,152 +0,0 @@ -package it.unibo.deathnote.impl; - -import it.unibo.deathnote.api.DeathNote; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Objects; - -public class DeathNoteImplementation implements DeathNote { - - private final Map deaths = new LinkedHashMap<>(); // Predictable iteration order - private String lastWrittenName; - - @Override - public String getRule(int ruleNumber) { - if (ruleNumber < 1 || ruleNumber > RULES.size()) { - throw new IllegalArgumentException("Rule index " + ruleNumber + " does not exist"); - } - return RULES.get(ruleNumber - 1); - } - - @Override - public void writeName(String name) { - Objects.requireNonNull(name); - lastWrittenName = name; - deaths.put(name, new Death()); - } - - @Override - public boolean writeDeathCause(final String cause) { - return updateDeath( - cause, - new DeathTransformer() { - @Override - public Death call(Death input) { - return input.writeCause(cause); - } - }); - } - - @Override - public boolean writeDetails(final String details) { - return updateDeath( - details, - new DeathTransformer() { - @Override - public Death call(Death input) { - return input.writeDetails(details); - } - } - ); - } - - @Override - public String getDeathCause(final String name) { - return getDeath(name).cause; - } - - @Override - public String getDeathDetails(final String name) { - return getDeath(name).details; - } - - @Override - public boolean isNameWritten(final String name) { - return deaths.containsKey(name); - } - - private Death getDeath(final String name) { - final var death = deaths.get(name); - if (death == null) { - throw new IllegalArgumentException(name + " has never been written in this notebook"); - } - return death; - } - - private boolean updateDeath(String update, DeathTransformer operation) { - if (lastWrittenName == null) { - throw new IllegalStateException("No name written yet"); - } - if (update == null) { - throw new IllegalStateException("No update provided"); - } - final var previous = deaths.get(lastWrittenName); - final var updated = operation.call(previous); - if (previous.equals(updated)) { - return false; - } else { - deaths.put(lastWrittenName, updated); - return true; - } - } - - private interface DeathTransformer { - Death call(Death input); - } - - private static final class Death { - private static final String DEFAULT_CAUSE = "heart attack"; - private static final byte VALID_CAUSE_TIMEOUT = 40; - private static final short VALID_DETAILS_TIMEOUT = 6000 + VALID_CAUSE_TIMEOUT; - private final String cause; - private final String details; - private final long timeOfDeath; - - // This object is immutable, so we can cache the hash - private int hash; - - private Death(final String cause, final String details) { - this.cause = cause; - this.details = details; - timeOfDeath = System.currentTimeMillis(); - } - - Death() { - this(DEFAULT_CAUSE, ""); - } - - private Death writeCause(final String cause) { - return System.currentTimeMillis() < timeOfDeath + VALID_CAUSE_TIMEOUT - ? new Death(cause, this.details) - : this; - } - - private Death writeDetails(final String details) { - return System.currentTimeMillis() < timeOfDeath + VALID_DETAILS_TIMEOUT - ? new Death(this.cause, details) - : this; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof Death other)) { - return false; - } - return Objects.equals(cause, other.cause) - && Objects.equals(details, other.details) - && timeOfDeath == other.timeOfDeath; - } - - @Override - public int hashCode() { - if (hash == 0) { - hash = Objects.hash(cause, details, timeOfDeath); - } - return hash; - } - } -} diff --git a/java/testing/tdd-deathnote/src/test/java/it/unibo/deathnote/TestDeathNote.java b/java/testing/tdd-deathnote/src/test/java/it/unibo/deathnote/TestDeathNote.java index 4fc4f74c..bf49d121 100644 --- a/java/testing/tdd-deathnote/src/test/java/it/unibo/deathnote/TestDeathNote.java +++ b/java/testing/tdd-deathnote/src/test/java/it/unibo/deathnote/TestDeathNote.java @@ -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 { } } \ No newline at end of file