Skip to content

Commit

Permalink
#26: Added option to inject Downloading delegate to Updatemanager to …
Browse files Browse the repository at this point in the history
…make it testable.
  • Loading branch information
jirkapok committed Jun 14, 2017
1 parent 5532073 commit 86b1dfc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Source/Terminals/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ private void SaveActiveConnections()

private void CheckForNewRelease()
{
Task<ReleaseInfo> downloadTask = UpdateManager.CheckForUpdates(false);
var updateManager = new UpdateManager();
Task<ReleaseInfo> downloadTask = updateManager.CheckForUpdates(false);
downloadTask.ContinueWith(this.CheckForNewRelease, TaskScheduler.FromCurrentSynchronizationContext());
}

Expand Down
36 changes: 26 additions & 10 deletions Source/Terminals/Updates/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,33 @@ internal class UpdateManager
/// </summary>
private const string RSS_URL = "http://terminals.codeplex.com/project/feeds/rss?ProjectRSSFeed=codeplex%3A%2F%2Frelease%2FTerminals&ProjectName=terminals";

private readonly Func<RssFeed> readReleases;

public UpdateManager() : this(ReadReleases)
{
}

internal UpdateManager(Func<RssFeed> readReleases)
{
this.readReleases = readReleases;
}

private static RssFeed ReadReleases()
{
return RssFeed.Read(RSS_URL);
}

/// <summary>
/// Check for available application updates.
/// </summary>
internal static Task<ReleaseInfo> CheckForUpdates(bool automaticallyUpdate)
internal Task<ReleaseInfo> CheckForUpdates(bool automaticallyUpdate)
{
return Task<ReleaseInfo>.Factory.StartNew((autoUpdate) => PerformCheck((bool)autoUpdate), automaticallyUpdate);
return Task<ReleaseInfo>.Factory.StartNew((autoUpdate) => this.PerformCheck((bool)autoUpdate), automaticallyUpdate);
}

private static ReleaseInfo PerformCheck(bool automaticallyUpdate)
private ReleaseInfo PerformCheck(bool automaticallyUpdate)
{
ReleaseInfo downLoaded = CheckForCodeplexRelease(Program.Info.BuildDate);
ReleaseInfo downLoaded = this.CheckForCodeplexRelease(Program.Info.BuildDate);

// todo the automatic updates point to wrong URL this feature is not working
bool autoUpdate = automaticallyUpdate; // obtain from command line arguments
Expand All @@ -35,11 +51,11 @@ private static ReleaseInfo PerformCheck(bool automaticallyUpdate)
return downLoaded;
}

internal static ReleaseInfo CheckForCodeplexRelease(DateTime buildDate)
internal ReleaseInfo CheckForCodeplexRelease(DateTime buildDate)
{
try
{
return TryCheckForCodeplexRelease(buildDate);
return this.TryCheckForCodeplexRelease(buildDate);
}
catch (Exception exception)
{
Expand All @@ -53,20 +69,20 @@ internal static ReleaseInfo CheckForCodeplexRelease(DateTime buildDate)
/// Returns not null info about obtained current release.
/// ReleaseInfo.NotAvailable in a case, new version was not checked or current version is the latest.
/// </summary>
private static ReleaseInfo TryCheckForCodeplexRelease(DateTime buildDate)
private ReleaseInfo TryCheckForCodeplexRelease(DateTime buildDate)
{
var checksFile = new UpdateChecksFile();
if (!checksFile.ShouldCheckForUpdate)
return ReleaseInfo.NotAvailable;

ReleaseInfo downLoaded = DownLoadLatestReleaseInfo(buildDate);
ReleaseInfo downLoaded = this.DownLoadLatestReleaseInfo(buildDate);
checksFile.WriteLastCheck();
return downLoaded;
}

private static ReleaseInfo DownLoadLatestReleaseInfo(DateTime buildDate)
private ReleaseInfo DownLoadLatestReleaseInfo(DateTime buildDate)
{
RssFeed feed = RssFeed.Read(RSS_URL);
RssFeed feed = this.readReleases();
if (feed != null)
{
RssItem newvestRssItem = SelectNewvestRssRssItem(feed, buildDate);
Expand Down
3 changes: 2 additions & 1 deletion Source/Tests/UpdateManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public void TodayCheckedDate_CheckForCodeplexRelease_DoesnotUpdateCheckDate()

private ReleaseInfo RunUpdateCheck()
{
return UpdateManager.CheckForCodeplexRelease(this.buildDate);
var updateManager = new UpdateManager();
return updateManager.CheckForCodeplexRelease(this.buildDate);
}

private void AssertLastUpdateCheck()
Expand Down

0 comments on commit 86b1dfc

Please sign in to comment.