1 //
2 // TaskItemTest.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.Specialized;
31 using System.IO;
32 using Microsoft.Build.Framework;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
35 
36 namespace MonoTests.Microsoft.Build.Utilities {
37 
38 	[TestFixture]
39 	public class TaskItemTest {
40 
41 		ITaskItem item,item1,item2;
42 		ICollection metadataNames;
43 
44 		[SetUp]
SetUp()45 		public void SetUp ()
46 		{
47 			string[] temp = new string[] {"FullPath", "RootDir", "Filename", "Extension", "RelativeDir", "Directory",
48 				"RecursiveDir", "Identity", "ModifiedTime", "CreatedTime", "AccessedTime"};
49 			ArrayList al = new ArrayList ();
50 			foreach (string s in temp)
51 				al.Add (s);
52 			metadataNames = al;
53 		}
54 
CompareStringCollections(ICollection compared, ICollection reference)55 		private bool CompareStringCollections (ICollection compared, ICollection reference)
56 		{
57 			Hashtable comparedHash;
58 			comparedHash = CollectionsUtil.CreateCaseInsensitiveHashtable ();
59 
60 			foreach (string s in compared)
61 				comparedHash.Add (s, null);
62 
63 			foreach (string s in reference) {
64 				if (comparedHash.ContainsKey (s) == false) {
65 					return false;
66 				}
67 			}
68 
69 			return true;
70 		}
71 
TestCloneCustomMetadata()72 		public void TestCloneCustomMetadata ()
73 		{
74 			item = new TaskItem ();
75 			item.SetMetadata ("AAA", "111");
76 			item.SetMetadata ("aaa", "222");
77 			item.SetMetadata ("BBB", "111");
78 
79 			string [] metakeys = new string [] { "aaa", "BBB" };
80 			IDictionary meta = item.CloneCustomMetadata ();
81 
82 			Assert.IsTrue (CompareStringCollections (meta.Keys, metakeys), "A1");
83 			metakeys [0] = "aAa";
84 			Assert.IsTrue (CompareStringCollections (meta.Keys, metakeys), "A2");
85 			Assert.AreEqual ("222", meta ["aaa"], "A3");
86 			Assert.AreEqual ("222", meta ["AAA"], "A4");
87 			Assert.AreEqual ("222", meta ["aAa"], "A5");
88 			Assert.AreEqual ("111", meta ["BbB"], "A5");
89 		}
90 
91 		[Test]
92 		[Ignore ("NRE on .NET 2.0")]
TestCtor1()93 		public void TestCtor1 ()
94 		{
95 			new TaskItem ((ITaskItem) null);
96 		}
97 
98 		[Test]
99 		[ExpectedException (typeof (ArgumentNullException))]
TestCtor2()100 		public void TestCtor2 ()
101 		{
102 			new TaskItem ((string) null);
103 		}
104 
105 		[Test]
106 		[ExpectedException (typeof (ArgumentNullException))]
TestCtor3()107 		public void TestCtor3 ()
108 		{
109 			new TaskItem ((string) null, new Hashtable ());
110 		}
111 
112 		[Test]
113 		[ExpectedException (typeof (ArgumentNullException))]
TestCtor4()114 		public void TestCtor4 ()
115 		{
116 			new TaskItem ("itemspec", null);
117 		}
118 
119 		[Test]
TestCtor_EscapedSpecialChar()120 		public void TestCtor_EscapedSpecialChar ()
121 		{
122 			// If we instantiate with the *escaped* metadata, it's unescaped automatically
123 			var metadata = "foo@2x.png";
124 			var escapedMetadata = global::Microsoft.Build.BuildEngine.Utilities.Escape ("foo@2x.png");
125 			var item = new TaskItem (escapedMetadata);
126 			item.SetMetadata ("mine", escapedMetadata);
127 
128 			Assert.AreEqual (metadata, item.ItemSpec, "#1");
129 			Assert.AreEqual (metadata, item.GetMetadata ("Identity"), "#2");
130 			Assert.AreEqual (Path.GetFileNameWithoutExtension (metadata), item.GetMetadata ("FileName"), "#3");
131 			Assert.IsTrue (item.GetMetadata ("FullPath").EndsWith (metadata), "#4");
132 			Assert.AreEqual (metadata, item.GetMetadata ("mine"), "#5");
133 		}
134 
135 		[Test]
TestCtor_EscapedSpecialChar_BrokenEscaping()136 		public void TestCtor_EscapedSpecialChar_BrokenEscaping ()
137 		{
138 			// This is badly escaped, but MSBuild does not care.
139 			var metadata = "foo%4@2x.png";
140 			var item = new TaskItem (metadata);
141 
142 			Assert.AreEqual (metadata, item.ItemSpec, "#1");
143 			Assert.AreEqual (metadata, item.GetMetadata ("Identity"), "#2");
144 			Assert.AreEqual (Path.GetFileNameWithoutExtension (metadata), item.GetMetadata ("FileName"), "#3");
145 			Assert.IsTrue (item.GetMetadata ("FullPath").EndsWith (metadata), "#4");
146 		}
147 
148 		[Test]
TestCtor_UnescapedSpecialChar()149 		public void TestCtor_UnescapedSpecialChar ()
150 		{
151 			// If we instantiate with unescaped metadata, we get the same value back
152 			var metadata = "foo@2x.png";
153 			var item = new TaskItem (metadata);
154 			item.SetMetadata ("mine", metadata);
155 
156 			Assert.AreEqual (metadata, item.ItemSpec, "#1");
157 			Assert.AreEqual (metadata, item.GetMetadata ("Identity"), "#2");
158 			Assert.AreEqual (Path.GetFileNameWithoutExtension (metadata), item.GetMetadata ("FileName"), "#3");
159 			Assert.IsTrue (item.GetMetadata ("FullPath").EndsWith (metadata), "#4");
160 			Assert.AreEqual (metadata, item.GetMetadata ("mine"), "#5");
161 		}
162 
163 		[Test]
TestCopyConstructor()164 		public void TestCopyConstructor ()
165 		{
166 			item1 = new TaskItem ("itemSpec");
167 			item1.SetMetadata ("meta1", "val1");
168 			item2 = new TaskItem (item1);
169 			Assert.AreEqual (item1.GetMetadata ("meta1"), item2.GetMetadata ("meta1"), "A1");
170 			item1.SetMetadata ("meta1", "val2");
171 			Assert.AreEqual ("val2", item1.GetMetadata ("meta1"), "A2");
172 			Assert.AreEqual ("val1", item2.GetMetadata ("meta1"), "A3");
173 			item2.SetMetadata ("meta1", "val3");
174 			Assert.AreEqual ("val2", item1.GetMetadata ("meta1"), "A4");
175 			Assert.AreEqual ("val3", item2.GetMetadata ("meta1"), "A5");
176 		}
177 
178 		[Test]
TestCopyMetadataTo()179 		public void TestCopyMetadataTo ()
180 		{
181 			item1 = new TaskItem ("itemSpec");
182 			item2 = new TaskItem ("itemSpec");
183 			item1.SetMetadata ("A", "1");
184 			item1.SetMetadata ("B", "1");
185 			item1.SetMetadata ("C", "1");
186 			item2.SetMetadata ("B", "2");
187 			item1.CopyMetadataTo (item2);
188 			Assert.AreEqual ("1", item2.GetMetadata ("A"), "1");
189 			Assert.AreEqual ("2", item2.GetMetadata ("B"), "2");
190 			Assert.AreEqual ("1", item2.GetMetadata ("C"), "3");
191 		}
192 
193 		[Test]
TestGetMetadata()194 		public void TestGetMetadata ()
195 		{
196 			item = new TaskItem ("itemSpec");
197 			item.SetMetadata ("Metadata", "Value");
198 			Assert.AreEqual ("Value", item.GetMetadata ("Metadata"), "A1");
199 			Assert.AreEqual (String.Empty, item.GetMetadata ("lala"), "A2");
200 			Assert.AreEqual ("itemSpec", item.GetMetadata ("iDentity"), "A3");
201 			Assert.AreEqual ("", item.GetMetadata ("extension"), "A4");
202 			Assert.AreEqual ("", item.GetMetadata ("ModifiedTime"), "A5");
203 			Assert.AreEqual ("", item.GetMetadata ("CreatedTime"), "A6");
204 			Assert.AreEqual ("", item.GetMetadata ("ModifiedTime"), "A7");
205 			Assert.AreEqual ("", item.GetMetadata ("AccessedTime"), "A8");
206 		}
207 
208 		[Test]
TestMetadataNames()209 		public void TestMetadataNames ()
210 		{
211 			item = new TaskItem ("itemSpec");
212 
213 			Assert.IsTrue (CompareStringCollections (item.MetadataNames, metadataNames), "A1");
214 
215 			item.SetMetadata ("a", "b");
216 
217 			Assert.AreEqual (12, item.MetadataNames.Count, "A2");
218 		}
219 
220 		[Test]
TestOpExplicit()221 		public void TestOpExplicit ()
222 		{
223 			TaskItem item = new TaskItem ("itemSpec");
224 			item.SetMetadata ("a", "b");
225 
226 			Assert.AreEqual ("itemSpec", (string) item, "A1");
227 		}
228 
229 		[Test]
230 		[ExpectedException (typeof (ArgumentException))]
TestRemoveMetadata1()231 		public void TestRemoveMetadata1 ()
232 		{
233 			item = new TaskItem ("lalala");
234 			item.RemoveMetadata ("EXTension");
235 		}
236 
237 		[Test]
238 		[ExpectedException (typeof (ArgumentNullException))]
TestRemoveMetadata2()239 		public void TestRemoveMetadata2 ()
240 		{
241 			item = new TaskItem ("lalala");
242 			item.RemoveMetadata (null);
243 		}
244 
245 		[Test]
TestRemoveMetadata3()246 		public void TestRemoveMetadata3 ()
247 		{
248 			item = new TaskItem ("lalala");
249 			item.SetMetadata ("a", "b");
250 			item.RemoveMetadata ("a");
251 
252 			Assert.AreEqual (11, item.MetadataCount, "A1");
253 		}
254 
255 		[Test]
TestSetMetadata1()256 		public void TestSetMetadata1 ()
257 		{
258 			item = new TaskItem ("itemSpec");
259 			item.SetMetadata ("Metadata", "Value1");
260 			item.SetMetadata ("Metadata", "Value2");
261 			Assert.AreEqual (item.MetadataCount, 12, "MetadataCount");
262 			Assert.AreEqual ("Value2", item.GetMetadata ("Metadata"));
263 		}
264 
265 		[Test]
266 		[ExpectedException (typeof (ArgumentNullException))]
TestSetMetadata2()267 		public void TestSetMetadata2 ()
268 		{
269 			item = new TaskItem ("itemSpec");
270 			item.SetMetadata (null, "value");
271 		}
272 
273 		[Test]
274 		[ExpectedException (typeof (ArgumentNullException))]
TestSetMetadata3()275 		public void TestSetMetadata3 ()
276 		{
277 			item = new TaskItem ("itemSpec");
278 			item.SetMetadata ("name", null);
279 		}
280 
281 		[Test]
282 		[ExpectedException (typeof (ArgumentException))]
TestSetReservedMetadata()283 		public void TestSetReservedMetadata ()
284 		{
285 			item = new TaskItem ("lalala");
286 			item.SetMetadata ("Identity", "some value");
287 		}
288 
289 		[Test]
TestSetItemSpec()290 		public void TestSetItemSpec ()
291 		{
292 			var itemSpec = "foo@2x.png";
293 			var escapedItemSpec =  global::Microsoft.Build.BuildEngine.Utilities.Escape (itemSpec);
294 
295 			var item = new TaskItem ("foo");
296 			item.ItemSpec = itemSpec;
297 			Assert.AreEqual (itemSpec, item.ItemSpec, "#1");
298 
299 			item.ItemSpec = escapedItemSpec;
300 			Assert.AreEqual (itemSpec, item.ItemSpec, "#2");
301 		}
302 	}
303 }
304