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.
14 namespace Castle
.Components
.Binder
.Tests
16 using System
.Collections
.Generic
;
17 using System
.Collections
.Specialized
;
18 using System
.Globalization
;
19 using System
.Threading
;
20 using NUnit
.Framework
;
23 public class DataBinderGenericListTestCase
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 SimpleBind()
50 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
52 object instance
= binder
.BindObject(typeof(List
<int>), "months", builder
.BuildSourceNode(args
));
54 Assert
.IsNotNull(instance
);
55 List
<int> result
= instance
as List
<int>;
56 Assert
.IsNotNull(result
);
58 Assert
.IsNotNull(result
);
59 Assert
.AreEqual(4, result
.Count
);
61 Assert
.AreEqual(1, result
[0]);
62 Assert
.AreEqual(2, result
[1]);
63 Assert
.AreEqual(3, result
[2]);
64 Assert
.AreEqual(4, result
[3]);
68 public void SimpleBind2()
77 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
79 object instance
= binder
.BindObject(typeof(Customer2
), "cust", builder
.BuildSourceNode(args
));
81 Assert
.IsNotNull(instance
);
82 Customer2 cust
= instance
as Customer2
;
83 Assert
.IsNotNull(cust
);
85 Assert
.IsNotNull(cust
.Months
);
86 Assert
.AreEqual(4, cust
.Months
.Count
);
88 Assert
.AreEqual(1, cust
.Months
[0]);
89 Assert
.AreEqual(2, cust
.Months
[1]);
90 Assert
.AreEqual(3, cust
.Months
[2]);
91 Assert
.AreEqual(4, cust
.Months
[3]);
95 public void NestedBind()
98 cust.addresses[0].street = st1
99 cust.addresses[0].number = 1
100 cust.addresses[1].street = st2
101 cust.addresses[1].number = 2
104 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
106 object instance
= binder
.BindObject(typeof(Customer2
), "cust", builder
.BuildSourceNode(args
));
108 Assert
.IsNotNull(instance
);
109 Customer2 cust
= instance
as Customer2
;
110 Assert
.IsNotNull(cust
);
111 Assert
.IsNotNull(cust
.Addresses
);
112 Assert
.AreEqual(2, cust
.Addresses
.Count
);
113 Assert
.AreEqual(1, cust
.Addresses
[0].Number
);
114 Assert
.AreEqual(2, cust
.Addresses
[1].Number
);
115 Assert
.AreEqual("st1", cust
.Addresses
[0].Street
);
116 Assert
.AreEqual("st2", cust
.Addresses
[1].Street
);
120 public void InstantiatedListNestedBind()
123 cust.addresses1[0].street = st1
124 cust.addresses1[0].number = 1
125 cust.addresses1[1].street = st2
126 cust.addresses1[1].number = 2
129 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
131 object instance
= binder
.BindObject(typeof(Customer2
), "cust", builder
.BuildSourceNode(args
));
133 Assert
.IsNotNull(instance
);
134 Customer2 cust
= instance
as Customer2
;
135 Assert
.IsNotNull(cust
);
136 Assert
.IsNotNull(cust
.Addresses1
);
137 Assert
.AreEqual(2, cust
.Addresses1
.Count
);
138 Assert
.AreEqual(1, cust
.Addresses1
[0].Number
);
139 Assert
.AreEqual(2, cust
.Addresses1
[1].Number
);
140 Assert
.AreEqual("st1", cust
.Addresses1
[0].Street
);
141 Assert
.AreEqual("st2", cust
.Addresses1
[1].Street
);
145 public void IListNestedBind()
148 cust.addresses2[0].street = st1
149 cust.addresses2[0].number = 1
150 cust.addresses2[1].street = st2
151 cust.addresses2[1].number = 2
154 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
156 object instance
= binder
.BindObject(typeof(Customer2
), "cust", builder
.BuildSourceNode(args
));
158 Assert
.IsNotNull(instance
);
159 Customer2 cust
= instance
as Customer2
;
160 Assert
.IsNotNull(cust
);
161 Assert
.IsNotNull(cust
.Addresses2
);
162 Assert
.AreEqual(2, cust
.Addresses2
.Count
);
163 Assert
.AreEqual(1, cust
.Addresses2
[0].Number
);
164 Assert
.AreEqual(2, cust
.Addresses2
[1].Number
);
165 Assert
.AreEqual("st1", cust
.Addresses2
[0].Street
);
166 Assert
.AreEqual("st2", cust
.Addresses2
[1].Street
);
170 public void IListBind()
179 NameValueCollection args
= TestUtils
.ParseNameValueString(data
);
181 object instance
= binder
.BindObject(typeof(Customer2
), "cust", builder
.BuildSourceNode(args
));
183 Assert
.IsNotNull(instance
);
184 Customer2 cust
= instance
as Customer2
;
185 Assert
.IsNotNull(cust
);
187 Assert
.IsNotNull(cust
.Years
);
188 Assert
.AreEqual(4, cust
.Years
.Count
);
190 Assert
.AreEqual(2006, cust
.Years
[0]);
191 Assert
.AreEqual(2007, cust
.Years
[1]);
192 Assert
.AreEqual(2008, cust
.Years
[2]);
193 Assert
.AreEqual(2009, cust
.Years
[3]);
199 private List
<Address
> addresses
;
201 private List
<Address
> addresses1
= new List
<Address
>();
203 private IList
<Address
> addresses2
;
205 private List
<int> months
;
207 private List
<int> years
;
209 public List
<Address
> Addresses
211 get { return addresses; }
212 set { addresses = value; }
215 public List
<Address
> Addresses1
217 get { return addresses1; }
218 set { addresses1 = value; }
221 public IList
<Address
> Addresses2
223 get { return addresses2; }
224 set { addresses2 = value; }
227 public List
<int> Months
229 get { return months; }
230 set { months = value; }
233 public IList
<int> Years
235 get { return years; }
236 set { years = new List<int>(value); }