2007-01-16 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Test / Microsoft.Build.BuildEngine / ProjectTest.cs
1 //
2 // ProjectTest.cs:
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 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 System.Collections.Generic;
31 using System.Xml;
32 using Microsoft.Build.BuildEngine;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
35 using NUnit.Framework;
36
37 namespace MonoTests.Microsoft.Build.BuildEngine {
38
39         class TestLogger : Logger {
40                 int target_started_events = 0;
41                 int target_finished_events = 0;
42
43                 public override void Initialize (IEventSource eventSource)
44                 {
45                         eventSource.TargetStarted += new TargetStartedEventHandler(TargetStarted);
46                         eventSource.TargetFinished += new TargetFinishedEventHandler(TargetFinished);
47                         eventSource.MessageRaised += new BuildMessageEventHandler(Message);
48                         eventSource.WarningRaised += new BuildWarningEventHandler(Warning);
49                 }
50
51                 void TargetStarted (object sender, TargetStartedEventArgs args)
52                 {
53                         target_started_events++;
54                 }
55
56                 void TargetFinished (object sender, TargetFinishedEventArgs args)
57                 {
58                         target_finished_events++;
59                 }
60
61                 void Message (object sender, BuildMessageEventArgs args)
62                 {
63                 }
64                 
65                 void Warning (object sender, BuildWarningEventArgs args)
66                 {
67                 }
68
69                 public int TargetStartedEvents { get { return target_started_events; } }
70
71                 public int TargetFinishedEvents { get { return target_finished_events; } }
72         }
73
74         [TestFixture]
75         public class ProjectTest {
76
77                 /*
78                 Import [] GetImports (ImportCollection ic)
79                 {
80                         List<Import> list = new List<Import> ();
81                         foreach (Import i in ic)
82                                 list.Add (i);
83                         return list.ToArray ();
84                 }
85                 */
86
87                 [Test]
88                 [ExpectedException (typeof (InvalidProjectFileException),
89                 @"The default XML namespace of the project must be the MSBuild XML namespace." + 
90                 " If the project is authored in the MSBuild 2003 format, please add " +
91                 "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" to the <Project> element. " +
92                 "If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.  ")]
93                 public void TestAssignment1 ()
94                 {
95                         Engine engine;
96                         Project project;
97                         string documentString =
98                                 "<Project></Project>";
99                         
100                         engine = new Engine (Consts.BinPath);
101                         DateTime time = DateTime.Now;
102                         project = engine.CreateNewProject ();
103                         project.LoadXml (documentString);
104
105                         Assert.AreEqual (true, project.BuildEnabled, "A1");
106                         Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
107                         Assert.AreEqual (String.Empty, project.FullFileName, "A3");
108                         Assert.AreEqual (false, project.IsDirty, "A4");
109                         Assert.AreEqual (false, project.IsValidated, "A5");
110                         Assert.AreEqual (engine, project.ParentEngine, "A6");
111                         Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
112                         Assert.IsTrue (String.Empty != project.Xml, "A8");
113                 }
114
115                 [Test]
116                 public void TestAssignment2 ()
117                 {
118                         Engine engine;
119                         Project project;
120                         string documentString =
121                                 "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"></Project>";
122                         
123                         engine = new Engine (Consts.BinPath);
124                         DateTime time = DateTime.Now;
125                         project = engine.CreateNewProject ();
126                         project.LoadXml (documentString);
127
128                         Assert.AreEqual (true, project.BuildEnabled, "A1");
129                         Assert.AreEqual (String.Empty, project.DefaultTargets, "A2");
130                         Assert.AreEqual (String.Empty, project.FullFileName, "A3");
131                         Assert.AreEqual (true, project.IsDirty, "A4");
132                         Assert.AreEqual (false, project.IsValidated, "A5");
133                         Assert.AreEqual (engine, project.ParentEngine, "A6");
134                         Assert.IsTrue (time <= project.TimeOfLastDirty, "A7");
135                         Assert.IsTrue (String.Empty != project.Xml, "A8");
136                 }
137
138                 [Test]
139                 [Category ("NotWorking")]
140                 public void TestAddNewImport1 ()
141                 {
142                         Engine engine;
143                         Project project;
144
145                         string documentString = @"
146                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
147                                         <PropertyGroup />
148                                         <ItemGroup />
149                                         <Target Name='a' />
150                                         <Import Project='Test/resources/Import.csproj' />
151                                 </Project>
152                         ";
153
154                         engine = new Engine (Consts.BinPath);
155                         project = engine.CreateNewProject ();
156                         project.LoadXml (documentString);
157
158                         project.AddNewImport ("a", "true");
159                         // reevaluation wasn't caused by anything so it has only old import
160                         Assert.AreEqual (1, project.Imports.Count, "A1");
161                 }
162
163                 [Test]
164                 [Ignore ("Too detailed probably (implementation specific)")]
165                 public void TestAddNewItem1 ()
166                 {
167                         Engine engine;
168                         Project project;
169                         BuildItemGroup [] groups = new BuildItemGroup [1];
170
171                         string documentString = @"
172                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
173                                 </Project>
174                         ";
175
176                         engine = new Engine (Consts.BinPath);
177                         project = engine.CreateNewProject ();
178                         project.LoadXml (documentString);
179
180                         BuildItem item = project.AddNewItem ("A", "B");
181
182                         Assert.AreEqual (1, project.ItemGroups.Count, "A1");
183                         project.ItemGroups.CopyTo (groups, 0);
184                         Assert.AreEqual (1, groups [0].Count, "A2");
185                         Assert.AreEqual ("B", groups [0] [0].Include, "A3");
186                         Assert.AreEqual ("B", groups [0] [0].FinalItemSpec, "A4");
187                         Assert.AreEqual ("A", groups [0] [0].Name, "A5");
188                         //Assert.AreNotSame (item, groups [0] [0], "A6");
189                         Assert.IsFalse (object.ReferenceEquals (item, groups [0] [0]), "A6");
190
191                         Assert.AreEqual (1, project.EvaluatedItems.Count, "A7");
192                         Assert.AreEqual ("B", project.EvaluatedItems [0].Include, "A8");
193                         Assert.AreEqual ("B", project.EvaluatedItems [0].FinalItemSpec, "A9");
194                         Assert.AreEqual ("A", project.EvaluatedItems [0].Name, "A10");
195                         //Assert.AreNotSame (item, project.EvaluatedItems [0], "A11");
196                         Assert.IsFalse (object.ReferenceEquals (item, project.EvaluatedItems [0]), "A11");
197                 }
198
199                 [Test]
200                 public void TestAddNewItemGroup ()
201                 {
202                         Engine engine;
203                         Project project;
204
205                         string documentString = @"
206                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
207                                 </Project>
208                         ";
209
210                         engine = new Engine (Consts.BinPath);
211                         project = engine.CreateNewProject ();
212                         project.LoadXml (documentString);
213
214                         BuildItemGroup big = project.AddNewItemGroup ();
215                         Assert.IsNotNull (big, "A1");
216                         Assert.AreEqual (String.Empty, big.Condition, "A2");
217                         Assert.AreEqual (0, big.Count, "A3");
218                         Assert.AreEqual (false, big.IsImported, "A4");
219                         Assert.IsTrue (project.IsDirty, "A5");
220                 }
221
222                 [Test]
223                 public void TestAddNewPropertyGroup ()
224                 {
225                         Engine engine;
226                         Project project;
227
228                         string documentString = @"
229                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
230                                 </Project>
231                         ";
232
233                         engine = new Engine (Consts.BinPath);
234                         project = engine.CreateNewProject ();
235                         project.LoadXml (documentString);
236
237                         BuildPropertyGroup bpg = project.AddNewPropertyGroup (false);
238                         Assert.IsNotNull (bpg, "A1");
239                         Assert.AreEqual (String.Empty, bpg.Condition, "A2");
240                         Assert.AreEqual (0, bpg.Count, "A3");
241                         Assert.AreEqual (false, bpg.IsImported, "A4");
242                         Assert.IsTrue (project.IsDirty, "A5");
243                 }
244
245                 [Test]
246                 [Category ("NotWorking")]
247                 public void TestBuild1 ()
248                 {
249                         Engine engine;
250                         Project project;
251                         IDictionary hashtable = new Hashtable ();
252                         
253                         string documentString = @"
254                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
255                                         <Target Name='Main'>
256                                                 <Microsoft.Build.Tasks.Message Text='Text' />
257                                         </Target>
258                                 </Project>
259                         ";
260                         
261                         engine = new Engine (Consts.BinPath);
262                         project = engine.CreateNewProject ();
263                         project.LoadXml (documentString);
264
265                         Assert.AreEqual (true, project.Build (new string[] { "Main" }, hashtable), "A1");
266                         Assert.AreEqual (1, hashtable.Count, "A2");
267                 }
268
269                 [Test]
270                 public void TestBuild2 ()
271                 {
272                         Engine engine;
273                         Project project;
274
275                         string documentString = @"
276                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
277                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
278                                                 <Message Text='text' />
279                                         </Target>
280                                 </Project>
281                         ";
282
283                         engine = new Engine (Consts.BinPath);
284                         TestLogger tl = new TestLogger ();
285                         engine.RegisterLogger (tl);
286                         project = engine.CreateNewProject ();
287                         project.LoadXml (documentString);
288
289                         project.Build ("T");
290                         project.Build ("T");
291
292                         Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
293                         Assert.AreEqual (2, tl.TargetFinishedEvents, "A2");
294                 }
295
296                 [Test]
297                 public void TestBuild3 ()
298                 {
299                         Engine engine;
300                         Project project;
301
302                         string documentString = @"
303                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
304                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
305                                                 <Message Text='text' />
306                                         </Target>
307                                 </Project>
308                         ";
309
310                         engine = new Engine (Consts.BinPath);
311                         TestLogger tl = new TestLogger ();
312                         engine.RegisterLogger (tl);
313                         project = engine.CreateNewProject ();
314                         project.LoadXml (documentString);
315
316                         project.Build (new string [1] { "T" }, null, BuildSettings.None);
317                         project.Build (new string [1] { "T" }, null, BuildSettings.None);
318
319                         Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
320                         Assert.AreEqual (2, tl.TargetFinishedEvents, "A2");
321                 }
322
323                 [Test]
324                 [Category ("NotWorking")]
325                 public void TestBuild4 ()
326                 {
327                         Engine engine;
328                         Project project;
329
330                         string documentString = @"
331                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
332                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
333                                                 <Message Text='text' />
334                                         </Target>
335                                 </Project>
336                         ";
337
338                         engine = new Engine (Consts.BinPath);
339                         TestLogger tl = new TestLogger ();
340                         engine.RegisterLogger (tl);
341                         project = engine.CreateNewProject ();
342                         project.LoadXml (documentString);
343
344                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
345                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
346
347                         Assert.AreEqual (1, tl.TargetStartedEvents, "A1");
348                         Assert.AreEqual (1, tl.TargetFinishedEvents, "A2");
349                 }
350
351                 [Test]
352                 public void TestBuild5 ()
353                 {
354                         Engine engine;
355                         Project project;
356
357                         string documentString = @"
358                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
359                                 </Project>
360                         ";
361
362                         engine = new Engine (Consts.BinPath);
363                         project = engine.CreateNewProject ();
364                         project.LoadXml (documentString);
365
366                         Assert.IsFalse (project.Build ("target_that_doesnt_exist"));
367                 }
368
369                 [Test]
370                 public void TestEvaluatedItems1 ()
371                 {
372                         Engine engine;
373                         Project project;
374
375                         string documentString = @"
376                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
377                                         <ItemGroup>
378                                                 <A Include='a' />
379                                                 <B Include='b' Condition='false' />
380                                         </ItemGroup>
381                                 </Project>
382                         ";
383
384                         engine = new Engine (Consts.BinPath);
385                         project = engine.CreateNewProject ();
386                         project.LoadXml (documentString);
387
388                         Assert.AreEqual (1, project.EvaluatedItems.Count, "A1");
389
390                         BuildItem bi = project.EvaluatedItems [0];
391
392                         bi.Name = "C";
393                         bi.Include = "c";
394
395                         BuildItemGroup [] big = new BuildItemGroup [1];
396                         project.ItemGroups.CopyTo (big, 0);
397                         Assert.AreEqual ("C", big [0] [0].Name, "A2");
398                         Assert.AreEqual ("c", big [0] [0].Include, "A3");
399                 }
400
401                 [Test]
402                 public void TestEvaluatedItems2 ()
403                 {
404                         Engine engine;
405                         Project project;
406
407                         string documentString = @"
408                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
409                                         <ItemGroup>
410                                                 <A Include='a;b;c' />
411                                         </ItemGroup>
412                                 </Project>
413                         ";
414
415                         engine = new Engine (Consts.BinPath);
416                         project = engine.CreateNewProject ();
417                         project.LoadXml (documentString);
418
419                         BuildItemGroup [] big = new BuildItemGroup [1];
420                         project.ItemGroups.CopyTo (big, 0);
421
422                         Assert.AreEqual (3, project.EvaluatedItems.Count, "A1");
423                         Assert.AreEqual ("a;b;c", big [0] [0].Include, "A2");
424                         Assert.AreEqual (1, big [0].Count, "A3");
425
426                         BuildItem bi = project.EvaluatedItems [0];
427
428                         bi.Include = "d";
429
430                         Assert.AreEqual (3, big [0].Count, "A4");
431                         Assert.AreEqual ("d", big [0] [0].Include, "A5");
432                         Assert.AreEqual ("b", big [0] [1].Include, "A6");
433                         Assert.AreEqual ("c", big [0] [2].Include, "A7");
434                 }
435
436                 [Test]
437                 [Category ("NotWorking")]
438                 public void TestGetConditionedPropertyValues ()
439                 {
440                         Engine engine;
441                         Project project;
442
443                         string documentString = @"
444                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
445                                         <PropertyGroup Condition='true'>
446                                                 <A>A</A>
447                                                 <B Condition='true'>A</B>
448                                         </PropertyGroup>
449                                         <PropertyGroup>
450                                                 <C Condition='true'>A</C>
451                                                 <C Condition='false'>B</C>
452                                                 <C Condition='!false'>C</C>
453                                                 <D>A</D>
454                                                 <E Condition="" '$(C)' == 'A' "">E</E>
455                                         </PropertyGroup>
456                                 </Project>
457                         ";
458
459                         engine = new Engine (Consts.BinPath);
460                         project = engine.CreateNewProject ();
461                         project.LoadXml (documentString);
462
463                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("A").Length, "A1");
464                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("B").Length, "A2");
465                         Assert.AreEqual (1, project.GetConditionedPropertyValues ("C").Length, "A3");
466                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("D").Length, "A4");
467                         Assert.AreEqual (0, project.GetConditionedPropertyValues ("E").Length, "A5");
468                         Assert.AreEqual ("A", project.GetConditionedPropertyValues ("C") [0], "A6");
469                 }
470
471                 [Test]
472                 [ExpectedException (typeof (ArgumentNullException))]
473                 public void TestGetEvaluatedItemsByName1 ()
474                 {
475                         Engine engine;
476                         Project project;
477
478                         string documentString = @"
479                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
480                                 </Project>
481                         ";
482
483                         engine = new Engine (Consts.BinPath);
484                         project = engine.CreateNewProject ();
485                         project.LoadXml (documentString);
486
487                         project.GetEvaluatedItemsByName (null);
488                 }
489
490                 [Test]
491                 public void TestGetEvaluatedItemsByName2 ()
492                 {
493                         Engine engine;
494                         Project project;
495
496                         string documentString = @"
497                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
498                                         <ItemGroup>
499                                                 <A Include='1' />
500                                                 <B Include='2' Condition='true' />
501                                                 <C Include='3' Condition='false' />
502                                         </ItemGroup>
503                                 </Project>
504                         ";
505
506                         engine = new Engine (Consts.BinPath);
507                         project = engine.CreateNewProject ();
508                         project.LoadXml (documentString);
509
510                         BuildItemGroup big;
511
512                         big = project.GetEvaluatedItemsByName (String.Empty);
513
514                         Assert.AreEqual (0, big.Count, "A1");
515
516                         big = project.GetEvaluatedItemsByName ("A");
517
518                         Assert.AreEqual (1, big.Count, "A2");
519                         Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
520
521                         big = project.GetEvaluatedItemsByName ("B");
522
523                         Assert.AreEqual (1, big.Count, "A4");
524                         Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
525
526                         big = project.GetEvaluatedItemsByName ("C");
527
528                         Assert.AreEqual (0, big.Count, "A6");
529                 }
530
531                 [Test]
532                 [ExpectedException (typeof (ArgumentNullException))]
533                 public void TestGetEvaluatedItemsByNameIgnoringCondition1 ()
534                 {
535                         Engine engine;
536                         Project project;
537
538                         string documentString = @"
539                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
540                                 </Project>
541                         ";
542
543                         engine = new Engine (Consts.BinPath);
544                         project = engine.CreateNewProject ();
545                         project.LoadXml (documentString);
546
547                         project.GetEvaluatedItemsByNameIgnoringCondition (null);
548                 }
549
550                 [Test]
551                 public void TestGetEvaluatedItemsByNameIgnoringCondition2 ()
552                 {
553                         Engine engine;
554                         Project project;
555
556                         string documentString = @"
557                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
558                                         <ItemGroup>
559                                                 <A Include='1' />
560                                                 <B Include='2' Condition='true' />
561                                                 <C Include='3' Condition='false' />
562                                         </ItemGroup>
563                                 </Project>
564                         ";
565
566                         engine = new Engine (Consts.BinPath);
567                         project = engine.CreateNewProject ();
568                         project.LoadXml (documentString);
569
570                         BuildItemGroup big;
571
572                         big = project.GetEvaluatedItemsByNameIgnoringCondition (String.Empty);
573
574                         Assert.AreEqual (0, big.Count, "A1");
575
576                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("A");
577
578                         Assert.AreEqual (1, big.Count, "A2");
579                         Assert.AreEqual ("1", big [0].FinalItemSpec, "A3");
580
581                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("B");
582
583                         Assert.AreEqual (1, big.Count, "A4");
584                         Assert.AreEqual ("2", big [0].FinalItemSpec, "A5");
585
586                         big = project.GetEvaluatedItemsByNameIgnoringCondition ("C");
587
588                         Assert.AreEqual (1, big.Count, "A6");
589                         Assert.AreEqual ("3", big [0].FinalItemSpec, "A7");
590                 }
591
592                 [Test]
593                 [ExpectedException (typeof (ArgumentNullException))]
594                 public void TestGetEvaluatedProperty1 ()
595                 {
596                         Engine engine;
597                         Project project;
598
599                         string documentString = @"
600                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
601                                 </Project>
602                         ";
603
604                         engine = new Engine (Consts.BinPath);
605                         project = engine.CreateNewProject ();
606                         project.LoadXml (documentString);
607
608                         project.GetEvaluatedProperty (null);
609                 }
610                 [Test]
611                 public void TestGetEvaluatedProperty2 ()
612                 {
613                         Engine engine;
614                         Project project;
615
616                         string documentString = @"
617                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
618                                         <PropertyGroup>
619                                                 <A>1</A>
620                                                 <B Condition='true'>2</B>
621                                                 <C Condition='false'>3</C>
622                                         </PropertyGroup>
623                                 </Project>
624                         ";
625
626                         engine = new Engine (Consts.BinPath);
627                         project = engine.CreateNewProject ();
628                         project.LoadXml (documentString);
629
630                         Assert.AreEqual ("1", project.GetEvaluatedProperty ("A"), "A1");
631                         Assert.AreEqual ("2", project.GetEvaluatedProperty ("B"), "A2");
632                         Assert.IsNull (project.GetEvaluatedProperty ("C"), "A3");
633                 }
634
635                 [Test]
636                 public void TestGetProjectExtensions ()
637                 {
638                         Engine engine;
639                         Project project;
640
641                         string documentString = @"
642                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
643                                         <ProjectExtensions>
644                                                 <Node>Text</Node>
645                                         </ProjectExtensions>
646                                 </Project>
647                         ";
648
649                         engine = new Engine (Consts.BinPath);
650                         project = engine.CreateNewProject ();
651                         project.LoadXml (documentString);
652
653                         Assert.AreEqual (String.Empty, project.GetProjectExtensions (null), "A1");
654                         Assert.AreEqual (String.Empty, project.GetProjectExtensions (String.Empty), "A2");
655                         Assert.AreEqual (String.Empty, project.GetProjectExtensions ("something"), "A3");
656                         Assert.AreEqual ("Text", project.GetProjectExtensions ("Node"), "A4");
657                 }
658
659                 [Test]
660                 public void TestGlobalProperties1 ()
661                 {
662                         Engine engine;
663                         Project project;
664                         
665                         string documentString = @"
666                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
667                                 </Project>
668                         ";
669                         
670                         engine = new Engine (Consts.BinPath);
671                         project = engine.CreateNewProject ();
672                         project.LoadXml (documentString);
673
674                         Assert.AreEqual (0, project.GlobalProperties.Count, "A1");
675                 }
676
677                 [Test]
678                 public void TestGlobalProperties2 ()
679                 {
680                         Engine engine;
681                         Project project;
682                         
683                         string documentString = @"
684                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
685                                 </Project>
686                         ";
687                         
688                         engine = new Engine (Consts.BinPath);
689                         engine.GlobalProperties.SetProperty ("Property", "Value");
690                         
691                         project = engine.CreateNewProject ();
692                         project.LoadXml (documentString);
693
694                         Assert.AreEqual (1, project.GlobalProperties.Count, "A1");
695                         Assert.AreEqual ("Property", project.GlobalProperties ["Property"].Name, "A2");
696                         Assert.AreEqual ("Value", project.GlobalProperties ["Property"].Value, "A3");
697                         Assert.AreEqual ("Value", project.GlobalProperties ["Property"].FinalValue, "A4");
698                         Assert.AreEqual ("Property", project.EvaluatedProperties ["Property"].Name, "A2");
699                         Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].Value, "A3");
700                         Assert.AreEqual ("Value", project.EvaluatedProperties ["Property"].FinalValue, "A4");
701                 }
702
703                 [Test]
704                 [Category ("NotDotNet")]
705                 [ExpectedException (typeof (ArgumentNullException))]
706                 public void TestGlobalProperties3 ()
707                 {
708                         Engine engine;
709                         Project project;
710                         
711                         string documentString = @"
712                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
713                                 </Project>
714                         ";
715                         
716                         engine = new Engine (Consts.BinPath);
717                         project = engine.CreateNewProject ();
718                         project.LoadXml (documentString);
719
720                         project.GlobalProperties = null;
721                 }
722
723                 [Test]
724                 [Ignore ("needs rewriting")]
725                 public void TestGlobalProperties4 ()
726                 {
727                         Engine engine;
728                         Project project;
729                         
730                         string documentString = @"
731                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
732                                         <PropertyGroup>
733                                                 <Property>a</Property>
734                                         </PropertyGroup>
735                                 </Project>
736                         ";
737                         
738                         engine = new Engine (Consts.BinPath);
739                         project = engine.CreateNewProject ();
740                         project.LoadXml (documentString);
741
742                         BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
743                         project.PropertyGroups.CopyTo (groups, 0);
744
745                         project.GlobalProperties = groups [0];
746                         project.GlobalProperties = project.EvaluatedProperties;
747                 }
748
749                 [Test]
750                 [Category ("NotDotNet")]
751                 [ExpectedException (typeof (InvalidOperationException))]
752                 public void TestGlobalProperties5 ()
753                 {
754                         Engine engine;
755                         Project project;
756                         
757                         string documentString = @"
758                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
759                                         <PropertyGroup>
760                                                 <Property>a</Property>
761                                         </PropertyGroup>
762                                 </Project>
763                         ";
764                         
765                         engine = new Engine (Consts.BinPath);
766                         project = engine.CreateNewProject ();
767                         project.LoadXml (documentString);
768
769                         BuildPropertyGroup[] groups = new BuildPropertyGroup [1];
770                         project.PropertyGroups.CopyTo (groups, 0);
771                         project.GlobalProperties = groups [0];
772                 }
773
774                 [Test]
775                 [ExpectedException (typeof (InvalidProjectFileException))]
776                 public void TestLoad1 ()
777                 {
778                         Engine engine;
779                         Project project;
780
781                         string documentString = @"
782                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
783                                         <PropertyGroup>
784                                 </Project>
785                         ";
786
787                         engine = new Engine (Consts.BinPath);
788                         project = engine.CreateNewProject ();
789                         project.LoadXml (documentString);
790                 }
791
792                 [Test]
793                 [ExpectedException (typeof (InvalidProjectFileException))]
794                 public void TestLoad2 ()
795                 {
796                         Engine engine;
797                         Project project;
798
799                         engine = new Engine (Consts.BinPath);
800                         project = engine.CreateNewProject ();
801                         project.LoadXml ("project_file_that_doesnt_exist");
802                 }
803
804                 [Test]
805                 public void TestParentEngine ()
806                 {
807                         Engine engine;
808                         Project project;
809                         
810                         engine = new Engine (Consts.BinPath);
811                         project = engine.CreateNewProject ();
812
813                         Assert.AreEqual (engine, project.ParentEngine, "A1");
814                 }
815
816                 [Test]
817                 [Category ("NotDotNet")]
818                 [ExpectedException (typeof (ArgumentNullException))]
819                 public void TestRemoveItemGroup1 ()
820                 {
821                         Engine engine;
822                         Project p1;
823
824                         engine = new Engine (Consts.BinPath);
825                         p1 = engine.CreateNewProject ();
826
827                         p1.RemoveItemGroup (null);
828                 }
829
830                 [Test]
831                 [ExpectedException (typeof (InvalidOperationException),
832                                         "The \"BuildItemGroup\" object specified does not belong to the correct \"Project\" object.")]
833                 [Category ("NotWorking")]
834                 public void TestRemoveItemGroup2 ()
835                 {
836                         Engine engine;
837                         Project p1;
838                         Project p2;
839                         BuildItemGroup [] groups  = new BuildItemGroup [1];
840
841                         engine = new Engine (Consts.BinPath);
842                         p1 = engine.CreateNewProject ();
843                         p2 = engine.CreateNewProject ();
844
845                         p1.AddNewItem ("A", "B");
846                         p1.ItemGroups.CopyTo (groups, 0);
847
848                         p2.RemoveItemGroup (groups [0]);
849                 }
850
851                 [Test]
852                 [Category ("NotDotNet")]
853                 [ExpectedException (typeof (ArgumentNullException))]
854                 public void TestRemoveItem1 ()
855                 {
856                         Engine engine;
857                         Project project;
858
859                         engine = new Engine (Consts.BinPath);
860                         project = engine.CreateNewProject ();
861
862                         project.RemoveItem (null);
863                 }
864
865                 [Test]
866                 [ExpectedException (typeof (InvalidOperationException),
867                         "The object passed in is not part of the project.")]
868                 public void TestRemoveItem2 ()
869                 {
870                         Engine engine;
871                         Project project;
872
873                         engine = new Engine (Consts.BinPath);
874                         project = engine.CreateNewProject ();
875
876                         project.RemoveItem (new BuildItem ("name", "include"));
877                 }
878
879                 [Test]
880                 [ExpectedException (typeof (InvalidOperationException),
881                                         "The \"BuildItemGroup\" object specified does not belong to the correct \"Project\" object.")]
882                 public void TestRemoveItem3 ()
883                 {
884                         Engine engine;
885                         Project p1;
886                         Project p2;
887
888                         engine = new Engine (Consts.BinPath);
889                         p1 = engine.CreateNewProject ();
890                         p2 = engine.CreateNewProject ();
891
892                         p1.AddNewItem ("A", "B");
893
894                         p2.RemoveItem (p1.EvaluatedItems [0]);
895                 }
896
897                 [Test]
898                 [Category ("NotDotNet")]
899                 [ExpectedException (typeof (InvalidOperationException))]
900                 public void TestRemoveItem4 ()
901                 {
902                         Engine engine;
903                         Project p1;
904                         Project p2;
905
906                         engine = new Engine (Consts.BinPath);
907                         p1 = engine.CreateNewProject ();
908                         p2 = engine.CreateNewProject ();
909
910                         p1.AddNewItem ("A", "B");
911                         p1.AddNewItem ("A", "C");
912
913                         p2.RemoveItem (p1.EvaluatedItems [0]);
914                 }
915
916                 [Test]
917                 public void TestRemoveItem5 ()
918                 {
919                         Engine engine;
920                         Project project;
921                         BuildItemGroup [] groups = new BuildItemGroup [1];
922
923                         string documentString = @"
924                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
925                                         <ItemGroup>
926                                                 <A Include='a'/>
927                                         </ItemGroup>
928                                 </Project>
929                         ";
930
931                         engine = new Engine (Consts.BinPath);
932                         project = engine.CreateNewProject ();
933                         project.LoadXml (documentString);
934
935                         project.RemoveItem (project.EvaluatedItems [0]);
936                         Assert.AreEqual (0, project.EvaluatedItems.Count, "A1");
937                         project.ItemGroups.CopyTo (groups, 0);
938                         Assert.IsNull (groups [0], "A2");
939                         Assert.AreEqual (0, project.ItemGroups.Count, "A3");
940                 }
941
942                 [Test]
943                 [Category ("NotWorking")]
944                 public void TestResetBuildStatus ()
945                 {
946                         Engine engine;
947                         Project project;
948
949                         string documentString = @"
950                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
951                                         <Target Name='T' Inputs='Test\resources\TestTasks.cs' Outputs='Test\resources\TestTasks.dll'>
952                                                 <Message Text='text' />
953                                         </Target>
954                                 </Project>
955                         ";
956
957                         engine = new Engine (Consts.BinPath);
958                         TestLogger tl = new TestLogger ();
959                         engine.RegisterLogger (tl);
960                         project = engine.CreateNewProject ();
961                         project.LoadXml (documentString);
962
963                         project.Build ("T");
964                         project.ResetBuildStatus ();
965                         project.Build (new string [1] { "T" }, null, BuildSettings.DoNotResetPreviouslyBuiltTargets);
966
967                         Assert.AreEqual (2, tl.TargetStartedEvents, "A1");
968                         Assert.AreEqual (2, tl.TargetFinishedEvents, "A1");
969                 }
970                 
971                 [Test]
972                 public void TestSchemaFile ()
973                 {
974                         Engine engine;
975                         Project project;
976                         
977                         string documentString = @"
978                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
979                                 </Project>
980                         ";
981                         
982                         engine = new Engine (Consts.BinPath);
983                         project = engine.CreateNewProject ();
984                         project.LoadXml (documentString);
985
986                         Assert.IsNull (project.SchemaFile, "A1");
987                 }
988                 [Test]
989                 [Category ("NotDotNet")]
990                 [ExpectedException (typeof (ArgumentNullException))]
991                 public void TestSetProjectExtensions1 ()
992                 {
993                         Engine engine;
994                         Project project;
995
996                         string documentString = @"
997                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
998                                 </Project>
999                         ";
1000
1001                         engine = new Engine (Consts.BinPath);
1002                         project = engine.CreateNewProject ();
1003                         project.LoadXml (documentString);
1004
1005                         project.SetProjectExtensions (null, null);
1006                 }
1007
1008                 [Test]
1009                 public void TestSetProjectExtensions2 ()
1010                 {
1011                         Engine engine;
1012                         Project project;
1013
1014                         string documentString = @"
1015                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1016                                 </Project>
1017                         ";
1018
1019                         engine = new Engine (Consts.BinPath);
1020                         project = engine.CreateNewProject ();
1021                         project.LoadXml (documentString);
1022
1023                         project.SetProjectExtensions ("name", "1");
1024                         Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
1025                         project.SetProjectExtensions ("name", "2");
1026                         Assert.AreEqual ("2", project.GetProjectExtensions ("name"), "A2");
1027                         Assert.IsTrue (project.IsDirty, "A3");
1028                 }
1029
1030                 [Test]
1031                 public void TestSetProjectExtensions3 ()
1032                 {
1033                         Engine engine;
1034                         Project project;
1035
1036                         string documentString = @"
1037                                 <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
1038                                         <ProjectExtensions>
1039                                         </ProjectExtensions>
1040                                 </Project>
1041                         ";
1042
1043                         engine = new Engine (Consts.BinPath);
1044                         project = engine.CreateNewProject ();
1045                         project.LoadXml (documentString);
1046
1047                         project.SetProjectExtensions ("name", "1");
1048                         Assert.AreEqual ("1", project.GetProjectExtensions ("name"), "A1");
1049                         Assert.IsTrue (project.IsDirty, "A2");
1050                 }
1051         }
1052 }