Add more tests for the Sone inserter.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 30 Sep 2014 19:21:39 +0000 (21:21 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 30 Sep 2014 19:21:39 +0000 (21:21 +0200)
src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java

index d43f383..c44ced6 100644 (file)
@@ -12,6 +12,7 @@ import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.argThat;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
@@ -221,4 +222,37 @@ public class SoneInserterTest {
                verify(core, never()).touchConfiguration();
        }
 
+       @Test
+       public void soneInserterExitsIfSoneIsUnknown() {
+               SoneModificationDetector soneModificationDetector =
+                               mock(SoneModificationDetector.class);
+               SoneInserter soneInserter =
+                               new SoneInserter(core, eventBus, freenetInterface, "SoneId",
+                                               soneModificationDetector, 1);
+               when(soneModificationDetector.isEligibleForInsert()).thenReturn(true);
+               when(core.getSone("SoneId")).thenReturn(Optional.<Sone>absent());
+               soneInserter.serviceRun();
+       }
+
+       @Test
+       public void soneInserterCatchesExceptionAndContinues() {
+               SoneModificationDetector soneModificationDetector =
+                               mock(SoneModificationDetector.class);
+               final SoneInserter soneInserter =
+                               new SoneInserter(core, eventBus, freenetInterface, "SoneId",
+                                               soneModificationDetector, 1);
+               Answer<Optional<Sone>> stopInserterAndThrowException =
+                               new Answer<Optional<Sone>>() {
+                                       @Override
+                                       public Optional<Sone> answer(
+                                                       InvocationOnMock invocation) {
+                                               soneInserter.stop();
+                                               throw new NullPointerException();
+                                       }
+                               };
+               when(soneModificationDetector.isEligibleForInsert()).thenAnswer(
+                               stopInserterAndThrowException);
+               soneInserter.serviceRun();
+       }
+
 }