BAEL-6600: add further test cases
This commit is contained in:
parent
10152801a5
commit
5942a934cf
|
@ -7,17 +7,21 @@ public class HashMapWithMaxSizeLimit<K, V> extends HashMap<K, V> {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private int maxSize = -1;
|
private int maxSize = -1;
|
||||||
|
|
||||||
|
public HashMapWithMaxSizeLimit() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
public HashMapWithMaxSizeLimit(int maxSize) {
|
public HashMapWithMaxSizeLimit(int maxSize) {
|
||||||
super();
|
super();
|
||||||
this.maxSize = maxSize;
|
this.maxSize = maxSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public V put(K key, V value) {
|
public V put(K key, V value) {
|
||||||
if (this.maxSize == -1 || this.containsKey(key) || this.size() < this.maxSize) {
|
if (this.maxSize == -1 || this.containsKey(key) || this.size() < this.maxSize) {
|
||||||
return super.put(key, value);
|
return super.put(key, value);
|
||||||
}
|
}
|
||||||
throw new RuntimeException("Max size exceeded!");
|
throw new RuntimeException("Max size exceeded!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung.map;
|
package com.baeldung.map;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class LimitMaxSizeHashMapByCustomHashMapUnitTest {
|
class LimitMaxSizeHashMapByCustomHashMapUnitTest {
|
||||||
|
@ -11,24 +11,49 @@ class LimitMaxSizeHashMapByCustomHashMapUnitTest {
|
||||||
private final int MAX_SIZE = 4;
|
private final int MAX_SIZE = 4;
|
||||||
private HashMapWithMaxSizeLimit<Integer, String> hashMapWithMaxSizeLimit;
|
private HashMapWithMaxSizeLimit<Integer, String> hashMapWithMaxSizeLimit;
|
||||||
|
|
||||||
@BeforeEach
|
@Test
|
||||||
void setUp() {
|
void givenCustomHashMapObject_whenThereIsNoLimit_thenDoesNotThrowException() {
|
||||||
hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>(MAX_SIZE);
|
hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>();
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
for (int i = 0; i < 10000; i++) {
|
||||||
|
hashMapWithMaxSizeLimit.put(i, i + "");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenCustomHashMapObject_whenAddingNewEntryAndLimitExceeded_thenThrowsException() {
|
void givenCustomHashMapObject_whenLimitNotReached_thenDoesNotThrowException() {
|
||||||
Exception exception = assertThrows(RuntimeException.class, () -> {
|
hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>(MAX_SIZE);
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
hashMapWithMaxSizeLimit.put(i, i + "");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCustomHashMapObject_whenReplacingValueWhenLimitIsReached_thenDoesNotThrowException() {
|
||||||
|
hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>(MAX_SIZE);
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
hashMapWithMaxSizeLimit.put(1, "One");
|
hashMapWithMaxSizeLimit.put(1, "One");
|
||||||
hashMapWithMaxSizeLimit.put(2, "Two");
|
hashMapWithMaxSizeLimit.put(2, "Two");
|
||||||
hashMapWithMaxSizeLimit.put(3, "Three");
|
hashMapWithMaxSizeLimit.put(3, "Three");
|
||||||
hashMapWithMaxSizeLimit.put(4, "Four");
|
hashMapWithMaxSizeLimit.put(4, "Four");
|
||||||
hashMapWithMaxSizeLimit.put(5, "Five");
|
hashMapWithMaxSizeLimit.put(4, "4");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenCustomHashMapObject_whenLimitExceeded_thenThrowsException() {
|
||||||
|
hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit<Integer, String>(MAX_SIZE);
|
||||||
|
Exception exception = assertThrows(RuntimeException.class, () -> {
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
hashMapWithMaxSizeLimit.put(i, i + "");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
String messageThrownWhenSizeExceedsLimit = "Max size exceeded!";
|
String messageThrownWhenSizeExceedsLimit = "Max size exceeded!";
|
||||||
String actualMessage = exception.getMessage();
|
String actualMessage = exception.getMessage();
|
||||||
|
|
||||||
assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit));
|
assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue