Skip to content

Commit

Permalink
Fixing update check
Browse files Browse the repository at this point in the history
  • Loading branch information
micheljung committed Sep 7, 2015
1 parent 43c9898 commit 07a98ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/main/java/com/faforever/client/update/CheckForUpdateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ public class CheckForUpdateTask extends PrioritizedTask<UpdateInfo> {

private final Gson gson;
private Environment environment;
private I18n i18n;
private ComparableVersion currentVersion;

public CheckForUpdateTask(Environment environment, I18n i18n, ComparableVersion currentVersion) {
super(i18n.get("clientUpdateCheckTask.title"), Priority.LOW);
this.environment = environment;
this.i18n = i18n;
this.currentVersion = currentVersion;
gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
Expand Down Expand Up @@ -61,8 +59,13 @@ protected UpdateInfo call() throws Exception {
List<GitHubRelease> releases = gson.fromJson(to.toString(), type);
GitHubRelease gitHubRelease = releases.get(0);

boolean isNewer = new ComparableVersion(gitHubRelease.getName()).compareTo(currentVersion) > 0;
if (!isNewer) {
// Strip the "v" prefix
String strippedVersion = gitHubRelease.getName().substring(1);
ComparableVersion latestVersion = new ComparableVersion(strippedVersion);

logger.info("Current version is {}, newest version is {}", currentVersion, gitHubRelease.getName());

if (!(latestVersion.compareTo(currentVersion) > 0)) {
return null;
}

Expand All @@ -73,6 +76,9 @@ protected UpdateInfo call() throws Exception {
gitHubAsset.getBrowserDownloadUrl(),
gitHubAsset.getSize(),
gitHubRelease.getHtmlUrl());
} catch (Throwable t) {
logger.error("Error", t);
throw t;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ public void tearDown() throws Exception {
}
}

/**
* Never version is available on server.
*/
@Test
public void testGetUpdateIsNewer() throws Exception {
instance.currentVersion = new ComparableVersion("0.4.8-alpha");

mockTaskService();
startFakeGitHubApiServer();

Expand All @@ -90,7 +95,7 @@ public void testGetUpdateIsNewer() throws Exception {
verify(notificationService).addNotification(captor.capture());
PersistentNotification persistentNotification = captor.getValue();

verify(i18n).get("clientUpdateAvailable.notification", "v0.4.7-alpha", Bytes.formatSize(56079360L));
verify(i18n).get("clientUpdateAvailable.notification", "v0.4.8.1-alpha", Bytes.formatSize(56079360L));
assertThat(persistentNotification.getSeverity(), is(INFO));
}

Expand Down Expand Up @@ -137,9 +142,12 @@ private void startFakeGitHubApiServer() throws IOException {
});
}

/**
* There is no newer version on the server.
*/
@Test
public void testGetUpdateIsOlder() throws Exception {
instance.currentVersion = new ComparableVersion("v1.0.0");
public void testGetUpdateIsCurrent() throws Exception {
instance.currentVersion = new ComparableVersion("0.4.8.1-alpha");
mockTaskService();
startFakeGitHubApiServer();

Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/sample-github-releases-response.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Connection: close
"upload_url": "https://uploads.github.com/repos/micheljung/downlords-faf-client/releases/1711121/assets{?name}",
"html_url": "https://github.com/micheljung/downlords-faf-client/releases/tag/v0.4.7-alpha",
"id": 1711121,
"tag_name": "v0.4.7-alpha",
"tag_name": "v0.4.8.1-alpha",
"target_commitish": "master",
"name": "v0.4.7-alpha",
"name": "v0.4.8.1-alpha",
"draft": false,
"author": {
"login": "micheljung",
Expand Down

0 comments on commit 07a98ec

Please sign in to comment.