1
/* Copyright (c) 2006 Google Inc.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
16
* Oct 13 2008 Joe Feser joseph.feser@gmail.com
27
using System.Collections;
28
using System.Configuration;
31
using NUnit.Framework;
32
using Google.GData.Client;
33
using Google.GData.Extensions;
34
using Google.GData.Calendar;
39
namespace Google.GData.Client.UnitTests
43
// dummy class to make porting autogenerated tests easier
44
public class TestContext
49
public class CoreTestSuite : BaseTestClass
51
//////////////////////////////////////////////////////////////////////
52
/// <summary>default empty constructor</summary>
53
//////////////////////////////////////////////////////////////////////
54
public CoreTestSuite()
58
//////////////////////////////////////////////////////////////////////
59
/// <summary>[Test] public QueryObjectTest()</summary>
60
//////////////////////////////////////////////////////////////////////
61
[Test] public void QueryObjectTest()
63
Tracing.TraceInfo("Entering QueryObject Test");
65
FeedQuery query = new FeedQuery();
66
query.Uri = new Uri(this.defaultHost);
68
AtomCategory aCat = new AtomCategory("Test", new AtomUri("urn:test.com"));
69
QueryCategory qCat = new QueryCategory(aCat);
70
query.Categories.Add(qCat);
72
aCat = new AtomCategory("TestNotAndOr", new AtomUri("urn:test.com"));
73
qCat = new QueryCategory(aCat);
74
qCat.Operator = QueryCategoryOperator.OR;
77
query.Categories.Add(qCat);
80
aCat = new AtomCategory("ANDTHISONE", new AtomUri(""));
81
qCat = new QueryCategory(aCat);
82
query.Categories.Add(qCat);
84
aCat = new AtomCategory("AnotherOrWithoutCategory");
85
qCat = new QueryCategory(aCat);
86
qCat.Operator = QueryCategoryOperator.OR;
88
query.Categories.Add(qCat);
90
query.Query = "Hospital";
91
query.NumberToRetrieve = 20;
92
Tracing.TraceInfo("query: " + query.Uri);
95
Tracing.TraceInfo("Uri: query= " + uri.Query);
97
Tracing.TraceInfo("Parsed Query URI: " + query.Uri);
98
Assert.IsTrue(uri.AbsolutePath.Equals(query.Uri.AbsolutePath), "both query URIs should be identical, uri: " + uri.AbsolutePath + " compared to query: " + query.Uri.AbsolutePath);
100
query.CategoryQueriesAsParameter = true;
103
Tracing.TraceInfo("Uri: query= " + uri.Query);
105
Tracing.TraceInfo("Parsed Query URI: " + query.Uri.AbsoluteUri);
106
Assert.IsTrue(uri.AbsolutePath.Equals(query.Uri.AbsolutePath), "both query URIs should be identical, uri: " + uri.AbsolutePath + " compared to query: " + query.Uri.AbsolutePath);
109
/////////////////////////////////////////////////////////////////////////////
111
//////////////////////////////////////////////////////////////////////
112
/// <summary>[Test] creates a feed object from scratch</summary>
113
//////////////////////////////////////////////////////////////////////
114
[Test]public void CreateFeed()
116
Tracing.TraceInfo("Entering Create Feed Test");
117
AtomFeed feed = new AtomFeed(new Uri("http://dummy"), null);
121
for (int i = 1; i <= this.iIterations; i++)
123
entry = ObjectModelHelper.CreateAtomEntry(i);
124
feed.Entries.Add(entry);
127
Tracing.TraceInfo("now persisting feed");
129
ObjectModelHelper.DumpAtomObject(feed, CreateDumpFileName("CreateFeed"));
131
Tracing.TraceInfo("now loadiing feed from disk");
133
Service service = new Service();
134
service.RequestFactory = this.factory;
136
FeedQuery query = new FeedQuery();
137
query.Uri = new Uri(CreateUriLogFileName("CreateFeed"));
139
feed = service.Query(query);
141
Assert.IsTrue(feed.Entries != null, "Feed.Entries should not be null");
142
Assert.AreEqual(this.iIterations, feed.Entries.Count, "Feed.Entries should have 50 elements");
143
if (feed.Entries != null)
145
for (int i = 1; i <= this.iIterations; i++)
147
entry = ObjectModelHelper.CreateAtomEntry(i);
148
AtomEntry theOtherEntry = feed.Entries[i-1];
149
Assert.IsTrue(ObjectModelHelper.IsEntryIdentical(entry, theOtherEntry));
153
/////////////////////////////////////////////////////////////////////////////
155
//////////////////////////////////////////////////////////////////////
156
/// <summary>[Test] creates a new entry, saves and loads it back</summary>
157
//////////////////////////////////////////////////////////////////////
158
[Test] public void CreateEntrySaveAndLoad()
161
Tracing.TraceMsg("Entering Create/Save/Load test");
163
AtomEntry entry = ObjectModelHelper.CreateAtomEntry(1);
165
ObjectModelHelper.DumpAtomObject(entry, CreateDumpFileName("CreateEntrySaveAndLoad"));
168
// let's try loading this...
169
Service service = new Service();
170
service.RequestFactory = this.factory;
172
FeedQuery query = new FeedQuery();
173
query.Uri = new Uri(CreateUriLogFileName("CreateEntrySaveAndLoad"));
174
AtomFeed feed = service.Query(query);
175
Assert.IsTrue(feed.Entries != null, "Feed.Entries should not be null");
176
Assert.AreEqual(1, feed.Entries.Count, "Feed.Entries should have ONE element");
177
// that feed should have ONE entry
178
if (feed.Entries != null)
180
AtomEntry theOtherEntry = feed.Entries[0];
181
Assert.IsTrue(ObjectModelHelper.IsEntryIdentical(entry, theOtherEntry));
185
/////////////////////////////////////////////////////////////////////////////
187
//////////////////////////////////////////////////////////////////////
188
/// <summary>[Test] Creates a feedquery and checks if the SSL translation works</summary>
189
//////////////////////////////////////////////////////////////////////
190
[Test] public void QuerySSLTest()
193
Tracing.TraceMsg("Entering QuerySSLTest test");
195
FeedQuery query = new FeedQuery();
196
query.Uri = new Uri("http://www.google.com/");
197
Assert.AreEqual("http://www.google.com/", query.Uri.ToString(), "both uris should be http now");
199
Assert.AreEqual("https://www.google.com/", query.Uri.ToString(), "both uris should be https now");
200
query.UseSSL = false;
201
Assert.AreEqual("http://www.google.com/", query.Uri.ToString(), "both uris should be http now");
203
// now construct the other way round
205
query = new FeedQuery();
206
query.Uri = new Uri("https://www.google.com/");
208
Assert.IsTrue(query.UseSSL, "Use SSL should be true due to detection of the https string");
210
Assert.AreEqual("https://www.google.com/", query.Uri.ToString(), "both uris should be https now");
211
query.UseSSL = false;
212
Assert.AreEqual("http://www.google.com/", query.Uri.ToString(), "both uris should be http now");
214
Assert.AreEqual("https://www.google.com/", query.Uri.ToString(), "both uris should be https now");
216
/////////////////////////////////////////////////////////////////////////////
221
//////////////////////////////////////////////////////////////////////
222
/// <summary>[Test] creates a new entry, saves and loads it back
223
/// uses HTML content to test the persistence/encoding code
225
//////////////////////////////////////////////////////////////////////
226
[Test] public void CreateHTMLEntrySaveAndLoad()
229
Tracing.TraceMsg("Entering CreateHTMLEntrySaveAndLoad");
231
AtomEntry entry = ObjectModelHelper.CreateAtomEntry(1);
232
entry.Content.Type = "html";
233
entry.Content.Content = HttpUtility.HtmlDecode("<b>this is a <test></b>");
235
Tracing.TraceMsg("Content: " + entry.Content.Content);
237
ObjectModelHelper.DumpAtomObject(entry, CreateDumpFileName("CreateHTMLEntrySaveAndLoad"));
240
// let's try loading this...
241
Service service = new Service();
242
service.RequestFactory = this.factory;
244
FeedQuery query = new FeedQuery();
245
query.Uri = new Uri(CreateUriLogFileName("CreateHTMLEntrySaveAndLoad"));
246
AtomFeed feed = service.Query(query);
247
Assert.IsTrue(feed.Entries != null, "Feed.Entries should not be null");
248
Assert.AreEqual(1, feed.Entries.Count, "Feed.Entries should have ONE element");
249
// that feed should have ONE entry
250
if (feed.Entries != null)
252
AtomEntry theOtherEntry = feed.Entries[0];
253
Tracing.TraceMsg("Loaded Content: " + theOtherEntry.Content.Content);
254
Assert.IsTrue(ObjectModelHelper.IsEntryIdentical(entry, theOtherEntry));
258
/////////////////////////////////////////////////////////////////////////////
260
//////////////////////////////////////////////////////////////////////
261
/// <summary>[Test] creates a new entry, saves and loads it back
262
/// uses XHTML content to test the persistence/encoding code
264
//////////////////////////////////////////////////////////////////////
265
[Test] public void CreateXHTMLEntrySaveAndLoad()
268
Tracing.TraceMsg("Entering CreateXHTMLEntrySaveAndLoad");
270
AtomEntry entry = ObjectModelHelper.CreateAtomEntry(1);
271
entry.Content.Type = "xhtml";
272
entry.Content.Content = HttpUtility.HtmlDecode("<div xmlns=\"http://www.w3.org/2005/Atom\"><b>this is a test</b></div>");
274
Tracing.TraceMsg("Content: " + entry.Content.Content);
276
ObjectModelHelper.DumpAtomObject(entry, CreateDumpFileName("CreateXHTMLEntrySaveAndLoad"));
278
Tracing.TraceMsg("saved in... CreateXHTMLEntrySaveAndLoad");
281
// let's try loading this...
282
Service service = new Service();
283
service.RequestFactory = this.factory;
285
FeedQuery query = new FeedQuery();
286
query.Uri = new Uri(CreateUriLogFileName("CreateXHTMLEntrySaveAndLoad"));
287
AtomFeed feed = service.Query(query);
289
Tracing.TraceMsg("loaded in... CreateXHTMLEntrySaveAndLoad");
291
Assert.IsTrue(feed.Entries != null, "Feed.Entries should not be null");
292
Assert.AreEqual(1, feed.Entries.Count, "Feed.Entries should have ONE element");
295
// that feed should have ONE entry
296
if (feed.Entries != null)
298
Tracing.TraceMsg("checking entries... CreateXHTMLEntrySaveAndLoad");
299
AtomEntry theOtherEntry = feed.Entries[0];
301
Assert.IsTrue(theOtherEntry.Content != null, "the entry should have a content element");
302
Assert.IsTrue(theOtherEntry.Content.Type.Equals("xhtml"), "the entry should have a content element of type xhtml");
303
Assert.IsTrue(theOtherEntry.Content.Content != null, "the entry should have a content element that is not empty");
305
Tracing.TraceMsg("Loaded Content: " + theOtherEntry.Content.Content);
306
Assert.IsTrue(ObjectModelHelper.IsEntryIdentical(entry, theOtherEntry));
307
Tracing.TraceMsg("done comparing entries... CreateXHTMLEntrySaveAndLoad");
311
/////////////////////////////////////////////////////////////////////////////
314
////////////////////////////////////////////////////////////////////
315
/// <summary>[Test] creates a new feed, saves and loads it back</summary>
316
//////////////////////////////////////////////////////////////////////
317
[Test] public void CreateFeedObjectSaveAndLoad()
320
Tracing.TraceMsg("Entering CreateFeedObjectSaveAndLoad test");
322
Service service = new Service();
323
AtomFeed feed=new AtomFeed(new Uri("http://www.atomfeed.com/"),service);
324
feed.Self="http://www.atomfeed.com/self";
325
feed.Feed="http://www.atomfeed.com/feed";
326
feed.NextChunk="http://www.atomfeed.com/next";
327
feed.PrevChunk="http://www.atomfeed.com/prev";
328
feed.Post = "http://www.atomfeed.com/post";
330
ObjectModelHelper.DumpAtomObject(feed, CreateDumpFileName("CreateFeedSaveAndLoad"));
333
// let's try loading this...
334
service.RequestFactory = this.factory;
336
FeedQuery query = new FeedQuery();
337
query.Uri = new Uri(CreateUriLogFileName("CreateFeedSaveAndLoad"));
339
feed = service.Query(query);
342
Assert.AreEqual("http://www.atomfeed.com/self", feed.Self, "Feed.Self is not correct");
343
Assert.AreEqual("http://www.atomfeed.com/feed", feed.Feed, "Feed.Feed is not correct");
344
Assert.AreEqual("http://www.atomfeed.com/next", feed.NextChunk, "Feed.Next is not correct");
345
Assert.AreEqual("http://www.atomfeed.com/prev", feed.PrevChunk, "Feed.Prev is not correct");
346
Assert.AreEqual("http://www.atomfeed.com/post", feed.Post, "Feed.Post is not correct");
349
/////////////////////////////////////////////////////////////////////////////
354
////////////////////////////////////////////////////////////////////
355
/// <summary>[Test] creates a new entry, saves and loads it back</summary>
356
//////////////////////////////////////////////////////////////////////
357
[Test] public void CreateEmptyEntrySaveAndLoad()
360
Tracing.TraceMsg("Entering Create/Save/Load test");
362
AtomEntry entry = ObjectModelHelper.CreateAtomEntry(1);
364
entry.Content.Type = "text";
365
entry.Content.Content = "";
367
ObjectModelHelper.DumpAtomObject(entry, CreateDumpFileName("CreateEmptyEntrySaveAndLoad"));
370
// let's try loading this...
371
Service service = new Service();
372
service.RequestFactory = this.factory;
374
FeedQuery query = new FeedQuery();
375
query.Uri = new Uri(CreateUriLogFileName("CreateEmptyEntrySaveAndLoad"));
376
AtomFeed feed = service.Query(query);
377
Assert.IsTrue(feed.Entries != null, "Feed.Entries should not be null");
378
Assert.AreEqual(1, feed.Entries.Count, "Feed.Entries should have ONE element");
379
// that feed should have ONE entry
380
if (feed.Entries != null)
382
AtomEntry theOtherEntry = feed.Entries[0];
383
Assert.IsTrue(ObjectModelHelper.IsEntryIdentical(entry, theOtherEntry), "Entries should be identical");
387
/////////////////////////////////////////////////////////////////////////////
389
//////////////////////////////////////////////////////////////////////
390
/// <summary>[Test] queries the remote feed, saves it, loads it and compares it</summary>
391
/// <param name="uriToQuery">the host to access, including query parameters</param>
392
//////////////////////////////////////////////////////////////////////
393
internal void RemoteHostQueryAndCompare(Uri uriToQuery)
396
Tracing.TraceMsg("Entering RemoteHostQueryAndCompare");
399
FeedQuery query = new FeedQuery();
400
query.Uri = uriToQuery;
402
Service service = new Service();
403
service.RequestFactory = this.factory;
405
AtomFeed f = service.Query(query);
407
ObjectModelHelper.DumpAtomObject(f, CreateDumpFileName("QueryRemoteHost"));
409
iCount = f.Entries.Count;
411
// let's try loading this...
412
Service service2 = new Service();
413
FeedQuery query2 = new FeedQuery();
414
query2.Uri = new Uri(CreateUriLogFileName("QueryRemoteHost"));
416
AtomFeed feed = service2.Query(query2);
417
Assert.AreEqual(iCount, feed.Entries.Count, "loaded feed has different number of entries");
420
Tracing.TraceInfo("Comparing feed objects as source");
421
Assert.IsTrue(ObjectModelHelper.IsSourceIdentical(f, feed), "Feeds are not identical");
422
if (feed.Entries != null)
424
AtomEntry theOtherEntry;
426
Tracing.TraceInfo("Comparing Entries");
427
for (int i = 0; i < feed.Entries.Count; i++)
429
theOtherEntry = feed.Entries[i];
430
Assert.IsTrue(ObjectModelHelper.IsEntryIdentical(f.Entries[i], theOtherEntry), "Entries are not identical");
435
Tracing.TraceInfo("Leaving RemoteHostQueryAndCompare for : " + uriToQuery.AbsoluteUri);
438
/////////////////////////////////////////////////////////////////////////////
442
//////////////////////////////////////////////////////////////////////
443
/// <summary>[Test] walks over the list of remotehosts out of the
444
/// unitTestExternalHosts
445
/// add key="Host1" value="http://www.franklinmint.fm/2005/09/26/test_entry2.xml"
446
/// section in the config file and queries and compares the object model
448
//////////////////////////////////////////////////////////////////////
449
[Test] public void RemoteHostQueryTest()
451
Tracing.TraceInfo("Entering RemoteHostQueryTest()");
452
if (this.externalHosts != null)
454
for (int i=0; i< this.iIterations; i++)
456
Tracing.TraceInfo("Having a dictionary RemoteHostQueryTest()");
457
foreach (DictionaryEntry de in this.externalHosts )
459
Tracing.TraceInfo("Using DictionaryEntry for external Query: " + de.Value);
460
Uri uriToQuery = new Uri((string) de.Value);
461
RemoteHostQueryAndCompare(uriToQuery);
467
/////////////////////////////////////////////////////////////////////////////
470
//////////////////////////////////////////////////////////////////////
471
/// <summary>creates a number or rows and delets them again</summary>
472
//////////////////////////////////////////////////////////////////////
473
[Test] public void DefaultHostExtensionTest()
475
Tracing.TraceMsg("Entering DefaultHostExtensionTest");
477
if (this.strRemoteHost != null)
480
FeedQuery query = new FeedQuery();
481
Service service = new Service();
483
service.NewAtomEntry += new FeedParserEventHandler(this.OnParsedNewEntry);
484
service.NewExtensionElement += new ExtensionElementEventHandler(this.OnNewExtensionElement);
487
service.RequestFactory = (IGDataRequestFactory) new GDataLoggingRequestFactory(this.ServiceName, this.ApplicationName);
489
query.Uri = new Uri(this.strRemoteHost);
491
AtomFeed returnFeed = service.Query(query);
493
ObjectModelHelper.DumpAtomObject(returnFeed,CreateDumpFileName("ExtensionFeed"));
496
/////////////////////////////////////////////////////////////////////////////
498
//////////////////////////////////////////////////////////////////////
499
/// <summary>tests the tokenizer collection</summary>
500
//////////////////////////////////////////////////////////////////////
501
[Test] public void TestTokenCollection()
503
Tracing.TraceMsg("Entering TestTokenCollection");
504
String toTest = "Test=Test?other=whatever\nTest2=Line2?other=whatishere";
506
TokenCollection tokens = new TokenCollection(toTest, new char[] {'\n', '='});
507
TokenCollection tokenSmart = new TokenCollection(toTest, '=', true, 2);
510
foreach (string token in tokens)
512
// tokens should have 5 tokens, as the = signs split into 5
516
Assert.IsTrue(token.Equals("Test"), "The first token should be Test, but it is: " + token);
520
Assert.IsTrue(token.Equals("Test2"), "The fourth token should be Test2 but it is: " + token);
525
foreach (string token in tokenSmart)
527
// tokens should have 5 tokens, as the = signs split into 5
531
Assert.IsTrue(token.Equals("Test"), "The first smart token should be Test, but it is: " + token);
535
Assert.IsTrue(token.Equals("Line2?other=whatishere"), "The fourth smart token should be whatishere, but it is: " + token);
540
//////////////////////////////////////////////////////////////////////
541
/// <summary>checks if the feedquery object is stable</summary>
542
//////////////////////////////////////////////////////////////////////
544
public void TestFeedQuery()
546
Tracing.TraceMsg("Entering DefaultHostExtensionTest");
550
FeedQuery query = new FeedQuery();
551
query.NumberToRetrieve = 20;
555
catch (UriFormatException)
561
Assert.Fail("That should not have crashed");
565
/////////////////////////////////////////////////////////////////////////////
568
} /// end of CoreTestSuite