BAEL-4856 Code review
This commit is contained in:
parent
081abb8dd7
commit
f3a5ca615e
@ -8,10 +8,10 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
public class BinaryFileDownloaderIntegrationTest {
|
public class BinaryFileDownloaderIntegrationTest {
|
||||||
|
|
||||||
@ -19,19 +19,21 @@ public class BinaryFileDownloaderIntegrationTest {
|
|||||||
public MockWebServer server = new MockWebServer();
|
public MockWebServer server = new MockWebServer();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenATextFile_whenDownload_thenExpectFileDownloaded() throws IOException {
|
public void givenATextFile_whenDownload_thenExpectFileDownloaded() {
|
||||||
String body = "Hello Baeldung Readers!";
|
String body = "Hello Baeldung Readers!";
|
||||||
server.enqueue(new MockResponse().setBody(body));
|
server.enqueue(new MockResponse().setBody(body));
|
||||||
String fileName = "download.txt";
|
String fileName = "download.txt";
|
||||||
BinaryFileWriter writer = new BinaryFileWriter(new FileOutputStream(fileName), progress -> assertEquals(100.0, progress, .0));
|
|
||||||
BinaryFileDownloader tested = new BinaryFileDownloader(new OkHttpClient(), writer);
|
|
||||||
|
|
||||||
|
ProgressCallback progressCallback = progress -> assertEquals(100.0, progress, .0);
|
||||||
|
try (BinaryFileWriter writer = new BinaryFileWriter(new FileOutputStream(fileName), progressCallback); BinaryFileDownloader tested = new BinaryFileDownloader(new OkHttpClient(), writer)) {
|
||||||
long downloaded = tested.download(server.url("/greetings").toString());
|
long downloaded = tested.download(server.url("/greetings").toString());
|
||||||
|
|
||||||
assertEquals(body.length(), downloaded);
|
assertEquals(body.length(), downloaded);
|
||||||
File downloadedFile = new File(fileName);
|
File downloadedFile = new File(fileName);
|
||||||
assertTrue(downloadedFile.isFile());
|
assertTrue(downloadedFile.isFile());
|
||||||
assertTrue(downloadedFile.delete());
|
assertTrue(downloadedFile.delete());
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("An unexpected exception has occurred: " + e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
|||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyDouble;
|
import static org.mockito.ArgumentMatchers.anyDouble;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
@ -53,7 +52,7 @@ public class BinaryFileDownloaderUnitTest {
|
|||||||
verify(writer).close();
|
verify(writer).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = IllegalStateException.class)
|
||||||
public void givenUrlAndResponseWithNullBody_whenDownload_thenExpectIllegalStateException() throws Exception {
|
public void givenUrlAndResponseWithNullBody_whenDownload_thenExpectIllegalStateException() throws Exception {
|
||||||
String url = "http://example.com/file";
|
String url = "http://example.com/file";
|
||||||
Call call = mock(Call.class);
|
Call call = mock(Call.class);
|
||||||
@ -61,7 +60,7 @@ public class BinaryFileDownloaderUnitTest {
|
|||||||
Response response = createResponse(url, null);
|
Response response = createResponse(url, null);
|
||||||
when(call.execute()).thenReturn(response);
|
when(call.execute()).thenReturn(response);
|
||||||
|
|
||||||
assertThrows(IllegalStateException.class, () -> tested.download(url));
|
tested.download(url);
|
||||||
|
|
||||||
verify(writer, times(0)).write(any(InputStream.class), anyDouble());
|
verify(writer, times(0)).write(any(InputStream.class), anyDouble());
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
@ -28,7 +28,7 @@ public class BinaryFileWriterUnitTest {
|
|||||||
InputStream inputStream = mock(InputStream.class);
|
InputStream inputStream = mock(InputStream.class);
|
||||||
when(inputStream.read(any(), anyInt(), anyInt())).thenReturn(10, -1);
|
when(inputStream.read(any(), anyInt(), anyInt())).thenReturn(10, -1);
|
||||||
|
|
||||||
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress))) {
|
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress, .0))) {
|
||||||
long result = tested.write(inputStream, 10);
|
long result = tested.write(inputStream, 10);
|
||||||
|
|
||||||
assertEquals(10, result);
|
assertEquals(10, result);
|
||||||
@ -42,7 +42,7 @@ public class BinaryFileWriterUnitTest {
|
|||||||
public void givenInputStreamEmpty_whenWrite_thenExpectNotWritten() throws Exception {
|
public void givenInputStreamEmpty_whenWrite_thenExpectNotWritten() throws Exception {
|
||||||
InputStream inputStream = mock(InputStream.class);
|
InputStream inputStream = mock(InputStream.class);
|
||||||
|
|
||||||
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress))) {
|
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream, progress -> assertEquals(100.0, progress, .0))) {
|
||||||
long result = tested.write(inputStream, 1);
|
long result = tested.write(inputStream, 1);
|
||||||
|
|
||||||
assertEquals(0, result);
|
assertEquals(0, result);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user