1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
.Collections
.Specialized
;
18 using System
.Globalization
;
19 using System
.Threading
;
20 using NUnit
.Framework
;
23 public class DataBinderArrayTestCase
25 private IDataBinder binder
;
26 private TreeBuilder builder
;
31 binder
= new DataBinder();
32 builder
= new TreeBuilder();
34 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
36 Thread
.CurrentThread
.CurrentCulture
= en
;
37 Thread
.CurrentThread
.CurrentUICulture
= en
;
41 public void SimpleArrayDataBind()
50 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
52 object instance
= binder
.BindObject(typeof(Person
[]), "Person", builder
.BuildSourceNode(args
));
54 Assert
.IsNotNull(instance
);
55 Person
[] sc
= instance
as Person
[];
57 Assert
.IsTrue(sc
.Length
== 2);
58 Assert
.AreEqual(sc
[0].Age
, 32);
59 Assert
.AreEqual(sc
[0].Name
, "John");
60 Assert
.AreEqual(sc
[1].Age
, 16);
61 Assert
.AreEqual(sc
[1].Name
, "Mary");
65 /// Binds to array with an indexed node
68 public void SimpleArrayDataBind2()
77 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
79 object instance
= binder
.BindObject(typeof(Person
), "person", builder
.BuildSourceNode(args
));
81 Assert
.IsNotNull(instance
);
82 Person person
= instance
as Person
;
83 Assert
.IsNotNull(person
);
84 Assert
.IsNotNull(person
.Months
);
85 Assert
.AreEqual(4, person
.Months
.Length
);
86 Assert
.AreEqual(10, person
.Months
[0]);
87 Assert
.AreEqual(20, person
.Months
[1]);
88 Assert
.AreEqual(30, person
.Months
[2]);
89 Assert
.AreEqual(40, person
.Months
[3]);
93 public void SimpleArrayDataBind4()
102 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
104 object instance
= binder
.BindObject(typeof(Person
), "person", builder
.BuildSourceNode(args
));
106 Assert
.IsNotNull(instance
);
107 Person person
= instance
as Person
;
108 Assert
.IsNotNull(person
);
109 Assert
.IsNotNull(person
.Months
);
110 Assert
.AreEqual(4, person
.Months
.Length
);
111 Assert
.AreEqual(10, person
.Months
[0]);
112 Assert
.AreEqual(20, person
.Months
[1]);
113 Assert
.AreEqual(30, person
.Months
[2]);
114 Assert
.AreEqual(40, person
.Months
[3]);
118 public void NestedArrayBinding()
121 cust.addresses[0].street = st1
122 cust.addresses[0].number = 1
123 cust.addresses[1].street = st2
124 cust.addresses[1].number = 2
127 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
129 object instance
= binder
.BindObject(typeof(Customer
), "cust", builder
.BuildSourceNode(args
));
131 Assert
.IsNotNull(instance
);
132 Customer cust
= instance
as Customer
;
133 Assert
.IsNotNull(cust
);
134 Assert
.IsNotNull(cust
.Addresses
);
135 Assert
.AreEqual(1, cust
.Addresses
[0].Number
);
136 Assert
.AreEqual(2, cust
.Addresses
[1].Number
);
137 Assert
.AreEqual("st1", cust
.Addresses
[0].Street
);
138 Assert
.AreEqual("st2", cust
.Addresses
[1].Street
);
142 public void IndexedArray()
151 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
153 object instance
= binder
.BindObject(typeof(Person
[]), "abc", builder
.BuildSourceNode(args
));
155 Assert
.IsNotNull(instance
);
156 Person
[] sc
= instance
as Person
[];
158 Assert
.IsNotNull(sc
);
159 Assert
.IsTrue(sc
.Length
== 2);
161 Assert
.AreEqual(32, sc
[0].Age
);
162 Assert
.AreEqual("John", sc
[0].Name
);
163 Assert
.AreEqual(16, sc
[1].Age
);
164 Assert
.AreEqual("Mary", sc
[1].Name
);
168 public void CanHandleProtoTypeSimpleArray()
170 string data
= @"abc[]=Foo,Bar";
171 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
172 object instance
= binder
.BindObject(typeof(string[]), "abc", builder
.BuildSourceNode(args
));
173 Assert
.IsNotNull(instance
);
174 string[] sc
= instance
as string[];
175 Assert
.IsNotNull(sc
);
176 Assert
.AreEqual(2, sc
.Length
);
177 Assert
.AreEqual("Foo", sc
[0]);
178 Assert
.AreEqual("Bar", sc
[1]);
182 public void CanHandleProtoTypeSimpleArrayWhenOnlyOneElementIsThere()
184 string data
= @"abc[]=Foo";
185 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
186 object instance
= binder
.BindObject(typeof(string[]), "abc", builder
.BuildSourceNode(args
));
187 Assert
.IsNotNull(instance
);
188 string[] sc
= instance
as string[];
189 Assert
.IsNotNull(sc
);
190 Assert
.AreEqual(1, sc
.Length
);
191 Assert
.AreEqual("Foo", sc
[0]);
195 public void CanHandleProtoTypeSimpleArrayWhenItIsEmpty()
197 NameValueCollection args
= new NameValueCollection();
198 args
.Add("abc[]", "");
199 object instance
= binder
.BindObject(typeof(string[]), "abc", builder
.BuildSourceNode(args
));
200 Assert
.IsNotNull(instance
);
201 string[] sc
= instance
as string[];
202 Assert
.IsNotNull(sc
);
203 Assert
.AreEqual(0, sc
.Length
);
207 public void CanHandleProtoTypeSimpleArrayWithoutSplittingWhenEmpty()
209 NameValueCollection args
= new NameValueCollection();
210 args
.Add("abc[]", null);
211 object instance
= binder
.BindObject(typeof(string[]), "abc", builder
.BuildSourceNode(args
));
212 Assert
.IsNotNull(instance
as string[]);
213 string[] sc
= instance
as string[];
214 Assert
.IsNotNull(sc
);
215 Assert
.AreEqual(0, sc
.Length
);
219 public void CanHandleProtoTypeSimpleArrayWithoutSplitting()
221 NameValueCollection args
= new NameValueCollection();
222 args
.Add("abc[]", "foo");
223 args
.Add("abc[]", "bar");
224 object instance
= binder
.BindObject(typeof(string[]), "abc", builder
.BuildSourceNode(args
));
225 Assert
.IsNotNull(instance
as string[]);
226 string[] sc
= instance
as string[];
227 Assert
.IsNotNull(sc
);
228 Assert
.AreEqual(2, sc
.Length
);
229 Assert
.AreEqual("foo", sc
[0]);
230 Assert
.AreEqual("bar", sc
[1]);
234 public void CanHandleProtoTypeSimpleArrayWithoutSplittingWithOneElement()
236 NameValueCollection args
= new NameValueCollection();
237 args
.Add("abc[]", "foo");
238 object instance
= binder
.BindObject(typeof(string[]), "abc", builder
.BuildSourceNode(args
));
239 Assert
.IsNotNull(instance
as string[]);
240 string[] sc
= instance
as string[];
241 Assert
.IsNotNull(sc
);
242 Assert
.AreEqual(1, sc
.Length
);
243 Assert
.AreEqual("foo", sc
[0]);
246 [Test
, Ignore("Behavior changed")] // Expected Exception
247 public void InvalidData()
250 person.Months[1] = 10
251 person.Months[2].Id = 20
252 person.Months[3].Name = 30
253 person.Months[4].Some = 40
256 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
258 binder
.BindObject(typeof(Person
), "person", builder
.BuildSourceNode(args
));