5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
30 using System.Collections.Specialized;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Utilities;
33 using NUnit.Framework;
35 namespace MonoTests.Microsoft.Build.Utilities {
38 public class TaskItemTest {
40 ITaskItem item,item1,item2;
41 ICollection metadataNames;
46 string[] temp = new string[] {"FullPath", "RootDir", "Filename", "Extension", "RelativeDir", "Directory",
47 "RecursiveDir", "Identity", "ModifiedTime", "CreatedTime", "AccessedTime"};
48 ArrayList al = new ArrayList ();
49 foreach (string s in temp)
54 private bool CompareStringCollections (ICollection compared, ICollection reference)
56 Hashtable comparedHash;
57 comparedHash = CollectionsUtil.CreateCaseInsensitiveHashtable ();
59 foreach (string s in compared)
60 comparedHash.Add (s, null);
62 foreach (string s in reference) {
63 if (comparedHash.ContainsKey (s) == false) {
71 public void TestCloneCustomMetadata ()
73 item = new TaskItem ();
74 item.SetMetadata ("AAA", "111");
75 item.SetMetadata ("aaa", "222");
76 item.SetMetadata ("BBB", "111");
78 string [] metakeys = new string [] { "aaa", "BBB" };
79 IDictionary meta = item.CloneCustomMetadata ();
81 Assert.IsTrue (CompareStringCollections (meta.Keys, metakeys), "A1");
83 Assert.IsTrue (CompareStringCollections (meta.Keys, metakeys), "A2");
84 Assert.AreEqual ("222", meta ["aaa"], "A3");
85 Assert.AreEqual ("222", meta ["AAA"], "A4");
86 Assert.AreEqual ("222", meta ["aAa"], "A5");
87 Assert.AreEqual ("111", meta ["BbB"], "A5");
91 [Ignore ("NRE on .NET 2.0")]
92 public void TestCtor1 ()
94 new TaskItem ((ITaskItem) null);
98 [ExpectedException (typeof (ArgumentNullException))]
99 public void TestCtor2 ()
101 new TaskItem ((string) null);
105 [ExpectedException (typeof (ArgumentNullException))]
106 public void TestCtor3 ()
108 new TaskItem ((string) null, new Hashtable ());
112 [ExpectedException (typeof (ArgumentNullException))]
113 public void TestCtor4 ()
115 new TaskItem ("itemspec", null);
119 public void TestCopyConstructor ()
121 item1 = new TaskItem ("itemSpec");
122 item1.SetMetadata ("meta1", "val1");
123 item2 = new TaskItem (item1);
124 Assert.AreEqual (item1.GetMetadata ("meta1"), item2.GetMetadata ("meta1"), "A1");
125 item1.SetMetadata ("meta1", "val2");
126 Assert.AreEqual ("val2", item1.GetMetadata ("meta1"), "A2");
127 Assert.AreEqual ("val1", item2.GetMetadata ("meta1"), "A3");
128 item2.SetMetadata ("meta1", "val3");
129 Assert.AreEqual ("val2", item1.GetMetadata ("meta1"), "A4");
130 Assert.AreEqual ("val3", item2.GetMetadata ("meta1"), "A5");
134 public void TestCopyMetadataTo ()
136 item1 = new TaskItem ("itemSpec");
137 item2 = new TaskItem ("itemSpec");
138 item1.SetMetadata ("A", "1");
139 item1.SetMetadata ("B", "1");
140 item1.SetMetadata ("C", "1");
141 item2.SetMetadata ("B", "2");
142 item1.CopyMetadataTo (item2);
143 Assert.AreEqual ("1", item2.GetMetadata ("A"), "1");
144 Assert.AreEqual ("2", item2.GetMetadata ("B"), "2");
145 Assert.AreEqual ("1", item2.GetMetadata ("C"), "3");
149 public void TestGetMetadata ()
151 item = new TaskItem ("itemSpec");
152 item.SetMetadata ("Metadata", "Value");
153 Assert.AreEqual ("Value", item.GetMetadata ("Metadata"), "A1");
154 Assert.AreEqual (String.Empty, item.GetMetadata ("lala"), "A2");
155 Assert.AreEqual ("itemSpec", item.GetMetadata ("iDentity"), "A3");
156 Assert.AreEqual ("", item.GetMetadata ("extension"), "A4");
157 Assert.AreEqual ("", item.GetMetadata ("ModifiedTime"), "A5");
158 Assert.AreEqual ("", item.GetMetadata ("CreatedTime"), "A6");
159 Assert.AreEqual ("", item.GetMetadata ("ModifiedTime"), "A7");
160 Assert.AreEqual ("", item.GetMetadata ("AccessedTime"), "A8");
164 public void TestMetadataNames ()
166 item = new TaskItem ("itemSpec");
168 Assert.IsTrue (CompareStringCollections (item.MetadataNames, metadataNames), "A1");
170 item.SetMetadata ("a", "b");
172 Assert.AreEqual (12, item.MetadataNames.Count, "A2");
176 public void TestOpExplicit ()
178 TaskItem item = new TaskItem ("itemSpec");
179 item.SetMetadata ("a", "b");
181 Assert.AreEqual ("itemSpec", (string) item, "A1");
185 [ExpectedException (typeof (ArgumentException))]
186 public void TestRemoveMetadata1 ()
188 item = new TaskItem ("lalala");
189 item.RemoveMetadata ("EXTension");
193 [ExpectedException (typeof (ArgumentNullException))]
194 public void TestRemoveMetadata2 ()
196 item = new TaskItem ("lalala");
197 item.RemoveMetadata (null);
201 public void TestRemoveMetadata3 ()
203 item = new TaskItem ("lalala");
204 item.SetMetadata ("a", "b");
205 item.RemoveMetadata ("a");
207 Assert.AreEqual (11, item.MetadataCount, "A1");
211 public void TestSetMetadata1 ()
213 item = new TaskItem ("itemSpec");
214 item.SetMetadata ("Metadata", "Value1");
215 item.SetMetadata ("Metadata", "Value2");
216 Assert.AreEqual (item.MetadataCount, 12, "MetadataCount");
217 Assert.AreEqual ("Value2", item.GetMetadata ("Metadata"));
221 [ExpectedException (typeof (ArgumentNullException))]
222 public void TestSetMetadata2 ()
224 item = new TaskItem ("itemSpec");
225 item.SetMetadata (null, "value");
229 [ExpectedException (typeof (ArgumentNullException))]
230 public void TestSetMetadata3 ()
232 item = new TaskItem ("itemSpec");
233 item.SetMetadata ("name", null);
237 [ExpectedException (typeof (ArgumentException))]
238 public void TestSetReservedMetadata ()
240 item = new TaskItem ("lalala");
241 item.SetMetadata ("Identity", "some value");