From 5942a934cf5d78e0b363e8625dc4a7fd140b8c8e Mon Sep 17 00:00:00 2001 From: macasari Date: Wed, 15 Nov 2023 23:53:42 +0100 Subject: [PATCH] BAEL-6600: add further test cases --- .../baeldung/map/HashMapWithMaxSizeLimit.java | 8 +++- ...MaxSizeHashMapByCustomHashMapUnitTest.java | 41 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/core-java-modules/core-java-collections-maps-7/src/main/java/com/baeldung/map/HashMapWithMaxSizeLimit.java b/core-java-modules/core-java-collections-maps-7/src/main/java/com/baeldung/map/HashMapWithMaxSizeLimit.java index 79c08dcb7c..517dac6362 100644 --- a/core-java-modules/core-java-collections-maps-7/src/main/java/com/baeldung/map/HashMapWithMaxSizeLimit.java +++ b/core-java-modules/core-java-collections-maps-7/src/main/java/com/baeldung/map/HashMapWithMaxSizeLimit.java @@ -7,17 +7,21 @@ public class HashMapWithMaxSizeLimit extends HashMap { private static final long serialVersionUID = 1L; private int maxSize = -1; + + public HashMapWithMaxSizeLimit() { + super(); + } public HashMapWithMaxSizeLimit(int maxSize) { super(); this.maxSize = maxSize; - } + } @Override public V put(K key, V value) { if (this.maxSize == -1 || this.containsKey(key) || this.size() < this.maxSize) { return super.put(key, value); - } + } throw new RuntimeException("Max size exceeded!"); } diff --git a/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByCustomHashMapUnitTest.java b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByCustomHashMapUnitTest.java index 09b2e2122a..0846baae1a 100644 --- a/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByCustomHashMapUnitTest.java +++ b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByCustomHashMapUnitTest.java @@ -1,9 +1,9 @@ package com.baeldung.map; import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class LimitMaxSizeHashMapByCustomHashMapUnitTest { @@ -11,24 +11,49 @@ class LimitMaxSizeHashMapByCustomHashMapUnitTest { private final int MAX_SIZE = 4; private HashMapWithMaxSizeLimit hashMapWithMaxSizeLimit; - @BeforeEach - void setUp() { - hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit(MAX_SIZE); + @Test + void givenCustomHashMapObject_whenThereIsNoLimit_thenDoesNotThrowException() { + hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit(); + assertDoesNotThrow(() -> { + for (int i = 0; i < 10000; i++) { + hashMapWithMaxSizeLimit.put(i, i + ""); + } + }); } @Test - void givenCustomHashMapObject_whenAddingNewEntryAndLimitExceeded_thenThrowsException() { - Exception exception = assertThrows(RuntimeException.class, () -> { + void givenCustomHashMapObject_whenLimitNotReached_thenDoesNotThrowException() { + hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit(MAX_SIZE); + assertDoesNotThrow(() -> { + for (int i = 0; i < 4; i++) { + hashMapWithMaxSizeLimit.put(i, i + ""); + } + }); + } + + @Test + void givenCustomHashMapObject_whenReplacingValueWhenLimitIsReached_thenDoesNotThrowException() { + hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit(MAX_SIZE); + assertDoesNotThrow(() -> { hashMapWithMaxSizeLimit.put(1, "One"); hashMapWithMaxSizeLimit.put(2, "Two"); hashMapWithMaxSizeLimit.put(3, "Three"); hashMapWithMaxSizeLimit.put(4, "Four"); - hashMapWithMaxSizeLimit.put(5, "Five"); + hashMapWithMaxSizeLimit.put(4, "4"); + }); + } + + @Test + void givenCustomHashMapObject_whenLimitExceeded_thenThrowsException() { + hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit(MAX_SIZE); + Exception exception = assertThrows(RuntimeException.class, () -> { + for (int i = 0; i < 5; i++) { + hashMapWithMaxSizeLimit.put(i, i + ""); + } }); String messageThrownWhenSizeExceedsLimit = "Max size exceeded!"; String actualMessage = exception.getMessage(); - assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit)); }