[corlib] Assume UTC if no $TZ set. Fixes #30360
[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 namespace MonoTests.System.ServiceModel.Dispatcher
41 {
42         [TestFixture]
43         public class DispatchOperationTest
44         {
45                 [Test]
46                 [ExpectedException (typeof (ArgumentNullException))]
47                 public void TestConstructorNullName ()
48                 {
49                         new DispatchOperation (CreateRuntime (), null, null);
50                 }
51
52                 [Test]
53                 public void TestConstructorNullAction ()
54                 {
55                         DispatchRuntime r = CreateRuntime ();
56                         // null Action is allowed.
57                         new DispatchOperation (r, String.Empty, null);
58                         // null ReplyAction as well.
59                         new DispatchOperation (r, String.Empty, null, null);
60                 }
61
62                 [Test]
63                 public void TestConstructor ()
64                 {
65                         DispatchOperation o = new DispatchOperation (
66                                 CreateRuntime (), String.Empty, null, null);
67                         Assert.IsTrue (o.DeserializeRequest, "#1");
68                         Assert.IsTrue (o.SerializeReply, "#2");
69                         Assert.IsNull (o.Formatter, "#3");
70                         Assert.AreEqual (0, o.FaultContractInfos.Count, "#4");
71                         Assert.IsNull (o.Invoker, "#5");
72                         Assert.IsFalse (o.IsOneWay, "#6");
73                         Assert.IsFalse (o.IsTerminating, "#7");
74                         Assert.IsFalse (o.ReleaseInstanceBeforeCall, "#8");
75                         Assert.IsFalse (o.ReleaseInstanceAfterCall, "#9");
76                         Assert.IsFalse (o.TransactionAutoComplete, "#10");
77                         Assert.IsFalse (o.TransactionRequired, "#11");
78                         Assert.AreEqual (0, o.ParameterInspectors.Count, "#12");
79                 }
80
81                 DispatchRuntime CreateRuntime ()
82                 {
83                         return new EndpointDispatcher (
84                                 new EndpointAddress ("http://localhost:8080"), "IFoo", "urn:foo").DispatchRuntime;
85                 }
86
87                 [Test]
88                 public void FaultContractInfos ()
89                 {
90                         var host = new ServiceHost (typeof (TestFaultContract));
91                         host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = false;
92                         host.AddServiceEndpoint (typeof (ITestFaultContract), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
93                         host.Open ();
94                         try {
95                                 var cf = new ChannelFactory<ITestFaultContract> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:37564"));
96                                 var cli = cf.CreateChannel ();
97                                 try {
98                                         cli.Run ("default");
99                                         Assert.Fail ("#1");
100                                 } catch (FaultException<PrivateAffairError> ex) {
101                                         var p  = ex.Detail;
102                                         Assert.AreEqual (5, p.ErrorCode, "#2");
103                                         Assert.AreEqual ("foobarerror", p.Text, "#3");
104                                 }
105
106                                 try {
107                                         cli.Run ("deriveddata");
108                                         Assert.Fail ("#4");
109                                 } catch (Exception ex) {
110                                         // The type must be explicitly listed in the [FaultContract],
111                                         // it is not allowed to use a subclass of the exception data type.
112                                         Assert.AreEqual (typeof (FaultException), ex.GetType (), "#5");
113                                 }
114
115                                 try {
116                                         cli.Run ("derivedexception");
117                                         Assert.Fail ("#6");
118                                 } catch (Exception ex) {
119                                         // However, it is allowed to derive from FaultException<T>, provided
120                                         // that T is explicitly listed in [FaultContract].  Bug #7177.
121                                         Assert.AreEqual (typeof (FaultException<PrivateAffairError>), ex.GetType (), "#7");
122                                 }
123                         } finally {
124                                 host.Close ();
125                         }
126                 }
127
128                 [ServiceContract]
129                 public interface ITestFaultContract
130                 {
131                         [OperationContract]
132                         [FaultContract (typeof (PrivateAffairError), Action = "urn:myfault")]
133                         string Run (string input);
134                 }
135
136                 class TestFaultContract : ITestFaultContract
137                 {
138                         public string Run (string input)
139                         {
140                                 Assert.AreEqual (1, ContractDescription.GetContract (typeof (TestFaultContract)).Operations [0].Faults.Count, "s#0");
141
142                                 var dr = OperationContext.Current.EndpointDispatcher.DispatchRuntime;
143                                 Assert.AreEqual (1, dr.Operations.Count);
144                                 var dop = dr.Operations [0];
145                                 Assert.AreEqual ("Run", dop.Name, "s#1");
146                                 Assert.AreEqual (1, dop.FaultContractInfos.Count, "s#2");
147                                 var fci = dop.FaultContractInfos [0];
148                                 Assert.AreEqual (typeof (PrivateAffairError), fci.Detail, "s#3");
149                                 if (input.Equals ("default"))
150                                         throw new FaultException<PrivateAffairError> (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
151                                 else if (input.Equals ("deriveddata"))
152                                         throw new FaultException<DerivedPrivateAffairError> (new DerivedPrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
153                                 else if (input.Equals ("derivedexception"))
154                                         throw new DerivedFaultException (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
155                                 else
156                                         throw new FaultException ("Invalid operation");
157                         }
158                 }
159
160                 [DataContract]
161                 class PrivateAffairError
162                 {
163                         [DataMember]
164                         public int ErrorCode { get; set; }
165                         [DataMember]
166                         public string Text { get; set; }
167                 }
168
169                 [DataContract]
170                 class DerivedPrivateAffairError : PrivateAffairError
171                 {
172                 }
173
174                 class DerivedFaultException : FaultException<PrivateAffairError>
175                 {
176                         public DerivedFaultException (PrivateAffairError error)
177                                 : base (error)
178                         { }
179                 }
180         }
181 }