Merge pull request #2585 from lambdageek/dev/bug-38222
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / ServiceKnownTypeAttributeTest.cs
1 //
2 // Author:
3 //      Atsushi Enomoto <atsushi@ximian.com>
4 //
5 // Copyright (C) 2011 Novell, Inc.  http://www.novell.com
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 using System;
27 using System.Collections.Generic;
28 using System.Linq;
29 using System.Reflection;
30 using System.ServiceModel;
31 using System.ServiceModel.Description;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.ServiceModel
35 {
36         [TestFixture]
37         public class ServiceKnownTypeAttributeTest
38         {
39                 [Test]
40                 public void MethodName ()
41                 {
42                         var cd = ContractDescription.GetContract (typeof (IService));
43                         var types = cd.Operations.First ().KnownTypes;
44                         Assert.AreEqual (1, types.Count, "#1");
45                         Assert.AreEqual (typeof (Bar), types [0], "#2");
46                 }
47
48                 [Test]
49                 [ExpectedException (typeof (InvalidOperationException))]
50                 public void MethodName2 ()
51                 {
52                         ContractDescription.GetContract (typeof (IService2));
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (InvalidOperationException))]
57                 public void MethodName3 ()
58                 {
59                         ContractDescription.GetContract (typeof (IService3));
60                 }
61
62                 [Test]
63                 [ExpectedException (typeof (InvalidOperationException))]
64                 public void MethodName4 ()
65                 {
66                         ContractDescription.GetContract (typeof (IService4));
67                 }
68
69                 public class Foo
70                 {
71                         public string S { get; set; }
72                 }
73
74                 public class Bar : Foo
75                 {
76                         public string T { get; set; }
77                 }
78
79                 class TypeProvider
80                 {
81                         static IEnumerable<Type> GetTypes (ICustomAttributeProvider provider)
82                         {
83                                 yield return typeof (Bar);
84                         }
85
86                         // wrong return value
87                         static Type GetTypes2 (ICustomAttributeProvider provider)
88                         {
89                                 return typeof (Bar);
90                         }
91
92                         // wrong argument
93                         static IEnumerable<Type> GetTypes3 ()
94                         {
95                                 yield return typeof (Bar);
96                         }
97
98                         // non-static
99                         public IEnumerable<Type> GetTypes4 ()
100                         {
101                                 yield return typeof (Bar);
102                         }
103                 }
104
105                 [ServiceKnownType ("GetTypes", typeof (TypeProvider))]
106                 [ServiceContract]
107                 public interface IService
108                 {
109                         [OperationContract]
110                         Foo X ();
111                 }
112
113                 [ServiceKnownType ("GetTypes2", typeof (TypeProvider))]
114                 [ServiceContract]
115                 public interface IService2
116                 {
117                         [OperationContract]
118                         Foo X ();
119                 }
120
121                 [ServiceKnownType ("GetTypes3", typeof (TypeProvider))]
122                 [ServiceContract]
123                 public interface IService3
124                 {
125                         [OperationContract]
126                         Foo X ();
127                 }
128
129                 [ServiceKnownType ("GetTypes4", typeof (TypeProvider))]
130                 [ServiceContract]
131                 public interface IService4
132                 {
133                         [OperationContract]
134                         Foo X ();
135                 }
136         }
137 }