UpdateCheck implements Updater { @Override public OngoingInstallation updateToLatestVersion() { assertCheckHasRun(); if (latestVersionIsNewerThanInstalledVersion()) { return latestUpdate.install(); } return new NullInstallation(); } UpdateCheck(UpdateConnection updateConnection); @Override UpdateAvailability hasUpdate(); @Override Version getInstalledVersion(); @Override Version getLatestVersion(); @Override OngoingInstallation updateToLatestVersion(); @Override synchronized void runCheck();  }
@Test(expected = IllegalStateException.class) public void expectsCheckToBeRunBeforeInstallingUpdates() throws Exception { UpdateCheck updateCheck = createUpdateCheck(); updateCheck.updateToLatestVersion(); }
@Test public void handsOutInstallationFromConnectionWhenNewerIsAvailable() throws Exception { InstallableUpdate update = mock(InstallableUpdate.class); when(update.isUpdateFrom(currentVersion)).thenReturn(UpdateAvailability.Available); when(connection.getLatestAvailableUpdate()).thenReturn(update); OngoingInstallation installation = mock(OngoingInstallation.class); when(update.install()).thenReturn(installation); UpdateCheck check = createExecutedUpdateCheck(); OngoingInstallation actualInstallation = check.updateToLatestVersion(); assertThat(actualInstallation, is(installation)); }
@Test public void returnsNullVersionOtherwise() throws Exception { when(connection.getLatestAvailableUpdate()).thenReturn(update); UpdateCheck check = createExecutedUpdateCheck(); OngoingInstallation actualInstallation = check.updateToLatestVersion(); assertThat(actualInstallation, is(instanceOf(NullInstallation.class))); }