1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 
4 //-----------------------------------------------------------------------
5 // <copyright file="UsingTaskCollection_Tests.cs" company="Microsoft">
6 //     Copyright (c) Microsoft Corporation.  All rights reserved.
7 // </copyright>
8 // <summary>Baseline Regression Tests for v9 OM Public Interface Compatibility: UsingTaskCollection Class</summary>
9 //-----------------------------------------------------------------------
10 
11 using System;
12 using System.IO;
13 using System.Xml;
14 using System.Collections.Generic;
15 
16 using NUnit.Framework;
17 
18 using Microsoft.Build.BuildEngine;
19 using Microsoft.Build.Framework;
20 using Microsoft.Build.UnitTests;
21 
22 namespace Microsoft.Build.UnitTests.OM.OrcasCompatibility
23 {
24     /// <summary>
25     /// Fixture Class for the v9 OM Public Interface Compatibility Tests. UsingTaskCollection Class.
26     /// Also see Toolset tests in the Project test class.
27     /// </summary>
28     [TestFixture]
29     public class UsingTaskCollection_Tests
30     {
31         /// <summary>
32         /// Imports Cache issue causes xml not to be loaded
33         /// This is a test case to reproduce some quirkiness  found when running tests out of order.
34         /// </summary>
35         [Test]
ImportsUsingTask()36         public void ImportsUsingTask()
37         {
38             string importPath = String.Empty;
39             try
40             {
41                 importPath = ObjectModelHelpers.CreateFileInTempProjectDirectory("import.proj", TestData.Content3SimpleTargetsDefaultSpecified);
42                 Project p = new Project();
43                 p.Save(importPath); // required to reproduce
44                 importPath = ObjectModelHelpers.CreateFileInTempProjectDirectory("import.proj", TestData.ContentUsingTaskFile);
45                 Project p2 = new Project(); // new Engine() here fixes testcase
46                 p2.AddNewImport(importPath, "true");
47                 object o = p2.EvaluatedProperties; // evaluate the import
48                 Assertion.AssertNull(CompatibilityTestHelpers.FindUsingTaskByName("TaskName", p2.UsingTasks)); // fails to find task
49             }
50             finally
51             {
52                 CompatibilityTestHelpers.RemoveFile(importPath);
53             }
54         }
55 
56         /// <summary>
57         /// Count Test. Increment Count on Import Add in OM
58         /// </summary>
59         [Test]
Count_IncrementOnAddFile()60         public void Count_IncrementOnAddFile()
61         {
62             Project p = new Project(new Engine());
63             Assertion.AssertEquals(0, p.UsingTasks.Count);
64             p.AddNewUsingTaskFromAssemblyFile("TaskName", "AssemblyFile.dll");
65             Assertion.AssertEquals(0, p.UsingTasks.Count);
66             object o = p.EvaluatedProperties;
67             Assertion.AssertEquals(1, p.UsingTasks.Count);
68         }
69 
70         /// <summary>
71         /// Count Test. Increment Count on Import Add in OM
72         /// </summary>
73         [Test]
Count_IncrementOnAddName()74         public void Count_IncrementOnAddName()
75         {
76             Project p = new Project(new Engine());
77             Assertion.AssertEquals(0, p.UsingTasks.Count);
78             p.AddNewUsingTaskFromAssemblyName("TaskName", "AssemblyName");
79             Assertion.AssertEquals(0, p.UsingTasks.Count);
80             object o = p.EvaluatedProperties;
81             Assertion.AssertEquals(1, p.UsingTasks.Count);
82         }
83 
84         /// <summary>
85         /// Count Test. Increment Count on Import Add in XML
86         /// </summary>
87         [Test]
Count_IncrementOnAddFileXml()88         public void Count_IncrementOnAddFileXml()
89         {
90             string projectPath = String.Empty;
91             try
92             {
93                 projectPath = ObjectModelHelpers.CreateFileInTempProjectDirectory("import.proj", TestData.ContentUsingTaskFile);
94                 Project p = new Project(new Engine());
95                 Assertion.AssertEquals(0, p.UsingTasks.Count);
96                 p.Load(projectPath);
97                 Assertion.AssertEquals(1, p.UsingTasks.Count);
98             }
99             finally
100             {
101                 CompatibilityTestHelpers.RemoveFile(projectPath);
102             }
103         }
104 
105         /// <summary>
106         /// Count Test. Decrement\Reset Count to 0 on reload project xml
107         /// </summary>
108         [Test]
Count_DecrementOnRemove()109         public void Count_DecrementOnRemove()
110         {
111             Project p = new Project(new Engine());
112             p.AddNewUsingTaskFromAssemblyFile("TaskName", "AssemblyFile.dll");
113             object o = p.EvaluatedProperties;
114             Assertion.AssertEquals(1, p.UsingTasks.Count);
115             p.LoadXml(TestData.ContentSimpleTools35);
116             Assertion.AssertEquals(0, p.UsingTasks.Count);
117             o = p.EvaluatedProperties;
118             Assertion.AssertEquals(0, p.UsingTasks.Count);
119         }
120 
121         /// <summary>
122         /// IsSynchronized Test
123         /// </summary>
124         [Test]
IsSynchronized()125         public void IsSynchronized()
126         {
127             Project p = new Project(new Engine());
128             p.AddNewUsingTaskFromAssemblyFile("TaskName", "AssemblyFile.dll");
129             Assertion.AssertEquals(false, p.UsingTasks.IsSynchronized);
130         }
131 
132         /// <summary>
133         /// SyncRoot Test, ensure that SyncRoot returns and we can take a lock on it.
134         /// </summary>
135         [Test]
SyncRoot()136         public void SyncRoot()
137         {
138             Project p = new Project(new Engine());
139             p.AddNewUsingTaskFromAssemblyFile("TaskName1", "AssemblyFile1.dll");
140             p.AddNewUsingTaskFromAssemblyFile("TaskName2", "AssemblyFile2.dll");
141             UsingTask[] usingTasks = new UsingTask[p.UsingTasks.Count];
142             p.UsingTasks.CopyTo(usingTasks, 0);
143             lock (p.UsingTasks.SyncRoot)
144             {
145                 int i = 0;
146                 foreach (UsingTask usingTask in p.UsingTasks)
147                 {
148                     Assertion.AssertEquals(usingTasks[i].AssemblyFile, usingTask.AssemblyFile);
149                     i++;
150                 }
151             }
152         }
153 
154         /// <summary>
155         /// SyncRoot Test, copy into a strongly typed array and assert content against the source collection.
156         /// </summary>
157         [Test]
CopyTo_ZeroIndex()158         public void CopyTo_ZeroIndex()
159         {
160             Project p = new Project(new Engine());
161             p.AddNewUsingTaskFromAssemblyFile("TaskName1", "AssemblyFile1.dll");
162             p.AddNewUsingTaskFromAssemblyFile("TaskName2", "AssemblyFile2.dll");
163             UsingTask[] usingTasks = new UsingTask[p.UsingTasks.Count];
164             p.UsingTasks.CopyTo(usingTasks, 0);
165             int i = 0;
166             foreach (UsingTask usingTask in p.UsingTasks)
167             {
168                 Assertion.AssertEquals(usingTasks[i].AssemblyFile, usingTask.AssemblyFile);
169                 i++;
170             }
171         }
172 
173         /// <summary>
174         /// SyncRoot Test, copy into a strongly typed array and assert content against the source collection.
175         /// </summary>
176         [Test]
CopyTo_OffsetIndex()177         public void CopyTo_OffsetIndex()
178         {
179             int offSet = 3;
180             Project p = new Project(new Engine());
181             p.AddNewUsingTaskFromAssemblyFile("TaskName1", "AssemblyFile1.dll");
182             p.AddNewUsingTaskFromAssemblyFile("TaskName2", "AssemblyFile2.dll");
183             UsingTask[] taskArray = new UsingTask[p.UsingTasks.Count + offSet];
184             p.UsingTasks.CopyTo(taskArray, offSet);
185             int i = offSet - 1;
186             Assertion.AssertNull(taskArray[offSet - 1]);
187             foreach (UsingTask usingTask in p.UsingTasks)
188             {
189                 Assertion.AssertEquals(taskArray[i].AssemblyFile, usingTask.AssemblyFile);
190                 i++;
191             }
192         }
193 
194         /// <summary>
195         /// SyncRoot Test, copy into a strongly typed array with an offset where the array is too small
196         /// </summary>
197         [Test]
198         [ExpectedException(typeof(ArgumentException))]
CopyTo_OffsetIndexArrayTooSmall()199         public void CopyTo_OffsetIndexArrayTooSmall()
200         {
201             int offSet = 3;
202             Project p = new Project(new Engine());
203             p.AddNewUsingTaskFromAssemblyFile("TaskName1", "AssemblyFile1.dll");
204             p.AddNewUsingTaskFromAssemblyFile("TaskName2", "AssemblyFile2.dll");
205             UsingTask[] usingTasks = new UsingTask[p.UsingTasks.Count];
206             p.UsingTasks.CopyTo(usingTasks, offSet);
207         }
208 
209         /// <summary>
210         /// Copy to a weakly typed array, no offset. Itterate over collection
211         /// </summary>
212         [Test]
CopyTo_WeakAndGetEnumerator()213         public void CopyTo_WeakAndGetEnumerator()
214         {
215             Project p = new Project(new Engine());
216             p.AddNewUsingTaskFromAssemblyFile("TaskName1", "AssemblyFile1.dll");
217             p.AddNewUsingTaskFromAssemblyFile("TaskName2", "AssemblyFile2.dll");
218             Array taskArray = Array.CreateInstance(typeof(UsingTask), p.UsingTasks.Count);
219             p.UsingTasks.CopyTo(taskArray, 0);
220             Assertion.AssertEquals(p.UsingTasks.Count, taskArray.Length);
221             int i = 0;
222             foreach (UsingTask usingTask in p.UsingTasks)
223             {
224                 Assertion.AssertEquals(((UsingTask)taskArray.GetValue(i)), usingTask.AssemblyFile);
225                 i++;
226             }
227         }
228     }
229 }
230