Check for disruptive versions on update
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 26 Feb 2016 23:57:08 +0000 (00:57 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 27 Feb 2016 00:34:23 +0000 (01:34 +0100)
src/main/java/net/pterodactylus/sone/core/UpdateChecker.java
src/main/java/net/pterodactylus/sone/core/event/UpdateFoundEvent.java
src/test/java/net/pterodactylus/sone/core/UpdateCheckerTest.java

index dbca3f1..2fba40b 100644 (file)
@@ -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;
+       }
+
 }
index e8cb8e5..85e4d25 100644 (file)
@@ -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;
+       }
+
 }
index fd7a02a..44819ee 100644 (file)
@@ -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> 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> 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);
+       }
+
 }