Refactor FileResourceWriter to throw a ResourceWriteException on a IOException.
Resolves gh-92.
This commit is contained in:
@@ -30,6 +30,8 @@ import org.springframework.core.io.Resource;
|
|||||||
import org.springframework.dao.DataAccessResourceFailureException;
|
import org.springframework.dao.DataAccessResourceFailureException;
|
||||||
import org.springframework.data.gemfire.util.ArrayUtils;
|
import org.springframework.data.gemfire.util.ArrayUtils;
|
||||||
import org.springframework.geode.core.io.AbstractResourceWriter;
|
import org.springframework.geode.core.io.AbstractResourceWriter;
|
||||||
|
import org.springframework.geode.core.io.ResourceDataAccessException;
|
||||||
|
import org.springframework.geode.core.io.ResourceWriteException;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
@@ -81,7 +83,7 @@ public class FileResourceWriter extends AbstractResourceWriter {
|
|||||||
String message = String.format("Failed to write data (%1$d byte(s)) to Resource using [%2$s]",
|
String message = String.format("Failed to write data (%1$d byte(s)) to Resource using [%2$s]",
|
||||||
data.length, getClass().getName());
|
data.length, getClass().getName());
|
||||||
|
|
||||||
throw new DataAccessResourceFailureException(message, cause);
|
throw new ResourceWriteException(message, cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -189,7 +191,7 @@ public class FileResourceWriter extends AbstractResourceWriter {
|
|||||||
String message = String.format("Failed to access the Resource [%s] as a file",
|
String message = String.format("Failed to access the Resource [%s] as a file",
|
||||||
resource.getDescription());
|
resource.getDescription());
|
||||||
|
|
||||||
throw new DataAccessResourceFailureException(message, cause);
|
throw new ResourceDataAccessException(message, cause);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.orElseThrow(() -> newIllegalStateException("Resource [%s] is not a file based resource",
|
.orElseThrow(() -> newIllegalStateException("Resource [%s] is not a file based resource",
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.WritableResource;
|
import org.springframework.core.io.WritableResource;
|
||||||
import org.springframework.dao.DataAccessResourceFailureException;
|
import org.springframework.geode.core.io.ResourceDataAccessException;
|
||||||
|
import org.springframework.geode.core.io.ResourceWriteException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit Tests for {@link FileResourceWriter}.
|
* Unit Tests for {@link FileResourceWriter}.
|
||||||
@@ -74,12 +75,12 @@ public class FileResourceWriterUnitTests {
|
|||||||
assertThat(out.toByteArray()).isEqualTo(data);
|
assertThat(out.toByteArray()).isEqualTo(data);
|
||||||
|
|
||||||
verify(mockResource, times(1)).isFile();
|
verify(mockResource, times(1)).isFile();
|
||||||
verify(mockResource, times(1)).isWritable();
|
|
||||||
verify(mockResource, times(1)).getOutputStream();
|
verify(mockResource, times(1)).getOutputStream();
|
||||||
verify(writer, times(1)).doWrite(eq(out), eq(data));
|
verify(writer, times(1)).doWrite(eq(out), eq(data));
|
||||||
|
verifyNoMoreInteractions(mockResource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = DataAccessResourceFailureException.class)
|
@Test(expected = ResourceWriteException.class)
|
||||||
public void doWriteHandlesIOExceptionThrowsDataAccessResourceFailureException() throws IOException {
|
public void doWriteHandlesIOExceptionThrowsDataAccessResourceFailureException() throws IOException {
|
||||||
|
|
||||||
byte[] data = { (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE };
|
byte[] data = { (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE };
|
||||||
@@ -95,7 +96,7 @@ public class FileResourceWriterUnitTests {
|
|||||||
try {
|
try {
|
||||||
writer.doWrite(mockOutputStream, data);
|
writer.doWrite(mockOutputStream, data);
|
||||||
}
|
}
|
||||||
catch (DataAccessResourceFailureException expected) {
|
catch (ResourceWriteException expected) {
|
||||||
|
|
||||||
assertThat(expected)
|
assertThat(expected)
|
||||||
.hasMessageStartingWith("Failed to write data (%d byte(s)) to Resource using [%s]",
|
.hasMessageStartingWith("Failed to write data (%d byte(s)) to Resource using [%s]",
|
||||||
@@ -264,8 +265,8 @@ public class FileResourceWriterUnitTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = DataAccessResourceFailureException.class)
|
@Test(expected = ResourceDataAccessException.class)
|
||||||
public void newFileOutputStreamHandlesIOExceptionThrowsDataAccessResourceFailureException() throws IOException {
|
public void newFileOutputStreamHandlesIOExceptionThrowsResourceDataAccessException() throws IOException {
|
||||||
|
|
||||||
Resource mockResource = mock(Resource.class);
|
Resource mockResource = mock(Resource.class);
|
||||||
|
|
||||||
@@ -280,7 +281,7 @@ public class FileResourceWriterUnitTests {
|
|||||||
try {
|
try {
|
||||||
writer.newFileOutputStream();
|
writer.newFileOutputStream();
|
||||||
}
|
}
|
||||||
catch (DataAccessResourceFailureException expected) {
|
catch (ResourceDataAccessException expected) {
|
||||||
|
|
||||||
assertThat(expected).hasMessageStartingWith("Failed to access the Resource [FILE] as a file");
|
assertThat(expected).hasMessageStartingWith("Failed to access the Resource [FILE] as a file");
|
||||||
assertThat(expected).hasCauseInstanceOf(IOException.class);
|
assertThat(expected).hasCauseInstanceOf(IOException.class);
|
||||||
|
|||||||
Reference in New Issue
Block a user