merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / SiteMapDataSourceTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.View.cs
3 //
4 // Author:
5 //      Yoni Klein (yonik@mainsoft.com)
6 //
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.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 #if NET_2_0
31
32 using NUnit.Framework;
33 using System;
34 using System.IO;
35 using System.Globalization;
36 using System.Configuration;
37 using System.Collections;
38 using System.Collections.Specialized;
39 using System.Web;
40 using System.Web.UI;
41 using System.Web.UI.WebControls;
42 using System.Security.Permissions;
43
44
45 namespace MonoTests.System.Web.UI.WebControls
46 {
47         class PokerSiteMapDataSource : SiteMapDataSource
48         {
49                 public PokerSiteMapDataSource ()
50                 {
51                         TrackViewState ();
52                 }
53                 public object SaveState ()
54                 {
55                         return SaveViewState ();
56                 }
57                 public void LoadState (object o)
58                 {
59                         LoadViewState (o);
60                 }
61                 public StateBag StateBag
62                 {
63                         get { return base.ViewState; }
64                 }
65                 public HierarchicalDataSourceView DoHierarchicalDataSourceView (string str)
66                 {
67                         return GetHierarchicalView (str);
68                 }
69         }
70
71         [TestFixture]
72         public class SiteMapDataSourceTest
73         {
74
75                 [Test]
76                 public void SiteMapDataSource_DefaultProperties ()
77                 {
78
79                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
80                         Assert.AreEqual (true, p.ShowStartingNode, "ShowStartingNode");
81                         Assert.AreEqual (string.Empty, p.SiteMapProvider, "SiteMapProvider");
82                         Assert.AreEqual (false, p.StartFromCurrentNode, "StartFromCurrentNode");
83                         Assert.AreEqual (0, p.StartingNodeOffset, "StartingNodeOffset");
84                         Assert.AreEqual (string.Empty, p.StartingNodeUrl, "StartingNodeUrl");
85                         
86                 }
87
88                 [Test]
89                 [Category ("NotWorking")]  // not implemented in mono
90                 public void SiteMapDataSource_NotWorkingDefaultProperties ()
91                 {       
92                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
93                         Assert.AreEqual (true, p.ContainsListCollection, "ContainsListCollection");
94                 }
95                 
96                 [Test]
97                 public void SiteMapDataSource_ChangeProperties ()
98                 {
99                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
100                         p.ShowStartingNode = false;
101                         Assert.AreEqual (false, p.ShowStartingNode, "ShowStartingNode");
102                         Assert.AreEqual (1, p.StateBag.Count, "ShowStartingNode#1");
103
104                         p.SiteMapProvider = "test";
105                         Assert.AreEqual ("test", p.SiteMapProvider, "SiteMapProvider");
106                         Assert.AreEqual (2, p.StateBag.Count, "SiteMapProvider#1");
107                         // null properties doe's not affects on state bag count
108                         p.SiteMapProvider = null;
109                         Assert.AreEqual (2, p.StateBag.Count, "SiteMapProvider#2");
110
111                         p.StartFromCurrentNode = true;
112                         Assert.AreEqual (true, p.StartFromCurrentNode, "StartFromCurrentNode");
113                         Assert.AreEqual (3, p.StateBag.Count, "StartFromCurrentNode#1");
114
115                         p.StartingNodeOffset = 1;
116                         Assert.AreEqual (1, p.StartingNodeOffset, "StartingNodeOffset");
117                         Assert.AreEqual (4, p.StateBag.Count, "StartingNodeOffset#2");
118
119                         p.StartingNodeUrl = "default.aspx";
120                         Assert.AreEqual ("default.aspx", p.StartingNodeUrl, "StartingNodeUrl");
121                         Assert.AreEqual (5, p.StateBag.Count, "StartingNodeUrl#1");
122                         // null properties doe's not affects on state bag count
123                         p.StartingNodeUrl = null;
124                         Assert.AreEqual (5, p.StateBag.Count, "StartingNodeUrl#2");
125                 }
126
127                 [Test]
128                 [Category ("NotWorking")]  // Throws NotImplementedException in mono
129                 public void SiteMapDataSource_GetList ()
130                 {
131                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
132                         Assert.IsNotNull (p.GetList (), "GetList");
133                         Assert.IsTrue (p.ContainsListCollection, "ContainsListCollection");
134                 }
135                 
136                 [Test]
137 #if TARGET_JVM //BUG #6489
138                 [Category ("NotWorking")]
139 #endif
140                 public void SiteMapDataSource_GetView ()
141                 {
142                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
143                         p.Provider = new mySiteMapProvider ();
144                         DataSourceView V  = p.GetView("");
145                         Assert.IsNotNull (V, "GetView");
146                 }
147
148                 [Test]
149                 public void SiteMapDataSource_ViewState ()
150                 {
151                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
152                         p.SiteMapProvider = "test";
153
154                         p.StartFromCurrentNode = false;
155                         p.StartingNodeOffset = 1;
156                         p.StartingNodeUrl = "default.aspx";
157
158                         object state = p.SaveState ();
159                         PokerSiteMapDataSource copy = new PokerSiteMapDataSource ();
160                         copy.LoadState (state);
161
162                         Assert.AreEqual ("test", copy.SiteMapProvider, "SiteMapProvider");
163                         Assert.AreEqual (false, copy.StartFromCurrentNode, "StartFromCurrentNode");
164                         Assert.AreEqual (1, copy.StartingNodeOffset, "StartingNodeOffset");
165                         Assert.AreEqual ("default.aspx", copy.StartingNodeUrl, "StartingNodeUrl");
166                 }
167
168                 [Test]
169                 [Category ("NotWorking")]  //throws System.IndexOutOfRangeException : Array index is out of range
170                 public void SiteMapDataSource_HierarchicalDataSourceView ()
171                 {
172                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
173                         p.Provider = new mySiteMapProvider ();
174                         HierarchicalDataSourceView h = p.DoHierarchicalDataSourceView ("1");
175                         Assert.IsNotNull (h, "HierarchicalDataSourceView");
176                 }
177
178                 [Test]
179                 [Category ("NotWorking")] //must be throw ConfigurationErrorsException but was IndexOutOfRangeException 
180                 [ExpectedException (typeof (ConfigurationErrorsException))]
181                 public void SiteMapDataSource_GetViewExeption1 ()
182                 {
183                         PokerSiteMapDataSource p = new PokerSiteMapDataSource ();
184                         p.GetView ("1");
185                 }
186         }
187
188    
189         // Helper Class
190         public class mySiteMapProvider : StaticSiteMapProvider
191         {
192                 private SiteMapNode rootNode = null;
193                 // Implement a default constructor.
194                 public mySiteMapProvider ()
195                 {
196                 }
197                 // Some basic state to help track the initialization state of the provider.
198                 private bool initialized = false;
199                 public virtual bool IsInitialized
200                 {
201                         get { return initialized; }
202                 }
203
204                 // Return the root node of the current site map.
205                 public override SiteMapNode RootNode
206                 {
207                         get
208                         {
209                                 SiteMapNode temp = null;
210                                 temp = BuildSiteMap ();
211                                 return temp;
212                         }
213                 }
214                 protected override SiteMapNode GetRootNodeCore ()
215                 {
216                         return RootNode;
217                 }
218                 // Initialize is used to initialize the properties and any state that the
219                 // AccessProvider holds, but is not used to build the site map.
220                 // The site map is built when the BuildSiteMap method is called.
221                 public override void Initialize (string name, NameValueCollection attributes)
222                 {
223                         if (IsInitialized)
224                                 return;
225
226                         base.Initialize (name, attributes);
227                         initialized = true;
228                 }
229
230                 ///
231                 /// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
232                 ///
233                 // Clean up any collections or other state that an instance of this may hold.
234                 protected override void Clear ()
235                 {
236                         lock (this) {
237                                 rootNode = null;
238                                 base.Clear ();
239                         }
240                 }
241
242                 // Build an in-memory representation from persistent
243                 // storage, and return the root node of the site map.
244                 public override SiteMapNode BuildSiteMap ()
245                 {
246                         // Since the SiteMap class is static, make sure that it is
247                         // not modified while the site map is built.
248                         lock (this) {
249
250                                 // If there is no root node, then there is no site map.
251                                 if (null == rootNode) {
252                                         // Start with a clean slate
253                                         Clear ();
254
255                                         // Select the root node of the site map .
256                                         rootNode = new SiteMapNode (this, "1", "default.aspx", "Default");
257
258                                 }
259
260                                 else return null;
261
262                                 SiteMapNode childNode = null;
263                                 childNode = new SiteMapNode (this, "2", "catalog.aspx", "catalog");
264                                 AddNode (childNode, rootNode);
265                                 childNode = new SiteMapNode (this, "3", "aboutus.aspx", "about us");
266                                 AddNode (childNode, rootNode);
267
268                                 return rootNode;
269                         }
270                 }
271         }
272
273         
274 }
275 #endif