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) {
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;
protected Identity identity;
protected boolean local;
protected long lastUpdated;
+ protected Client client;
@Override
public SoneBuilder from(Identity identity) {
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");
}
}
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;
* @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;
}
//
* @return This Sone (for method chaining)
*/
public Sone setClient(Client client) {
- this.client = client;
return this;
}
package net.pterodactylus.sone.database;
+import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.freenet.wot.Identity;
SoneBuilder local();
SoneBuilder lastUpdated(long lastUpdated);
+ SoneBuilder using(Client client);
Sone build() throws IllegalStateException;
}
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);
@Override
public Sone build() throws IllegalStateException {
validate();
- return new SoneImpl(database, identity, local, lastUpdated);
+ return new SoneImpl(database, identity, local, lastUpdated, client);
}
}
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;
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();
}
}