RuleDatabaseItemUpdateRunnable implements Runnable { void downloadFile(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, HttpURLConnection connection) throws IOException { InputStream inStream = connection.getInputStream(); FileOutputStream outStream = singleWriterMultipleReaderFile.startWrite(); try { copyStream(inStream, outStream); singleWriterMultipleReaderFile.finishWrite(outStream); outStream = null; if (connection.getLastModified() == 0 || !file.setLastModified(connection.getLastModified())) { Log.d(TAG, "downloadFile: Could not set last modified"); } } finally { if (outStream != null) singleWriterMultipleReaderFile.failWrite(outStream); } } RuleDatabaseItemUpdateRunnable(@NonNull RuleDatabaseUpdateTask parentTask, @NonNull Context context, @NonNull Configuration.Item item); @Override void run();  }
@Test public void testDownloadFile() throws Exception { RuleDatabaseItemUpdateRunnable itemUpdateRunnable = new RuleDatabaseItemUpdateRunnable(realTask, mockContext, mock(Configuration.Item.class)); byte[] bar = new byte[]{'h', 'a', 'l', 'l', 'o'}; final ByteArrayInputStream bis = new ByteArrayInputStream(bar); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); FileOutputStream fos = mock(FileOutputStream.class); when(fos, "write", any(byte[].class), anyInt(), anyInt()).then(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { byte[] buffer = invocation.getArgumentAt(0, byte[].class); int off = invocation.getArgumentAt(1, Integer.class); int len = invocation.getArgumentAt(2, Integer.class); bos.write(buffer, off, len); return null; } }); when(connection.getInputStream()).thenReturn(bis); when(singleWriterMultipleReaderFile.startWrite()).thenReturn(fos); itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection); assertArrayEquals(bar, bos.toByteArray()); assertEquals(0, failAnswer.numCalls); assertEquals(1, finishAnswer.numCalls); bis.reset(); bos.reset(); }
@Test public void testDownloadFile_exception() throws Exception { RuleDatabaseItemUpdateRunnable itemUpdateRunnable = new RuleDatabaseItemUpdateRunnable(realTask, mockContext, mock(Configuration.Item.class)); FileOutputStream fos = mock(FileOutputStream.class); InputStream is = mock(InputStream.class); when(connection.getInputStream()).thenReturn(is); when(singleWriterMultipleReaderFile.startWrite()).thenReturn(fos); doThrow(new IOException("foobar")).when(fos, "write", any(byte[].class), anyInt(), anyInt()); try { itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection); fail("Should have thrown exception"); } catch (IOException e) { assertEquals("foobar", e.getMessage()); assertEquals(1, failAnswer.numCalls); assertEquals(0, finishAnswer.numCalls); } }
@Test public void testDownloadFile_lastModifiedFail() throws Exception { CountingAnswer debugAnswer = new CountingAnswer(null); CountingAnswer setLastModifiedAnswerTrue = new CountingAnswer(true); CountingAnswer setLastModifiedAnswerFalse = new CountingAnswer(false); RuleDatabaseItemUpdateRunnable itemUpdateRunnable = new RuleDatabaseItemUpdateRunnable(realTask, mockContext, mock(Configuration.Item.class)); FileOutputStream fos = mock(FileOutputStream.class); InputStream is = mock(InputStream.class); when(connection.getInputStream()).thenReturn(is); when(singleWriterMultipleReaderFile.startWrite()).thenReturn(fos); when(is.read(any(byte[].class))).thenReturn(-1); when(Log.d(anyString(), anyString())).then(debugAnswer); when(connection.getLastModified()).thenReturn(0L); when(file.setLastModified(anyLong())).then(setLastModifiedAnswerFalse); itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection); assertEquals(0, failAnswer.numCalls); assertEquals(1, debugAnswer.numCalls); assertEquals(1, finishAnswer.numCalls); when(connection.getLastModified()).thenReturn(0L); when(file.setLastModified(anyLong())).then(setLastModifiedAnswerTrue); itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection); assertEquals(0, failAnswer.numCalls); assertEquals(2, debugAnswer.numCalls); assertEquals(2, finishAnswer.numCalls); when(connection.getLastModified()).thenReturn(1L); when(file.setLastModified(anyLong())).then(setLastModifiedAnswerFalse); itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection); assertEquals(0, failAnswer.numCalls); assertEquals(3, debugAnswer.numCalls); assertEquals(3, finishAnswer.numCalls); when(connection.getLastModified()).thenReturn(1L); when(file.setLastModified(anyLong())).then(setLastModifiedAnswerTrue); itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection); assertEquals(0, failAnswer.numCalls); assertEquals(3, debugAnswer.numCalls); assertEquals(4, finishAnswer.numCalls); }