Refactor the ResourceResolver interface, require(..) method to throw a ResourceNotFoundException.
Edit Javadoc. Resolves gh-92.
This commit is contained in:
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.geode.core.io;
|
||||
|
||||
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -27,6 +25,7 @@ import org.springframework.util.ClassUtils;
|
||||
* Interface defining a contract encapsulating an algorithm/strategy for resolving {@link Resource Resources}.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see java.lang.FunctionalInterface
|
||||
* @see org.springframework.core.io.Resource
|
||||
* @since 1.3.1.
|
||||
*/
|
||||
@@ -68,9 +67,9 @@ public interface ResourceResolver {
|
||||
*
|
||||
* @param location {@link String location} identifying the {@link Resource} to resolve;
|
||||
* must not be {@literal null}.
|
||||
* @return a {@literal non-null}, {@literal existing} {@link Resource} handle for the resolved
|
||||
* {@link String location}.
|
||||
* @throws IllegalStateException if a {@link Resource} cannot be resolved from the given {@link String location}.
|
||||
* @return a {@literal non-null}, {@literal existing} {@link Resource} handle for
|
||||
* the resolved {@link String location}.
|
||||
* @throws ResourceNotFoundException if a {@link Resource} cannot be resolved from the given {@link String location}.
|
||||
* A {@link Resource} is unresolvable if the given {@link String location} does not exist (physically);
|
||||
* see {@link Resource#exists()}.
|
||||
* @see org.springframework.core.io.Resource
|
||||
@@ -79,6 +78,6 @@ public interface ResourceResolver {
|
||||
default @NonNull Resource require(@NonNull String location) {
|
||||
return resolve(location)
|
||||
.filter(Resource::exists)
|
||||
.orElseThrow(() -> newIllegalStateException("Resource [%s] does not exist", location));
|
||||
.orElseThrow(() -> new ResourceNotFoundException(String.format("Resource [%s] does not exist", location)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -78,10 +79,11 @@ public class ResourceResolverUnitTests {
|
||||
|
||||
verify(resourceResolver, times(1)).resolve(eq(location));
|
||||
verify(mockResource, times(1)).exists();
|
||||
verifyNoMoreInteractions(mockResource);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void requireCallsResolveReturningNonExistingResourceThrowsIllegalStateException() {
|
||||
@Test(expected = ResourceNotFoundException.class)
|
||||
public void requireCallsResolveReturningNonExistingResourceThrowsResourceNotFoundException() {
|
||||
|
||||
String location = "/path/to/non-existing/resource.dat";
|
||||
|
||||
@@ -96,7 +98,7 @@ public class ResourceResolverUnitTests {
|
||||
try {
|
||||
resourceResolver.require(location);
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
catch (ResourceNotFoundException expected) {
|
||||
|
||||
assertThat(expected).hasMessage("Resource [%s] does not exist", location);
|
||||
assertThat(expected).hasNoCause();
|
||||
@@ -106,23 +108,24 @@ public class ResourceResolverUnitTests {
|
||||
finally {
|
||||
verify(resourceResolver, times(1)).resolve(eq(location));
|
||||
verify(mockResource, times(1)).exists();
|
||||
verifyNoMoreInteractions(mockResource);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void requireCallsResolveReturningNoResourceThrowsIllegalStateException() {
|
||||
@Test(expected = ResourceNotFoundException.class)
|
||||
public void requireCallsResolveReturningNoResourceThrowsResourceNotFoundException() {
|
||||
|
||||
String location = "/path/to/nowhere";
|
||||
|
||||
ResourceResolver resourceResolver = mock(ResourceResolver.class);
|
||||
|
||||
doReturn(Optional.empty()).when(resourceResolver).resolve(eq(location));
|
||||
doReturn(Optional.empty()).when(resourceResolver).resolve(any());
|
||||
doCallRealMethod().when(resourceResolver).require(any());
|
||||
|
||||
try {
|
||||
resourceResolver.require(location);
|
||||
}
|
||||
catch (IllegalStateException expected) {
|
||||
catch (ResourceNotFoundException expected) {
|
||||
|
||||
assertThat(expected).hasMessage("Resource [%s] does not exist", location);
|
||||
assertThat(expected).hasNoCause();
|
||||
|
||||
Reference in New Issue
Block a user