From: David ‘Bombe’ Roden Date: Fri, 26 Feb 2016 23:57:08 +0000 (+0100) Subject: Check for disruptive versions on update X-Git-Tag: 0.9.5^2~43^2~4 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=00a353b7c10c9511fb69cb1120580e4ff95e9c0e;p=Sone.git Check for disruptive versions on update --- diff --git a/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java b/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java index dbca3f1..2fba40b 100644 --- a/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java +++ b/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java @@ -224,9 +224,22 @@ public class UpdateChecker { if (version.compareTo(currentLatestVersion) > 0) { currentLatestVersion = version; latestVersionDate = releaseTime; - logger.log(Level.INFO, String.format("Found new version: %s (%tc)", version, new Date(releaseTime))); - eventBus.post(new UpdateFoundEvent(version, releaseTime, edition)); + boolean disruptive = disruptiveVersionBetweenCurrentAndFound(properties); + logger.log(Level.INFO, String.format("Found new version: %s (%tc%s)", version, new Date(releaseTime), disruptive ? ", disruptive" : "")); + eventBus.post(new UpdateFoundEvent(version, releaseTime, edition, disruptive)); } } + private boolean disruptiveVersionBetweenCurrentAndFound(Properties properties) { + for (String key : properties.stringPropertyNames()) { + if (key.startsWith("DisruptiveVersion/")) { + Version disruptiveVersion = Version.parse(key.substring("DisruptiveVersion/".length())); + if (disruptiveVersion.compareTo(currentRunningVersion) > 0) { + return true; + } + } + } + return false; + } + } diff --git a/src/main/java/net/pterodactylus/sone/core/event/UpdateFoundEvent.java b/src/main/java/net/pterodactylus/sone/core/event/UpdateFoundEvent.java index e8cb8e5..85e4d25 100644 --- a/src/main/java/net/pterodactylus/sone/core/event/UpdateFoundEvent.java +++ b/src/main/java/net/pterodactylus/sone/core/event/UpdateFoundEvent.java @@ -26,61 +26,32 @@ import net.pterodactylus.util.version.Version; */ public class UpdateFoundEvent { - /** The version that was found. */ private final Version version; - - /** The time the update was released. */ private final long releaseTime; - - /** The latest edition of the update page. */ private final long latestEdition; + private final boolean disruptive; - /** - * Creates a new “update found” event. - * - * @param version - * The version of the update - * @param releaseTime - * The release time of the update - * @param latestEdition - * The latest edition of the update page - */ - public UpdateFoundEvent(Version version, long releaseTime, long latestEdition) { + public UpdateFoundEvent(Version version, long releaseTime, long latestEdition, boolean disruptive) { this.version = version; this.releaseTime = releaseTime; this.latestEdition = latestEdition; + this.disruptive = disruptive; } - // - // ACCESSORS - // - - /** - * Returns the version of the update. - * - * @return The version of the update - */ public Version version() { return version; } - /** - * Returns the release time of the update. - * - * @return The releae time of the update (in milliseconds since Jan 1, 1970 - * UTC) - */ public long releaseTime() { return releaseTime; } - /** - * Returns the latest edition of the update page. - * - * @return The latest edition of the update page - */ public long latestEdition() { return latestEdition; } + public boolean disruptive() { + return disruptive; + } + } diff --git a/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java b/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java index fd7a02a..44819ee 100644 --- a/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java +++ b/src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java @@ -1,7 +1,6 @@ package net.pterodactylus.sone.core; import static java.lang.Long.MAX_VALUE; -import static net.pterodactylus.sone.main.SonePlugin.VERSION; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -88,23 +87,33 @@ public class UpdateCheckerTest { setupFetchResult(createFutureFetchResult()); setupCallbackWithEdition(MAX_VALUE, true, false); verifyAFreenetUriIsFetched(); - ArgumentCaptor updateFoundEvent = forClass(UpdateFoundEvent.class); - verify(eventBus, times(1)).post(updateFoundEvent.capture()); - assertThat(updateFoundEvent.getValue().version(), is(new Version(99, 0, 0))); - assertThat(updateFoundEvent.getValue().releaseTime(), is(11865368297000L)); - assertThat(updateChecker.getLatestVersion(), is(new Version(99, 0, 0))); - assertThat(updateChecker.getLatestVersionDate(), is(11865368297000L)); - assertThat(updateChecker.hasLatestVersion(), is(true)); + verifyEventIsFired(new Version(99, 0, 0), 11865368297000L, false); + verifyThatUpdateCheckerKnowsLatestVersion(new Version(99, 0, 0), 11865368297000L); } private FetchResult createFutureFetchResult() { ClientMetadata clientMetadata = new ClientMetadata("application/xml"); Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" + "CurrentVersion/Version: 99.0.0\n" + - "CurrentVersion/ReleaseTime: 11865368297000").getBytes()); + "CurrentVersion/ReleaseTime: 11865368297000\n" + + "DisruptiveVersion/0.1.2: true").getBytes()); return new FetchResult(clientMetadata, fetched); } + private void verifyEventIsFired(Version version, long releaseTime, boolean disruptive) { + ArgumentCaptor updateFoundEvent = forClass(UpdateFoundEvent.class); + verify(eventBus, times(1)).post(updateFoundEvent.capture()); + assertThat(updateFoundEvent.getValue().version(), is(version)); + assertThat(updateFoundEvent.getValue().releaseTime(), is(releaseTime)); + assertThat(updateFoundEvent.getValue().disruptive(), is(disruptive)); + } + + private void verifyThatUpdateCheckerKnowsLatestVersion(Version version, long releaseTime) { + assertThat(updateChecker.getLatestVersion(), is(version)); + assertThat(updateChecker.getLatestVersionDate(), is(releaseTime)); + assertThat(updateChecker.hasLatestVersion(), is(true)); + } + @Test public void callbackDoesNotStartIfNoNewEditionIsFound() { setupFetchResult(createPastFetchResult()); @@ -231,4 +240,22 @@ public class UpdateCheckerTest { return new FetchResult(clientMetadata, fetched); } + @Test + public void disruptiveVersionGetsNotification() { + setupFetchResult(createDisruptiveVersionFetchResult()); + setupCallbackWithEdition(MAX_VALUE, true, false); + verifyAFreenetUriIsFetched(); + verifyEventIsFired(new Version(1, 2, 3), 1289417883000L, true); + verifyThatUpdateCheckerKnowsLatestVersion(new Version(1, 2, 3), 1289417883000L); + } + + private FetchResult createDisruptiveVersionFetchResult() { + ClientMetadata clientMetadata = new ClientMetadata("application/xml"); + Bucket fetched = new ArrayBucket(("# MapConfigurationBackendVersion=1\n" + + "CurrentVersion/Version: 1.2.3\n" + + "CurrentVersion/ReleaseTime: 1289417883000\n" + + "DisruptiveVersion/1.2.3: true").getBytes()); + return new FetchResult(clientMetadata, fetched); + } + }