Merge pull request #3716 from vargaz/unbox-stobj-null
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Dispatcher / DispatchOperationTest.cs
1 //
2 // DispatchOperationTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 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 using System;
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Dispatcher;
36 using System.ServiceModel.Security;
37 using System.Xml;
38 using NUnit.Framework;
39
40 using MonoTests.Helpers;
41
42 namespace MonoTests.System.ServiceModel.Dispatcher
43 {
44         [TestFixture]
45         public class DispatchOperationTest
46         {
47                 [Test]
48                 [ExpectedException (typeof (ArgumentNullException))]
49                 public void TestConstructorNullName ()
50                 {
51                         new DispatchOperation (CreateRuntime (), null, null);
52                 }
53
54                 [Test]
55                 public void TestConstructorNullAction ()
56                 {
57                         DispatchRuntime r = CreateRuntime ();
58                         // null Action is allowed.
59                         new DispatchOperation (r, String.Empty, null);
60                         // null ReplyAction as well.
61                         new DispatchOperation (r, String.Empty, null, null);
62                 }
63
64                 [Test]
65                 public void TestConstructor ()
66                 {
67                         DispatchOperation o = new DispatchOperation (
68                                 CreateRuntime (), String.Empty, null, null);
69                         Assert.IsTrue (o.DeserializeRequest, "#1");
70                         Assert.IsTrue (o.SerializeReply, "#2");
71                         Assert.IsNull (o.Formatter, "#3");
72                         Assert.AreEqual (0, o.FaultContractInfos.Count, "#4");
73                         Assert.IsNull (o.Invoker, "#5");
74                         Assert.IsFalse (o.IsOneWay, "#6");
75                         Assert.IsFalse (o.IsTerminating, "#7");
76                         Assert.IsFalse (o.ReleaseInstanceBeforeCall, "#8");
77                         Assert.IsFalse (o.ReleaseInstanceAfterCall, "#9");
78                         Assert.IsFalse (o.TransactionAutoComplete, "#10");
79                         Assert.IsFalse (o.TransactionRequired, "#11");
80                         Assert.AreEqual (0, o.ParameterInspectors.Count, "#12");
81                 }
82
83                 DispatchRuntime CreateRuntime ()
84                 {
85                         return new EndpointDispatcher (
86                                 new EndpointAddress ("http://localhost:8080"), "IFoo", "urn:foo").DispatchRuntime;
87                 }
88
89                 [Test]
90                 public void FaultContractInfos ()
91                 {
92                         var host = new ServiceHost (typeof (TestFaultContract));
93                         int port = NetworkHelpers.FindFreePort ();
94                         host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = false;
95                         host.AddServiceEndpoint (typeof (ITestFaultContract), new BasicHttpBinding (), new Uri ("http://localhost:" + port));
96                         host.Open ();
97                         try {
98                                 var cf = new ChannelFactory<ITestFaultContract> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:" + port));
99                                 var cli = cf.CreateChannel ();
100                                 try {
101                                         cli.Run ("default");
102                                         Assert.Fail ("#1");
103                                 } catch (FaultException<PrivateAffairError> ex) {
104                                         var p  = ex.Detail;
105                                         Assert.AreEqual (5, p.ErrorCode, "#2");
106                                         Assert.AreEqual ("foobarerror", p.Text, "#3");
107                                 }
108                         } finally {
109                                 host.Close ();
110                         }
111                 }
112
113                 [Test]
114                 public void FaultContractInfos_2 ()
115                 {
116                         var host = new ServiceHost (typeof (TestFaultContract));
117                         int port = NetworkHelpers.FindFreePort ();
118                         host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = false;
119                         host.AddServiceEndpoint (typeof (ITestFaultContract), new BasicHttpBinding (), new Uri ("http://localhost:" + port));
120                         host.Open ();
121                         try {
122                                 var cf = new ChannelFactory<ITestFaultContract> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:" + port));
123                                 var cli = cf.CreateChannel ();
124
125                                 try {
126                                         cli.Run ("deriveddata");
127                                         Assert.Fail ("#4");
128                                 } catch (FaultException ex) {
129                                         // The type must be explicitly listed in the [FaultContract],
130                                         // it is not allowed to use a subclass of the exception data type.
131                                 }
132                         } finally {
133                                 host.Close ();
134                         }
135                 }
136
137                 [Test]
138                 public void FaultContractInfos_3 ()
139                 {
140                         var host = new ServiceHost (typeof (TestFaultContract));
141                         int port = NetworkHelpers.FindFreePort ();
142                         host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = false;
143                         host.AddServiceEndpoint (typeof (ITestFaultContract), new BasicHttpBinding (), new Uri ("http://localhost:" + port));
144                         host.Open ();
145                         try {
146                                 var cf = new ChannelFactory<ITestFaultContract> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:" + port));
147                                 var cli = cf.CreateChannel ();
148                                 
149                                 try {
150                                         cli.Run ("derivedexception");
151                                         Assert.Fail ("#6");
152                                 } catch (FaultException<PrivateAffairError> ex) {
153                                         // However, it is allowed to derive from FaultException<T>, provided
154                                         // that T is explicitly listed in [FaultContract].  Bug #7177.
155                                 }
156                         } finally {
157                                 host.Close ();
158                         }
159                 }
160
161                 [ServiceContract]
162                 public interface ITestFaultContract
163                 {
164                         [OperationContract]
165                         [FaultContract (typeof (PrivateAffairError), Action = "urn:myfault")]
166                         string Run (string input);
167                 }
168
169                 class TestFaultContract : ITestFaultContract
170                 {
171                         public string Run (string input)
172                         {
173                                 Assert.AreEqual (1, ContractDescription.GetContract (typeof (TestFaultContract)).Operations [0].Faults.Count, "s#0");
174
175                                 var dr = OperationContext.Current.EndpointDispatcher.DispatchRuntime;
176                                 Assert.AreEqual (1, dr.Operations.Count);
177                                 var dop = dr.Operations [0];
178                                 Assert.AreEqual ("Run", dop.Name, "s#1");
179                                 Assert.AreEqual (1, dop.FaultContractInfos.Count, "s#2");
180                                 var fci = dop.FaultContractInfos [0];
181                                 Assert.AreEqual (typeof (PrivateAffairError), fci.Detail, "s#3");
182                                 if (input.Equals ("default"))
183                                         throw new FaultException<PrivateAffairError> (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
184                                 else if (input.Equals ("deriveddata"))
185                                         throw new FaultException<DerivedPrivateAffairError> (new DerivedPrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
186                                 else if (input.Equals ("derivedexception"))
187                                         throw new DerivedFaultException (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
188                                 else
189                                         throw new FaultException ("Invalid operation");
190                         }
191                 }
192
193                 [DataContract]
194                 class PrivateAffairError
195                 {
196                         [DataMember]
197                         public int ErrorCode { get; set; }
198                         [DataMember]
199                         public string Text { get; set; }
200                 }
201
202                 [DataContract]
203                 class DerivedPrivateAffairError : PrivateAffairError
204                 {
205                 }
206
207                 class DerivedFaultException : FaultException<PrivateAffairError>
208                 {
209                         public DerivedFaultException (PrivateAffairError error)
210                                 : base (error)
211                         { }
212                 }
213         }
214 }