Backport of r120376.
[mono.git] / mcs / class / System.Web / Test / System.Web.Compilation / AppSettingsExpressionBuilderTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.ListBoxTest.cs
3 //
4 // Author:
5 //  Vladimir Krasnov (vladimirk@mainsoft.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32
33 using NUnit.Framework;
34 using System;
35 using System.IO;
36 using System.Drawing;
37 using System.Collections.Specialized;
38 using System.Globalization;
39 using System.Web;
40 using System.Web.UI;
41 using System.Web.UI.WebControls;
42 using System.Data;
43 using MonoTests.stand_alone.WebHarness;
44 using MonoTests.SystemWeb.Framework;
45 using System.Web.Compilation;
46
47 namespace MonoTests.System.Web.Compilation
48 {
49         public class SettingTestingType
50         {
51                 private string strProp;
52                 private int intProp;
53                 private DateTime dateTimeProp;
54                 private Type typeProp;
55
56                 public string StrProp {
57                         get { return strProp; }
58                         set { strProp = value; }
59                 }
60
61                 public int IntProp {
62                         get { return intProp; }
63                         set { intProp = value; }
64                 }
65
66                 public DateTime DateTimeProp {
67                         get { return dateTimeProp; }
68                         set { dateTimeProp = value; }
69                 }
70
71                 public Type TypeProp {
72                         get { return typeProp; }
73                         set { typeProp = value; }
74                 }
75         }
76
77         [TestFixture]
78         public class AppSettingsExpressionBuilderTest
79         {
80                 [Test] // GetAppSetting (String)
81                 [Category ("NunitWeb")]
82                 public void GetAppSetting1 ()
83                 {
84                         PageDelegates pd = new PageDelegates ();
85                         pd.Load = GetAppSetting1_Load;
86                         WebTest test = new WebTest (new PageInvoker (pd));
87                         test.Run ();
88                 }
89
90                 [Test] // GetAppSetting (String)
91                 public void GetAppSetting1_Key_DoesNotExist ()
92                 {
93                         try {
94                                 AppSettingsExpressionBuilder.GetAppSetting ("DoesNotExist");
95                                 Assert.Fail ("#1");
96                         } catch (InvalidOperationException ex) {
97                                 // The application setting 'DoesNotExist' was
98                                 // not found in the applications configuration
99                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
100                                 Assert.IsNull (ex.InnerException, "#3");
101                                 Assert.IsNotNull (ex.Message, "#4");
102                                 Assert.IsTrue (ex.Message.IndexOf ("'DoesNotExist'") != -1, "#5");
103                         }
104                 }
105
106                 [Test] // GetAppSetting (String)
107                 public void GetAppSetting1_Key_Null ()
108                 {
109                         try {
110                                 AppSettingsExpressionBuilder.GetAppSetting ((string) null);
111                                 Assert.Fail ("#1");
112                         } catch (InvalidOperationException ex) {
113                                 // The application setting '' was not found in
114                                 // the applications configuration
115                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
116                                 Assert.IsNull (ex.InnerException, "#3");
117                                 Assert.IsNotNull (ex.Message, "#4");
118                                 Assert.IsTrue (ex.Message.IndexOf ("''") != -1, "#5");
119                         }
120                 }
121
122                 [Test] // GetAppSetting (String, Type, String)
123                 [Category ("NunitWeb")]
124                 public void GetAppSetting2 ()
125                 {
126                         PageDelegates pd = new PageDelegates ();
127                         pd.Load = GetAppSetting2_Load;
128                         WebTest test = new WebTest (new PageInvoker (pd));
129                         test.Run ();
130                 }
131
132                 [Test] // GetAppSetting (String, Type, String)
133                 public void GetAppSetting2_Key_Null ()
134                 {
135                         try {
136                                 AppSettingsExpressionBuilder.GetAppSetting (
137                                         (string) null, 
138                                         typeof (SettingTestingType),
139                                         "StrProp");
140                                 Assert.Fail ("#1");
141                         } catch (InvalidOperationException ex) {
142                                 // The application setting '' was not found in
143                                 // the applications configuration
144                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
145                                 Assert.IsNull (ex.InnerException, "#3");
146                                 Assert.IsNotNull (ex.Message, "#4");
147                                 Assert.IsTrue (ex.Message.IndexOf ("''") != -1, "#5");
148                         }
149                 }
150
151                 [Test]
152                 public void SupportsEvaluate ()
153                 {
154                         AppSettingsExpressionBuilder aseb = new AppSettingsExpressionBuilder ();
155                         Assert.IsTrue (aseb.SupportsEvaluate);
156                 }
157
158                 public static void GetAppSetting1_Load (Page p)
159                 {
160                         object o = AppSettingsExpressionBuilder.GetAppSetting ("strvalue");
161                         Assert.AreEqual (typeof (string), o.GetType (), "#A1");
162                         Assert.AreEqual ("str", o, "#A2");
163
164                         o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue");
165                         Assert.AreEqual (typeof (string), o.GetType (), "#B1");
166                         Assert.AreEqual ("123", o, "#B2");
167                 }
168
169                 public static void GetAppSetting2_Load (Page p)
170                 {
171                         object o = AppSettingsExpressionBuilder.GetAppSetting ("strvalue", typeof (SettingTestingType), "StrProp");
172                         Assert.AreEqual (typeof (string), o.GetType (), "#A1");
173                         Assert.AreEqual ("str", o, "#A2");
174
175                         // property does not exist
176                         o = AppSettingsExpressionBuilder.GetAppSetting ("strvalue", typeof (SettingTestingType), "NotExistsProp");
177                         Assert.AreEqual (typeof (string), o.GetType (), "#B1");
178                         Assert.AreEqual ("str", o, "#B2");
179
180                         o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue", typeof (SettingTestingType), "IntProp");
181                         Assert.AreEqual (typeof (int), o.GetType (), "#C1");
182                         Assert.AreEqual (123, o, "#C2");
183
184                         // conversion
185                         o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue", typeof (SettingTestingType), "StrProp");
186                         Assert.AreEqual (typeof (string), o.GetType (), "#D1");
187                         Assert.AreEqual ("123", o, "#D2");
188
189                         // property does not exist
190                         o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue", typeof (SettingTestingType), "NotExistsProp");
191                         Assert.AreEqual (typeof (string), o.GetType (), "#E1");
192                         Assert.AreEqual ("123", o, "#E2");
193
194                         // targetType null
195                         o = AppSettingsExpressionBuilder.GetAppSetting ("intvalue", (Type) null, "NotExistsProp");
196                         Assert.AreEqual (typeof (string), o.GetType (), "#F1");
197                         Assert.AreEqual ("123", o, "#F2");
198
199                         // conversion failed
200                         try {
201                                 AppSettingsExpressionBuilder.GetAppSetting ("intvalue",
202                                         typeof (SettingTestingType), "DateTimeProp");
203                                 Assert.Fail ("#G1");
204                         } catch (FormatException ex) {
205                                 // String was not recognized as a valid DateTime
206                                 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#G2");
207                                 Assert.IsNotNull (ex.Message, "#G3");
208                         }
209
210                         // conversion not supported
211                         try {
212                                 AppSettingsExpressionBuilder.GetAppSetting ("intvalue",
213                                         typeof (SettingTestingType), "TypeProp");
214                                 Assert.Fail ("#H1");
215                         } catch (InvalidOperationException ex) {
216                                 // Could not convert the AppSetting '123' to the
217                                 // type 'Type' on property 'TypeProp'
218                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#H2");
219                                 Assert.IsNull (ex.InnerException, "#H3");
220                                 Assert.IsNotNull (ex.Message, "#H4");
221                                 Assert.IsTrue (ex.Message.IndexOf ("'123'") != -1, "#H5");
222                                 Assert.IsTrue (ex.Message.IndexOf ("'Type'") != -1, "#H6");
223                                 Assert.IsTrue (ex.Message.IndexOf ("'TypeProp'") != -1, "#H7");
224                         }
225
226                         // propertyName null
227                         try {
228                                 AppSettingsExpressionBuilder.GetAppSetting ("intvalue",
229                                         typeof (SettingTestingType), (string) null);
230                                 Assert.Fail ("#I1");
231                         } catch (ArgumentNullException ex) {
232                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#I2");
233                                 Assert.IsNull (ex.InnerException, "#I3");
234                                 Assert.IsNotNull (ex.Message, "#I4");
235                                 //Assert.AreEqual ("key", ex.ParamName, "#I5");
236                         }
237                 }
238         }
239 }
240 #endif