CommunicationObjectTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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;
  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. }
  90. [TestFixture]
  91. public class CommunicationObjectTest
  92. {
  93. [Test]
  94. public void OpenClose ()
  95. {
  96. ExtCommObj obj = new ExtCommObj ();
  97. Assert.AreEqual (CommunicationState.Created, obj.State, "#1");
  98. obj.Open ();
  99. Assert.AreEqual (CommunicationState.Opened, obj.State, "#2");
  100. Assert.IsTrue (obj.Opened, "#2-2");
  101. obj.Close ();
  102. Assert.AreEqual (CommunicationState.Closed, obj.State, "#3");
  103. Assert.IsTrue (obj.Closed, "#3-2");
  104. Assert.AreEqual (true, obj.IsDisposed, "#4");
  105. }
  106. [Test]
  107. [ExpectedException (typeof (InvalidOperationException))]
  108. public void OpenOpenFails ()
  109. {
  110. ExtCommObj obj = new ExtCommObj ();
  111. obj.Open ();
  112. obj.Open ();
  113. }
  114. [Test]
  115. [Category ("NotWorking")]
  116. public void CloseAtInitialState ()
  117. {
  118. ExtCommObj obj = new ExtCommObj ();
  119. obj.Close (); // Aborted() is called.
  120. Assert.IsTrue (obj.Aborted);
  121. }
  122. [Test]
  123. [ExpectedException (typeof (ObjectDisposedException))]
  124. public void OpenClosedItemFails ()
  125. {
  126. ExtCommObj obj = new ExtCommObj ();
  127. obj.Open ();
  128. obj.Close ();
  129. obj.Open ();
  130. }
  131. [Test]
  132. public void Fault ()
  133. {
  134. ExtCommObj obj = new ExtCommObj ();
  135. obj.XFault ();
  136. obj = new ExtCommObj ();
  137. obj.Open ();
  138. obj.XFault ();
  139. Assert.AreEqual (CommunicationState.Faulted, obj.State);
  140. Assert.AreEqual (false, obj.IsDisposed);
  141. }
  142. [Test]
  143. [ExpectedException (typeof (CommunicationObjectFaultedException))]
  144. public void OpenFaulted ()
  145. {
  146. ExtCommObj obj = new ExtCommObj ();
  147. obj.XFault ();
  148. obj.Open ();
  149. }
  150. [Test]
  151. [ExpectedException (typeof (CommunicationObjectFaultedException))]
  152. public void CloseFaulted ()
  153. {
  154. ExtCommObj obj = new ExtCommObj ();
  155. obj.Open ();
  156. obj.XFault ();
  157. obj.Close ();
  158. }
  159. }
  160. }