Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / InversionOfControl / Castle.MicroKernel.Tests / DefaultConversionManagerTestCase.cs
blobacc300e539bb01624505b75606d5382f8eef0144
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MicroKernel.Tests
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using Castle.Core.Configuration;
21 using Castle.MicroKernel.SubSystems.Conversion;
22 using NUnit.Framework;
24 [TestFixture]
25 public class DefaultConversionManagerTestCase
27 private DefaultConversionManager conversionMng = new DefaultConversionManager();
29 [Test]
30 public void PerformConversionInt()
32 Assert.AreEqual(100, conversionMng.PerformConversion("100", typeof(int)));
33 Assert.AreEqual(1234, conversionMng.PerformConversion("1234", typeof(int)));
36 [Test]
37 public void PerformConversionChar()
39 Assert.AreEqual('a', conversionMng.PerformConversion("a", typeof(Char)));
42 [Test]
43 public void PerformConversionBool()
45 Assert.AreEqual(true, conversionMng.PerformConversion("true", typeof(bool)));
46 Assert.AreEqual(false, conversionMng.PerformConversion("false", typeof(bool)));
49 [Test]
50 public void PerformConversionType()
52 Assert.AreEqual(typeof(DefaultConversionManagerTestCase),
53 conversionMng.PerformConversion(
54 "Castle.MicroKernel.Tests.DefaultConversionManagerTestCase, Castle.MicroKernel.Tests",
55 typeof(Type)));
58 [Test]
59 public void PerformConversionList()
61 MutableConfiguration config = new MutableConfiguration("list");
62 config.Attributes["type"] = "System.String";
64 config.Children.Add(new MutableConfiguration("item", "first"));
65 config.Children.Add(new MutableConfiguration("item", "second"));
66 config.Children.Add(new MutableConfiguration("item", "third"));
68 Assert.IsTrue(conversionMng.CanHandleType(typeof(IList)));
69 Assert.IsTrue(conversionMng.CanHandleType(typeof(ArrayList)));
71 IList list = (IList) conversionMng.PerformConversion(config, typeof(IList));
72 Assert.IsNotNull(list);
73 Assert.AreEqual("first", list[0]);
74 Assert.AreEqual("second", list[1]);
75 Assert.AreEqual("third", list[2]);
78 [Test]
79 public void Dictionary()
81 MutableConfiguration config = new MutableConfiguration("dictionary");
82 config.Attributes["keyType"] = "System.String";
83 config.Attributes["valueType"] = "System.String";
85 config.Children.Add(new MutableConfiguration("item", "first")).Attributes["key"] = "key1";
86 config.Children.Add(new MutableConfiguration("item", "second")).Attributes["key"] = "key2";
87 config.Children.Add(new MutableConfiguration("item", "third")).Attributes["key"] = "key3";
89 MutableConfiguration intItem = new MutableConfiguration("item", "40");
90 intItem.Attributes["key"] = "4";
91 intItem.Attributes["keyType"] = "System.Int32, mscorlib";
92 intItem.Attributes["valueType"] = "System.Int32, mscorlib";
94 config.Children.Add(intItem);
96 MutableConfiguration dateItem = new MutableConfiguration("item", "2005/12/1");
97 dateItem.Attributes["key"] = "2000/1/1";
98 dateItem.Attributes["keyType"] = "System.DateTime, mscorlib";
99 dateItem.Attributes["valueType"] = "System.DateTime, mscorlib";
101 config.Children.Add(dateItem);
103 Assert.IsTrue(conversionMng.CanHandleType(typeof(IDictionary)));
104 Assert.IsTrue(conversionMng.CanHandleType(typeof(Hashtable)));
106 IDictionary dict = (IDictionary)
107 conversionMng.PerformConversion(config, typeof(IDictionary));
109 Assert.IsNotNull(dict);
111 Assert.AreEqual("first", dict["key1"]);
112 Assert.AreEqual("second", dict["key2"]);
113 Assert.AreEqual("third", dict["key3"]);
114 Assert.AreEqual(40, dict[4]);
115 Assert.AreEqual(new DateTime(2005, 12, 1), dict[new DateTime(2000, 1, 1)]);
118 [Test]
119 public void DictionaryWithDifferentValueTypes()
121 MutableConfiguration config = new MutableConfiguration("dictionary");
123 config.CreateChild("entry")
124 .Attribute("key", "intentry")
125 .Attribute("valueType", "System.Int32, mscorlib")
126 .Value = "123";
128 config.CreateChild("entry")
129 .Attribute("key", "values")
130 .Attribute("valueType", "System.Int32[], mscorlib")
131 .CreateChild("array")
132 .Attribute("type", "System.Int32, mscorlib")
133 .CreateChild("item", "400");
135 IDictionary dict =
136 (IDictionary) conversionMng.PerformConversion(config, typeof(IDictionary));
138 Assert.IsNotNull(dict);
140 Assert.AreEqual(123, dict["intentry"]);
141 int[] values = (int[]) dict["values"];
142 Assert.IsNotNull(values);
143 Assert.AreEqual(1, values.Length);
144 Assert.AreEqual(400, values[0]);
147 [Test]
148 public void GenericPerformConversionList()
150 MutableConfiguration config = new MutableConfiguration("list");
151 config.Attributes["type"] = "System.Int64";
153 config.Children.Add(new MutableConfiguration("item", "345"));
154 config.Children.Add(new MutableConfiguration("item", "3147"));
155 config.Children.Add(new MutableConfiguration("item", "997"));
157 Assert.IsTrue(conversionMng.CanHandleType(typeof(IList<double>)));
158 Assert.IsTrue(conversionMng.CanHandleType(typeof(List<string>)));
160 IList<long> list = (IList<long>) conversionMng.PerformConversion(config, typeof(IList<long>));
161 Assert.IsNotNull(list);
162 Assert.AreEqual(345L, list[0]);
163 Assert.AreEqual(3147L, list[1]);
164 Assert.AreEqual(997L, list[2]);
167 [Test]
168 public void ListOfLongGuessingType()
170 MutableConfiguration config = new MutableConfiguration("list");
172 config.Children.Add(new MutableConfiguration("item", "345"));
173 config.Children.Add(new MutableConfiguration("item", "3147"));
174 config.Children.Add(new MutableConfiguration("item", "997"));
176 Assert.IsTrue(conversionMng.CanHandleType(typeof(IList<double>)));
177 Assert.IsTrue(conversionMng.CanHandleType(typeof(List<string>)));
179 IList<long> list = (IList<long>) conversionMng.PerformConversion(config, typeof(IList<long>));
180 Assert.IsNotNull(list);
181 Assert.AreEqual(345L, list[0]);
182 Assert.AreEqual(3147L, list[1]);
183 Assert.AreEqual(997L, list[2]);
186 [Test]
187 public void GenericDictionary()
189 MutableConfiguration config = new MutableConfiguration("dictionary");
190 config.Attributes["keyType"] = "System.String";
191 config.Attributes["valueType"] = "System.Int32";
193 config.Children.Add(new MutableConfiguration("item", "1")).Attributes["key"] = "key1";
194 config.Children.Add(new MutableConfiguration("item", "2")).Attributes["key"] = "key2";
195 config.Children.Add(new MutableConfiguration("item", "3")).Attributes["key"] = "key3";
197 Assert.IsTrue(conversionMng.CanHandleType(typeof(IDictionary<string, string>)));
198 Assert.IsTrue(conversionMng.CanHandleType(typeof(Dictionary<string, int>)));
200 IDictionary<string, int> dict =
201 (IDictionary<string, int>) conversionMng.PerformConversion(config, typeof(IDictionary<string, int>));
203 Assert.IsNotNull(dict);
205 Assert.AreEqual(1, dict["key1"]);
206 Assert.AreEqual(2, dict["key2"]);
207 Assert.AreEqual(3, dict["key3"]);
210 [Test]
211 public void Array()
213 MutableConfiguration config = new MutableConfiguration("array");
215 config.Children.Add(new MutableConfiguration("item", "first"));
216 config.Children.Add(new MutableConfiguration("item", "second"));
217 config.Children.Add(new MutableConfiguration("item", "third"));
219 Assert.IsTrue(conversionMng.CanHandleType(typeof(String[])));
221 String[] array = (String[])
222 conversionMng.PerformConversion(config, typeof(String[]));
224 Assert.IsNotNull(array);
226 Assert.AreEqual("first", array[0]);
227 Assert.AreEqual("second", array[1]);
228 Assert.AreEqual("third", array[2]);
231 [Test]
232 public void PerformConversionTimeSpan()
234 Assert.AreEqual(TimeSpan.Zero, conversionMng.PerformConversion("0", typeof(TimeSpan)));
235 Assert.AreEqual(TimeSpan.FromDays(14), conversionMng.PerformConversion("14", typeof(TimeSpan)));
236 Assert.AreEqual(new TimeSpan(0, 1, 2, 3), conversionMng.PerformConversion("1:2:3", typeof(TimeSpan)));
237 Assert.AreEqual(new TimeSpan(0, 0, 0, 0, 250), conversionMng.PerformConversion("0:0:0.250", typeof(TimeSpan)));
238 Assert.AreEqual(new TimeSpan(10, 20, 30, 40, 500),
239 conversionMng.PerformConversion("10.20:30:40.50", typeof(TimeSpan)));