test_class.declarator: mockContext
test_class.modifier: private
test_class.original_string: private Context mockContext;
test_class.type: Context
test_class.var_name: mockContext
test_class.declarator: file
test_class.modifier: private
test_class.original_string: private File file;
test_class.type: File
test_class.var_name: file
test_class.declarator: singleWriterMultipleReaderFile
test_class.modifier: private
test_class.original_string: private SingleWriterMultipleReaderFile singleWriterMultipleReaderFile;
test_class.type: SingleWriterMultipleReaderFile
test_class.var_name: singleWriterMultipleReaderFile
test_class.declarator: connection
test_class.modifier: private
test_class.original_string: private HttpURLConnection connection;
test_class.type: HttpURLConnection
test_class.var_name: connection
test_class.declarator: finishAnswer
test_class.modifier: private
test_class.original_string: private CountingAnswer finishAnswer;
test_class.type: CountingAnswer
test_class.var_name: finishAnswer
test_class.declarator: failAnswer
test_class.modifier: private
test_class.original_string: private CountingAnswer failAnswer;
test_class.type: CountingAnswer
test_class.var_name: failAnswer
test_class.declarator: url
test_class.modifier: private
test_class.original_string: private URL url;
test_class.type: URL
test_class.var_name: url
test_class.declarator: realTask
test_class.modifier: private
test_class.original_string: private RuleDatabaseUpdateTask realTask;
test_class.type: RuleDatabaseUpdateTask
test_class.var_name: realTask
test_class.declarator: mockTask
test_class.modifier: private
test_class.original_string: private RuleDatabaseUpdateTask mockTask;
test_class.type: RuleDatabaseUpdateTask
test_class.var_name: mockTask
test_class.declarator: mockResolver
test_class.modifier: private
test_class.original_string: private ContentResolver mockResolver;
test_class.type: ContentResolver
test_class.var_name: mockResolver
test_class.file: app/src/test/java/org/jak_linux/dns66/db/RuleDatabaseItemUpdateRunnableTest.java
test_class.identifier: RuleDatabaseItemUpdateRunnableTest
test_case.body: @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://foo";
  
          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);
  
  
          // Scenario 1: Validate response fails
          assertTrue(itemUpdateRunnable.shouldDownload());
          itemUpdateRunnable.run();
          assertEquals(0, downloadCount.numCalls);
          assertEquals(1, mockTask.done.size());
  
          // Scenario 2: Validate response succeeds
          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());
  
          // Scenario 3: Download file throws an exception
          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.*"));
      }
test_case.class_method_signature: RuleDatabaseItemUpdateRunnableTest.testRun()
test_case.full_signature: @Test public void testRun()
test_case.identifier: testRun
test_case.invocations: thenCallRealMethod when doInBackground add add add add get get get thenCallRealMethod when any anyString thenCallRealMethod when any mock get thenCallRealMethod when thenCallRealMethod when thenReturn when getCommand any then when any any any assertTrue shouldDownload run assertEquals assertEquals size thenReturn when validateResponse any thenCallRealMethod when getHttpURLConnection any any any thenReturn when internalOpenHttpConnection any assertTrue shouldDownload run assertEquals assertEquals size answer then when any any any assertTrue shouldDownload run assertEquals assertEquals size assertEquals size assertTrue get matches get
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testRun()
focal_class.declarator: CONNECT_TIMEOUT_MILLIS = 30000
focal_class.modifier: private static final
focal_class.original_string: private static final int CONNECT_TIMEOUT_MILLIS = 30000;
focal_class.type: int
focal_class.var_name: CONNECT_TIMEOUT_MILLIS
focal_class.declarator: READ_TIMEOUT_MILLIS = 10000
focal_class.modifier: private static final
focal_class.original_string: private static final int READ_TIMEOUT_MILLIS = 10000;
focal_class.type: int
focal_class.var_name: READ_TIMEOUT_MILLIS
focal_class.declarator: TAG = "RuleDbItemUpdate"
focal_class.modifier: private static final
focal_class.original_string: private static final String TAG = "RuleDbItemUpdate";
focal_class.type: String
focal_class.var_name: TAG
focal_class.declarator: parentTask
focal_class.original_string: RuleDatabaseUpdateTask parentTask;
focal_class.type: RuleDatabaseUpdateTask
focal_class.var_name: parentTask
focal_class.declarator: item
focal_class.original_string: Configuration.Item item;
focal_class.type: Configuration.Item
focal_class.var_name: item
focal_class.declarator: context
focal_class.original_string: Context context;
focal_class.type: Context
focal_class.var_name: context
focal_class.declarator: url
focal_class.modifier: private
focal_class.original_string: private URL url;
focal_class.type: URL
focal_class.var_name: url
focal_class.declarator: file
focal_class.modifier: private
focal_class.original_string: private File file;
focal_class.type: File
focal_class.var_name: file
focal_class.file: app/src/main/java/org/jak_linux/dns66/db/RuleDatabaseItemUpdateRunnable.java
focal_class.identifier: RuleDatabaseItemUpdateRunnable
focal_class.interfaces: implements Runnable
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.RuleDatabaseItemUpdateRunnable(@NonNull RuleDatabaseUpdateTask parentTask, @NonNull Context context, @NonNull Configuration.Item item)
focal_class.full_signature:   RuleDatabaseItemUpdateRunnable(@NonNull RuleDatabaseUpdateTask parentTask, @NonNull Context context, @NonNull Configuration.Item item)
focal_class.identifier: RuleDatabaseItemUpdateRunnable
focal_class.parameters: (@NonNull RuleDatabaseUpdateTask parentTask, @NonNull Context context, @NonNull Configuration.Item item)
focal_class.signature:  RuleDatabaseItemUpdateRunnable(@NonNull RuleDatabaseUpdateTask parentTask, @NonNull Context context, @NonNull Configuration.Item item)
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.shouldDownload()
focal_class.full_signature:  boolean shouldDownload()
focal_class.identifier: shouldDownload
focal_class.parameters: ()
focal_class.return: boolean
focal_class.signature: boolean shouldDownload()
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.run()
focal_class.full_signature: @Override public void run()
focal_class.identifier: run
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void run()
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.parseUri(String uri)
focal_class.full_signature:  Uri parseUri(String uri)
focal_class.identifier: parseUri
focal_class.parameters: (String uri)
focal_class.return: Uri
focal_class.signature: Uri parseUri(String uri)
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.getHttpURLConnection(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, URL url)
focal_class.full_signature: @NonNull HttpURLConnection getHttpURLConnection(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, URL url)
focal_class.identifier: getHttpURLConnection
focal_class.modifiers: @NonNull
focal_class.parameters: (File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, URL url)
focal_class.return: HttpURLConnection
focal_class.signature: HttpURLConnection getHttpURLConnection(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, URL url)
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.internalOpenHttpConnection(URL url)
focal_class.full_signature:  HttpURLConnection internalOpenHttpConnection(URL url)
focal_class.identifier: internalOpenHttpConnection
focal_class.parameters: (URL url)
focal_class.return: HttpURLConnection
focal_class.signature: HttpURLConnection internalOpenHttpConnection(URL url)
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.validateResponse(HttpURLConnection connection)
focal_class.full_signature:  boolean validateResponse(HttpURLConnection connection)
focal_class.identifier: validateResponse
focal_class.parameters: (HttpURLConnection connection)
focal_class.return: boolean
focal_class.signature: boolean validateResponse(HttpURLConnection connection)
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.downloadFile(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, HttpURLConnection connection)
focal_class.full_signature:  void downloadFile(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, HttpURLConnection connection)
focal_class.identifier: downloadFile
focal_class.parameters: (File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, HttpURLConnection connection)
focal_class.return: void
focal_class.signature: void downloadFile(File file, SingleWriterMultipleReaderFile singleWriterMultipleReaderFile, HttpURLConnection connection)
focal_class.class_method_signature: RuleDatabaseItemUpdateRunnable.copyStream(InputStream inStream, OutputStream outStream)
focal_class.full_signature:  void copyStream(InputStream inStream, OutputStream outStream)
focal_class.identifier: copyStream
focal_class.parameters: (InputStream inStream, OutputStream outStream)
focal_class.return: void
focal_class.signature: void copyStream(InputStream inStream, OutputStream outStream)
focal_method.body: @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();
          }
      }
focal_method.class_method_signature: RuleDatabaseItemUpdateRunnable.run()
focal_method.full_signature: @Override public void run()
focal_method.identifier: run
focal_method.invocations: setThreadPriority contains toString startsWith parseUri takePersistableUriPermission getContentResolver close openInputStream getContentResolver d d addError getString addError getString addError getString getLocalizedMessage addBegin getHttpURLConnection validateResponse downloadFile addError getString addError getString toString addDone disconnect
focal_method.modifiers: @Override public
focal_method.parameters: ()
focal_method.return: void
focal_method.signature: void run()