RobotsLoaderImpl implements RobotsLoader { @Nonnull @Override public Robots load(@Nonnull URI robotsResource) { checkNotNull(robotsResource, "robotsResource"); log.debug("Loading: {}", robotsResource); return load(robotsResource, charSourceSupplier.get(robotsResource)); } RobotsLoaderImpl(@Nonnull RobotsFactory factory); @Nonnull @Override Robots load(@Nonnull URI robotsResource); void close();  }
@Test(expected = NullPointerException.class) public void givenNullSource_whenLoad_thenThrowsNPE() throws IOException { instance.load(null); }
@Test public void givenEmptySource_whenLoad_thenHandlerGetInvoked() throws IOException { when(charSourceSupplier.get(any(URI.class))) .thenReturn(CharSource.empty()); instance.load(EXAMPLE_URI); verify(handler).get(); }
@Test(expected = ExpectedError.class) public void givenSourceThrowsError_whenLoad_thenThrowsError() { when(charSourceSupplier.get(any(URI.class))) .thenReturn(new CharSource() { @Nonnull @Override public Reader openStream() { throw new ExpectedError(); } }); instance.load(EXAMPLE_URI); }
@Test(expected = ExpectedRuntimeException.class) public void givenSourceThrowsRuntimeException_whenLoad_thenThrowsRuntimeException() { when(charSourceSupplier.get(any(URI.class))) .thenReturn(new CharSource() { @Nonnull @Override public Reader openStream() { throw new ExpectedRuntimeException(); } }); instance.load(EXAMPLE_URI); }
@Test public void givenSourceThrowsIOException_whenLoad_thenAllowAll() { when(charSourceSupplier.get(any(URI.class))) .thenReturn(new CharSource() { @Nonnull @Override public Reader openStream() throws IOException { throw new IOException(); } }); Robots result = instance.load(EXAMPLE_URI); assertThat(result, equalTo(ALLOW_ALL)); }
@Test public void givenSupplierThrowsTemporaryAllow_whenLoad_thenAllowAll() throws IOException { CharSource charSource = mock(CharSource.class); when(charSource.openBufferedStream()).thenThrow(new TemporaryAllow("")); when(charSourceSupplier.get(any(URI.class))).thenReturn(charSource); Robots result = instance.load(EXAMPLE_URI); assertThat(result, equalTo(ALLOW_ALL)); }
@Test public void givenSupplierThrowsTemporaryDisallow_whenLoad_thenDisallowAll() throws IOException { CharSource charSource = mock(CharSource.class); when(charSource.openBufferedStream()).thenThrow(new TemporaryDisallow("")); when(charSourceSupplier.get(any(URI.class))).thenReturn(charSource); Robots result = instance.load(EXAMPLE_URI); assertThat(result, equalTo(DISALLOW_ALL)); }
@Test public void givenSupplierThrowsIOECausedByTimeout_whenLoad_thenAllowAll() throws IOException { CharSource charSource = mock(CharSource.class); when(charSource.openBufferedStream()).thenThrow(new IOException(new TimeoutException())); when(charSourceSupplier.get(any(URI.class))).thenReturn(charSource); Robots result = instance.load(EXAMPLE_URI); assertThat(result, equalTo(ALLOW_ALL)); }
@Test public void givenParserThrowsParseException_whenLoad_thenAllowAll() throws IOException, ParseException { when(charSourceSupplier.get(any(URI.class))) .thenReturn(CharSource.empty()); doThrow(new ParseException()).when(parser).parse(any(RobotsParseHandler.class)); when(factory.createRobotsParser(any(Reader.class))).thenReturn(parser); Robots result = instance.load(EXAMPLE_URI); assertThat(result, equalTo(ALLOW_ALL)); }