1 // Copyright 2004-2007 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
.MonoRail
.Framework
.Tests
.Helpers
18 using System
.Collections
;
19 using System
.Collections
.Generic
;
21 using System
.Globalization
;
23 using System
.Threading
;
25 using Castle
.MonoRail
.Framework
.Helpers
;
26 using Castle
.MonoRail
.Framework
.Tests
.Controllers
;
28 using NUnit
.Framework
;
31 public class FormHelperSelectTestCase
33 private FormHelper helper
;
34 private Product product
;
35 private SimpleUser user
;
36 private Subscription subscription
;
37 private Month
[] months
;
38 private Contact contact
;
39 private DataTable workTable
;
44 CultureInfo en
= CultureInfo
.CreateSpecificCulture("en");
46 Thread
.CurrentThread
.CurrentCulture
= en
;
47 Thread
.CurrentThread
.CurrentUICulture
= en
;
49 helper
= new FormHelper();
51 subscription
= new Subscription();
52 months
= new Month
[] {new Month(1, "January"), new Month(1, "February")}
;
53 product
= new Product("memory card", 10, (decimal) 12.30);
54 user
= new SimpleUser();
55 contact
= new Contact();
57 HomeController controller
= new HomeController();
59 controller
.PropertyBag
.Add("product", product
);
60 controller
.PropertyBag
.Add("user", user
);
61 controller
.PropertyBag
.Add("roles", new Role
[] { new Role(1, "a"), new Role(2, "b"), new Role(3, "c") }
);
62 controller
.PropertyBag
.Add("sendemail", true);
63 controller
.PropertyBag
.Add("confirmation", "abc");
64 controller
.PropertyBag
.Add("fileaccess", FileAccess
.Read
);
65 controller
.PropertyBag
.Add("subscription", subscription
);
66 controller
.PropertyBag
.Add("months", months
);
67 controller
.PropertyBag
.Add("contact", contact
);
69 workTable
= new DataTable("Customers");
70 DataColumn workCol
= workTable
.Columns
.Add("CustID", typeof(Int32
));
71 workCol
.AllowDBNull
= false;
72 workCol
.Unique
= true;
73 workTable
.Columns
.Add("Name", typeof(String
));
75 DataRow row
= workTable
.NewRow();
77 row
[1] = "chris rocks";
78 workTable
.Rows
.Add(row
);
79 row
= workTable
.NewRow();
81 row
[1] = "will ferrell";
82 workTable
.Rows
.Add(row
);
84 helper
.SetController(controller
);
88 public void SimplisticSelect()
90 ArrayList list
= new ArrayList();
95 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
96 "<option value=\"cat1\">cat1</option>" + Environment
.NewLine
+ "<option value=\"cat2\">cat2</option>" + Environment
.NewLine
+ "</select>",
97 helper
.Select("product.category.id", list
));
101 public void SelectWithFirstOption()
103 ArrayList list
= new ArrayList();
107 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
108 "<option value=\"0\">Please select</option>" + Environment
.NewLine
+
109 "<option value=\"cat1\">cat1</option>" + Environment
.NewLine
+ "<option value=\"cat2\">cat2</option>" + Environment
.NewLine
+ "</select>",
110 helper
.Select("product.category.id", list
, DictHelper
.Create("firstoption=Please select") ));
114 public void SelectWithValueAndText()
116 ArrayList list
= new ArrayList();
117 list
.Add(new ProductCategory(1, "cat1"));
118 list
.Add(new ProductCategory(2, "cat2"));
120 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
121 "<option value=\"1\">cat1</option>" + Environment
.NewLine
+ "<option value=\"2\">cat2</option>" + Environment
.NewLine
+ "</select>",
122 helper
.Select("product.category.id", list
, DictHelper
.Create("value=id", "text=name") ));
126 public void SelectOnPrimitiveArrayWithoutValueAndText()
128 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
129 "<option value=\"1\">1</option>" + Environment
.NewLine
+ "<option value=\"2\">2</option>" + Environment
.NewLine
+ "<option value=\"3\">3</option>" + Environment
.NewLine
+ "<option value=\"4\">4</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"5\">5</option>" + Environment
.NewLine
+ "</select>",
130 helper
.Select("product.category.id", 5, new int[] { 1, 2, 3, 4, 5}
, DictHelper
.Create() ));
134 public void SelectWithValueAndTextAndSelect()
136 ArrayList list
= new ArrayList();
137 list
.Add(new ProductCategory(1, "cat1"));
138 list
.Add(new ProductCategory(2, "cat2"));
140 product
.Category
.Id
= 2;
142 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
143 "<option value=\"1\">cat1</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"2\">cat2</option>" + Environment
.NewLine
+ "</select>",
144 helper
.Select("product.category.id", list
, DictHelper
.Create("value=id", "text=name") ));
148 public void SelectWithNoSelection()
150 ArrayList list
= new ArrayList();
151 list
.Add(new Role(1, "role1"));
152 list
.Add(new Role(2, "role2"));
154 Assert
.AreEqual("<select id=\"user_roles\" name=\"user.roles\" >" + Environment
.NewLine
+
155 "<option value=\"1\">role1</option>" + Environment
.NewLine
+ "<option value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
156 helper
.Select("user.roles", list
, DictHelper
.Create("value=id", "text=name") ));
160 public void SelectWithSelection()
162 ArrayList list
= new ArrayList();
163 list
.Add(new Role(1, "role1"));
164 list
.Add(new Role(2, "role2"));
166 user
.Roles
.Add(new Role(1, "role1"));
167 user
.Roles
.Add(new Role(2, "role2"));
169 Assert
.AreEqual("<select id=\"user_roles\" name=\"user.roles\" >" + Environment
.NewLine
+
170 "<option selected=\"selected\" value=\"1\">role1</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
171 helper
.Select("user.roles", list
, DictHelper
.Create("value=id", "text=name") ));
175 public void SelectWithSelection2()
177 ArrayList list
= new ArrayList();
178 list
.Add(new Role(1, "role1"));
179 list
.Add(new Role(2, "role2"));
181 user
.Roles
.Add(new Role(1, "role1"));
183 Assert
.AreEqual("<select id=\"user_roles\" name=\"user.roles\" >" + Environment
.NewLine
+
184 "<option selected=\"selected\" value=\"1\">role1</option>" + Environment
.NewLine
+ "<option value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
185 helper
.Select("user.roles", list
, DictHelper
.Create("value=id", "text=name") ));
189 public void SelectWithArraySelection()
191 ArrayList list
= new ArrayList();
192 list
.Add(new Role(1, "role1"));
193 list
.Add(new Role(2, "role2"));
195 user
.Roles
.Add(new Role(1, "role1"));
196 user
.Roles
.Add(new Role(2, "role2"));
198 Assert
.AreEqual("<select id=\"user_RolesAsArray\" name=\"user.RolesAsArray\" >" + Environment
.NewLine
+
199 "<option selected=\"selected\" value=\"1\">role1</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
200 helper
.Select("user.RolesAsArray", list
, DictHelper
.Create("value=id", "text=name") ));
204 public void SelectWithArraySelection2()
206 ArrayList list
= new ArrayList();
207 list
.Add(new Role(1, "role1"));
208 list
.Add(new Role(2, "role2"));
210 user
.Roles
.Add(new Role(1, "role1"));
212 Assert
.AreEqual("<select id=\"user_RolesAsArray\" name=\"user.RolesAsArray\" >" + Environment
.NewLine
+
213 "<option selected=\"selected\" value=\"1\">role1</option>" + Environment
.NewLine
+ "<option value=\"2\">role2</option>" + Environment
.NewLine
+ "</select>",
214 helper
.Select("user.RolesAsArray", list
, DictHelper
.Create("value=id", "text=name") ));
218 public void SelectWithArraySelectionNoId()
220 ArrayList list
= new ArrayList();
221 list
.Add(new Role(1, "role1"));
222 list
.Add(new Role(2, "role2"));
224 user
.Roles
.Add(new Role(1, "role1"));
225 user
.Roles
.Add(new Role(2, "role2"));
227 Assert
.AreEqual("<select id=\"user_RolesAsArray\" name=\"user.RolesAsArray\" >" + Environment
.NewLine
+
228 "<option selected=\"selected\" value=\"role1\">role1</option>" + Environment
.NewLine
+ "<option selected=\"selected\" value=\"role2\">role2</option>" + Environment
.NewLine
+ "</select>",
229 helper
.Select("user.RolesAsArray", list
));
233 public void UsingDataTable()
236 "<select id=\"user_id\" name=\"user.id\" >" + Environment
.NewLine
+
237 "<option value=\"1\">chris rocks</option>" + Environment
.NewLine
+ "<option value=\"2\">will ferrell</option>" + Environment
.NewLine
+ "</select>",
238 helper
.Select("user.id", workTable
.Rows
, DictHelper
.Create("value=custid", "text=name", "sourceProperty=id")));
242 public void UsingEnums()
245 "<select id=\"user_registration\" name=\"user.registration\" >" + Environment
.NewLine
+
246 "<option value=\"1\">unregistered</option>" + Environment
.NewLine
+
247 "<option value=\"2\">pending</option>" + Environment
.NewLine
+
248 "<option selected=\"selected\" value=\"6\">registered</option>" + Environment
.NewLine
+
250 helper
.Select("user.registration", Enum
.GetValues(typeof(SimpleUser
.RegistrationEnum
))));
254 public void UsingEnumsAsText()
257 "<select id=\"user_registration\" name=\"user.registration\" >" + Environment
.NewLine
+
258 "<option value=\"unregistered\">unregistered</option>" + Environment
.NewLine
+
259 "<option value=\"pending\">pending</option>" + Environment
.NewLine
+
260 "<option selected=\"selected\" value=\"registered\">registered</option>" + Environment
.NewLine
+
262 helper
.Select("user.registration", Enum
.GetNames(typeof(SimpleUser
.RegistrationEnum
))));
267 public void UsingInterface()
269 List
<IInterfacedList
> list
= new List
<IInterfacedList
>();
271 list
.Add(new InterfacedClassA(1, "ernst"));
272 list
.Add(new InterfacedClassB(2, "enix"));
274 Assert
.AreEqual("<select id=\"user_name\" name=\"user.name\" >" + Environment
.NewLine
+
275 "<option value=\"1\">ernst</option>" + Environment
.NewLine
+ "<option value=\"2\">enix</option>" + Environment
.NewLine
+ "</select>",
276 helper
.Select("user.name", list
, DictHelper
.Create("value=id", "text=name")));
280 public void BasicFunctionalityInDotNet2()
282 List
<Month
> list
= new List
<Month
>();
283 list
.Add(new Month(1, "Jan"));
284 list
.Add(new Month(2, "Feb"));
286 Assert
.AreEqual("<select id=\"contact_dobmonth_id\" name=\"contact.dobmonth.id\" >" + Environment
.NewLine
+
287 "<option value=\"1\">Jan</option>" + Environment
.NewLine
+ "<option value=\"2\">Feb</option>" + Environment
.NewLine
+ "</select>",
288 helper
.Select("contact.dobmonth.id", list
, DictHelper
.Create("value=id", "text=name")));
292 public void SelectWithCompositKey()
294 ArrayList list
= new ArrayList();
295 list
.Add(new ClassWithCompositKey(1, "cat1"));
296 list
.Add(new ClassWithCompositKey(2, "cat2"));
298 Assert
.AreEqual("<select id=\"product_category_id\" name=\"product.category.id\" >" + Environment
.NewLine
+
299 "<option value=\"1\">cat1</option>" + Environment
.NewLine
+ "<option value=\"2\">cat2</option>" + Environment
.NewLine
+ "</select>",
300 helper
.Select("product.category.id", list
, DictHelper
.Create("value=Key.Id", "text=name")));