Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Components / Binder / Castle.Components.Binder.Tests / DataBinderSingleValueTestCase.cs
blob6c8152b595c8111263a6cb0d9cecd8cb6df87648
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.Components.Binder.Tests
17 using System;
18 using System.Globalization;
19 using System.IO;
20 using System.Threading;
21 using Nullables;
22 using NUnit.Framework;
24 [TestFixture]
25 public class DataBinderSingleValueTestCase
27 private IDataBinder binder;
29 [TestFixtureSetUp]
30 public void Init()
32 binder = new DataBinder();
35 [SetUp]
36 public void SetUp()
38 CultureInfo en = CultureInfo.CreateSpecificCulture("en");
40 Thread.CurrentThread.CurrentCulture = en;
41 Thread.CurrentThread.CurrentUICulture = en;
44 [Test]
45 public void ArrayBindingWithEmptyNode()
47 CompositeNode node = new CompositeNode("unnamed");
48 Assert.AreEqual(new String[0], binder.BindParameter(typeof(String[]), "name", node));
50 Assert.AreEqual(new String[0], binder.BindParameter(typeof(String[]), "name", node));
52 Assert.AreEqual(new int[0], binder.BindParameter(typeof(int[]), "name", node));
54 Assert.AreEqual(new int[0], binder.BindParameter(typeof(int[]), "name", node));
57 [Test]
58 public void SimpleBinding_Int()
60 CompositeNode node = new CompositeNode("unnamed");
61 node.AddChildNode(new LeafNode(typeof(String), "name", "200"));
62 Assert.AreEqual(200, binder.BindParameter(typeof(int), "name", node));
64 node = new CompositeNode("unnamed");
65 node.AddChildNode(new LeafNode(typeof(int), "name", 200));
66 Assert.AreEqual(200, binder.BindParameter(typeof(int), "name", node));
68 node = new CompositeNode("unnamed");
69 node.AddChildNode(new LeafNode(typeof(float), "name", 200.1f));
70 Assert.AreEqual(200, binder.BindParameter(typeof(int), "name", node));
72 node = new CompositeNode("unnamed");
73 node.AddChildNode(new LeafNode(typeof(String), "name", ""));
74 Assert.AreEqual(null, binder.BindParameter(typeof(int), "name", node));
77 [Test, ExpectedException(typeof(BindingException), ExpectedMessage = "Exception converting param 'name' to System.Int32. Check inner exception for details")]
78 public void SimpleBinding_Int_Invalid()
80 CompositeNode node = new CompositeNode("unnamed");
81 node.AddChildNode(new LeafNode(typeof(String), "name", long.MaxValue.ToString()));
82 binder.BindParameter(typeof(int), "name", node);
85 [Test]
86 public void SimpleBinding_Decimal()
88 CompositeNode node = new CompositeNode("unnamed");
89 node.AddChildNode(new LeafNode(typeof(String), "name", "12.2"));
90 Assert.AreEqual((decimal)12.2, binder.BindParameter(typeof(decimal), "name", node));
92 node = new CompositeNode("unnamed");
93 node.AddChildNode(new LeafNode(typeof(double), "name", 12.2));
94 Assert.AreEqual((decimal)12.2, binder.BindParameter(typeof(decimal), "name", node));
96 node = new CompositeNode("unnamed");
97 node.AddChildNode(new LeafNode(typeof(float), "name", 12.2f));
98 Assert.AreEqual((decimal)12.2, binder.BindParameter(typeof(decimal), "name", node));
100 node = new CompositeNode("unnamed");
101 node.AddChildNode(new LeafNode(typeof(String), "name", ""));
102 Assert.AreEqual(null, binder.BindParameter(typeof(decimal), "name", node));
105 [Test]
106 public void SimpleBinding_Bool()
108 CompositeNode node = new CompositeNode("unnamed");
109 node.AddChildNode(new LeafNode(typeof(String), "name", "1"));
110 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
112 node = new CompositeNode("unnamed");
113 node.AddChildNode(new LeafNode(typeof(String), "name", "0"));
114 Assert.AreEqual(false, binder.BindParameter(typeof(bool), "name", node));
116 node = new CompositeNode("unnamed");
117 node.AddChildNode(new LeafNode(typeof(String[]), "name", new string[] { "1", "0" }));
118 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
120 node = new CompositeNode("unnamed");
121 node.AddChildNode(new LeafNode(typeof(String[]), "name", new string[] { "0", "0" }));
122 Assert.AreEqual(false, binder.BindParameter(typeof(bool), "name", node));
124 node = new CompositeNode("unnamed");
125 node.AddChildNode(new LeafNode(typeof(String), "name", "yes"));
126 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
128 node = new CompositeNode("unnamed");
129 node.AddChildNode(new LeafNode(typeof(String), "name", "true"));
130 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
132 node = new CompositeNode("unnamed");
133 node.AddChildNode(new LeafNode(typeof(String), "name", "false"));
134 Assert.AreEqual(false, binder.BindParameter(typeof(bool), "name", node));
136 node = new CompositeNode("unnamed");
137 node.AddChildNode(new LeafNode(typeof(int), "name", 1));
138 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
140 node = new CompositeNode("unnamed");
141 node.AddChildNode(new LeafNode(typeof(String), "name", ""));
142 Assert.AreEqual(false, binder.BindParameter(typeof(bool), "name", node));
144 node = new CompositeNode("unnamed");
145 Assert.AreEqual(null, binder.BindParameter(typeof(bool), "name", node));
148 [Test]
149 public void SimpleBinding_String()
151 CompositeNode node = new CompositeNode("unnamed");
152 node.AddChildNode(new LeafNode(typeof(String), "name", "hammett"));
153 Assert.AreEqual("hammett", binder.BindParameter(typeof(String), "name", node));
156 [Test]
157 public void SimpleBindingWithEmptyNode()
159 CompositeNode node = new CompositeNode("unnamed");
160 Assert.AreEqual(null, binder.BindParameter(typeof(String), "name", node));
162 Assert.AreEqual(null, binder.BindParameter(typeof(long), "name", node));
164 Assert.AreEqual(null, binder.BindParameter(typeof(int), "name", node));
166 Assert.AreEqual(null, binder.BindParameter(typeof(float), "name", node));
169 [Test]
170 public void ArrayBindingWithSimpleEntries()
172 CompositeNode node = new CompositeNode("unnamed");
173 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] {"1", "2"}));
174 Assert.AreEqual(new String[] {"1", "2"}, binder.BindParameter(typeof(String[]), "name", node));
176 node = new CompositeNode("unnamed");
177 node.AddChildNode(new LeafNode(typeof(String), "name", "1"));
178 Assert.AreEqual(new String[] {"1"}, binder.BindParameter(typeof(String[]), "name", node));
180 node = new CompositeNode("unnamed");
181 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] {"1", "2"}));
182 Assert.AreEqual(new int[] {1, 2}, binder.BindParameter(typeof(int[]), "name", node));
184 node = new CompositeNode("unnamed");
185 node.AddChildNode(new LeafNode(typeof(String), "name", "1"));
186 Assert.AreEqual(new int[] {1}, binder.BindParameter(typeof(int[]), "name", node));
189 [Test]
190 public void ArrayBindingWithIndexedNodes()
192 CompositeNode node = new CompositeNode("unnamed");
193 IndexedNode indexNode = new IndexedNode("emails");
194 node.AddChildNode(indexNode);
195 indexNode.AddChildNode(new LeafNode(typeof(String), "", "e1"));
196 indexNode.AddChildNode(new LeafNode(typeof(String), "", "e2"));
197 Assert.AreEqual(new String[] {"e1", "e2"}, binder.BindParameter(typeof(String[]), "emails", node));
200 /// <summary>
201 /// Tests dates passed as whole values (month/day/year)
202 /// </summary>
203 [Test]
204 public void DateTimeArrayBinding()
206 CompositeNode node = new CompositeNode("unnamed");
207 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] {"03/09/2006", "02/08/2006"}));
208 Assert.AreEqual(new DateTime[] {new DateTime(2006, 03, 09), new DateTime(2006, 02, 08)},
209 binder.BindParameter(typeof(DateTime[]), "name", node));
211 node = new CompositeNode("unnamed");
212 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] {"03/09/2006"}));
213 Assert.AreEqual(new DateTime[] {new DateTime(2006, 03, 09)},
214 binder.BindParameter(typeof(DateTime[]), "name", node));
216 node = new CompositeNode("unnamed");
217 Assert.AreEqual(new DateTime[0],
218 binder.BindParameter(typeof(DateTime[]), "name", node));
221 /// <summary>
222 /// Tests dates passed as 'paramname'day, 'paramname'month, 'paramname'year
223 /// </summary>
224 [Test]
225 public void DateTimeAlternativeSourceBinding()
227 CompositeNode node = new CompositeNode("unnamed");
229 node.AddChildNode(new LeafNode(typeof(String), "nameday", "09"));
230 node.AddChildNode(new LeafNode(typeof(String), "namemonth", "03"));
231 node.AddChildNode(new LeafNode(typeof(String), "nameyear", "2006"));
233 Assert.AreEqual(new DateTime(2006, 03, 09), binder.BindParameter(typeof(DateTime), "name", node));
235 node = new CompositeNode("unnamed");
237 node.AddChildNode(new LeafNode(typeof(int), "nameday", 9));
238 node.AddChildNode(new LeafNode(typeof(int), "namemonth", 3));
239 node.AddChildNode(new LeafNode(typeof(int), "nameyear", 2006));
241 Assert.AreEqual(new DateTime(2006, 03, 09), binder.BindParameter(typeof(DateTime), "name", node));
244 [Test]
245 public void DateTimeAlternativeSourceBindingWithNullableDateTime()
247 CompositeNode node = new CompositeNode("unnamed");
249 node.AddChildNode(new LeafNode(typeof(String), "nameday", "09"));
250 node.AddChildNode(new LeafNode(typeof(String), "namemonth", "03"));
251 node.AddChildNode(new LeafNode(typeof(String), "nameyear", "2006"));
253 NullableDateTime expected = new NullableDateTime(new DateTime(2006, 03, 09));
254 NullableDateTime actual = (NullableDateTime) binder.BindParameter(typeof(NullableDateTime), "name", node);
256 Assert.IsTrue(actual.HasValue);
257 Assert.AreEqual(expected.Value.Year, actual.Value.Year);
258 Assert.AreEqual(expected.Value.Month, actual.Value.Month);
259 Assert.AreEqual(expected.Value.Day, actual.Value.Day);
261 node = new CompositeNode("unnamed");
263 node.AddChildNode(new LeafNode(typeof(int), "nameday", 9));
264 node.AddChildNode(new LeafNode(typeof(int), "namemonth", 3));
265 node.AddChildNode(new LeafNode(typeof(int), "nameyear", 2006));
267 expected = new NullableDateTime(new DateTime(2006, 03, 09));
268 actual = (NullableDateTime)binder.BindParameter(typeof(NullableDateTime), "name", node);
270 Assert.IsTrue(actual.HasValue);
271 Assert.AreEqual(expected.Value.Year, actual.Value.Year);
272 Assert.AreEqual(expected.Value.Month, actual.Value.Month);
273 Assert.AreEqual(expected.Value.Day, actual.Value.Day);
276 [Test]
277 public void DateTimeAlternativeSourceBindingWithNullableDateTime2()
279 CompositeNode node = new CompositeNode("unnamed");
281 node.AddChildNode(new LeafNode(typeof(String), "nameday", "09"));
282 node.AddChildNode(new LeafNode(typeof(String), "namemonth", "03"));
283 node.AddChildNode(new LeafNode(typeof(String), "nameyear", "2006"));
285 Assert.AreEqual(new DateTime?(new DateTime(2006, 03, 09)),
286 binder.BindParameter(typeof(DateTime?), "name", node));
288 node = new CompositeNode("unnamed");
290 node.AddChildNode(new LeafNode(typeof(int), "nameday", 9));
291 node.AddChildNode(new LeafNode(typeof(int), "namemonth", 3));
292 node.AddChildNode(new LeafNode(typeof(int), "nameyear", 2006));
294 Assert.AreEqual(new DateTime?(new DateTime(2006, 03, 09)),
295 binder.BindParameter(typeof(DateTime?), "name", node));
298 /// <summary>
299 /// Common Enum convertion
300 /// </summary>
301 [Test]
302 public void EnumSourceBinding()
304 CompositeNode node = new CompositeNode("unnamed");
305 node.AddChildNode(new LeafNode(typeof(FileAccess), "name", FileAccess.Read));
306 Assert.AreEqual(FileAccess.Read, binder.BindParameter(typeof(FileAccess), "name", node));
308 node = new CompositeNode("unnamed");
309 node.AddChildNode(new LeafNode(typeof(String), "name", "Read"));
310 Assert.AreEqual(FileAccess.Read, binder.BindParameter(typeof(FileAccess), "name", node));
312 node = new CompositeNode("unnamed");
313 node.AddChildNode(new LeafNode(typeof(String), "name", "2"));
314 Assert.AreEqual(FileAccess.Write, binder.BindParameter(typeof(FileAccess), "name", node));
316 node = new CompositeNode("unnamed");
317 node.AddChildNode(new LeafNode(typeof(int), "name", 1));
318 Assert.AreEqual(FileAccess.Read, binder.BindParameter(typeof(FileAccess), "name", node));
320 node = new CompositeNode("unnamed");
321 node.AddChildNode(new LeafNode(typeof(int), "name", 2));
322 Assert.AreEqual(FileAccess.Write, binder.BindParameter(typeof(FileAccess), "name", node));
324 node = new CompositeNode("unnamed");
325 node.AddChildNode(new LeafNode(typeof(String), "name", ""));
326 Assert.AreEqual(null, binder.BindParameter(typeof(FileAccess), "name", node));
329 /// <summary>
330 /// Enum Flags convertion
331 /// </summary>
332 [Test]
333 public void EnumSourceFlagsBinding()
335 CompositeNode node = new CompositeNode("unnamed");
336 node.AddChildNode(new LeafNode(typeof(FileAttributes), "name", FileAttributes.Device|FileAttributes.Directory));
337 Assert.AreEqual(FileAttributes.Device|FileAttributes.Directory, binder.BindParameter(typeof(FileAttributes), "name", node));
339 node = new CompositeNode("unnamed");
340 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] { "Device", "Directory" }));
341 Assert.AreEqual(FileAttributes.Device|FileAttributes.Directory, binder.BindParameter(typeof(FileAttributes), "name", node));