RuleDatabaseItemUpdateRunnable implements Runnable { @Override public void run() { try { android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); } catch (UnsatisfiedLinkError e) { } catch (RuntimeException e) { if (!e.toString().contains("not mocked")) throw e; } if (item.location.startsWith("content:/")) { try { Uri uri = parseUri(item.location); context.getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); context.getContentResolver().openInputStream(uri).close(); Log.d(TAG, "run: Permission requested for " + item.location); } catch (SecurityException e) { Log.d(TAG, "doInBackground: Error taking permission: ", e); parentTask.addError(item, context.getString(R.string.permission_denied)); } catch (FileNotFoundException e) { parentTask.addError(item, context.getString(R.string.file_not_found)); } catch (IOException e) { parentTask.addError(item, context.getString(R.string.unknown_error_s, e.getLocalizedMessage())); } return; } SingleWriterMultipleReaderFile singleWriterMultipleReaderFile = new SingleWriterMultipleReaderFile(file); HttpURLConnection connection = null; parentTask.addBegin(item); try { connection = getHttpURLConnection(file, singleWriterMultipleReaderFile, url); if (!validateResponse(connection)) return; downloadFile(file, singleWriterMultipleReaderFile, connection); } catch (SocketTimeoutException e) { parentTask.addError(item, context.getString(R.string.requested_timed_out)); }catch (IOException e) { parentTask.addError(item, context.getString(R.string.unknown_error_s, e.toString())); } finally { parentTask.addDone(item); if (connection != null) connection.disconnect(); } } RuleDatabaseItemUpdateRunnable(@NonNull RuleDatabaseUpdateTask parentTask, @NonNull Context context, @NonNull Configuration.Item item); @Override void run();  }
@Test public void testRun() throws Exception { CountingAnswer downloadCount = new CountingAnswer(null); when(mockTask.doInBackground()).thenCallRealMethod(); mockTask.context = mockContext; mockTask.errors = new ArrayList<>(); mockTask.done = new ArrayList<>(); mockTask.pending = new ArrayList<>(); mockTask.configuration = new Configuration(); mockTask.configuration.hosts = new Configuration.Hosts(); mockTask.configuration.hosts.items.add(new Configuration.Item()); mockTask.configuration.hosts.items.add(new Configuration.Item()); mockTask.configuration.hosts.items.add(new Configuration.Item()); mockTask.configuration.hosts.items.add(new Configuration.Item()); mockTask.configuration.hosts.items.get(0).title = "http-title"; mockTask.configuration.hosts.items.get(0).state = Configuration.Item.STATE_DENY; mockTask.configuration.hosts.items.get(0).location = "http: when(mockTask, "addError", any(Configuration.Item.class), anyString()).thenCallRealMethod(); when(mockTask, "addDone", any(Configuration.Item.class)).thenCallRealMethod(); RuleDatabaseItemUpdateRunnable itemUpdateRunnable = mock(RuleDatabaseItemUpdateRunnable.class); itemUpdateRunnable.parentTask = mockTask; itemUpdateRunnable.context = mockContext; itemUpdateRunnable.item = mockTask.configuration.hosts.items.get(0); when(itemUpdateRunnable, "run").thenCallRealMethod(); when(itemUpdateRunnable, "shouldDownload").thenCallRealMethod(); when(mockTask.getCommand(any(Configuration.Item.class))).thenReturn(itemUpdateRunnable); when(itemUpdateRunnable, "downloadFile", any(File.class), any(SingleWriterMultipleReaderFile.class), any(HttpURLConnection.class)).then(downloadCount); assertTrue(itemUpdateRunnable.shouldDownload()); itemUpdateRunnable.run(); assertEquals(0, downloadCount.numCalls); assertEquals(1, mockTask.done.size()); when(itemUpdateRunnable.validateResponse(any(HttpURLConnection.class))).thenReturn(true); when(itemUpdateRunnable.getHttpURLConnection(any(File.class), any(SingleWriterMultipleReaderFile.class), any(URL.class))).thenCallRealMethod(); when(itemUpdateRunnable.internalOpenHttpConnection(any(URL.class))).thenReturn(connection); assertTrue(itemUpdateRunnable.shouldDownload()); itemUpdateRunnable.run(); assertEquals(1, downloadCount.numCalls); assertEquals(2, mockTask.done.size()); CountingAnswer downloadExceptionCount = new CountingAnswer(null) { @Override public Object answer(InvocationOnMock invocation) throws Throwable { super.answer(invocation); throw new IOException("FooBarException"); } }; when(itemUpdateRunnable, "downloadFile", any(File.class), any(SingleWriterMultipleReaderFile.class), any(HttpURLConnection.class)).then(downloadExceptionCount); assertTrue(itemUpdateRunnable.shouldDownload()); itemUpdateRunnable.run(); assertEquals(1, downloadExceptionCount.numCalls); assertEquals(1, mockTask.errors.size()); assertEquals(3, mockTask.done.size()); assertTrue("http-title in" + mockTask.errors.get(0), mockTask.errors.get(0).matches(".*http-title.*")); }