PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Eas.WebCrawler.Test.Scenario/InvalidRequestTests.cs

#
C# | 241 lines | 181 code | 33 blank | 27 comment | 6 complexity | 2c93858b54079567b1664a6506762201 MD5 | raw file
  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. using System;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. namespace Eas.WebCrawler.Test.Scenario {
  7. /// <summary>
  8. /// Summary description for InvalidRequestTests
  9. /// </summary>
  10. [TestClass]
  11. public class InvalidRequestTests {
  12. public InvalidRequestTests() {
  13. //
  14. // TODO: Add constructor logic here
  15. //
  16. }
  17. #region Additional test attributes
  18. //
  19. // You can use the following additional attributes as you write your tests:
  20. //
  21. // Use ClassInitialize to run code before running the first test in the class
  22. // [ClassInitialize()]
  23. // public static void MyClassInitialize(TestContext testContext) { }
  24. //
  25. // Use ClassCleanup to run code after all tests in a class have run
  26. // [ClassCleanup()]
  27. // public static void MyClassCleanup() { }
  28. //
  29. NMock2.Mockery _Mockery = null;
  30. Eas.WebCrawler.Common.Test.TestDirectory _Directory = null;
  31. TestEventHandler _Handler = null;
  32. Dictionary<Uri, object> _ExpectedUris = null;
  33. // Use TestInitialize to run code before running each test
  34. [TestInitialize()]
  35. public void MyTestInitialize() {
  36. _ExpectedUris = new Dictionary<Uri, object>();
  37. TestGateway.Clear();
  38. LogErrorAction.errors.Clear();
  39. LogTraceAction.traces.Clear();
  40. LogWebFailureAction.failures.Clear();
  41. _Mockery = new NMock2.Mockery();
  42. _Directory = new Eas.WebCrawler.Common.Test.TestDirectory(this.GetType().Name, "Resources");
  43. _Handler = _Mockery.NewMock<TestEventHandler>();
  44. }
  45. //
  46. // Use TestCleanup to run code after each test has run
  47. [TestCleanup()]
  48. public void MyTestCleanup() {
  49. Assert.AreEqual<int>(0, _ExpectedUris.Count);
  50. _Directory.Dispose();
  51. _Directory = null;
  52. _Handler = null;
  53. _Mockery.VerifyAllExpectationsHaveBeenMet();
  54. _Mockery.Dispose();
  55. _Mockery = null;
  56. }
  57. //
  58. #endregion
  59. [TestMethod]
  60. /// <summary>Executes the AnotherFailOnBegin test.</summary>
  61. public void AnotherFailOnBegin() {
  62. string[] startingUris = new string[] { "test.html" };
  63. string[] processedUris = new string[] { "test.html" };
  64. string[] historyFiles = new string[] { };
  65. int workerCount = 1;
  66. int errorCount = 0;
  67. int webFailureCount = 1;
  68. LoadHistoryFiles(historyFiles, _Directory);
  69. string[] initialUriList = CreateUriList(startingUris, _Directory);
  70. using (Eas.WebCrawler.Interfaces.WebCrawlerEngine engine = TestHelper.CreateEngine(new TestGateway(), _Handler, _Directory, workerCount, -1, initialUriList)) {
  71. NMock2.IAction failOnBeginAction = new SetFailOnBeginAction(engine);
  72. Eas.WebCrawler.Common.Threading.EventHandlerCompletionEvent waitEvent = PrepareEngine(_Handler, engine);
  73. ExpectProcessedUris(processedUris, failOnBeginAction, _Handler, _Directory, engine);
  74. RunEngine(engine, waitEvent);
  75. CheckErrors(errorCount, webFailureCount);
  76. }
  77. }
  78. [TestMethod]
  79. /// <summary>Executes the FailBeginAction test.</summary>
  80. public void FailBeginAction() {
  81. TestGateway._ThrowFor = ThrowFor.BeginGetResponse;
  82. string[] startingUris = new string[] { "test.html" };
  83. string[] processedUris = new string[] { };
  84. string[] historyFiles = new string[] { };
  85. int workerCount = 1;
  86. int errorCount = 0;
  87. int webFailureCount = 1;
  88. PerformTest(startingUris, processedUris, historyFiles, workerCount, errorCount, webFailureCount, null);
  89. }
  90. [TestMethod]
  91. /// <summary>Executes the TestFailedStart test.</summary>
  92. public void TestFailedStart() {
  93. string[] startingUris = new string[] { "test.html" };
  94. string[] processedUris = new string[] { "test.html", "nolinks.html" };
  95. string[] historyFiles = new string[] { };
  96. int workerCount = 1;
  97. int errorCount = 1;
  98. int webFailureCount = 0;
  99. LoadHistoryFiles(historyFiles, _Directory);
  100. string[] initialUriList = CreateUriList(startingUris, _Directory);
  101. using (Eas.WebCrawler.Interfaces.WebCrawlerEngine engine = TestHelper.CreateEngine(new TestGateway(), _Handler, _Directory, workerCount, -1, initialUriList)) {
  102. NMock2.IAction startAction = new StartAction(engine);
  103. Eas.WebCrawler.Common.Threading.EventHandlerCompletionEvent waitEvent = PrepareEngine(_Handler, engine);
  104. ExpectProcessedUris(processedUris, startAction, _Handler, _Directory, engine);
  105. RunEngine(engine, waitEvent);
  106. CheckErrors(errorCount, webFailureCount);
  107. }
  108. }
  109. [ExpectedException(typeof(System.InvalidOperationException))]
  110. [TestMethod]
  111. /// <summary>Executes the TestFailedStop test.</summary>
  112. public void TestFailedStop() {
  113. string[] startingUris = new string[] { "test.html" };
  114. string[] processedUris = new string[] { "test.html" };
  115. string[] historyFiles = new string[] { };
  116. int workerCount = 1;
  117. int errorCount = 0;
  118. int webFailureCount = 0;
  119. LoadHistoryFiles(historyFiles, _Directory);
  120. string[] initialUriList = CreateUriList(startingUris, _Directory);
  121. using (Eas.WebCrawler.Interfaces.WebCrawlerEngine engine = TestHelper.CreateEngine(new TestGateway(), _Handler, _Directory, workerCount, -1, initialUriList)) {
  122. NMock2.IAction stopAction = new StopAction(engine);
  123. Eas.WebCrawler.Common.Threading.EventHandlerCompletionEvent waitEvent = PrepareEngine(_Handler, engine);
  124. ExpectProcessedUris(processedUris, stopAction, _Handler, _Directory, engine);
  125. RunEngine(engine, waitEvent);
  126. engine.Stop();
  127. CheckErrors(errorCount, webFailureCount);
  128. }
  129. }
  130. [ExpectedException(typeof(System.InvalidOperationException))]
  131. [TestMethod]
  132. /// <summary>Executes the TestFailedPause test.</summary>
  133. public void TestFailedPause() {
  134. string[] startingUris = new string[] { "test.html" };
  135. string[] processedUris = new string[] { "test.html" };
  136. string[] historyFiles = new string[] { };
  137. int workerCount = 1;
  138. LoadHistoryFiles(historyFiles, _Directory);
  139. string[] initialUriList = CreateUriList(startingUris, _Directory);
  140. using (Eas.WebCrawler.Interfaces.WebCrawlerEngine engine = TestHelper.CreateEngine(new TestGateway(), _Handler, _Directory, workerCount, -1, initialUriList)) {
  141. engine.Pause();
  142. }
  143. }
  144. void PerformTest(string[] startingUris, string[] processedUris, string[] historyFiles, int workerCount, int errorCount, int webFailureCount, NMock2.IAction processedAction) {
  145. LoadHistoryFiles(historyFiles, _Directory);
  146. string[] initialUriList = CreateUriList(startingUris, _Directory);
  147. using (Eas.WebCrawler.Interfaces.WebCrawlerEngine engine = TestHelper.CreateEngine(new TestGateway(), _Handler, _Directory, workerCount, -1, initialUriList)) {
  148. Eas.WebCrawler.Common.Threading.EventHandlerCompletionEvent waitEvent = PrepareEngine(_Handler, engine);
  149. ExpectProcessedUris(processedUris, processedAction, _Handler, _Directory, engine);
  150. RunEngine(engine, waitEvent);
  151. CheckErrors(errorCount, webFailureCount);
  152. }
  153. }
  154. private void CheckErrors(int errorCount, int webFailureCount) {
  155. Assert.AreEqual<int>(errorCount, LogErrorAction.errors.Count);
  156. Assert.AreEqual<int>(webFailureCount, LogWebFailureAction.failures.Count);
  157. }
  158. private void RunEngine(Eas.WebCrawler.Interfaces.WebCrawlerEngine engine, Eas.WebCrawler.Common.Threading.EventHandlerCompletionEvent waitEvent) {
  159. try {
  160. engine.Start();
  161. waitEvent.WaitOne();
  162. }
  163. catch (System.Exception ex) {
  164. Console.WriteLine(ex.ToString());
  165. }
  166. }
  167. private void ExpectProcessedUris(string[] processedUris, NMock2.IAction processedAction, TestEventHandler handler, Eas.WebCrawler.Common.Test.TestDirectory directory, Eas.WebCrawler.Interfaces.WebCrawlerEngine engine) {
  168. UriProcessedCheckAction action = new UriProcessedCheckAction(_ExpectedUris);
  169. foreach (string uri in CreateUriList(processedUris, _Directory)) {
  170. _ExpectedUris.Add(new Uri(uri), null);
  171. if (processedAction == null) {
  172. NMock2.Expect.Once.On(_Handler).Method("UrlProcessed").WithAnyArguments().Will(action);
  173. }
  174. else {
  175. NMock2.Expect.Once.On(_Handler).Method("UrlProcessed").WithAnyArguments().Will(action, processedAction);
  176. }
  177. }
  178. Assert.AreEqual<int>(processedUris.Length, _ExpectedUris.Count);
  179. }
  180. private Eas.WebCrawler.Common.Threading.EventHandlerCompletionEvent PrepareEngine(TestEventHandler handler, Eas.WebCrawler.Interfaces.WebCrawlerEngine engine) {
  181. Eas.WebCrawler.Common.Threading.EventHandlerCompletionEvent waitEvent = new Eas.WebCrawler.Common.Threading.EventHandlerCompletionEvent();
  182. engine.Events.Stopped += waitEvent.CreateEventHandler();
  183. engine.Events.TraceLevel = Eas.WebCrawler.Interfaces.TraceLevel.Detailed;
  184. LogErrorAction.errors.Clear();
  185. NMock2.Expect.AtLeast(0).On(handler).Method("Error").WithAnyArguments().Will(new LogErrorAction());
  186. NMock2.Expect.AtLeast(0).On(handler).Method("Trace").WithAnyArguments().Will(new LogTraceAction());
  187. NMock2.Expect.AtLeast(0).On(handler).Method("UrlFailed").WithAnyArguments().Will(new LogWebFailureAction());
  188. NMock2.Expect.Once.On(handler).Method("BeforeStart").WithAnyArguments();
  189. NMock2.Expect.Once.On(handler).Method("AfterStop").WithAnyArguments();
  190. NMock2.Expect.AtLeast(0).On(handler).Method("UrlProcessingBegan").WithAnyArguments();
  191. return waitEvent;
  192. }
  193. private void LoadHistoryFiles(string[] historyFiles, Eas.WebCrawler.Common.Test.TestDirectory directory) {
  194. if (historyFiles != null) {
  195. foreach (string file in historyFiles) {
  196. TestHelper.AddToHistoryFile(directory, file);
  197. }
  198. }
  199. }
  200. private string[] CreateUriList(string[] startingUris, Eas.WebCrawler.Common.Test.TestDirectory directory) {
  201. string[] initialUriList = new string[startingUris.Length];
  202. for (int i = 0; i < startingUris.Length; i++) {
  203. initialUriList[i] = "file://" + directory.CreateFileName(startingUris[i]);
  204. }
  205. return initialUriList;
  206. }
  207. }//
  208. }