SiteMapDataSourceTest.cs 8.1 KB

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