From 3752ebf20fcb0c71287f2b7fed358b14c7649d7f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 7 Dec 2014 02:06:08 +0100 Subject: [PATCH] Set client in Sone builder. --- .../net/pterodactylus/sone/core/SoneParser.java | 8 ++++--- .../sone/data/impl/AbstractSoneBuilder.java | 9 +++++++ .../net/pterodactylus/sone/data/impl/SoneImpl.java | 6 ++--- .../pterodactylus/sone/database/SoneBuilder.java | 2 ++ .../sone/database/memory/MemoryDatabase.java | 4 ++-- .../sone/database/memory/MemorySoneBuilder.java | 2 +- .../sone/data/impl/AbstractSoneBuilderTest.java | 28 ++++++++++++++++------ 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 6983d25..0323796 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -106,18 +106,20 @@ public class SoneParser { logger.log(Level.WARNING, String.format("Downloaded Sone %s with invalid time: %s", originalSone, soneTime)); return null; } - Sone sone = soneBuilder.build(); SimpleXML clientXml = soneXml.getNode("client"); if (clientXml != null) { String clientName = clientXml.getValue("name", null); String clientVersion = clientXml.getValue("version", null); if ((clientName == null) || (clientVersion == null)) { - logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone)); + logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", originalSone)); return null; } - sone.setClient(new Client(clientName, clientVersion)); + soneBuilder.using(new Client(clientName, clientVersion)); + } else { + soneBuilder.using(new Client("Unknown Client", "0.0")); } + Sone sone = soneBuilder.build(); SimpleXML profileXml = soneXml.getNode("profile"); if (profileXml == null) { diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java index b7dda3f..1ce38e4 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java @@ -2,6 +2,7 @@ package net.pterodactylus.sone.data.impl; import static com.google.common.base.Preconditions.checkState; +import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.database.SoneBuilder; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -16,6 +17,7 @@ public abstract class AbstractSoneBuilder implements SoneBuilder { protected Identity identity; protected boolean local; protected long lastUpdated; + protected Client client; @Override public SoneBuilder from(Identity identity) { @@ -35,11 +37,18 @@ public abstract class AbstractSoneBuilder implements SoneBuilder { return this; } + @Override + public SoneBuilder using(Client client) { + this.client = client; + return this; + } + protected void validate() throws IllegalStateException { checkState(identity != null, "identity must not be null"); checkState(!local || (identity instanceof OwnIdentity), "can not create local Sone from remote identity"); checkState(lastUpdated > 0, "last update time must be set"); + checkState(client != null, "client must not be null"); } } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java index eccce4b..6da31fc 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java @@ -87,7 +87,7 @@ public class SoneImpl implements LocalSone { private volatile Profile profile = new Profile(this); /** The client used by the Sone. */ - private volatile Client client; + private final Client client; /** Whether this Sone is known. */ private volatile boolean known; @@ -119,12 +119,13 @@ public class SoneImpl implements LocalSone { * @param local * {@code true} if the Sone is a local Sone, {@code false} otherwise */ - public SoneImpl(Database database, Identity identity, boolean local, long time) { + public SoneImpl(Database database, Identity identity, boolean local, long time, Client client) { this.database = database; this.id = identity.getId(); this.identity = identity; this.local = local; this.time = time; + this.client = client; } // @@ -302,7 +303,6 @@ public class SoneImpl implements LocalSone { * @return This Sone (for method chaining) */ public Sone setClient(Client client) { - this.client = client; return this; } diff --git a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java index 5ad9bcc..8fc895b 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java @@ -1,5 +1,6 @@ package net.pterodactylus.sone.database; +import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.wot.Identity; @@ -14,6 +15,7 @@ public interface SoneBuilder { SoneBuilder local(); SoneBuilder lastUpdated(long lastUpdated); + SoneBuilder using(Client client); Sone build() throws IllegalStateException; diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index d5d5396..2d80ca5 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -182,12 +182,12 @@ public class MemoryDatabase extends AbstractService implements Database { } private LocalSone loadLocalSone(OwnIdentity ownIdentity) { - LocalSone localSone = (LocalSone) newSoneBuilder().local().from(ownIdentity).build(); + LocalSone localSone = (LocalSone) newSoneBuilder().local().from(ownIdentity).using( + new Client("Sone", SonePlugin.VERSION.toString())).build(); localSone.setLatestEdition( Optional.fromNullable( Longs.tryParse(ownIdentity.getProperty(LATEST_EDITION_PROPERTY))) .or(0L)); - localSone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); localSone.setKnown(true); loadSone(localSone); diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java index 7c135ce..565c7c4 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java @@ -21,7 +21,7 @@ public class MemorySoneBuilder extends AbstractSoneBuilder { @Override public Sone build() throws IllegalStateException { validate(); - return new SoneImpl(database, identity, local, lastUpdated); + return new SoneImpl(database, identity, local, lastUpdated, client); } } diff --git a/src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java b/src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java index 9b64b6a..8c9e1a9 100644 --- a/src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java +++ b/src/test/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilderTest.java @@ -2,6 +2,7 @@ package net.pterodactylus.sone.data.impl; import static org.mockito.Mockito.mock; +import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -22,45 +23,58 @@ public class AbstractSoneBuilderTest { return null; } }; + private final Client client = new Client("Test Client", "1.0"); @Test public void localSoneIsValidated() { Identity ownIdentity = mock(OwnIdentity.class); - soneBuilder.local().from(ownIdentity).lastUpdated(1).build(); + soneBuilder.local().from(ownIdentity).lastUpdated(1).using(client).build(); } @Test(expected = IllegalStateException.class) public void localSoneIsNotValidatedIfIdentityIsNotAnOwnIdentity() { Identity identity = mock(Identity.class); - soneBuilder.local().from(identity).lastUpdated(1).build(); + soneBuilder.local().from(identity).lastUpdated(1).using(client).build(); } @Test(expected = IllegalStateException.class) public void localSoneIsNotValidatedIfIdentityIsNull() { - soneBuilder.local().lastUpdated(1).build(); + soneBuilder.local().lastUpdated(1).using(client).build(); } @Test public void remoteSoneIsValidated() { Identity identity = mock(Identity.class); - soneBuilder.from(identity).lastUpdated(1).build(); + soneBuilder.from(identity).lastUpdated(1).using(client).build(); } @Test(expected = IllegalStateException.class) public void remoteSoneIsNotValidatedIfIdentityIsNull() { - soneBuilder.lastUpdated(1).build(); + soneBuilder.lastUpdated(1).using(client).build(); } @Test(expected = IllegalStateException.class) public void localSoneIsNotValidatedWithoutUpdateTime() { Identity identity = mock(OwnIdentity.class); - soneBuilder.from(identity).local().build(); + soneBuilder.from(identity).local().using(client).build(); } @Test(expected = IllegalStateException.class) public void remoteSoneIsNotValidatedWithoutUpdateTime() { Identity identity = mock(Identity.class); - soneBuilder.from(identity).build(); + soneBuilder.from(identity).using(client).build(); + } + + @Test(expected = IllegalStateException.class) + public void localSoneIsNotValidatedWithoutClient() { + Identity identity = mock(OwnIdentity.class); + soneBuilder.from(identity).local().lastUpdated(1L).build(); + } + + @Test(expected = IllegalStateException.class) + public void remoteSoneIsNotValidatedWithoutClient() { + Identity identity = mock(Identity.class); + soneBuilder.from(identity).lastUpdated(1L).build(); } } -- 2.7.4