[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / LoginNameTest.cs
1 //
2 // LoginNameTest.cs - Unit tests for System.Web.UI.WebControls.LoginName
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using System;
31 using System.IO;
32 using System.Security.Principal;
33 using System.Web;
34 using System.Web.UI;
35 using System.Web.UI.WebControls;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Web.UI.WebControls {
40
41         public class TestLoginName : LoginName {
42
43                 public HttpContext HttpContext {
44                         get { return base.Context; }
45                 }
46
47                 public string Tag {
48                         get { return base.TagName; }
49                 }
50
51                 public StateBag StateBag {
52                         get { return base.ViewState; }
53                 }
54
55                 private HtmlTextWriter GetWriter ()
56                 {
57                         StringWriter sw = new StringWriter ();
58                         sw.NewLine = "\n";
59                         return new HtmlTextWriter (sw);
60                 }
61
62                 public string Render ()
63                 {
64                         HtmlTextWriter writer = GetWriter ();
65                         base.Render (writer);
66                         return writer.InnerWriter.ToString ();
67                 }
68
69                 public string RenderContents ()
70                 {
71                         HtmlTextWriter writer = GetWriter ();
72                         base.RenderContents (writer);
73                         return writer.InnerWriter.ToString ();
74                 }
75
76                 public string RenderBeginTag ()
77                 {
78                         HtmlTextWriter writer = GetWriter ();
79                         base.RenderBeginTag (writer);
80                         return writer.InnerWriter.ToString ();
81                 }
82
83                 public string RenderEndTag (bool includeBeginTag)
84                 {
85                         HtmlTextWriter writer = GetWriter ();
86                         if (includeBeginTag) {
87                                 // required before calling RenderEndTag
88                                 base.RenderBeginTag (writer); 
89                                 // unless we're not really calling our base...
90                         }
91                         base.RenderEndTag (writer);
92                         return writer.InnerWriter.ToString ();
93                 }
94         }
95
96         public class ContextPage : Page {
97
98                 HttpContext ctx;
99
100                 public ContextPage ()
101                 {
102                 }
103
104                 public ContextPage (IPrincipal principal)
105                 {
106                         Context.User = principal;
107                 }
108
109                 protected internal override HttpContext Context {
110                         get {
111                                 if (ctx == null) {
112                                         ctx = new HttpContext (
113                                                 new HttpRequest (String.Empty, "http://www.mono-project.com", String.Empty),
114                                                 new HttpResponse (new StringWriter ())
115                                                 );
116                                 }
117                                 return ctx;
118                         }
119                 }
120         }
121
122         public class UnauthenticatedIdentity : GenericIdentity  {
123
124                 public UnauthenticatedIdentity (string name)
125                         : base (name)
126                 {
127                 }
128
129                 public override bool IsAuthenticated {
130                         get { return false; }
131                 }
132         }
133
134         [TestFixture]
135         public class LoginNameTest {
136
137                 private IPrincipal GetPrincipal (string name)
138                 {
139                         return new GenericPrincipal (new GenericIdentity (name), null);
140                 }
141
142                 private IPrincipal GetUnauthenticatedPrincipal (string name)
143                 {
144                         return new GenericPrincipal (new UnauthenticatedIdentity (name), null);
145                 }
146
147                 [Test]
148                 public void DefaultProperties ()
149                 {
150                         TestLoginName ln = new TestLoginName ();
151                         Assert.AreEqual (0, ln.Attributes.Count, "Attributes.Count");
152                         Assert.AreEqual (0, ln.StateBag.Count, "ViewState.Count");
153
154                         Assert.AreEqual ("{0}", ln.FormatString, "FormatString");
155
156                         Assert.AreEqual ("span", ln.Tag, "span");
157                         Assert.AreEqual (0, ln.Attributes.Count, "Attributes.Count-1");
158                         Assert.AreEqual (0, ln.StateBag.Count, "ViewState.Count-1");
159                 }
160
161                 [Test]
162                 public void SetOriginalProperties ()
163                 {
164                         TestLoginName ln = new TestLoginName ();
165                         ln.FormatString = "{0}";
166                         Assert.AreEqual (1, ln.StateBag.Count, "ViewState.Count-1");
167                 }
168
169                 [Test]
170                 public void CleanProperties ()
171                 {
172                         TestLoginName ln = new TestLoginName ();
173                         ln.FormatString = "Hola {0}!";
174                         Assert.AreEqual ("Hola {0}!", ln.FormatString, "FormatString-1");
175                         Assert.AreEqual (1, ln.StateBag.Count, "ViewState.Count-1");
176                         ln.FormatString = "{0}";
177                         Assert.AreEqual ("{0}", ln.FormatString, "FormatString-2");
178                         Assert.AreEqual (1, ln.StateBag.Count, "ViewState.Count-2");
179                         ln.FormatString = String.Empty;
180                         Assert.AreEqual (String.Empty, ln.FormatString, "FormatString-3");
181                         Assert.AreEqual (1, ln.StateBag.Count, "ViewState.Count-3");
182                         ln.FormatString = null;
183                         Assert.AreEqual ("{0}", ln.FormatString, "FormatString-4");
184                         Assert.AreEqual (0, ln.StateBag.Count, "ViewState.Count-4");
185                 }
186
187                 [Test]
188                 public void CacheIdentity ()
189                 {
190                         TestLoginName ln = new TestLoginName ();
191                         Assert.AreEqual (String.Empty, ln.RenderContents (), "Anonymous");
192                         ln.Page = new ContextPage (GetPrincipal ("me"));
193                         Assert.AreEqual ("me", ln.RenderContents (), "me");
194                         ln.Page = new ContextPage (GetPrincipal ("you"));
195                         Assert.AreEqual ("you", ln.RenderContents (), "you");
196                 }
197
198                 [Test]
199                 public void Render_NoPage ()
200                 {
201                         TestLoginName ln = new TestLoginName ();
202                         Assert.AreEqual (String.Empty, ln.Render (), "Render");
203                         Assert.AreEqual (String.Empty, ln.RenderContents (), "RenderContents");
204                         Assert.AreEqual (String.Empty, ln.RenderBeginTag (), "RenderBeginTag");
205                         Assert.AreEqual (String.Empty, ln.RenderEndTag (false), "RenderEndTag");
206                 }
207
208                 [Test]
209                 public void Render_Anonymous_NoPrincipal ()
210                 {
211                         ContextPage page = new ContextPage ();
212                         TestLoginName ln = new TestLoginName ();
213                         ln.Page = page;
214                         Assert.AreEqual (String.Empty, ln.Render (), "Render");
215                         Assert.AreEqual (String.Empty, ln.RenderContents (), "RenderContents");
216                         Assert.AreEqual (String.Empty, ln.RenderBeginTag (), "RenderBeginTag");
217                         Assert.AreEqual (String.Empty, ln.RenderEndTag (false), "RenderEndTag");
218                 }
219
220                 [Test]
221                 public void Render_Anonymous_IPrincipal ()
222                 {
223                         ContextPage page = new ContextPage (GetPrincipal (String.Empty));
224                         TestLoginName ln = new TestLoginName ();
225                         ln.Page = page;
226                         Assert.AreEqual (String.Empty, ln.Render (), "Render");
227                         Assert.AreEqual (String.Empty, ln.RenderContents (), "RenderContents");
228                         Assert.AreEqual (String.Empty, ln.RenderBeginTag (), "RenderBeginTag");
229                         Assert.AreEqual (String.Empty, ln.RenderEndTag (false), "RenderEndTag");
230                 }
231
232                 [Test]
233                 public void Render_User ()
234                 {
235                         ContextPage page = new ContextPage (GetPrincipal ("me"));
236                         TestLoginName ln = new TestLoginName ();
237                         ln.Page = page;
238                         Assert.IsTrue (page.User.Identity.IsAuthenticated, "IsAuthenticated");
239                         Assert.AreEqual ("<span>me</span>", ln.Render (), "Render");
240                         Assert.AreEqual ("me", ln.RenderContents (), "RenderContents");
241                         Assert.AreEqual ("<span>", ln.RenderBeginTag (), "RenderBeginTag");
242                         Assert.AreEqual ("<span></span>", ln.RenderEndTag (true), "RenderEndTag");
243                 }
244
245                 [Test]
246                 public void Render_UnauthenticatedUser ()
247                 {
248                         ContextPage page = new ContextPage (GetUnauthenticatedPrincipal ("me"));
249                         TestLoginName ln = new TestLoginName ();
250                         ln.Page = page;
251                         Assert.IsFalse (page.User.Identity.IsAuthenticated, "IsAuthenticated");
252                         Assert.AreEqual ("<span>me</span>", ln.Render (), "Render");
253                         Assert.AreEqual ("me", ln.RenderContents (), "RenderContents");
254                         Assert.AreEqual ("<span>", ln.RenderBeginTag (), "RenderBeginTag");
255                         Assert.AreEqual ("<span></span>", ln.RenderEndTag (true), "RenderEndTag");
256                 }
257
258                 [Test]
259                 public void Render_StringFormat ()
260                 {
261                         ContextPage page = new ContextPage (GetPrincipal ("me"));
262                         TestLoginName ln = new TestLoginName ();
263                         ln.Page = page;
264                         ln.FormatString = "Hola {0}!";
265                         Assert.AreEqual ("Hola me!", ln.RenderContents (), "RenderContents");
266                 }
267
268                 [Test]
269                 public void Render_StringFormat_Empty ()
270                 {
271                         ContextPage page = new ContextPage (GetPrincipal ("me"));
272                         TestLoginName ln = new TestLoginName ();
273                         ln.Page = page;
274                         ln.FormatString = String.Empty;
275                         Assert.AreEqual ("me", ln.RenderContents (), "RenderContents");
276                 }
277
278                 [Test]
279                 public void Render_StringFormat_NoVar ()
280                 {
281                         ContextPage page = new ContextPage (GetPrincipal ("me"));
282                         TestLoginName ln = new TestLoginName ();
283                         ln.Page = page;
284                         ln.FormatString = "Hola!";
285                         Assert.AreEqual ("Hola!", ln.RenderContents (), "RenderContents");
286                 }
287
288                 [Test]
289                 [ExpectedException (typeof (FormatException))]
290                 public void Render_StringFormat_TwoVars ()
291                 {
292                         ContextPage page = new ContextPage (GetPrincipal ("me"));
293                         TestLoginName ln = new TestLoginName ();
294                         ln.Page = page;
295                         ln.FormatString = "Hola {0} {1}!";
296                         Assert.AreEqual ("Hola me!", ln.RenderContents (), "RenderContents");
297                 }
298         }
299 }
300