From d7f1091754e82f8ba7e902bf0bc7c4380443d93f Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 9 Jul 2020 17:08:29 -0700 Subject: [PATCH] Add null-safe isReadable(:Resource) and isWritable(:Resource) utility methods. Resolve gh-92. --- .../geode/core/io/support/ResourceUtils.java | 45 +++++++++--- .../io/support/ResourceUtilsUnitTests.java | 73 +++++++++++++++++++ 2 files changed, 108 insertions(+), 10 deletions(-) diff --git a/spring-geode/src/main/java/org/springframework/geode/core/io/support/ResourceUtils.java b/spring-geode/src/main/java/org/springframework/geode/core/io/support/ResourceUtils.java index 2bc576d2..131e15ba 100644 --- a/spring-geode/src/main/java/org/springframework/geode/core/io/support/ResourceUtils.java +++ b/spring-geode/src/main/java/org/springframework/geode/core/io/support/ResourceUtils.java @@ -34,6 +34,41 @@ import org.springframework.lang.Nullable; */ public abstract class ResourceUtils { + /** + * Determines whether the given byte array is {@literal null} or {@literal empty}. + * + * @param array byte array to evaluate. + * @return a boolean value indicating whether the given byte array is {@literal null} or {@literal empty}. + */ + public static boolean isNotEmpty(@Nullable byte[] array) { + return array != null && array.length > 0; + } + + /** + * Null-safe operation to determine whether the given {@link Resource} is readable. + * + * @param resource {@link Resource} to evaluate. + * @return a boolean value indicating whether the given {@link Resource} is readable. + * @see org.springframework.core.io.Resource#isReadable() + * @see org.springframework.core.io.Resource + */ + public static boolean isReadable(@Nullable Resource resource) { + return resource != null && resource.isReadable(); + } + + /** + * Null-safe operation to determine whether the given {@link Resource} is writable. + * + * @param resource {@link Resource} to evaluate. + * @return a boolean value indicating whether the given {@link Resource} is writable. + * @see org.springframework.core.io.WritableResource#isWritable() + * @see org.springframework.core.io.WritableResource + * @see org.springframework.core.io.Resource + */ + public static boolean isWritable(@Nullable Resource resource) { + return resource instanceof WritableResource && ((WritableResource) resource).isWritable(); + } + /** * Returns the {@link Resource} as a {@link WritableResource} if possible. * @@ -56,16 +91,6 @@ public abstract class ResourceUtils { ResourceUtils.nullSafeGetDescription(resource))); } - /** - * Determines whether the given byte array is {@literal null} or {@literal empty}. - * - * @param array byte array to evaluate. - * @return a boolean value indicating whether the given byte array is {@literal null} or {@literal empty}. - */ - public static boolean isNotEmpty(@Nullable byte[] array) { - return array != null && array.length > 0; - } - /** * Null-safe method to get the {@link Resource#getDescription() description} of the given {@link Resource}. * diff --git a/spring-geode/src/test/java/org/springframework/geode/core/io/support/ResourceUtilsUnitTests.java b/spring-geode/src/test/java/org/springframework/geode/core/io/support/ResourceUtilsUnitTests.java index f5d98058..b45940c5 100644 --- a/spring-geode/src/test/java/org/springframework/geode/core/io/support/ResourceUtilsUnitTests.java +++ b/spring-geode/src/test/java/org/springframework/geode/core/io/support/ResourceUtilsUnitTests.java @@ -20,6 +20,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.verifyNoInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; import org.junit.Test; @@ -40,6 +41,78 @@ import org.springframework.core.io.WritableResource; */ public class ResourceUtilsUnitTests { + @Test + public void isReadableWithReadableResourceReturnsTrue() { + + Resource mockResource = mock(Resource.class); + + doReturn(true).when(mockResource).isReadable(); + + assertThat(ResourceUtils.isReadable(mockResource)).isTrue(); + + verify(mockResource, times(1)).isReadable(); + verifyNoMoreInteractions(mockResource); + } + + @Test + public void isReadableWithNonReadableResourceReturnsFalse() { + + Resource mockResource = mock(Resource.class); + + doReturn(false).when(mockResource).isReadable(); + + assertThat(ResourceUtils.isReadable(mockResource)).isFalse(); + + verify(mockResource, times(1)).isReadable(); + verifyNoMoreInteractions(mockResource); + } + + @Test + public void isReadableWithNullResourceIsNullSafeReturnsFalse() { + assertThat(ResourceUtils.isReadable(null)).isFalse(); + } + + @Test + public void isWritableWithWritableResourceReturnsTrue() { + + WritableResource mockResource = mock(WritableResource.class); + + doReturn(true).when(mockResource).isWritable(); + + assertThat(ResourceUtils.isWritable(mockResource)).isTrue(); + + verify(mockResource, times(1)).isWritable(); + verifyNoMoreInteractions(mockResource); + } + + @Test + public void isWritableWithNonWritableResourceReturnsFalse() { + + WritableResource mockResource = mock(WritableResource.class); + + doReturn(false).when(mockResource).isWritable(); + + assertThat(ResourceUtils.isWritable(mockResource)).isFalse(); + + verify(mockResource, times(1)).isWritable(); + verifyNoMoreInteractions(mockResource); + } + + @Test + public void isWritableWithNonWritableResourceTypeReturnsFalse() { + + Resource mockResource = mock(Resource.class); + + assertThat(ResourceUtils.isWritable(mockResource)).isFalse(); + + verifyNoInteractions(mockResource); + } + + @Test + public void isWritableWithNullResourceIsNullSafeReturnsFalse() { + assertThat(ResourceUtils.isWritable(null)).isFalse(); + } + @Test public void getWritableResourceReturnsWritableResourceFromResource() {