From: David ‘Bombe’ Roden Date: Fri, 12 Sep 2014 20:35:57 +0000 (+0200) Subject: Add interface between freenet interface and Sone downloader. X-Git-Tag: 0.9-rc1^2~3^2~132 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=40a1ac4c90c1480936c9c782727209cf23acd5e5 Add interface between freenet interface and Sone downloader. --- diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index 5dd8fbf..b0c108a 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -182,16 +182,7 @@ public class FreenetInterface { } } - /** - * Registers the USK for the given Sone and notifies the given - * {@link SoneDownloader} if an update was found. - * - * @param sone - * The Sone to watch - * @param soneDownloader - * The Sone download to notify on updates - */ - public void registerUsk(final Sone sone, final SoneDownloader soneDownloader) { + public void registerUsk(final Sone sone, final SoneUpdater soneUpdater) { try { logger.log(Level.FINE, String.format("Registering Sone “%s” for USK updates at %s…", sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" }))); USKCallback uskCallback = new USKCallback() { @@ -200,10 +191,7 @@ public class FreenetInterface { @SuppressWarnings("synthetic-access") public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) { logger.log(Level.FINE, String.format("Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.", sone, key, newKnownGood, newSlotToo)); - if (edition > sone.getLatestEdition()) { - sone.setLatestEdition(edition); - new Thread(soneDownloader.fetchSoneAction(sone), "Sone Downloader").start(); - } + soneUpdater.updateSone(sone, edition); } @Override diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 7270662..be0be02 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -14,7 +14,6 @@ import freenet.keys.FreenetURI; public interface SoneDownloader extends Service { void addSone(Sone sone); - void fetchSone(Sone sone); void fetchSone(Sone sone, FreenetURI soneUri); Sone fetchSone(Sone sone, FreenetURI soneUri, boolean fetchOnly); diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java index ebdbecb..fed6528 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java @@ -103,18 +103,19 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade if (!sones.add(sone)) { freenetInterface.unregisterUsk(sone); } - freenetInterface.registerUsk(sone, this); + freenetInterface.registerUsk(sone, new SoneUpdater() { + @Override + public void updateSone(Sone sone, long edition) { + if (edition > sone.getLatestEdition()) { + sone.setLatestEdition(edition); + new Thread(fetchSoneAction(sone), + "Sone Downloader").start(); + } + } + }); } - /** - * Fetches the updated Sone. This method is a callback method for - * {@link FreenetInterface#registerUsk(Sone, SoneDownloader)}. - * - * @param sone - * The Sone to fetch - */ - @Override - public void fetchSone(Sone sone) { + private void fetchSone(Sone sone) { fetchSone(sone, sone.getRequestUri().sskForUSK()); } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneUpdater.java b/src/main/java/net/pterodactylus/sone/core/SoneUpdater.java new file mode 100644 index 0000000..58c38f5 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/SoneUpdater.java @@ -0,0 +1,15 @@ +package net.pterodactylus.sone.core; + +import net.pterodactylus.sone.data.Sone; + +/** + * Component that decides whether a Sone needs to be downloaded because a + * newer edition was found. + * + * @author David ‘Bombe’ Roden + */ +public interface SoneUpdater { + + void updateSone(Sone sone, long edition); + +} diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java index f4217eb..e92054a 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -93,6 +93,7 @@ public class FreenetInterfaceTest { private final Sone sone = mock(Sone.class); private final ArgumentCaptor callbackCaptor = forClass(USKCallback.class); private final SoneDownloader soneDownloader = mock(SoneDownloader.class); + private final SoneUpdater soneUpdater = mock(SoneUpdater.class); private final Image image = mock(Image.class); private InsertToken insertToken; @@ -290,32 +291,9 @@ public class FreenetInterfaceTest { @Test public void callbackForRegisteredSoneWithHigherEditionTriggersDownload() throws InterruptedException { - freenetInterface.registerUsk(sone, soneDownloader); - final CountDownLatch downloadTriggered = new CountDownLatch(1); - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - downloadTriggered.countDown(); - return null; - } - }).when(soneDownloader).fetchSone(sone); + freenetInterface.registerUsk(sone, soneUpdater); callbackCaptor.getValue().onFoundEdition(1, null, null, null, false, (short) 0, null, false, false); - assertThat(downloadTriggered.await(1, SECONDS), is(true)); - } - - @Test - public void callbackForRegisteredSoneWithTheSameEditionDoesNotTriggerDownload() throws InterruptedException { - freenetInterface.registerUsk(sone, soneDownloader); - final CountDownLatch downloadTriggered = new CountDownLatch(1); - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - downloadTriggered.countDown(); - return null; - } - }).when(soneDownloader).fetchSone(sone); - callbackCaptor.getValue().onFoundEdition(0, null, null, null, false, (short) 0, null, false, false); - assertThat(downloadTriggered.await(1, SECONDS), is(false)); + verify(soneUpdater).updateSone(sone, 1); } @Test diff --git a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java index b05d2ca..24a1ac9 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneDownloaderTest.java @@ -75,6 +75,7 @@ public class SoneDownloaderTest { private final Core core = mock(Core.class); private final FreenetInterface freenetInterface = mock(FreenetInterface.class); private final SoneDownloaderImpl soneDownloader = new SoneDownloaderImpl(core, freenetInterface); + private final SoneUpdater soneUpdater = mock(SoneUpdater.class); private final FreenetURI requestUri = mock(FreenetURI.class); private final Sone sone = mock(Sone.class); private final PostBuilder postBuilder = mock(PostBuilder.class); @@ -341,7 +342,7 @@ public class SoneDownloaderTest { @Test public void addingASoneWillRegisterItsKey() { soneDownloader.addSone(sone); - verify(freenetInterface).registerUsk(sone, soneDownloader); + verify(freenetInterface).registerUsk(eq(sone), any(SoneUpdater.class)); verify(freenetInterface, never()).unregisterUsk(sone); } @@ -349,7 +350,7 @@ public class SoneDownloaderTest { public void addingASoneTwiceWillAlsoDeregisterItsKey() { soneDownloader.addSone(sone); soneDownloader.addSone(sone); - verify(freenetInterface, times(2)).registerUsk(sone, soneDownloader); + verify(freenetInterface, times(2)).registerUsk(eq(sone), any(SoneUpdater.class)); verify(freenetInterface).unregisterUsk(sone); } @@ -730,7 +731,7 @@ public class SoneDownloaderTest { @Test public void notBeingAbleToFetchAnUnknownSoneDoesNotUpdateCore() { - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); verify(freenetInterface).fetchUri(requestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown); verify(core, never()).updateSone(any(Sone.class)); @@ -746,7 +747,7 @@ public class SoneDownloaderTest { @Test public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() { when(sone.getTime()).thenReturn(1000L); - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); verify(freenetInterface).fetchUri(requestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle); verify(core, never()).updateSone(any(Sone.class)); @@ -756,7 +757,7 @@ public class SoneDownloaderTest { public void exceptionWhileFetchingAnUnknownSoneDoesNotUpdateCore() { when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class); try { - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); } finally { verify(freenetInterface).fetchUri(requestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(unknown); @@ -769,7 +770,7 @@ public class SoneDownloaderTest { when(sone.getTime()).thenReturn(1000L); when(freenetInterface.fetchUri(requestUri)).thenThrow(NullPointerException.class); try { - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); } finally { verify(freenetInterface).fetchUri(requestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle); @@ -781,7 +782,7 @@ public class SoneDownloaderTest { public void successfulFetchingOfSoneWithUskRequestUriUpdatesTheCoreWithASone() throws IOException { final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml")); when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); verifyThatParsedSoneHasTheSameIdAsTheOriginalSone(); } @@ -796,7 +797,7 @@ public class SoneDownloaderTest { when(requestUri.getKeyType()).thenReturn("SSK"); final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml")); when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); verifyThatParsedSoneHasTheSameIdAsTheOriginalSone(); } @@ -805,7 +806,7 @@ public class SoneDownloaderTest { when(requestUri.getKeyType()).thenReturn("SSK"); final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-with-zero-time.xml")); when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); verifyThatParsedSoneHasTheSameIdAsTheOriginalSone(); } @@ -813,7 +814,7 @@ public class SoneDownloaderTest { public void fetchingSoneWithInvalidXmlWillNotUpdateTheCore() throws IOException { final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-not-xml.xml")); when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); verify(core, never()).updateSone(any(Sone.class)); } @@ -822,7 +823,7 @@ public class SoneDownloaderTest { final Fetched fetchResult = createFetchResult(requestUri, getClass().getResourceAsStream("sone-parser-no-payload.xml")); when(sone.getId()).thenThrow(NullPointerException.class); when(freenetInterface.fetchUri(requestUri)).thenReturn(fetchResult); - soneDownloader.fetchSone(sone); + soneDownloader.fetchSoneAction(sone).run(); verify(core, never()).updateSone(any(Sone.class)); }