/Playground/CruiseControl.Core.Tests/Configuration/ExampleTests.cs

https://github.com/bertvan/CruiseControl.NET · C# · 334 lines · 298 code · 36 blank · 0 comment · 4 complexity · c75eef452988609a895855af2428391d MD5 · raw file

  1. namespace CruiseControl.Core.Tests.Configuration
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Xaml;
  8. using CruiseControl.Core;
  9. using CruiseControl.Core.Channels;
  10. using CruiseControl.Core.Structure;
  11. using CruiseControl.Core.Tasks;
  12. using CruiseControl.Core.Xaml;
  13. using NUnit.Framework;
  14. [TestFixture]
  15. public class ExampleTests
  16. {
  17. [Test]
  18. public void ReadSingleProject()
  19. {
  20. var configuration = LoadConfiguration(
  21. AssemblyHelper.RetrieveExampleFile("SingleProject"));
  22. Assert.IsNotNull(configuration);
  23. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  24. this.VerifyChildren(
  25. configuration.Children,
  26. new Project("TestProject"));
  27. }
  28. [Test]
  29. public void WriteSingleProject()
  30. {
  31. var configuration = new Project(
  32. "TestProject",
  33. new Comment("TestComment", "A Test Comment"));
  34. PerformSerialisationTest(configuration, "SingleProject");
  35. }
  36. [Test]
  37. public void ReadSimpleSourceControlExample()
  38. {
  39. var configuration = LoadConfiguration(
  40. AssemblyHelper.RetrieveExampleFile("SimpleSourceControlExample"));
  41. Assert.IsNotNull(configuration);
  42. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  43. this.VerifyChildren(
  44. configuration.Children,
  45. new Project("CCNetvNext"));
  46. }
  47. [Test]
  48. public void ReadScmProject()
  49. {
  50. var configuration = LoadConfiguration(
  51. AssemblyHelper.RetrieveExampleFile("ScmProject"));
  52. Assert.IsNotNull(configuration);
  53. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  54. this.VerifyChildren(
  55. configuration.Children,
  56. new ScmProject("OldStyle"));
  57. }
  58. [Test]
  59. public void ReadComplexSourceControlExample()
  60. {
  61. var configuration = LoadConfiguration(
  62. AssemblyHelper.RetrieveExampleFile("ComplexSourceControlExample"));
  63. Assert.IsNotNull(configuration);
  64. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  65. this.VerifyChildren(
  66. configuration.Children,
  67. new Project("CCNetvNext"));
  68. }
  69. [Test]
  70. public void ReadCommonTaskProperties()
  71. {
  72. var configuration = LoadConfiguration(
  73. AssemblyHelper.RetrieveExampleFile("CommonTaskProperties"));
  74. Assert.IsNotNull(configuration);
  75. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  76. this.VerifyChildren(
  77. configuration.Children,
  78. new Project("SampleProject"));
  79. }
  80. [Test]
  81. public void ReadSimpleQueue()
  82. {
  83. var configuration = LoadConfiguration(
  84. AssemblyHelper.RetrieveExampleFile("SimpleQueue"));
  85. Assert.IsNotNull(configuration);
  86. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  87. this.VerifyChildren(
  88. configuration.Children,
  89. new Queue("Queue1", new Project("Project1"), new Project("Project2")));
  90. var queue = configuration.Children[0] as Queue;
  91. Assert.AreEqual(3, Queue.GetPriority(queue.Children[0]));
  92. }
  93. [Test]
  94. public void WriteSimpleQueue()
  95. {
  96. var project = new Project("Project1");
  97. Queue.SetPriority(project, 3);
  98. var configuration = new Queue(
  99. "Queue1",
  100. project,
  101. new Project("Project2"));
  102. PerformSerialisationTest(configuration, "SimpleQueue");
  103. }
  104. [Test]
  105. public void ReadSimpleGate()
  106. {
  107. var configuration = LoadConfiguration(
  108. AssemblyHelper.RetrieveExampleFile("SimpleGate"));
  109. Assert.IsNotNull(configuration);
  110. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  111. this.VerifyChildren(
  112. configuration.Children,
  113. new Gate("Gate1", new Project("Project1"), new Project("Project2")));
  114. }
  115. [Test]
  116. public void WriteSimpleGate()
  117. {
  118. var configuration = new Gate(
  119. "Gate1",
  120. new Project("Project1"),
  121. new Project("Project2"));
  122. PerformSerialisationTest(configuration, "SimpleGate");
  123. }
  124. [Test]
  125. public void ReadSimplePipeline()
  126. {
  127. var configuration = LoadConfiguration(
  128. AssemblyHelper.RetrieveExampleFile("SimplePipeline"));
  129. Assert.IsNotNull(configuration);
  130. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  131. this.VerifyChildren(
  132. configuration.Children,
  133. new Pipeline("Pipeline1", new Project("Project1"), new Project("Project2")));
  134. }
  135. [Test]
  136. public void WriteSimplePipeline()
  137. {
  138. var configuration = new Pipeline(
  139. "Pipeline1",
  140. new Project("Project1"),
  141. new Project("Project2"));
  142. PerformSerialisationTest(configuration, "SimplePipeline");
  143. }
  144. [Test]
  145. public void ReadPipelineWithGates()
  146. {
  147. var configuration = LoadConfiguration(
  148. AssemblyHelper.RetrieveExampleFile("PipelineWithGates"));
  149. Assert.IsNotNull(configuration);
  150. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  151. var gate = new Gate("Gate1", new Project("Project2"), new Project("Project3"));
  152. this.VerifyChildren(
  153. configuration.Children,
  154. new Pipeline("Pipeline1", new Project("Project1"), gate, new Project("Project4")));
  155. }
  156. [Test]
  157. public void WritePipelineWithGates()
  158. {
  159. var gate = new Gate("Gate1", new Project("Project2"), new Project("Project3"));
  160. var configuration = new Pipeline(
  161. "Pipeline1",
  162. new Project("Project1"),
  163. gate,
  164. new Project("Project4"));
  165. PerformSerialisationTest(configuration, "PipelineWithGates");
  166. }
  167. [Test]
  168. public void ReadQueueOfQueues()
  169. {
  170. var configuration = LoadConfiguration(
  171. AssemblyHelper.RetrieveExampleFile("QueueOfQueues"));
  172. Assert.IsNotNull(configuration);
  173. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  174. var queue1 = new Queue("Queue1", new Project("Project1"), new Project("Project2"));
  175. var queue2 = new Queue("Queue2", new Project("Project3"), new Project("Project4"));
  176. this.VerifyChildren(
  177. configuration.Children,
  178. new Queue("Queue3", queue1, queue2));
  179. }
  180. [Test]
  181. public void WriteQueueOfQueues()
  182. {
  183. var queue1 = new Queue("Queue1", new Project("Project1"), new Project("Project2"));
  184. var queue2 = new Queue("Queue2", new Project("Project3"), new Project("Project4"));
  185. var configuration = new Queue("Queue3", queue1, queue2)
  186. {
  187. AllowedActive = 2
  188. };
  189. PerformSerialisationTest(configuration, "QueueOfQueues");
  190. }
  191. [Test]
  192. public void ReadRoundRobinOfQueues()
  193. {
  194. var configuration = LoadConfiguration(
  195. AssemblyHelper.RetrieveExampleFile("RoundRobinOfQueues"));
  196. Assert.IsNotNull(configuration);
  197. Assert.AreEqual("2.0", configuration.Version.ToString(2));
  198. var queue1 = new Queue("Queue1", new Project("Project1"), new Project("Project2"));
  199. var queue2 = new Queue("Queue2", new Project("Project3"), new Project("Project4"));
  200. this.VerifyChildren(
  201. configuration.Children,
  202. new RoundRobin("RoundRobin", queue1, queue2));
  203. }
  204. [Test]
  205. public void WriteRoundRobinOfQueues()
  206. {
  207. var queue1 = new Queue("Queue1", new Project("Project1"), new Project("Project2"));
  208. var queue2 = new Queue("Queue2", new Project("Project3"), new Project("Project4"));
  209. var configuration = new RoundRobin("RoundRobin", queue1, queue2);
  210. PerformSerialisationTest(configuration, "RoundRobinOfQueues");
  211. }
  212. [Test]
  213. public void ReadWcfChannel()
  214. {
  215. var configuration = LoadConfiguration(AssemblyHelper.RetrieveExampleFile("WcfChannel"));
  216. Assert.IsNotNull(configuration);
  217. Assert.AreEqual(1, configuration.ClientChannels.Count);
  218. Assert.IsInstanceOf<Wcf>(configuration.ClientChannels[0]);
  219. var channel = configuration.ClientChannels[0] as Wcf;
  220. Assert.AreEqual("TestChannel", channel.Name);
  221. }
  222. [Test]
  223. public void WriteWcfChannel()
  224. {
  225. var channel = new Wcf("TestChannel");
  226. PerformSerialisationTest(channel, "WcfChannel");
  227. }
  228. [Test]
  229. public void ReadMergeFiles()
  230. {
  231. var configuration = LoadConfiguration(AssemblyHelper.RetrieveExampleFile("MergeFiles"));
  232. var project = configuration.Children[0] as Project;
  233. Assert.IsNotNull(project);
  234. Assert.IsInstanceOf<MergeFiles>(project.Tasks[0]);
  235. var task = project.Tasks[0] as MergeFiles;
  236. Assert.AreEqual("TempFile.txt", task.Files[0].File);
  237. Assert.IsTrue(task.Files[0].Delete);
  238. Assert.AreEqual("PermanentFile.txt", task.Files[1].File);
  239. Assert.IsFalse(task.Files[1].Delete);
  240. }
  241. private void VerifyChildren(IList<ServerItem> actual, params ServerItem[] expected)
  242. {
  243. Assert.AreEqual(expected.Length, actual.Count);
  244. for (var loop = 0; loop < actual.Count && loop < expected.Length; loop++)
  245. {
  246. Assert.AreEqual(expected[loop].GetType().FullName, actual[loop].GetType().FullName, "Type do not match for item #" + loop);
  247. Assert.AreEqual(expected[loop].Name, actual[loop].Name, "Names do not match for item #" + loop);
  248. var container = expected[loop] as IServerItemContainer;
  249. if (container != null)
  250. {
  251. this.VerifyChildren(
  252. (actual[loop] as IServerItemContainer).Children,
  253. container.Children.ToArray());
  254. }
  255. }
  256. }
  257. private static void PerformSerialisationTest(ServerItem configuration, string example)
  258. {
  259. var server = new Server
  260. {
  261. Version = new Version(2, 0)
  262. };
  263. server.Children.Add(configuration);
  264. var xaml = XamlServices.Save(server);
  265. using (var stream = AssemblyHelper.RetrieveExampleFile(example))
  266. {
  267. using (var reader = new StreamReader(stream))
  268. {
  269. var expected = reader.ReadToEnd();
  270. Assert.AreEqual(expected, xaml);
  271. }
  272. }
  273. }
  274. private static void PerformSerialisationTest(ClientChannel configuration, string example)
  275. {
  276. var server = new Server
  277. {
  278. Version = new Version(2, 0)
  279. };
  280. server.ClientChannels.Add(configuration);
  281. var xaml = XamlServices.Save(server);
  282. using (var stream = AssemblyHelper.RetrieveExampleFile(example))
  283. {
  284. using (var reader = new StreamReader(stream))
  285. {
  286. var expected = reader.ReadToEnd();
  287. Assert.AreEqual(expected, xaml);
  288. }
  289. }
  290. }
  291. private static Server LoadConfiguration(Stream stream)
  292. {
  293. var service = new ConfigurationService();
  294. var config = service.Load(stream);
  295. return config;
  296. }
  297. }
  298. }