RuleDatabase { boolean loadReader(Configuration.Item item, Reader reader) throws InterruptedException { int count = 0; try { Log.d(TAG, "loadBlockedHosts: Reading: " + item.location); try (BufferedReader br = new BufferedReader(reader)) { String line; while ((line = br.readLine()) != null) { if (Thread.interrupted()) throw new InterruptedException("Interrupted"); String host = parseLine(line); if (host != null) { count += 1; addHost(item, host); } } } Log.d(TAG, "loadBlockedHosts: Loaded " + count + " hosts from " + item.location); return true; } catch (IOException e) { Log.e(TAG, "loadBlockedHosts: Error while reading " + item.location + " after " + count + " items", e); return false; } finally { FileHelper.closeOrWarn(reader, TAG, "loadBlockedHosts: Error closing " + item.location); } } RuleDatabase(); static RuleDatabase getInstance(); boolean isBlocked(String host); synchronized void initialize(Context context);  }
@Test public void testLoadReader() throws Exception { RuleDatabase db = new RuleDatabase(); db.nextBlockedHosts = db.blockedHosts.get(); Configuration.Item item = new Configuration.Item(); item.location = "<some random file>"; item.state = Configuration.Item.STATE_IGNORE; assertTrue(db.loadReader(item, new StringReader("example.com"))); assertTrue(db.isEmpty()); assertFalse(db.isBlocked("example.com")); item.state = Configuration.Item.STATE_DENY; assertTrue(db.loadReader(item, new StringReader("example.com"))); assertFalse(db.isEmpty()); assertTrue(db.isBlocked("example.com")); item.state = Configuration.Item.STATE_ALLOW; assertTrue(db.loadReader(item, new StringReader("example.com"))); assertTrue(db.isEmpty()); assertFalse(db.isBlocked("example.com")); item.state = Configuration.Item.STATE_DENY; assertFalse(db.isBlocked("example.com")); assertFalse(db.isBlocked("foo.com")); assertTrue(db.loadReader(item, new StringReader("example.com\n127.0.0.1 foo.com"))); assertFalse(db.isEmpty()); assertTrue(db.isBlocked("example.com")); assertTrue(db.isBlocked("foo.com")); Thread.currentThread().interrupt(); try { db.loadReader(item, new StringReader("example.com")); fail("Interrupted thread did not cause reader to be interrupted"); } catch (InterruptedException e) { } item.state = Configuration.Item.STATE_DENY; assertTrue(db.loadReader(item, new StringReader("invalid line\notherhost.com"))); assertTrue(db.isBlocked("otherhost.com")); item.state = Configuration.Item.STATE_ALLOW; assertTrue(db.loadReader(item, new StringReader("invalid line\notherhost.com"))); assertFalse(db.isBlocked("otherhost.com")); Reader reader = Mockito.mock(Reader.class); doThrow(new IOException()).when(reader).read((char[]) any()); doThrow(new IOException()).when(reader).read((char[]) any(), anyInt(), anyInt()); doThrow(new IOException()).when(reader).read(any(CharBuffer.class)); assertFalse(db.loadReader(item, reader)); }