Skip to content

Commit

Permalink
Consistently throw NamespacedHierarchicalStoreException
Browse files Browse the repository at this point in the history
NamespacedHierarchicalStore previously threw an IllegalStateException
if the store was accessed or modified after it had been closed;
however, we typically aim to throw an instance of JUnitException
whenever we intentionally throw an exception.

With this commit, NamespacedHierarchicalStore now consistently throws
NamespacedHierarchicalStoreException (which is a subclass of
JUnitException) instead of IllegalStateException.

See #3614
  • Loading branch information
sbrannen committed May 11, 2024
1 parent 7eef670 commit a8ce52d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Expand Up @@ -135,7 +135,8 @@ public void close() {
* @param namespace the namespace; never {@code null}
* @param key the key; never {@code null}
* @return the stored value; may be {@code null}
* @throws IllegalStateException if this store has already been closed
* @throws NamespacedHierarchicalStoreException if this store has already been
* closed
*/
public Object get(N namespace, Object key) {
rejectIfClosed();
Expand All @@ -152,8 +153,7 @@ public Object get(N namespace, Object key) {
* @param requiredType the required type of the value; never {@code null}
* @return the stored value; may be {@code null}
* @throws NamespacedHierarchicalStoreException if the stored value cannot
* be cast to the required type
* @throws IllegalStateException if this store has already been closed
* be cast to the required type, or if this store has already been closed
*/
public <T> T get(N namespace, Object key, Class<T> requiredType) throws NamespacedHierarchicalStoreException {
rejectIfClosed();
Expand All @@ -170,7 +170,8 @@ public <T> T get(N namespace, Object key, Class<T> requiredType) throws Namespac
* @param defaultCreator the function called with the supplied {@code key}
* to create a new value; never {@code null} but may return {@code null}
* @return the stored value; may be {@code null}
* @throws IllegalStateException if this store has already been closed
* @throws NamespacedHierarchicalStoreException if this store has already been
* closed
*/
public <K, V> Object getOrComputeIfAbsent(N namespace, K key, Function<K, V> defaultCreator) {
rejectIfClosed();
Expand All @@ -196,8 +197,7 @@ public <K, V> Object getOrComputeIfAbsent(N namespace, K key, Function<K, V> def
* @param requiredType the required type of the value; never {@code null}
* @return the stored value; may be {@code null}
* @throws NamespacedHierarchicalStoreException if the stored value cannot
* be cast to the required type
* @throws IllegalStateException if this store has already been closed
* be cast to the required type, or if this store has already been closed
*/
public <K, V> V getOrComputeIfAbsent(N namespace, K key, Function<K, V> defaultCreator, Class<V> requiredType)
throws NamespacedHierarchicalStoreException {
Expand All @@ -219,8 +219,7 @@ public <K, V> V getOrComputeIfAbsent(N namespace, K key, Function<K, V> defaultC
* @param value the value to store; may be {@code null}
* @return the previously stored value; may be {@code null}
* @throws NamespacedHierarchicalStoreException if an error occurs while
* storing the value
* @throws IllegalStateException if this store has already been closed
* storing the value, or if this store has already been closed
*/
public Object put(N namespace, Object key, Object value) throws NamespacedHierarchicalStoreException {
rejectIfClosed();
Expand All @@ -238,7 +237,8 @@ public Object put(N namespace, Object key, Object value) throws NamespacedHierar
* @param namespace the namespace; never {@code null}
* @param key the key; never {@code null}
* @return the previously stored value; may be {@code null}
* @throws IllegalStateException if this store has already been closed
* @throws NamespacedHierarchicalStoreException if this store has already been
* closed
*/
public Object remove(N namespace, Object key) {
rejectIfClosed();
Expand All @@ -258,8 +258,7 @@ public Object remove(N namespace, Object key) {
* @param requiredType the required type of the value; never {@code null}
* @return the previously stored value; may be {@code null}
* @throws NamespacedHierarchicalStoreException if the stored value cannot
* be cast to the required type
* @throws IllegalStateException if this store has already been closed
* be cast to the required type, or if this store has already been closed
*/
public <T> T remove(N namespace, Object key, Class<T> requiredType) throws NamespacedHierarchicalStoreException {
rejectIfClosed();
Expand Down Expand Up @@ -302,7 +301,7 @@ private <T> T castToRequiredType(Object key, Object value, Class<T> requiredType

private void rejectIfClosed() {
if (this.closed) {
throw new IllegalStateException(
throw new NamespacedHierarchicalStoreException(
"A NamespacedHierarchicalStore cannot be modified or queried after it has been closed");
}
}
Expand Down
Expand Up @@ -524,21 +524,22 @@ void rejectsModificationAfterClose() {
store.close();
assertClosed();

assertThrows(IllegalStateException.class, () -> store.put(namespace, "key1", "value1"));
assertThrows(IllegalStateException.class, () -> store.remove(namespace, "key1"));
assertThrows(IllegalStateException.class, () -> store.remove(namespace, "key1", Number.class));
assertThrows(NamespacedHierarchicalStoreException.class, () -> store.put(namespace, "key1", "value1"));
assertThrows(NamespacedHierarchicalStoreException.class, () -> store.remove(namespace, "key1"));
assertThrows(NamespacedHierarchicalStoreException.class,
() -> store.remove(namespace, "key1", Number.class));
}

@Test
void rejectsQueryAfterClose() {
store.close();
assertClosed();

assertThrows(IllegalStateException.class, () -> store.get(namespace, "key1"));
assertThrows(IllegalStateException.class, () -> store.get(namespace, "key1", Integer.class));
assertThrows(IllegalStateException.class,
assertThrows(NamespacedHierarchicalStoreException.class, () -> store.get(namespace, "key1"));
assertThrows(NamespacedHierarchicalStoreException.class, () -> store.get(namespace, "key1", Integer.class));
assertThrows(NamespacedHierarchicalStoreException.class,
() -> store.getOrComputeIfAbsent(namespace, "key1", k -> "value"));
assertThrows(IllegalStateException.class,
assertThrows(NamespacedHierarchicalStoreException.class,
() -> store.getOrComputeIfAbsent(namespace, "key1", k -> 1337, Integer.class));
}

Expand Down

0 comments on commit a8ce52d

Please sign in to comment.