Merge pull request #1909 from esdrubal/reflection
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / TargetCollectionTest.cs
1 //
2 // TargetCollectionTest.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 TargetCollectionTest {
38                 
39                 Engine                  engine;
40                 Project                 project;
41                 
42                 [Test]
43                 public void TestEmpty ()
44                 {
45                         string documentString = @"
46                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
47                                 </Project>
48                         ";
49
50                         engine = new Engine (Consts.BinPath);
51
52                         project = engine.CreateNewProject ();
53                         project.LoadXml (documentString);
54
55                         Assert.AreEqual (0, project.Targets.Count, "A1");
56                         Assert.IsFalse (project.Targets.IsSynchronized, "A2");
57                         Assert.IsNotNull (project.Targets.SyncRoot, "A3");
58                 }
59
60                 [Test]
61                 public void TestAddNewTarget1 ()
62                 {
63                         string documentString = @"
64                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
65                                 </Project>
66                         ";
67
68                         engine = new Engine (Consts.BinPath);
69
70                         project = engine.CreateNewProject ();
71                         project.LoadXml (documentString);
72
73                         project.Targets.AddNewTarget ("name");
74
75                         Assert.AreEqual (1, project.Targets.Count, "A1");
76                         Assert.AreEqual ("name", project.Targets ["name"].Name, "A2");
77                         Assert.IsFalse (project.Targets ["name"].IsImported, "A3");
78                         Assert.AreEqual (String.Empty, project.Targets ["name"].Condition, "A4");
79                         Assert.AreEqual (String.Empty, project.Targets ["name"].DependsOnTargets, "A5");        
80                 }
81
82                 [Test]
83                 [ExpectedException (typeof (InvalidProjectFileException))]
84                 public void TestAddNewTarget2 ()
85                 {
86                         string documentString = @"
87                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
88                                 </Project>
89                         ";
90
91                         engine = new Engine (Consts.BinPath);
92
93                         project = engine.CreateNewProject ();
94                         project.LoadXml (documentString);
95
96                         project.Targets.AddNewTarget (null);
97                 }
98
99                 [Test]
100                 public void TestAddNewTarget3 ()
101                 {
102                         engine = new Engine (Consts.BinPath);
103
104                         project = engine.CreateNewProject ();
105
106                         project.Targets.AddNewTarget ("Name");
107                         project.Targets.AddNewTarget ("Name");
108                         Assert.AreEqual (1, project.Targets.Count, "A1");
109                 }
110
111                 [Test]
112                 public void TestAddNewTarget4 ()
113                 {
114                         string documentString = @"
115                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
116                                         <Target Name='A' />
117                                         <Target Name='A' />
118                                 </Project>
119                         ";
120
121                         engine = new Engine (Consts.BinPath);
122
123                         project = engine.CreateNewProject ();
124                         project.LoadXml (documentString);
125                         Assert.AreEqual (1, project.Targets.Count, "A1");
126                 }
127
128                 [Test]
129                 [ExpectedException (typeof (ArgumentNullException))]
130                 public void TestCopyTo1 ()
131                 {
132                         string documentString = @"
133                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
134                                         <Target Name='a' />
135                                 </Project>
136                         ";
137
138                         engine = new Engine (Consts.BinPath);
139
140                         project = engine.CreateNewProject ();
141                         project.LoadXml (documentString);
142
143                         project.Targets.CopyTo (null, 0);
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
148                 public void TestCopyTo2 ()
149                 {
150                         string documentString = @"
151                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
152                                         <Target Name='a' />
153                                 </Project>
154                         ";
155
156                         engine = new Engine (Consts.BinPath);
157
158                         project = engine.CreateNewProject ();
159                         project.LoadXml (documentString);
160
161                         project.Targets.CopyTo (new Target [1], -1);
162                 }
163
164                 [Test]
165                 [ExpectedException (typeof (InvalidCastException))]
166                 public void TestCopyTo3 ()
167                 {
168                         string documentString = @"
169                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
170                                         <Target Name='a' />
171                                 </Project>
172                         ";
173
174                         engine = new Engine (Consts.BinPath);
175
176                         project = engine.CreateNewProject ();
177                         project.LoadXml (documentString);
178
179                         project.Targets.CopyTo (new Target [][] { new Target [] { null } }, 0);
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
184                 public void TestCopyTo4 ()
185                 {
186                         string documentString = @"
187                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
188                                         <Target Name='a' />
189                                 </Project>
190                         ";
191
192                         engine = new Engine (Consts.BinPath);
193
194                         project = engine.CreateNewProject ();
195                         project.LoadXml (documentString);
196
197                         project.Targets.CopyTo (new Target [1], 2);
198                 }
199
200                 [Test]
201                 [ExpectedException (typeof (ArgumentException))]
202                 public void TestCopyTo5 ()
203                 {
204                         string documentString = @"
205                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
206                                         <Target Name='a' />
207                                 </Project>
208                         ";
209
210                         engine = new Engine (Consts.BinPath);
211
212                         project = engine.CreateNewProject ();
213                         project.LoadXml (documentString);
214
215                         project.Targets.CopyTo (new Target [1], 1);
216                 }
217                 
218                 [Test]
219                 public void TestExists1 ()
220                 {
221                         string documentString = @"
222                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
223                                         <Target Name='name' />
224                                 </Project>
225                         ";
226
227                         engine = new Engine (Consts.BinPath);
228
229                         project = engine.CreateNewProject ();
230                         project.LoadXml (documentString);
231
232                         Assert.AreEqual (1, project.Targets.Count, "A1");
233                         Assert.IsTrue (project.Targets.Exists ("name"), "A2");
234                         Assert.IsTrue (project.Targets.Exists ("NAME"), "A3");
235                         Assert.IsFalse (project.Targets.Exists ("something_that_doesnt_exist"), "A4");
236                 }
237
238                 [Test]
239                 [ExpectedException (typeof (ArgumentNullException))]
240                 public void TestExists2 ()
241                 {
242                         string documentString = @"
243                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
244                                         <Target Name='name' />
245                                 </Project>
246                         ";
247
248                         engine = new Engine (Consts.BinPath);
249
250                         project = engine.CreateNewProject ();
251                         project.LoadXml (documentString);
252
253                         project.Targets.Exists (null);
254                 }
255
256                 [Test]
257                 public void TestGetEnumerator ()
258                 {
259                         string documentString = @"
260                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
261                                         <Target Name='first' />
262                                         <Target Name='second' />
263                                 </Project>
264                         ";
265
266                         engine = new Engine (Consts.BinPath);
267
268                         project = engine.CreateNewProject ();
269                         project.LoadXml (documentString);
270
271                         ArrayList targets = new ArrayList ();
272
273                         foreach (Target t in project.Targets)
274                                 targets.Add (t);
275
276                         Assert.AreEqual ("first", ((Target) targets [0]).Name, "A1");
277                         Assert.AreEqual ("second", ((Target) targets [1]).Name, "A1");
278
279                 }
280
281                 [Test]
282                 public void TestRemoveTarget1 ()
283                 {
284                         string documentString = @"
285                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
286                                         <Target Name='first' />
287                                         <Target Name='second' />
288                                 </Project>
289                         ";
290
291                         engine = new Engine (Consts.BinPath);
292
293                         project = engine.CreateNewProject ();
294                         project.LoadXml (documentString);
295
296                         Assert.IsTrue (project.Targets.Exists ("first"), "A1");
297                         Assert.IsTrue (project.Targets.Exists ("second"), "A1");
298                         
299                         project.Targets.RemoveTarget (project.Targets ["first"]);
300                         
301                         Assert.IsFalse (project.Targets.Exists ("first"), "A1");
302                         Assert.IsTrue (project.Targets.Exists ("second"), "A1");
303                 }
304
305                 [Test]
306                 [ExpectedException (typeof (ArgumentNullException))]
307                 [Category ("NotDotNet")]
308                 public void TestRemoveTarget2 ()
309                 {
310                         string documentString = @"
311                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
312                                         <Target Name='first' />
313                                 </Project>
314                         ";
315
316                         engine = new Engine (Consts.BinPath);
317
318                         project = engine.CreateNewProject ();
319                         project.LoadXml (documentString);
320
321                         project.Targets.RemoveTarget (null);
322                 }
323
324                 [Test]
325                 public void TestIndexer1 ()
326                 {
327                         string documentString = @"
328                                 <Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
329                                         <Target Name='first' />
330                                 </Project>
331                         ";
332
333                         engine = new Engine (Consts.BinPath);
334
335                         project = engine.CreateNewProject ();
336                         project.LoadXml (documentString);
337
338                         Target t1 = project.Targets ["first"];
339
340                         Assert.AreEqual ("first", t1.Name, "A1");
341
342                         Target t2 = project.Targets ["target_that_doesnt_exist"];
343
344                         Assert.IsNull (t2, "A2");
345                 }
346         }
347 }