Skip to content
Browse files

Added SyncManager and its first test

  • Loading branch information...
1 parent 3aed5be commit 94b92ee7915d9ba34678cd17d2377920600f0d3b @frantic committed Feb 29, 2012
Showing with 101 additions and 2 deletions.
  1. +3 −0 src/RssParser.pas
  2. +14 −1 src/SyncManager.pas
  3. +84 −1 test/TestSyncManager.pas
View
3 src/RssParser.pas
@@ -13,6 +13,9 @@ ERSSParserException = class(Exception);
function ParseRSSFeed(XML: string): TRSSFeed;
end;
+var
+ DefaultRSSParser: IRSSParser = nil;
+
implementation
end.
View
15 src/SyncManager.pas
@@ -6,9 +6,22 @@ procedure Sync(URL: string);
implementation
+uses
+ HttpClient, RssParser, RssModel, RssStorage, UINotification;
+
procedure Sync(URL: string);
+var
+ Xml: string;
+ RSS: TRSSFeed;
+ i: Integer;
begin
-
+ Xml := DefaultHttpClient.GetPageByURL(URL);
+ RSS := DefaultRSSParser.ParseRSSFeed(Xml);
+ for i := 0 to RSS.Items.Count - 1 do
+ begin
+ DefaultRSSStorage.StoreItem(RSS.Items[i]);
+ DefaultUINotification.Notify(RSS.Items[i]);
+ end;
end;
end.
View
85 test/TestSyncManager.pas
@@ -3,10 +3,27 @@
interface
uses
- TestFramework, SyncManager;
+ TestFramework, SyncManager, RssModel,
+ SysUtils,
+ HttpClient, FakeHttpClient,
+ RssParser, FakeRSSParser,
+ UINotification, FakeUINotification,
+ RssStorage, FakeRSSStorage;
type
TSyncManagerTest = class(TTestCase)
+ private
+ FRSSFeed: TRSSFeed;
+ FFirstFeedItem: TRSSItem;
+ FSecondFeedItem: TRSSItem;
+
+ FFakeHttpClient: TFakeHttpClient;
+ FFakeRSSParser: TFakeRSSParser;
+ FFakeUINotification: TFakeUINotificationService;
+ FFakeRSSStorage: TFakeRSSStorage;
+ protected
+ procedure SetUp; override;
+ procedure TearDown; override;
published
procedure ItGetsAndStoresNewItemsWithNotification;
end;
@@ -17,8 +34,74 @@ implementation
{ TSyncManagerTest }
procedure TSyncManagerTest.ItGetsAndStoresNewItemsWithNotification;
+var
+ StoredItemsCount: Integer;
+ NotificationsCount: Integer;
begin
+ // HttpClient will return some dummy XML
+ FFakeHttpClient.OnGetPageByURL :=
+ function (URL: string): string
+ begin
+ CheckEquals('http://delphi.frantic.im/feed/', URL);
+ Result := 'SomeXML';
+ end;
+
+ // Parser should be called with that dummy XML
+ FFakeRSSParser.OnParseRSSFeed :=
+ function (XML: string): TRSSFeed
+ begin
+ CheckEquals('SomeXML', XML);
+ Result := FRSSFeed;
+ end;
+
+ // Storage should receive both items from RSS
+ StoredItemsCount := 0;
+ FFakeRSSStorage.OnStoreItem :=
+ procedure (Item: TRSSItem)
+ begin
+ Check((Item = FFirstFeedItem) or (Item = FSecondFeedItem),
+ 'StoreItem() was called with unknown item');
+ Inc(StoredItemsCount);
+ end;
+
+ // Sync should call Notify function for each stored item
+ FFakeUINotification.OnNotify :=
+ procedure (Item: TRSSItem)
+ begin
+ Check((Item = FFirstFeedItem) or (Item = FSecondFeedItem),
+ 'Notify() was called with unknown item');
+ Inc(NotificationsCount);
+ end;
Sync('http://delphi.frantic.im/feed/');
+ CheckEquals(2, StoredItemsCount, 'StoreItem should have been called 2 times');
+ CheckEquals(2, NotificationsCount, 'Notify should have been called 2 times');
+end;
+
+procedure TSyncManagerTest.SetUp;
+begin
+ FFakeHttpClient := TFakeHttpClient.Create;
+ FFakeRSSParser := TFakeRSSParser.Create;
+ FFakeUINotification := TFakeUINotificationService.Create;
+ FFakeRSSStorage := TFakeRSSStorage.Create;
+
+ DefaultHttpClient := FFakeHttpClient;
+ DefaultRSSStorage := FFakeRSSStorage;
+ DefaultRSSParser := FFakeRSSParser;
+ DefaultUINotification := FFakeUINotification;
+
+ // Construct test data
+ FRSSFeed := TRSSFeed.Create;
+ FFirstFeedItem := FRSSFeed.AddItem;
+ FSecondFeedItem := FRSSFeed.AddItem;
+end;
+
+procedure TSyncManagerTest.TearDown;
+begin
+ DefaultHttpClient := nil;
+ DefaultRSSStorage := nil;
+ DefaultRSSParser := nil;
+ DefaultUINotification := nil;
+ FreeAndNil(FRSSFeed);
end;
initialization

0 comments on commit 94b92ee

Please sign in to comment.
Something went wrong with that request. Please try again.