Skip to content

Commit

Permalink
update check improved with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
subwiz committed Aug 1, 2016
1 parent 6e32a95 commit b9565e8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
Expand Up @@ -8,7 +8,7 @@
* @author subwiz
*/
public final class Versions {
public static final String CURRENT = "3.6.1";
public static final String CURRENT = "3.6.2";

private static final Version MIN = new VersionImpl("3.0");
private static final Version MAX = new VersionImpl(CURRENT);
Expand Down
Expand Up @@ -30,7 +30,7 @@ public class AppUpdateRunner implements Runnable {

private static final String PROP_UPDATE_CHECK_LAST = "update.check.last";
private static final String PROP_UPDATE_CHECK_ENABLED = "update.check.enabled";
private static final long TIME_GAP = 604800000l; // 1 week in millis
static final long TIME_GAP = 604800000l; // 1 week in millis

private void openUrl(String url) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
Expand All @@ -43,6 +43,31 @@ private void openUrl(String url) {
}
}
}

boolean doUpdateCheck(long lastUpdateCheck) {
if((lastUpdateCheck + TIME_GAP) < System.currentTimeMillis()) {
return true;
}
return false;
}

boolean requiresUpdate(Version latestVersion) {
if(latestVersion.isGreaterThan(new VersionImpl(Versions.CURRENT))) {
return true;
}
return false;
}

private VersionUrl data;

boolean requiresUpdate() throws IOException {
data = VersionWSUtil.getLatestVersion(UPDATE_URL);
final Version latestVersion = data.getVersion();
if(requiresUpdate(latestVersion)) {
return true;
}
return false;
}

@Override
public void run() {
Expand All @@ -55,19 +80,14 @@ public void run() {
final String strLastRun = options.getProperty(PROP_UPDATE_CHECK_LAST);
if(strLastRun != null) {
final long lastRun = Long.parseLong(strLastRun);
if((lastRun+TIME_GAP) > System.currentTimeMillis()) {
if(!doUpdateCheck(lastRun)) {
return;
}
}

// Verify version:
try {
final VersionUrl data = VersionWSUtil.getLatestVersion(UPDATE_URL);
Version latestVersion = data.getVersion();
Version currentVersion = new VersionImpl(Versions.CURRENT);

// Newer version available:
if(latestVersion.isGreaterThan(currentVersion)) {
if(requiresUpdate()) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Expand Down
@@ -0,0 +1,55 @@
package org.wiztools.restclient.ui.update;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.wiztools.appupdate.VersionImpl;

/**
*
* @author subhash
*/
public class AppUpdateRunnerTest {

public AppUpdateRunnerTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

/**
* Test of doUpdateCheck method, of class AppUpdateRunner.
*/
@Test
public void testDoUpdateCheck() {
System.out.println("doUpdateCheck");
long lastUpdateCheck = 0L;
AppUpdateRunner instance = new AppUpdateRunner();
boolean expResult = true;
boolean result = instance.doUpdateCheck(lastUpdateCheck);
assertEquals(expResult, result);
}

@Test
public void testRequiresUpdate() throws Exception {
AppUpdateRunner instance = new AppUpdateRunner();
boolean result = instance.requiresUpdate(new VersionImpl("10.0"));
assertTrue(result);
}
}

0 comments on commit b9565e8

Please sign in to comment.