From: David ‘Bombe’ Roden Date: Fri, 12 Jun 2015 18:47:55 +0000 (+0200) Subject: Merge branch 'last-working' into next X-Git-Tag: 0.9-rc1^2~3 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=f229fe41f708d2b275c20ceb9aba5993761218a3;hp=1d3da2623a113f74f76422030af040a0abc9c3d7 Merge branch 'last-working' into next This is the current state with lots and lots of changes since the last release. I have been using this state during the last year and it should work pretty much the same as before (minus a couple of bugs). Conflicts: src/main/java/net/pterodactylus/sone/core/FreenetInterface.java src/main/java/net/pterodactylus/sone/core/SoneInserter.java src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java --- diff --git a/pom.xml b/pom.xml index 0dfc383..67e29c9 100644 --- a/pom.xml +++ b/pom.xml @@ -29,13 +29,13 @@ org.freenetproject fred - 0.7.5.1405 + 0.7.5.1467.99.3 provided org.freenetproject freenet-ext - 26 + 29 provided diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index dc0e3ea..e802ba2 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -38,8 +38,6 @@ import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; -import com.db4o.ObjectContainer; - import com.google.common.base.Function; import com.google.common.eventbus.EventBus; import com.google.inject.Inject; @@ -47,6 +45,7 @@ import com.google.inject.Singleton; import freenet.client.ClientMetadata; import freenet.client.FetchException; +import freenet.client.FetchException.FetchExceptionMode; import freenet.client.FetchResult; import freenet.client.HighLevelSimpleClient; import freenet.client.InsertBlock; @@ -64,7 +63,9 @@ import freenet.node.Node; import freenet.node.RequestClient; import freenet.node.RequestStarter; import freenet.support.api.Bucket; +import freenet.support.api.RandomAccessBucket; import freenet.support.io.ArrayBucket; +import freenet.support.io.ResumeFailedException; /** * Contains all necessary functionality for interacting with the Freenet node. @@ -92,6 +93,18 @@ public class FreenetInterface { /** The not-Sone-related USK callbacks. */ private final Map uriUskCallbacks = Collections.synchronizedMap(new HashMap()); + private final RequestClient imageInserts = new RequestClient() { + @Override + public boolean persistent() { + return false; + } + + @Override + public boolean realTimeFlag() { + return true; + } + }; + /** * Creates a new Freenet interface. * @@ -125,7 +138,7 @@ public class FreenetInterface { FetchResult fetchResult = client.fetch(currentUri); return new Fetched(currentUri, fetchResult); } catch (FetchException fe1) { - if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) { + if (fe1.getMode() == FetchExceptionMode.PERMANENT_REDIRECT) { currentUri = fe1.newURI; continue; } @@ -154,11 +167,12 @@ public class FreenetInterface { InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, ""); FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint); InsertContext insertContext = client.getInsertContext(true); - Bucket bucket = new ArrayBucket(temporaryImage.getImageData()); + RandomAccessBucket bucket = new ArrayBucket(temporaryImage.getImageData()); + insertToken.setBucket(bucket); ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType()); InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri); try { - ClientPutter clientPutter = client.insert(insertBlock, false, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS); + ClientPutter clientPutter = client.insert(insertBlock, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS); insertToken.setClientPutter(clientPutter); } catch (InsertException ie1) { throw new SoneInsertException("Could not start image insert.", ie1); @@ -245,7 +259,7 @@ public class FreenetInterface { USKCallback uskCallback = new USKCallback() { @Override - public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) { + public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) { callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo); } @@ -377,6 +391,7 @@ public class FreenetInterface { /** The client putter. */ private ClientPutter clientPutter; + private Bucket bucket; /** The final URI. */ private volatile FreenetURI resultingUri; @@ -408,6 +423,10 @@ public class FreenetInterface { eventBus.post(new ImageInsertStartedEvent(image)); } + public void setBucket(Bucket bucket) { + this.bucket = bucket; + } + // // ACTIONS // @@ -417,20 +436,23 @@ public class FreenetInterface { */ @SuppressWarnings("synthetic-access") public void cancel() { - clientPutter.cancel(null, node.clientCore.clientContext); + clientPutter.cancel(node.clientCore.clientContext); eventBus.post(new ImageInsertAbortedEvent(image)); + bucket.free(); } // // INTERFACE ClientPutCallback // - /** - * {@inheritDoc} - */ @Override - public void onMajorProgress(ObjectContainer objectContainer) { - /* ignore, we don’t care. */ + public RequestClient getRequestClient() { + return imageInserts; + } + + @Override + public void onResume(ClientContext context) throws ResumeFailedException { + /* ignore. */ } /** @@ -438,19 +460,20 @@ public class FreenetInterface { */ @Override @SuppressWarnings("synthetic-access") - public void onFailure(InsertException insertException, BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onFailure(InsertException insertException, BaseClientPutter clientPutter) { if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) { eventBus.post(new ImageInsertAbortedEvent(image)); } else { eventBus.post(new ImageInsertFailedEvent(image, insertException)); } + bucket.free(); } /** * {@inheritDoc} */ @Override - public void onFetchable(BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onFetchable(BaseClientPutter clientPutter) { /* ignore, we don’t care. */ } @@ -458,7 +481,7 @@ public class FreenetInterface { * {@inheritDoc} */ @Override - public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter) { /* ignore, we don’t care. */ } @@ -466,7 +489,7 @@ public class FreenetInterface { * {@inheritDoc} */ @Override - public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter) { resultingUri = generatedUri; } @@ -475,8 +498,9 @@ public class FreenetInterface { */ @Override @SuppressWarnings("synthetic-access") - public void onSuccess(BaseClientPutter clientPutter, ObjectContainer objectContainer) { + public void onSuccess(BaseClientPutter clientPutter) { eventBus.post(new ImageInsertFinishedEvent(image, resultingUri)); + bucket.free(); } } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java index ff0eaf7..b36c2d3 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloaderImpl.java @@ -118,7 +118,6 @@ public class SoneDownloaderImpl extends AbstractService implements SoneDownloade @Override @SuppressWarnings("synthetic-access") public void onFoundEdition(long edition, USK key, - ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) { diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 33bc240..86bf049 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -22,6 +22,7 @@ import static java.lang.System.currentTimeMillis; import static java.util.logging.Logger.getLogger; import static net.pterodactylus.sone.data.Album.NOT_EMPTY; +import java.io.Closeable; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; @@ -29,6 +30,7 @@ import java.nio.charset.Charset; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -43,7 +45,6 @@ import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; -import net.pterodactylus.sone.freenet.StringBucket; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.service.AbstractService; @@ -57,14 +58,18 @@ import net.pterodactylus.util.template.TemplateParser; import net.pterodactylus.util.template.XmlFilter; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Charsets; import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; import com.google.common.collect.Ordering; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; -import freenet.client.async.ManifestElement; import freenet.keys.FreenetURI; +import freenet.support.api.Bucket; +import freenet.support.api.ManifestElement; +import freenet.support.api.RandomAccessBucket; +import freenet.support.io.ArrayBucket; /** * A Sone inserter is responsible for inserting a Sone if it has changed. @@ -244,6 +249,7 @@ public class SoneInserter extends AbstractService { eventBus.post(new SoneInsertAbortedEvent(sone, se1)); logger.log(Level.WARNING, String.format("Could not insert Sone “%s”!", sone.getName()), se1); } finally { + insertInformation.close(); sone.setStatus(SoneStatus.idle); } @@ -280,8 +286,10 @@ public class SoneInserter extends AbstractService { * @author David ‘Bombe’ Roden */ @VisibleForTesting - class InsertInformation { + class InsertInformation implements Closeable { + /** All properties of the Sone, copied for thread safety. */ + private final Map soneProperties = new HashMap(); private final String fingerprint; private final ManifestCreator manifestCreator; @@ -341,6 +349,11 @@ public class SoneInserter extends AbstractService { return manifestEntries; } + @Override + public void close() { + manifestCreator.close(); + } + } /** @@ -349,10 +362,11 @@ public class SoneInserter extends AbstractService { * @author David ‘Bombe’ Roden */ @VisibleForTesting - static class ManifestCreator { + static class ManifestCreator implements Closeable { private final Core core; private final Map soneProperties; + private final Set buckets = new HashSet(); ManifestCreator(Core core, Map soneProperties) { this.core = core; @@ -381,19 +395,22 @@ public class SoneInserter extends AbstractService { templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition()); templateContext.set("version", SonePlugin.VERSION); StringWriter writer = new StringWriter(); - StringBucket bucket = null; try { template.render(templateContext, writer); - bucket = new StringBucket(writer.toString(), utf8Charset); + RandomAccessBucket bucket = new ArrayBucket(writer.toString().getBytes(Charsets.UTF_8)); + buckets.add(bucket); return new ManifestElement(name, bucket, contentType, bucket.size()); } catch (TemplateException te1) { logger.log(Level.SEVERE, String.format("Could not render template “%s”!", templateName), te1); return null; } finally { Closer.close(writer); - if (bucket != null) { - bucket.free(); - } + } + } + + public void close() { + for (Bucket bucket : buckets) { + bucket.free(); } } diff --git a/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java b/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java index 0ed6dd5..eb16a09 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java +++ b/src/main/java/net/pterodactylus/sone/freenet/PluginStoreConfigurationBackend.java @@ -25,7 +25,7 @@ import net.pterodactylus.util.config.AttributeNotFoundException; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.config.ExtendedConfigurationBackend; -import freenet.client.async.DatabaseDisabledException; +import freenet.client.async.PersistenceDisabledException; import freenet.pluginmanager.PluginRespirator; import freenet.pluginmanager.PluginStore; @@ -51,15 +51,12 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac * * @param pluginRespirator * The plugin respirator - * @throws DatabaseDisabledException + * @throws PersistenceDisabledException * if the plugin store is not available */ - public PluginStoreConfigurationBackend(PluginRespirator pluginRespirator) throws DatabaseDisabledException { + public PluginStoreConfigurationBackend(PluginRespirator pluginRespirator) throws PersistenceDisabledException { this.pluginRespirator = pluginRespirator; this.pluginStore = pluginRespirator.getStore(); - if (this.pluginStore == null) { - throw new DatabaseDisabledException(); - } } /** @@ -177,8 +174,8 @@ public class PluginStoreConfigurationBackend implements ExtendedConfigurationBac public void save() throws ConfigurationException { try { pluginRespirator.putStore(pluginStore); - } catch (DatabaseDisabledException dde1) { - throw new ConfigurationException("Could not store plugin store, database is disabled.", dde1); + } catch (PersistenceDisabledException pde1) { + throw new ConfigurationException("Could not store plugin store, persistence is disabled.", pde1); } } diff --git a/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java b/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java deleted file mode 100644 index c96a3cd..0000000 --- a/src/main/java/net/pterodactylus/sone/freenet/StringBucket.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Sone - StringBucket.java - Copyright © 2010–2013 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.freenet; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; - -import com.db4o.ObjectContainer; - -import freenet.support.api.Bucket; - -/** - * {@link Bucket} implementation wrapped around a {@link String}. - * - * @author David ‘Bombe’ Roden - */ -public class StringBucket implements Bucket { - - /** The string to deliver. */ - private final String string; - - /** The encoding for the data. */ - private final Charset encoding; - - /** - * Creates a new string bucket using the default encoding. - * - * @param string - * The string to wrap - */ - public StringBucket(String string) { - this(string, Charset.defaultCharset()); - } - - /** - * Creates a new string bucket, using the given encoding to create a byte - * array from the string. - * - * @param string - * The string to wrap - * @param encoding - * The encoding of the data - */ - public StringBucket(String string, Charset encoding) { - this.string = string; - this.encoding = encoding; - } - - /** - * {@inheritDoc} - */ - @Override - public Bucket createShadow() { - return new StringBucket(string); - } - - /** - * {@inheritDoc} - */ - @Override - public void free() { - /* ignore. */ - } - - /** - * {@inheritDoc} - */ - @Override - public InputStream getInputStream() { - return new ByteArrayInputStream(string.getBytes(encoding)); - } - - /** - * {@inheritDoc} - */ - @Override - public String getName() { - return getClass().getName() + "@" + hashCode(); - } - - /** - * {@inheritDoc} - */ - @Override - public OutputStream getOutputStream() { - return null; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isReadOnly() { - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public void removeFrom(ObjectContainer objectContainer) { - /* ignore. */ - } - - /** - * {@inheritDoc} - */ - @Override - public void setReadOnly() { - /* ignore, it is already read-only. */ - } - - /** - * {@inheritDoc} - */ - @Override - public long size() { - return string.getBytes(encoding).length; - } - - /** - * {@inheritDoc} - */ - @Override - public void storeTo(ObjectContainer objectContainer) { - /* ignore. */ - } - -} diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index f6fc60a..2970269 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -61,7 +61,7 @@ import com.google.inject.spi.InjectionListener; import com.google.inject.spi.TypeEncounter; import com.google.inject.spi.TypeListener; -import freenet.client.async.DatabaseDisabledException; +import freenet.client.async.PersistenceDisabledException; import freenet.l10n.BaseL10n.LANGUAGE; import freenet.l10n.PluginL10n; import freenet.node.Node; @@ -201,7 +201,7 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr try { oldConfiguration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator)); logger.log(Level.INFO, "Plugin store loaded."); - } catch (DatabaseDisabledException dde1) { + } catch (PersistenceDisabledException pde1) { logger.log(Level.SEVERE, "Could not load any configuration, using empty configuration!"); oldConfiguration = new Configuration(new MapConfigurationBackend()); } diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java index c753740..091c93f 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -1,7 +1,5 @@ package net.pterodactylus.sone.core; -import static freenet.client.InsertException.CANCELLED; -import static freenet.client.InsertException.INTERNAL_ERROR; import static freenet.keys.InsertableClientSSK.createRandom; import static freenet.node.RequestStarter.INTERACTIVE_PRIORITY_CLASS; import static freenet.node.RequestStarter.PREFETCH_PRIORITY_CLASS; @@ -41,15 +39,16 @@ import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.impl.ImageImpl; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; -import net.pterodactylus.sone.freenet.StringBucket; import freenet.client.ClientMetadata; import freenet.client.FetchException; +import freenet.client.FetchException.FetchExceptionMode; import freenet.client.FetchResult; import freenet.client.HighLevelSimpleClient; import freenet.client.InsertBlock; import freenet.client.InsertContext; import freenet.client.InsertException; +import freenet.client.InsertException.InsertExceptionMode; import freenet.client.async.ClientPutter; import freenet.client.async.USKCallback; import freenet.client.async.USKManager; @@ -63,6 +62,8 @@ import freenet.node.NodeClientCore; import freenet.node.RequestClient; import freenet.support.Base64; import freenet.support.api.Bucket; +import freenet.support.io.ArrayBucket; +import freenet.support.io.ResumeFailedException; import com.google.common.eventbus.EventBus; import org.junit.Before; @@ -87,6 +88,7 @@ public class FreenetInterfaceTest { private final ArgumentCaptor callbackCaptor = forClass(USKCallback.class); private final Image image = mock(Image.class); private InsertToken insertToken; + private final Bucket bucket = mock(Bucket.class); @Before public void setupFreenetInterface() { @@ -96,6 +98,7 @@ public class FreenetInterfaceTest { setFinalField(nodeClientCore, "uskManager", uskManager); freenetInterface = new FreenetInterface(eventBus, node); insertToken = freenetInterface.new InsertToken(image); + insertToken.setBucket(bucket); } @Before @@ -126,7 +129,7 @@ public class FreenetInterfaceTest { FreenetURI freenetUri = new FreenetURI("KSK@GPLv2.txt"); FreenetURI newFreenetUri = new FreenetURI("KSK@GPLv3.txt"); FetchResult fetchResult = createFetchResult(); - FetchException fetchException = new FetchException(FetchException.PERMANENT_REDIRECT, newFreenetUri); + FetchException fetchException = new FetchException(FetchExceptionMode.PERMANENT_REDIRECT, newFreenetUri); when(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException); when(highLevelSimpleClient.fetch(newFreenetUri)).thenReturn(fetchResult); Fetched fetched = freenetInterface.fetchUri(freenetUri); @@ -137,7 +140,7 @@ public class FreenetInterfaceTest { @Test public void fetchReturnsNullOnFetchExceptions() throws MalformedURLException, FetchException { FreenetURI freenetUri = new FreenetURI("KSK@GPLv2.txt"); - FetchException fetchException = new FetchException(FetchException.ALL_DATA_NOT_FOUND); + FetchException fetchException = new FetchException(FetchExceptionMode.ALL_DATA_NOT_FOUND); when(highLevelSimpleClient.fetch(freenetUri)).thenThrow(fetchException); Fetched fetched = freenetInterface.fetchUri(freenetUri); assertThat(fetched, nullValue()); @@ -145,7 +148,7 @@ public class FreenetInterfaceTest { private FetchResult createFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("text/plain"); - Bucket bucket = new StringBucket("Some Data."); + Bucket bucket = new ArrayBucket("Some Data.".getBytes()); return new FetchResult(clientMetadata, bucket); } @@ -161,7 +164,7 @@ public class FreenetInterfaceTest { when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext); ClientPutter clientPutter = mock(ClientPutter.class); ArgumentCaptor insertBlockCaptor = forClass(InsertBlock.class); - when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(false), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenReturn(clientPutter); + when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenReturn(clientPutter); freenetInterface.insertImage(temporaryImage, image, insertToken); assertThat(insertBlockCaptor.getValue().getData().getInputStream(), delivers(new byte[] { 1, 2, 3, 4 })); assertThat(TestUtil.getPrivateField(insertToken, "clientPutter"), is(clientPutter)); @@ -179,7 +182,7 @@ public class FreenetInterfaceTest { InsertContext insertContext = mock(InsertContext.class); when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext); ArgumentCaptor insertBlockCaptor = forClass(InsertBlock.class); - when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq(false), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenThrow(InsertException.class); + when(highLevelSimpleClient.insert(insertBlockCaptor.capture(), eq((String) null), eq(false), eq(insertContext), eq(insertToken), anyShort())).thenThrow(InsertException.class); freenetInterface.insertImage(temporaryImage, image, insertToken); } @@ -312,7 +315,7 @@ public class FreenetInterfaceTest { freenetInterface.registerUsk(uri, callback); USK key = mock(USK.class); when(key.getURI()).thenReturn(uri); - callbackCaptor.getValue().onFoundEdition(3, key, null, null, false, (short) 0, null, true, true); + callbackCaptor.getValue().onFoundEdition(3, key, null, false, (short) 0, null, true, true); verify(callback).editionFound(eq(uri), eq(3L), eq(true), eq(true)); } @@ -335,51 +338,56 @@ public class FreenetInterfaceTest { insertToken.cancel(); ArgumentCaptor imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class); verify(eventBus, times(2)).post(imageInsertAbortedEvent.capture()); + verify(bucket).free(); assertThat(imageInsertAbortedEvent.getValue().image(), is(image)); } @Test public void failureWithoutExceptionSendsFailedEvent() { - insertToken.onFailure(null, null, null); + insertToken.onFailure(null, null); ArgumentCaptor imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class); verify(eventBus).post(imageInsertFailedEvent.capture()); + verify(bucket).free(); assertThat(imageInsertFailedEvent.getValue().image(), is(image)); assertThat(imageInsertFailedEvent.getValue().cause(), nullValue()); } @Test public void failureSendsFailedEventWithException() { - InsertException insertException = new InsertException(INTERNAL_ERROR, "Internal error", null); - insertToken.onFailure(insertException, null, null); + InsertException insertException = new InsertException(InsertExceptionMode.INTERNAL_ERROR, "Internal error", null); + insertToken.onFailure(insertException, null); ArgumentCaptor imageInsertFailedEvent = forClass(ImageInsertFailedEvent.class); verify(eventBus).post(imageInsertFailedEvent.capture()); + verify(bucket).free(); assertThat(imageInsertFailedEvent.getValue().image(), is(image)); assertThat(imageInsertFailedEvent.getValue().cause(), is((Throwable) insertException)); } @Test public void failureBecauseCancelledByUserSendsAbortedEvent() { - InsertException insertException = new InsertException(CANCELLED, null); - insertToken.onFailure(insertException, null, null); + InsertException insertException = new InsertException(InsertExceptionMode.CANCELLED, null); + insertToken.onFailure(insertException, null); ArgumentCaptor imageInsertAbortedEvent = forClass(ImageInsertAbortedEvent.class); verify(eventBus).post(imageInsertAbortedEvent.capture()); + verify(bucket).free(); assertThat(imageInsertAbortedEvent.getValue().image(), is(image)); } @Test - public void ignoredMethodsDoNotThrowExceptions() { - insertToken.onMajorProgress(null); - insertToken.onFetchable(null, null); - insertToken.onGeneratedMetadata(null, null, null); + public void ignoredMethodsDoNotThrowExceptions() throws ResumeFailedException { + insertToken.onResume(null); + insertToken.onFetchable(null); + insertToken.onGeneratedMetadata(null, null); } @Test public void generatedUriIsPostedOnSuccess() { FreenetURI generatedUri = mock(FreenetURI.class); - insertToken.onGeneratedURI(generatedUri, null, null); - insertToken.onSuccess(null, null); + insertToken.onGeneratedURI(generatedUri, null); + insertToken.onSuccess(null); ArgumentCaptor imageInsertFinishedEvent = forClass(ImageInsertFinishedEvent.class); verify(eventBus).post(imageInsertFinishedEvent.capture()); + verify(bucket).free(); assertThat(imageInsertFinishedEvent.getValue().image(), is(image)); assertThat(imageInsertFinishedEvent.getValue().resultingUri(), is(generatedUri)); } diff --git a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java index e0ff3a5..552746e 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java @@ -5,7 +5,6 @@ import static com.google.common.io.ByteStreams.toByteArray; import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; import static java.lang.System.currentTimeMillis; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -25,7 +24,6 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import net.pterodactylus.sone.core.SoneInserter.InsertInformation; import net.pterodactylus.sone.core.SoneInserter.ManifestCreator; import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.core.event.SoneEvent; @@ -36,8 +34,8 @@ import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.main.SonePlugin; -import freenet.client.async.ManifestElement; import freenet.keys.FreenetURI; +import freenet.support.api.ManifestElement; import com.google.common.base.Charsets; import com.google.common.base.Optional; diff --git a/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java b/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java index b3f8c08..a5e3b2a 100644 --- a/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java +++ b/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java @@ -20,13 +20,13 @@ import java.io.InputStream; import net.pterodactylus.sone.core.FreenetInterface.Callback; import net.pterodactylus.sone.core.FreenetInterface.Fetched; import net.pterodactylus.sone.core.event.UpdateFoundEvent; -import net.pterodactylus.sone.freenet.StringBucket; import net.pterodactylus.util.version.Version; import freenet.client.ClientMetadata; import freenet.client.FetchResult; import freenet.keys.FreenetURI; import freenet.support.api.Bucket; +import freenet.support.io.ArrayBucket; import com.google.common.eventbus.EventBus; import org.junit.Before; @@ -98,9 +98,9 @@ public class UpdateCheckerTest { private FetchResult createFutureFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("application/xml"); - Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" + + Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" + "CurrentVersion/Version: 99.0.0\n" + - "CurrentVersion/ReleaseTime: 11865368297000"); + "CurrentVersion/ReleaseTime: 11865368297000").getBytes()); return new FetchResult(clientMetadata, fetched); } @@ -124,9 +124,9 @@ public class UpdateCheckerTest { private FetchResult createPastFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("application/xml"); - Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" + + Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" + "CurrentVersion/Version: 0.2\n" + - "CurrentVersion/ReleaseTime: 1289417883000"); + "CurrentVersion/ReleaseTime: 1289417883000").getBytes()); return new FetchResult(clientMetadata, fetched); } @@ -140,7 +140,7 @@ public class UpdateCheckerTest { private FetchResult createInvalidFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("text/plain"); - Bucket fetched = new StringBucket("Some other data."); + Bucket fetched = new ArrayBucket("Some other data.".getBytes()); return new FetchResult(clientMetadata, fetched); } @@ -169,7 +169,7 @@ public class UpdateCheckerTest { private FetchResult createBrokenBucketFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("text/plain"); - Bucket fetched = new StringBucket("Some other data.") { + Bucket fetched = new ArrayBucket("Some other data.".getBytes()) { @Override public InputStream getInputStream() { try { @@ -193,9 +193,9 @@ public class UpdateCheckerTest { private FetchResult createInvalidTimeFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("application/xml"); - Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" + + Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" + "CurrentVersion/Version: 0.2\n" + - "CurrentVersion/ReleaseTime: invalid"); + "CurrentVersion/ReleaseTime: invalid").getBytes()); return new FetchResult(clientMetadata, fetched); } @@ -209,8 +209,8 @@ public class UpdateCheckerTest { private FetchResult createMissingTimeFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("application/xml"); - Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" + - "CurrentVersion/Version: 0.2\n"); + Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" + + "CurrentVersion/Version: 0.2\n").getBytes()); return new FetchResult(clientMetadata, fetched); } @@ -224,9 +224,9 @@ public class UpdateCheckerTest { private FetchResult createInvalidVersionFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("application/xml"); - Bucket fetched = new StringBucket("# MapConfigurationBackendVersion=1\n" + + Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" + "CurrentVersion/Version: foo\n" + - "CurrentVersion/ReleaseTime: 1289417883000"); + "CurrentVersion/ReleaseTime: 1289417883000").getBytes()); return new FetchResult(clientMetadata, fetched); }