Minor changes:
[castle.git] / Components / Binder / Castle.Components.Binder.Tests / DataBinderSingleValueTestCase.cs
blob5f60330aba9bf3f79130579c7d482e13999e665a
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();
34 CultureInfo en = CultureInfo.CreateSpecificCulture("en");
36 Thread.CurrentThread.CurrentCulture = en;
37 Thread.CurrentThread.CurrentUICulture = en;
40 [Test]
41 public void ArrayBindingWithEmptyNode()
43 CompositeNode node = new CompositeNode("unnamed");
44 Assert.AreEqual(new String[0], binder.BindParameter(typeof(String[]), "name", node));
46 Assert.AreEqual(new String[0], binder.BindParameter(typeof(String[]), "name", node));
48 Assert.AreEqual(new int[0], binder.BindParameter(typeof(int[]), "name", node));
50 Assert.AreEqual(new int[0], binder.BindParameter(typeof(int[]), "name", node));
53 [Test]
54 public void SimpleBinding_Int()
56 CompositeNode node = new CompositeNode("unnamed");
57 node.AddChildNode(new LeafNode(typeof(String), "name", "200"));
58 Assert.AreEqual(200, binder.BindParameter(typeof(int), "name", node));
60 node = new CompositeNode("unnamed");
61 node.AddChildNode(new LeafNode(typeof(int), "name", 200));
62 Assert.AreEqual(200, binder.BindParameter(typeof(int), "name", node));
64 node = new CompositeNode("unnamed");
65 node.AddChildNode(new LeafNode(typeof(float), "name", 200.1f));
66 Assert.AreEqual(200, binder.BindParameter(typeof(int), "name", node));
68 node = new CompositeNode("unnamed");
69 node.AddChildNode(new LeafNode(typeof(String), "name", ""));
70 Assert.AreEqual(null, binder.BindParameter(typeof(int), "name", node));
73 [Test, ExpectedException(typeof(BindingException), ExpectedMessage = "Exception converting param 'name' to System.Int32. Check inner exception for details")]
74 public void SimpleBinding_Int_Invalid()
76 CompositeNode node = new CompositeNode("unnamed");
77 node.AddChildNode(new LeafNode(typeof(String), "name", long.MaxValue.ToString()));
78 binder.BindParameter(typeof(int), "name", node);
81 [Test]
82 public void SimpleBinding_Decimal()
84 CompositeNode node = new CompositeNode("unnamed");
85 node.AddChildNode(new LeafNode(typeof(String), "name", "12.2"));
86 Assert.AreEqual((decimal)12.2, binder.BindParameter(typeof(decimal), "name", node));
88 node = new CompositeNode("unnamed");
89 node.AddChildNode(new LeafNode(typeof(double), "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(float), "name", 12.2f));
94 Assert.AreEqual((decimal)12.2, binder.BindParameter(typeof(decimal), "name", node));
96 node = new CompositeNode("unnamed");
97 node.AddChildNode(new LeafNode(typeof(String), "name", ""));
98 Assert.AreEqual(null, binder.BindParameter(typeof(decimal), "name", node));
101 [Test]
102 public void SimpleBinding_Bool()
104 CompositeNode node = new CompositeNode("unnamed");
105 node.AddChildNode(new LeafNode(typeof(String), "name", "1"));
106 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
108 node = new CompositeNode("unnamed");
109 node.AddChildNode(new LeafNode(typeof(String), "name", "0"));
110 Assert.AreEqual(false, binder.BindParameter(typeof(bool), "name", node));
112 node = new CompositeNode("unnamed");
113 node.AddChildNode(new LeafNode(typeof(String[]), "name", new string[] { "1", "0" }));
114 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
116 node = new CompositeNode("unnamed");
117 node.AddChildNode(new LeafNode(typeof(String[]), "name", new string[] { "0", "0" }));
118 Assert.AreEqual(false, binder.BindParameter(typeof(bool), "name", node));
120 node = new CompositeNode("unnamed");
121 node.AddChildNode(new LeafNode(typeof(String), "name", "yes"));
122 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
124 node = new CompositeNode("unnamed");
125 node.AddChildNode(new LeafNode(typeof(String), "name", "true"));
126 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
128 node = new CompositeNode("unnamed");
129 node.AddChildNode(new LeafNode(typeof(String), "name", "false"));
130 Assert.AreEqual(false, binder.BindParameter(typeof(bool), "name", node));
132 node = new CompositeNode("unnamed");
133 node.AddChildNode(new LeafNode(typeof(int), "name", 1));
134 Assert.AreEqual(true, binder.BindParameter(typeof(bool), "name", node));
136 node = new CompositeNode("unnamed");
137 node.AddChildNode(new LeafNode(typeof(String), "name", ""));
138 Assert.AreEqual(false, binder.BindParameter(typeof(bool), "name", node));
140 node = new CompositeNode("unnamed");
141 Assert.AreEqual(null, binder.BindParameter(typeof(bool), "name", node));
144 [Test]
145 public void SimpleBinding_String()
147 CompositeNode node = new CompositeNode("unnamed");
148 node.AddChildNode(new LeafNode(typeof(String), "name", "hammett"));
149 Assert.AreEqual("hammett", binder.BindParameter(typeof(String), "name", node));
152 [Test]
153 public void SimpleBindingWithEmptyNode()
155 CompositeNode node = new CompositeNode("unnamed");
156 Assert.AreEqual(null, binder.BindParameter(typeof(String), "name", node));
158 Assert.AreEqual(null, binder.BindParameter(typeof(long), "name", node));
160 Assert.AreEqual(null, binder.BindParameter(typeof(int), "name", node));
162 Assert.AreEqual(null, binder.BindParameter(typeof(float), "name", node));
165 [Test]
166 public void ArrayBindingWithSimpleEntries()
168 CompositeNode node = new CompositeNode("unnamed");
169 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] {"1", "2"}));
170 Assert.AreEqual(new String[] {"1", "2"}, binder.BindParameter(typeof(String[]), "name", node));
172 node = new CompositeNode("unnamed");
173 node.AddChildNode(new LeafNode(typeof(String), "name", "1"));
174 Assert.AreEqual(new String[] {"1"}, binder.BindParameter(typeof(String[]), "name", node));
176 node = new CompositeNode("unnamed");
177 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] {"1", "2"}));
178 Assert.AreEqual(new int[] {1, 2}, binder.BindParameter(typeof(int[]), "name", node));
180 node = new CompositeNode("unnamed");
181 node.AddChildNode(new LeafNode(typeof(String), "name", "1"));
182 Assert.AreEqual(new int[] {1}, binder.BindParameter(typeof(int[]), "name", node));
185 [Test]
186 public void ArrayBindingWithIndexedNodes()
188 CompositeNode node = new CompositeNode("unnamed");
189 IndexedNode indexNode = new IndexedNode("emails");
190 node.AddChildNode(indexNode);
191 indexNode.AddChildNode(new LeafNode(typeof(String), "", "e1"));
192 indexNode.AddChildNode(new LeafNode(typeof(String), "", "e2"));
193 Assert.AreEqual(new String[] {"e1", "e2"}, binder.BindParameter(typeof(String[]), "emails", node));
196 /// <summary>
197 /// Tests dates passed as whole values (month/day/year)
198 /// </summary>
199 [Test]
200 public void DateTimeArrayBinding()
202 CompositeNode node = new CompositeNode("unnamed");
203 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] {"03/09/2006", "02/08/2006"}));
204 Assert.AreEqual(new DateTime[] {new DateTime(2006, 03, 09), new DateTime(2006, 02, 08)},
205 binder.BindParameter(typeof(DateTime[]), "name", node));
207 node = new CompositeNode("unnamed");
208 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] {"03/09/2006"}));
209 Assert.AreEqual(new DateTime[] {new DateTime(2006, 03, 09)},
210 binder.BindParameter(typeof(DateTime[]), "name", node));
212 node = new CompositeNode("unnamed");
213 Assert.AreEqual(new DateTime[0],
214 binder.BindParameter(typeof(DateTime[]), "name", node));
217 /// <summary>
218 /// Tests dates passed as 'paramname'day, 'paramname'month, 'paramname'year
219 /// </summary>
220 [Test]
221 public void DateTimeAlternativeSourceBinding()
223 CompositeNode node = new CompositeNode("unnamed");
225 node.AddChildNode(new LeafNode(typeof(String), "nameday", "09"));
226 node.AddChildNode(new LeafNode(typeof(String), "namemonth", "03"));
227 node.AddChildNode(new LeafNode(typeof(String), "nameyear", "2006"));
229 Assert.AreEqual(new DateTime(2006, 03, 09), binder.BindParameter(typeof(DateTime), "name", node));
231 node = new CompositeNode("unnamed");
233 node.AddChildNode(new LeafNode(typeof(int), "nameday", 9));
234 node.AddChildNode(new LeafNode(typeof(int), "namemonth", 3));
235 node.AddChildNode(new LeafNode(typeof(int), "nameyear", 2006));
237 Assert.AreEqual(new DateTime(2006, 03, 09), binder.BindParameter(typeof(DateTime), "name", node));
240 [Test]
241 public void DateTimeAlternativeSourceBindingWithNullableDateTime()
243 CompositeNode node = new CompositeNode("unnamed");
245 node.AddChildNode(new LeafNode(typeof(String), "nameday", "09"));
246 node.AddChildNode(new LeafNode(typeof(String), "namemonth", "03"));
247 node.AddChildNode(new LeafNode(typeof(String), "nameyear", "2006"));
249 NullableDateTime expected = new NullableDateTime(new DateTime(2006, 03, 09));
250 NullableDateTime actual = (NullableDateTime) binder.BindParameter(typeof(NullableDateTime), "name", node);
252 Assert.IsTrue(actual.HasValue);
253 Assert.AreEqual(expected.Value.Year, actual.Value.Year);
254 Assert.AreEqual(expected.Value.Month, actual.Value.Month);
255 Assert.AreEqual(expected.Value.Day, actual.Value.Day);
257 node = new CompositeNode("unnamed");
259 node.AddChildNode(new LeafNode(typeof(int), "nameday", 9));
260 node.AddChildNode(new LeafNode(typeof(int), "namemonth", 3));
261 node.AddChildNode(new LeafNode(typeof(int), "nameyear", 2006));
263 expected = new NullableDateTime(new DateTime(2006, 03, 09));
264 actual = (NullableDateTime)binder.BindParameter(typeof(NullableDateTime), "name", node);
266 Assert.IsTrue(actual.HasValue);
267 Assert.AreEqual(expected.Value.Year, actual.Value.Year);
268 Assert.AreEqual(expected.Value.Month, actual.Value.Month);
269 Assert.AreEqual(expected.Value.Day, actual.Value.Day);
272 [Test]
273 public void DateTimeAlternativeSourceBindingWithNullableDateTime2()
275 CompositeNode node = new CompositeNode("unnamed");
277 node.AddChildNode(new LeafNode(typeof(String), "nameday", "09"));
278 node.AddChildNode(new LeafNode(typeof(String), "namemonth", "03"));
279 node.AddChildNode(new LeafNode(typeof(String), "nameyear", "2006"));
281 Assert.AreEqual(new DateTime?(new DateTime(2006, 03, 09)),
282 binder.BindParameter(typeof(DateTime?), "name", node));
284 node = new CompositeNode("unnamed");
286 node.AddChildNode(new LeafNode(typeof(int), "nameday", 9));
287 node.AddChildNode(new LeafNode(typeof(int), "namemonth", 3));
288 node.AddChildNode(new LeafNode(typeof(int), "nameyear", 2006));
290 Assert.AreEqual(new DateTime?(new DateTime(2006, 03, 09)),
291 binder.BindParameter(typeof(DateTime?), "name", node));
294 /// <summary>
295 /// Common Enum convertion
296 /// </summary>
297 [Test]
298 public void EnumSourceBinding()
300 CompositeNode node = new CompositeNode("unnamed");
301 node.AddChildNode(new LeafNode(typeof(FileAccess), "name", FileAccess.Read));
302 Assert.AreEqual(FileAccess.Read, binder.BindParameter(typeof(FileAccess), "name", node));
304 node = new CompositeNode("unnamed");
305 node.AddChildNode(new LeafNode(typeof(String), "name", "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", "2"));
310 Assert.AreEqual(FileAccess.Write, binder.BindParameter(typeof(FileAccess), "name", node));
312 node = new CompositeNode("unnamed");
313 node.AddChildNode(new LeafNode(typeof(int), "name", 1));
314 Assert.AreEqual(FileAccess.Read, binder.BindParameter(typeof(FileAccess), "name", node));
316 node = new CompositeNode("unnamed");
317 node.AddChildNode(new LeafNode(typeof(int), "name", 2));
318 Assert.AreEqual(FileAccess.Write, binder.BindParameter(typeof(FileAccess), "name", node));
320 node = new CompositeNode("unnamed");
321 node.AddChildNode(new LeafNode(typeof(String), "name", ""));
322 Assert.AreEqual(null, binder.BindParameter(typeof(FileAccess), "name", node));
325 /// <summary>
326 /// Enum Flags convertion
327 /// </summary>
328 [Test]
329 public void EnumSourceFlagsBinding()
331 CompositeNode node = new CompositeNode("unnamed");
332 node.AddChildNode(new LeafNode(typeof(FileAttributes), "name", FileAttributes.Device|FileAttributes.Directory));
333 Assert.AreEqual(FileAttributes.Device|FileAttributes.Directory, binder.BindParameter(typeof(FileAttributes), "name", node));
335 node = new CompositeNode("unnamed");
336 node.AddChildNode(new LeafNode(typeof(String[]), "name", new String[] { "Device", "Directory" }));
337 Assert.AreEqual(FileAttributes.Device|FileAttributes.Directory, binder.BindParameter(typeof(FileAttributes), "name", node));