csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 1 | #region Copyright notice and license
|
csharptest | f1816be | 2011-05-19 12:01:16 -0500 | [diff] [blame] | 2 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 3 | // Protocol Buffers - Google's data interchange format
|
| 4 | // Copyright 2008 Google Inc. All rights reserved.
|
| 5 | // http://github.com/jskeet/dotnet-protobufs/
|
| 6 | // Original C++/Java/Python code:
|
| 7 | // http://code.google.com/p/protobuf/
|
| 8 | //
|
| 9 | // Redistribution and use in source and binary forms, with or without
|
| 10 | // modification, are permitted provided that the following conditions are
|
| 11 | // met:
|
| 12 | //
|
| 13 | // * Redistributions of source code must retain the above copyright
|
| 14 | // notice, this list of conditions and the following disclaimer.
|
| 15 | // * Redistributions in binary form must reproduce the above
|
| 16 | // copyright notice, this list of conditions and the following disclaimer
|
| 17 | // in the documentation and/or other materials provided with the
|
| 18 | // distribution.
|
| 19 | // * Neither the name of Google Inc. nor the names of its
|
| 20 | // contributors may be used to endorse or promote products derived from
|
| 21 | // this software without specific prior written permission.
|
| 22 | //
|
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 34 |
|
| 35 | #endregion
|
| 36 |
|
| 37 | using System;
|
| 38 | using Google.ProtocolBuffers.Descriptors;
|
| 39 | using Google.ProtocolBuffers.TestProtos;
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame^] | 40 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 41 | using Rhino.Mocks;
|
| 42 | using Rhino.Mocks.Constraints;
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame^] | 43 | using Is = Rhino.Mocks.Constraints.Is;
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 44 |
|
| 45 | namespace Google.ProtocolBuffers
|
| 46 | {
|
| 47 | /// <summary>
|
| 48 | /// Tests for generated service classes.
|
| 49 | /// TODO(jonskeet): Convert the mocking tests using Rhino.Mocks.
|
| 50 | /// </summary>
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame^] | 51 | [TestClass]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 52 | public class ServiceTest
|
| 53 | {
|
| 54 | private delegate void Action<T1, T2>(T1 t1, T2 t2);
|
| 55 |
|
| 56 | private static readonly MethodDescriptor FooDescriptor = TestGenericService.Descriptor.Methods[0];
|
| 57 | private static readonly MethodDescriptor BarDescriptor = TestGenericService.Descriptor.Methods[1];
|
| 58 |
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame^] | 59 | [TestMethod]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 60 | public void GetRequestPrototype()
|
| 61 | {
|
| 62 | TestGenericService service = new TestServiceImpl();
|
| 63 |
|
| 64 | Assert.AreSame(service.GetRequestPrototype(FooDescriptor), FooRequest.DefaultInstance);
|
| 65 | Assert.AreSame(service.GetRequestPrototype(BarDescriptor), BarRequest.DefaultInstance);
|
| 66 | }
|
| 67 |
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame^] | 68 | [TestMethod]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 69 | public void GetResponsePrototype()
|
| 70 | {
|
| 71 | TestGenericService service = new TestServiceImpl();
|
| 72 |
|
| 73 | Assert.AreSame(service.GetResponsePrototype(FooDescriptor), FooResponse.DefaultInstance);
|
| 74 | Assert.AreSame(service.GetResponsePrototype(BarDescriptor), BarResponse.DefaultInstance);
|
| 75 | }
|
| 76 |
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame^] | 77 | [TestMethod]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 78 | public void CallMethodFoo()
|
| 79 | {
|
| 80 | MockRepository mocks = new MockRepository();
|
| 81 | FooRequest fooRequest = FooRequest.CreateBuilder().Build();
|
| 82 | FooResponse fooResponse = FooResponse.CreateBuilder().Build();
|
| 83 | IRpcController controller = mocks.StrictMock<IRpcController>();
|
| 84 |
|
| 85 | bool fooCalled = false;
|
| 86 |
|
| 87 | TestGenericService service = new TestServiceImpl((request, responseAction) =>
|
| 88 | {
|
| 89 | Assert.AreSame(fooRequest, request);
|
| 90 | fooCalled = true;
|
| 91 | responseAction(fooResponse);
|
| 92 | }, null, controller);
|
| 93 |
|
| 94 | bool doneHandlerCalled = false;
|
| 95 | Action<IMessage> doneHandler = (response =>
|
| 96 | {
|
| 97 | Assert.AreSame(fooResponse, response);
|
| 98 | doneHandlerCalled = true;
|
| 99 | });
|
| 100 |
|
| 101 | using (mocks.Record())
|
| 102 | {
|
| 103 | // No mock interactions to record
|
| 104 | }
|
| 105 |
|
| 106 | service.CallMethod(FooDescriptor, controller, fooRequest, doneHandler);
|
| 107 |
|
| 108 | Assert.IsTrue(doneHandlerCalled);
|
| 109 | Assert.IsTrue(fooCalled);
|
| 110 | mocks.VerifyAll();
|
| 111 | }
|
| 112 |
|
| 113 | private delegate void CallFooDelegate(MethodDescriptor descriptor, IRpcController controller,
|
| 114 | IMessage request, IMessage response, Action<IMessage> doneHandler);
|
| 115 |
|
| 116 | /// <summary>
|
| 117 | /// Tests the generated stub handling of Foo. By this stage we're reasonably confident
|
| 118 | /// that the choice between Foo and Bar is arbitrary, hence the lack of a corresponding Bar
|
| 119 | /// test.
|
| 120 | /// </summary>
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame^] | 121 | [TestMethod]
|
| 122 | [Ignore, System.ComponentModel.Description("Crashes Mono - needs further investigation")]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 123 | public void GeneratedStubFooCall()
|
| 124 | {
|
| 125 | FooRequest fooRequest = FooRequest.CreateBuilder().Build();
|
| 126 | MockRepository mocks = new MockRepository();
|
| 127 | IRpcChannel mockChannel = mocks.StrictMock<IRpcChannel>();
|
| 128 | IRpcController mockController = mocks.StrictMock<IRpcController>();
|
| 129 | TestGenericService service = TestGenericService.CreateStub(mockChannel);
|
| 130 | Action<FooResponse> doneHandler = mocks.StrictMock<Action<FooResponse>>();
|
| 131 |
|
| 132 | using (mocks.Record())
|
| 133 | {
|
| 134 | // Nasty way of mocking out "the channel calls the done handler".
|
| 135 | Expect.Call(() => mockChannel.CallMethod(null, null, null, null, null))
|
| 136 | .IgnoreArguments()
|
| 137 | .Constraints(Is.Same(FooDescriptor), Is.Same(mockController), Is.Same(fooRequest),
|
| 138 | Is.Same(FooResponse.DefaultInstance), Is.Anything())
|
| 139 | .Do((CallFooDelegate) ((p1, p2, p3, response, done) => done(response)));
|
| 140 | doneHandler(FooResponse.DefaultInstance);
|
| 141 | }
|
| 142 |
|
| 143 | service.Foo(mockController, fooRequest, doneHandler);
|
| 144 |
|
| 145 | mocks.VerifyAll();
|
| 146 | }
|
| 147 |
|
csharptest | eac64a5 | 2011-10-04 13:43:26 -0500 | [diff] [blame^] | 148 | [TestMethod]
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 149 | public void CallMethodBar()
|
| 150 | {
|
| 151 | MockRepository mocks = new MockRepository();
|
| 152 | BarRequest barRequest = BarRequest.CreateBuilder().Build();
|
| 153 | BarResponse barResponse = BarResponse.CreateBuilder().Build();
|
| 154 | IRpcController controller = mocks.StrictMock<IRpcController>();
|
| 155 |
|
| 156 | bool barCalled = false;
|
| 157 |
|
| 158 | TestGenericService service = new TestServiceImpl(null, (request, responseAction) =>
|
| 159 | {
|
| 160 | Assert.AreSame(barRequest, request);
|
| 161 | barCalled = true;
|
| 162 | responseAction(barResponse);
|
| 163 | }, controller);
|
| 164 |
|
| 165 | bool doneHandlerCalled = false;
|
| 166 | Action<IMessage> doneHandler = (response =>
|
| 167 | {
|
| 168 | Assert.AreSame(barResponse, response);
|
| 169 | doneHandlerCalled = true;
|
| 170 | });
|
| 171 |
|
| 172 | using (mocks.Record())
|
| 173 | {
|
| 174 | // No mock interactions to record
|
| 175 | }
|
| 176 |
|
| 177 | service.CallMethod(BarDescriptor, controller, barRequest, doneHandler);
|
| 178 |
|
| 179 | Assert.IsTrue(doneHandlerCalled);
|
| 180 | Assert.IsTrue(barCalled);
|
| 181 | mocks.VerifyAll();
|
| 182 | }
|
| 183 |
|
| 184 |
|
| 185 | private class TestServiceImpl : TestGenericService
|
| 186 | {
|
| 187 | private readonly Action<FooRequest, Action<FooResponse>> fooHandler;
|
| 188 | private readonly Action<BarRequest, Action<BarResponse>> barHandler;
|
| 189 | private readonly IRpcController expectedController;
|
| 190 |
|
| 191 | internal TestServiceImpl()
|
| 192 | {
|
| 193 | }
|
| 194 |
|
| 195 | internal TestServiceImpl(Action<FooRequest, Action<FooResponse>> fooHandler,
|
| 196 | Action<BarRequest, Action<BarResponse>> barHandler,
|
| 197 | IRpcController expectedController)
|
| 198 | {
|
| 199 | this.fooHandler = fooHandler;
|
| 200 | this.barHandler = barHandler;
|
| 201 | this.expectedController = expectedController;
|
| 202 | }
|
| 203 |
|
| 204 | public override void Foo(IRpcController controller, FooRequest request, Action<FooResponse> done)
|
| 205 | {
|
| 206 | Assert.AreSame(expectedController, controller);
|
| 207 | fooHandler(request, done);
|
| 208 | }
|
| 209 |
|
| 210 | public override void Bar(IRpcController controller, BarRequest request, Action<BarResponse> done)
|
| 211 | {
|
| 212 | Assert.AreSame(expectedController, controller);
|
| 213 | barHandler(request, done);
|
| 214 | }
|
| 215 | }
|
| 216 | }
|
| 217 | } |