Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. |
| 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | using System; |
| 17 | using Google.ProtocolBuffers.Descriptors; |
| 18 | using Google.ProtocolBuffers.TestProtos; |
| 19 | using NUnit.Framework; |
| 20 | using Rhino.Mocks; |
| 21 | using Rhino.Mocks.Constraints; |
| 22 | |
| 23 | namespace Google.ProtocolBuffers { |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Tests for generated service classes. |
| 27 | /// TODO(jonskeet): Convert the mocking tests using Rhino.Mocks. |
| 28 | /// </summary> |
| 29 | [TestFixture] |
| 30 | public class ServiceTest { |
| 31 | |
| 32 | delegate void Action<T1, T2>(T1 t1, T2 t2); |
| 33 | |
| 34 | private static readonly MethodDescriptor FooDescriptor = TestService.Descriptor.Methods[0]; |
| 35 | private static readonly MethodDescriptor BarDescriptor = TestService.Descriptor.Methods[1]; |
| 36 | |
| 37 | [Test] |
| 38 | public void GetRequestPrototype() { |
| 39 | TestService service = new TestServiceImpl(); |
| 40 | |
| 41 | Assert.AreSame(service.GetRequestPrototype(FooDescriptor), FooRequest.DefaultInstance); |
| 42 | Assert.AreSame(service.GetRequestPrototype(BarDescriptor), BarRequest.DefaultInstance); |
| 43 | } |
| 44 | |
| 45 | [Test] |
| 46 | public void GetResponsePrototype() { |
| 47 | TestService service = new TestServiceImpl(); |
| 48 | |
| 49 | Assert.AreSame(service.GetResponsePrototype(FooDescriptor), FooResponse.DefaultInstance); |
| 50 | Assert.AreSame(service.GetResponsePrototype(BarDescriptor), BarResponse.DefaultInstance); |
| 51 | } |
| 52 | |
| 53 | [Test] |
| 54 | public void CallMethodFoo() { |
| 55 | MockRepository mocks = new MockRepository(); |
| 56 | FooRequest fooRequest = FooRequest.CreateBuilder().Build(); |
| 57 | FooResponse fooResponse = FooResponse.CreateBuilder().Build(); |
| 58 | IRpcController controller = mocks.StrictMock<IRpcController>(); |
| 59 | |
| 60 | bool fooCalled = false; |
| 61 | |
| 62 | TestService service = new TestServiceImpl((request, responseAction) => { |
| 63 | Assert.AreSame(fooRequest, request); |
| 64 | fooCalled = true; |
| 65 | responseAction(fooResponse); |
| 66 | }, null, controller); |
| 67 | |
| 68 | bool doneHandlerCalled = false; |
| 69 | Action<IMessage> doneHandler = (response => { |
| 70 | Assert.AreSame(fooResponse, response); |
| 71 | doneHandlerCalled = true; |
| 72 | }); |
| 73 | |
| 74 | using (mocks.Record()) { |
| 75 | // No mock interactions to record |
| 76 | } |
| 77 | |
| 78 | service.CallMethod(FooDescriptor, controller, fooRequest, doneHandler); |
| 79 | |
| 80 | Assert.IsTrue(doneHandlerCalled); |
| 81 | Assert.IsTrue(fooCalled); |
| 82 | mocks.VerifyAll(); |
| 83 | } |
| 84 | |
| 85 | delegate void CallFooDelegate(MethodDescriptor descriptor, IRpcController controller, |
| 86 | IMessage request, IMessage response, Action<IMessage> doneHandler); |
| 87 | |
| 88 | /// <summary> |
| 89 | /// Tests the generated stub handling of Foo. By this stage we're reasonably confident |
| 90 | /// that the choice between Foo and Bar is arbitrary, hence the lack of a corresponding Bar |
| 91 | /// test. |
| 92 | /// </summary> |
| 93 | [Test] |
| 94 | public void GeneratedStubFooCall() { |
| 95 | FooRequest fooRequest = FooRequest.CreateBuilder().Build(); |
| 96 | MockRepository mocks = new MockRepository(); |
| 97 | IRpcChannel mockChannel = mocks.StrictMock<IRpcChannel>(); |
| 98 | IRpcController mockController = mocks.StrictMock<IRpcController>(); |
| 99 | TestService service = TestService.CreateStub(mockChannel); |
| 100 | Action<FooResponse> doneHandler = mocks.StrictMock<Action<FooResponse>>(); |
| 101 | |
| 102 | using (mocks.Record()) { |
| 103 | |
| 104 | // Nasty way of mocking out "the channel calls the done handler". |
| 105 | Expect.Call(() => mockChannel.CallMethod(null, null, null, null, null)) |
| 106 | .IgnoreArguments() |
| 107 | .Constraints(Is.Same(FooDescriptor), Is.Same(mockController), Is.Same(fooRequest), |
| 108 | Is.Same(FooResponse.DefaultInstance), Is.Anything()) |
| 109 | .Do((CallFooDelegate) ((p1, p2, p3, response, done) => done(response))); |
| 110 | doneHandler.Invoke(FooResponse.DefaultInstance); |
| 111 | } |
| 112 | |
| 113 | service.Foo(mockController, fooRequest, doneHandler); |
| 114 | |
| 115 | mocks.VerifyAll(); |
| 116 | } |
| 117 | |
| 118 | [Test] |
| 119 | public void CallMethodBar() { |
| 120 | MockRepository mocks = new MockRepository(); |
| 121 | BarRequest barRequest = BarRequest.CreateBuilder().Build(); |
| 122 | BarResponse barResponse = BarResponse.CreateBuilder().Build(); |
| 123 | IRpcController controller = mocks.StrictMock<IRpcController>(); |
| 124 | |
| 125 | bool barCalled = false; |
| 126 | |
| 127 | TestService service = new TestServiceImpl(null, (request, responseAction) => { |
| 128 | Assert.AreSame(barRequest, request); |
| 129 | barCalled = true; |
| 130 | responseAction(barResponse); |
| 131 | }, controller); |
| 132 | |
| 133 | bool doneHandlerCalled = false; |
| 134 | Action<IMessage> doneHandler = (response => { |
| 135 | Assert.AreSame(barResponse, response); |
| 136 | doneHandlerCalled = true; |
| 137 | }); |
| 138 | |
| 139 | using (mocks.Record()) { |
| 140 | // No mock interactions to record |
| 141 | } |
| 142 | |
| 143 | service.CallMethod(BarDescriptor, controller, barRequest, doneHandler); |
| 144 | |
| 145 | Assert.IsTrue(doneHandlerCalled); |
| 146 | Assert.IsTrue(barCalled); |
| 147 | mocks.VerifyAll(); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | class TestServiceImpl : TestService { |
| 152 | private readonly Action<FooRequest, Action<FooResponse>> fooHandler; |
| 153 | private readonly Action<BarRequest, Action<BarResponse>> barHandler; |
| 154 | private readonly IRpcController expectedController; |
| 155 | |
| 156 | internal TestServiceImpl() { |
| 157 | } |
| 158 | |
| 159 | internal TestServiceImpl(Action<FooRequest, Action<FooResponse>> fooHandler, |
| 160 | Action<BarRequest, Action<BarResponse>> barHandler, |
| 161 | IRpcController expectedController) { |
| 162 | this.fooHandler = fooHandler; |
| 163 | this.barHandler = barHandler; |
| 164 | this.expectedController = expectedController; |
| 165 | } |
| 166 | |
| 167 | public override void Foo(IRpcController controller, FooRequest request, Action<FooResponse> done) { |
| 168 | Assert.AreSame(expectedController, controller); |
| 169 | fooHandler(request, done); |
| 170 | } |
| 171 | |
| 172 | public override void Bar(IRpcController controller, BarRequest request, Action<BarResponse> done) { |
| 173 | Assert.AreSame(expectedController, controller); |
| 174 | barHandler(request, done); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |