CommunicationObjectTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // CommunicationObjectTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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. using System;
  29. using System.ServiceModel;
  30. using System.ServiceModel.Channels;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.ServiceModel.Channels
  33. {
  34. class ExtCommObj : CommunicationObject
  35. {
  36. public new bool Aborted, Opened, Closed, OnClosedCalled;
  37. public ExtCommObj () : base ()
  38. {
  39. }
  40. protected override TimeSpan DefaultCloseTimeout {
  41. get { return TimeSpan.Zero; }
  42. }
  43. protected override TimeSpan DefaultOpenTimeout {
  44. get { return TimeSpan.Zero; }
  45. }
  46. public new bool IsDisposed {
  47. get { return base.IsDisposed; }
  48. }
  49. public void XFault ()
  50. {
  51. Fault ();
  52. }
  53. protected override void OnAbort ()
  54. {
  55. if (Aborted)
  56. throw new Exception ("Already aborted");
  57. Aborted = true;
  58. }
  59. protected override IAsyncResult OnBeginClose (
  60. TimeSpan timeout, AsyncCallback callback, object state)
  61. {
  62. throw new NotImplementedException ();
  63. }
  64. protected override IAsyncResult OnBeginOpen (
  65. TimeSpan timeout, AsyncCallback callback, object state)
  66. {
  67. throw new NotImplementedException ();
  68. }
  69. protected override void OnEndClose (IAsyncResult result)
  70. {
  71. throw new NotImplementedException ();
  72. }
  73. protected override void OnEndOpen (IAsyncResult result)
  74. {
  75. throw new NotImplementedException ();
  76. }
  77. protected override void OnClose (TimeSpan timeout)
  78. {
  79. if (Closed)
  80. throw new Exception ("Already closed");
  81. Closed = true;
  82. }
  83. protected override void OnOpen (TimeSpan timeout)
  84. {
  85. if (Opened)
  86. throw new Exception ("Already opened");
  87. Opened = true;
  88. }
  89. protected override void OnClosed ()
  90. {
  91. if (OnClosedCalled)
  92. throw new Exception ("OnClosed() already called");
  93. OnClosedCalled = true;
  94. base.OnClosed ();
  95. }
  96. }
  97. class ExtCommObj2 : ExtCommObj
  98. {
  99. public bool OnClosedCalled;
  100. // It does not call base -> Abort() detects it as an error.
  101. protected override void OnClosed ()
  102. {
  103. if (OnClosedCalled)
  104. throw new Exception ("OnClosed() already called");
  105. OnClosedCalled = true;
  106. }
  107. }
  108. [TestFixture]
  109. public class CommunicationObjectTest
  110. {
  111. [Test]
  112. public void OpenClose ()
  113. {
  114. ExtCommObj obj = new ExtCommObj ();
  115. Assert.AreEqual (CommunicationState.Created, obj.State, "#1");
  116. obj.Open ();
  117. Assert.AreEqual (CommunicationState.Opened, obj.State, "#2");
  118. Assert.IsTrue (obj.Opened, "#2-2");
  119. obj.Close ();
  120. Assert.AreEqual (CommunicationState.Closed, obj.State, "#3");
  121. Assert.IsTrue (obj.Closed, "#3-2");
  122. Assert.AreEqual (true, obj.IsDisposed, "#4");
  123. }
  124. [Test]
  125. [ExpectedException (typeof (InvalidOperationException))]
  126. public void OpenOpenFails ()
  127. {
  128. ExtCommObj obj = new ExtCommObj ();
  129. obj.Open ();
  130. obj.Open ();
  131. }
  132. [Test]
  133. public void CloseAtInitialState ()
  134. {
  135. ExtCommObj obj = new ExtCommObj ();
  136. obj.Close ();
  137. Assert.IsTrue (obj.Aborted, "#1"); // OnAbort() is called.
  138. Assert.IsFalse (obj.Closed, "#2"); // OnClose() is *not* called.
  139. Assert.IsTrue (obj.OnClosedCalled, "#3");
  140. }
  141. [Test]
  142. public void CloseAtInitialStateAsync ()
  143. {
  144. ExtCommObj obj = new ExtCommObj ();
  145. obj.EndClose (obj.BeginClose (null, null)); // does not call OnBeginClose() / OnEndClose().
  146. Assert.IsTrue (obj.Aborted, "#1");
  147. Assert.IsFalse (obj.Closed, "#2");
  148. Assert.IsTrue (obj.OnClosedCalled, "#3");
  149. }
  150. [Test]
  151. public void CloseAtOpenedState ()
  152. {
  153. ExtCommObj obj = new ExtCommObj ();
  154. obj.Open ();
  155. obj.Close (); // Aborted() is *not* called.
  156. Assert.IsFalse (obj.Aborted, "#1");
  157. Assert.IsTrue (obj.Closed, "#2");
  158. }
  159. [Test]
  160. [ExpectedException (typeof (ObjectDisposedException))]
  161. public void OpenClosedItemFails ()
  162. {
  163. ExtCommObj obj = new ExtCommObj ();
  164. obj.Open ();
  165. obj.Close ();
  166. obj.Open ();
  167. }
  168. [Test]
  169. public void Fault ()
  170. {
  171. ExtCommObj obj = new ExtCommObj ();
  172. obj.XFault ();
  173. obj = new ExtCommObj ();
  174. obj.Open ();
  175. obj.XFault ();
  176. Assert.AreEqual (CommunicationState.Faulted, obj.State, "#1");
  177. Assert.AreEqual (false, obj.IsDisposed, "#2");
  178. }
  179. [Test]
  180. [ExpectedException (typeof (CommunicationObjectFaultedException))]
  181. public void OpenFaulted ()
  182. {
  183. ExtCommObj obj = new ExtCommObj ();
  184. obj.XFault ();
  185. obj.Open ();
  186. }
  187. [Test]
  188. [ExpectedException (typeof (CommunicationObjectFaultedException))]
  189. public void CloseFaulted ()
  190. {
  191. ExtCommObj obj = new ExtCommObj ();
  192. obj.Open ();
  193. obj.XFault ();
  194. obj.Close ();
  195. }
  196. [Test]
  197. public void AbortFaulted ()
  198. {
  199. ExtCommObj obj = new ExtCommObj ();
  200. obj.Open ();
  201. obj.XFault ();
  202. Assert.AreEqual (CommunicationState.Faulted, obj.State, "#1");
  203. obj.Abort (); // does not raise an error
  204. Assert.AreEqual (CommunicationState.Closed, obj.State, "#2");
  205. Assert.IsTrue (obj.Aborted, "#3");
  206. Assert.IsFalse (obj.Closed, "#4");
  207. obj.Abort (); // does not raise an error!
  208. }
  209. [Test]
  210. public void AbortCreated ()
  211. {
  212. ExtCommObj obj = new ExtCommObj ();
  213. obj.Abort ();
  214. Assert.IsTrue (obj.Aborted, "#1"); // OnAbort() is called.
  215. Assert.IsFalse (obj.Closed, "#2"); // OnClose() is *not* called.
  216. }
  217. [Test]
  218. [ExpectedException (typeof (InvalidOperationException))]
  219. public void OnClosedImplementedWithoutCallingBase ()
  220. {
  221. ExtCommObj obj = new ExtCommObj2 ();
  222. obj.Close ();
  223. }
  224. }
  225. }