2007-01-12 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / UsingTaskCollectionTest.cs
1 //
2 // UsingTaskCollectionTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2006 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 using System;
29 using System.Collections;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
34
35 namespace MonoTests.Microsoft.Build.BuildEngine {
36         [TestFixture]
37         public class UsingTaskCollectionTest {
38                 
39                 Engine          engine;
40                 Project         project;
41                 
42                 [Test]
43                 public void TestAssemblyFile1 ()
44                 {
45                         string documentString = @"
46                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
47                                         <UsingTask
48                                                 AssemblyFile='Test/resources/TestTasks.dll'
49                                                 TaskName='TrueTestTask'
50                                         />
51                                 </Project>
52                         ";
53
54                         engine = new Engine (Consts.BinPath);
55
56                         project = engine.CreateNewProject ();
57                         project.LoadXml (documentString);
58                         
59                         Assert.AreEqual (1, project.UsingTasks.Count, "A1");
60                         Assert.AreEqual (false, project.UsingTasks.IsSynchronized, "A2");
61                         Assert.AreEqual (typeof (object), project.UsingTasks.SyncRoot.GetType (), "A3");
62                 }
63                 
64                 [Test]
65                 public void TestGetEnumerator ()
66                 {
67                         string documentString = @"
68                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
69                                         <UsingTask
70                                                 AssemblyFile='Test/resources/TestTasks.dll'
71                                                 TaskName='TrueTestTask'
72                                         />
73                                         <UsingTask
74                                                 AssemblyFile='Test/resources/TestTasks.dll'
75                                                 TaskName='FalseTestTask'
76                                         />
77                                 </Project>
78                         ";
79
80                         engine = new Engine (Consts.BinPath);
81                         project = engine.CreateNewProject ();
82                         project.LoadXml (documentString);
83                         
84                         IEnumerator en = project.UsingTasks.GetEnumerator ();
85                         en.MoveNext ();
86
87                         Assert.AreEqual ("Test/resources/TestTasks.dll", ((UsingTask) en.Current).AssemblyFile, "A1");
88                         Assert.AreEqual ("TrueTestTask", ((UsingTask) en.Current).TaskName, "A2");
89
90                         en.MoveNext ();
91
92                         Assert.AreEqual ("Test/resources/TestTasks.dll", ((UsingTask) en.Current).AssemblyFile, "A3");
93                         Assert.AreEqual ("FalseTestTask", ((UsingTask) en.Current).TaskName, "A4");
94
95                         Assert.IsFalse (en.MoveNext ());
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (ArgumentNullException))]
100                 public void TestCopyTo1 ()
101                 {
102                         string documentString = @"
103                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
104                                         <UsingTask
105                                                 AssemblyFile='Test/resources/TestTasks.dll'
106                                                 TaskName='TrueTestTask'
107                                         />
108                                 </Project>
109                         ";
110
111                         engine = new Engine (Consts.BinPath);
112
113                         project = engine.CreateNewProject ();
114                         project.LoadXml (documentString);
115
116                         project.UsingTasks.CopyTo (null, 0);
117                 }
118
119                 [Test]
120                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
121                 public void TestCopyTo2 ()
122                 {
123                         string documentString = @"
124                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
125                                         <UsingTask
126                                                 AssemblyFile='Test/resources/TestTasks.dll'
127                                                 TaskName='TrueTestTask'
128                                         />
129                                 </Project>
130                         ";
131
132                         engine = new Engine (Consts.BinPath);
133
134                         project = engine.CreateNewProject ();
135                         project.LoadXml (documentString);
136
137                         project.UsingTasks.CopyTo (new UsingTask [1], -1);
138                 }
139
140                 [Test]
141                 [Ignore ("Throws InvalidCastException on MS NET 2.0")]
142                 public void TestCopyTo3 ()
143                 {
144                         string documentString = @"
145                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
146                                         <UsingTask
147                                                 AssemblyFile='Test/resources/TestTasks.dll'
148                                                 TaskName='TrueTestTask'
149                                         />
150                                 </Project>
151                         ";
152
153                         engine = new Engine (Consts.BinPath);
154
155                         project = engine.CreateNewProject ();
156                         project.LoadXml (documentString);
157
158                         project.UsingTasks.CopyTo (new UsingTask [][] { new UsingTask [] {
159                                 null}}, 0);
160                 }
161
162                 [Test]
163                 [ExpectedException (typeof (ArgumentException))]
164                 public void TestCopyTo4 ()
165                 {
166                         string documentString = @"
167                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
168                                         <UsingTask
169                                                 AssemblyFile='Test/resources/TestTasks.dll'
170                                                 TaskName='TrueTestTask'
171                                         />
172                                 </Project>
173                         ";
174
175                         engine = new Engine (Consts.BinPath);
176
177                         project = engine.CreateNewProject ();
178                         project.LoadXml (documentString);
179
180                         project.UsingTasks.CopyTo (new UsingTask [1], 2);
181                 }
182                 [Test]
183                 [ExpectedException (typeof (ArgumentException))]
184                 public void TestCopyTo5 ()
185                 {
186                         string documentString = @"
187                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
188                                         <UsingTask
189                                                 AssemblyFile='Test/resources/TestTasks.dll'
190                                                 TaskName='TrueTestTask'
191                                         />
192                                 </Project>
193                         ";
194
195                         engine = new Engine (Consts.BinPath);
196
197                         project = engine.CreateNewProject ();
198                         project.LoadXml (documentString);
199
200                         project.UsingTasks.CopyTo (new UsingTask [1], 1);
201                 }
202
203                 [Test]
204                 public void TestCopyTo6 ()
205                 {
206                         string documentString = @"
207                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
208                                         <UsingTask
209                                                 AssemblyFile='Test/resources/TestTasks.dll'
210                                                 TaskName='TrueTestTask'
211                                         />
212                                 </Project>
213                         ";
214
215                         engine = new Engine (Consts.BinPath);
216
217                         project = engine.CreateNewProject ();
218                         project.LoadXml (documentString);
219
220                         UsingTask[] array = new UsingTask [1];
221                         project.UsingTasks.CopyTo (array, 0);
222
223                         Assert.AreEqual ("TrueTestTask", array [0].TaskName, "A1");
224                 }
225
226                 [Test]
227                 public void TestCopyTo7 ()
228                 {
229                         string documentString = @"
230                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
231                                         <UsingTask
232                                                 AssemblyFile='Test/resources/TestTasks.dll'
233                                                 TaskName='TrueTestTask'
234                                         />
235                                 </Project>
236                         ";
237
238                         engine = new Engine (Consts.BinPath);
239
240                         project = engine.CreateNewProject ();
241                         project.LoadXml (documentString);
242
243                         UsingTask [] array = new UsingTask [1];
244                         project.UsingTasks.CopyTo ((Array) array, 0);
245                 }
246         }
247 }
248