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