1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
9
// * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
15
// * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
package com.google.protobuf;
33
import com.google.protobuf.Descriptors.FileDescriptor;
34
import com.google.protobuf.Descriptors.MethodDescriptor;
35
import google.protobuf.no_generic_services_test.UnittestNoGenericServices;
36
import protobuf_unittest.MessageWithNoOuter;
37
import protobuf_unittest.ServiceWithNoOuter;
38
import protobuf_unittest.UnittestProto.TestAllTypes;
39
import protobuf_unittest.UnittestProto.TestService;
40
import protobuf_unittest.UnittestProto.FooRequest;
41
import protobuf_unittest.UnittestProto.FooResponse;
42
import protobuf_unittest.UnittestProto.BarRequest;
43
import protobuf_unittest.UnittestProto.BarResponse;
45
import org.easymock.classextension.EasyMock;
46
import org.easymock.classextension.IMocksControl;
47
import org.easymock.IArgumentMatcher;
49
import java.util.HashSet;
52
import junit.framework.TestCase;
55
* Tests services and stubs.
57
* @author kenton@google.com Kenton Varda
59
public class ServiceTest extends TestCase {
60
private IMocksControl control;
61
private RpcController mockController;
63
private final Descriptors.MethodDescriptor fooDescriptor =
64
TestService.getDescriptor().getMethods().get(0);
65
private final Descriptors.MethodDescriptor barDescriptor =
66
TestService.getDescriptor().getMethods().get(1);
69
protected void setUp() throws Exception {
71
control = EasyMock.createStrictControl();
72
mockController = control.createMock(RpcController.class);
75
// =================================================================
77
/** Tests Service.callMethod(). */
78
public void testCallMethod() throws Exception {
79
FooRequest fooRequest = FooRequest.newBuilder().build();
80
BarRequest barRequest = BarRequest.newBuilder().build();
81
MockCallback<Message> fooCallback = new MockCallback<Message>();
82
MockCallback<Message> barCallback = new MockCallback<Message>();
83
TestService mockService = control.createMock(TestService.class);
85
mockService.foo(EasyMock.same(mockController), EasyMock.same(fooRequest),
86
this.<FooResponse>wrapsCallback(fooCallback));
87
mockService.bar(EasyMock.same(mockController), EasyMock.same(barRequest),
88
this.<BarResponse>wrapsCallback(barCallback));
91
mockService.callMethod(fooDescriptor, mockController,
92
fooRequest, fooCallback);
93
mockService.callMethod(barDescriptor, mockController,
94
barRequest, barCallback);
98
/** Tests Service.get{Request,Response}Prototype(). */
99
public void testGetPrototype() throws Exception {
100
TestService mockService = control.createMock(TestService.class);
102
assertSame(mockService.getRequestPrototype(fooDescriptor),
103
FooRequest.getDefaultInstance());
104
assertSame(mockService.getResponsePrototype(fooDescriptor),
105
FooResponse.getDefaultInstance());
106
assertSame(mockService.getRequestPrototype(barDescriptor),
107
BarRequest.getDefaultInstance());
108
assertSame(mockService.getResponsePrototype(barDescriptor),
109
BarResponse.getDefaultInstance());
112
/** Tests generated stubs. */
113
public void testStub() throws Exception {
114
FooRequest fooRequest = FooRequest.newBuilder().build();
115
BarRequest barRequest = BarRequest.newBuilder().build();
116
MockCallback<FooResponse> fooCallback = new MockCallback<FooResponse>();
117
MockCallback<BarResponse> barCallback = new MockCallback<BarResponse>();
118
RpcChannel mockChannel = control.createMock(RpcChannel.class);
119
TestService stub = TestService.newStub(mockChannel);
121
mockChannel.callMethod(
122
EasyMock.same(fooDescriptor),
123
EasyMock.same(mockController),
124
EasyMock.same(fooRequest),
125
EasyMock.same(FooResponse.getDefaultInstance()),
126
this.<Message>wrapsCallback(fooCallback));
127
mockChannel.callMethod(
128
EasyMock.same(barDescriptor),
129
EasyMock.same(mockController),
130
EasyMock.same(barRequest),
131
EasyMock.same(BarResponse.getDefaultInstance()),
132
this.<Message>wrapsCallback(barCallback));
135
stub.foo(mockController, fooRequest, fooCallback);
136
stub.bar(mockController, barRequest, barCallback);
140
/** Tests generated blocking stubs. */
141
public void testBlockingStub() throws Exception {
142
FooRequest fooRequest = FooRequest.newBuilder().build();
143
BarRequest barRequest = BarRequest.newBuilder().build();
144
BlockingRpcChannel mockChannel =
145
control.createMock(BlockingRpcChannel.class);
146
TestService.BlockingInterface stub =
147
TestService.newBlockingStub(mockChannel);
149
FooResponse fooResponse = FooResponse.newBuilder().build();
150
BarResponse barResponse = BarResponse.newBuilder().build();
152
EasyMock.expect(mockChannel.callBlockingMethod(
153
EasyMock.same(fooDescriptor),
154
EasyMock.same(mockController),
155
EasyMock.same(fooRequest),
156
EasyMock.same(FooResponse.getDefaultInstance()))).andReturn(fooResponse);
157
EasyMock.expect(mockChannel.callBlockingMethod(
158
EasyMock.same(barDescriptor),
159
EasyMock.same(mockController),
160
EasyMock.same(barRequest),
161
EasyMock.same(BarResponse.getDefaultInstance()))).andReturn(barResponse);
164
assertSame(fooResponse, stub.foo(mockController, fooRequest));
165
assertSame(barResponse, stub.bar(mockController, barRequest));
169
public void testNewReflectiveService() {
170
ServiceWithNoOuter.Interface impl =
171
control.createMock(ServiceWithNoOuter.Interface.class);
172
RpcController controller = control.createMock(RpcController.class);
173
Service service = ServiceWithNoOuter.newReflectiveService(impl);
175
MethodDescriptor fooMethod =
176
ServiceWithNoOuter.getDescriptor().findMethodByName("Foo");
177
MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance();
178
RpcCallback<Message> callback = new RpcCallback<Message>() {
179
public void run(Message parameter) {
180
// No reason this should be run.
184
RpcCallback<TestAllTypes> specializedCallback =
185
RpcUtil.specializeCallback(callback);
187
impl.foo(EasyMock.same(controller), EasyMock.same(request),
188
EasyMock.same(specializedCallback));
189
EasyMock.expectLastCall();
193
service.callMethod(fooMethod, controller, request, callback);
198
public void testNewReflectiveBlockingService() throws ServiceException {
199
ServiceWithNoOuter.BlockingInterface impl =
200
control.createMock(ServiceWithNoOuter.BlockingInterface.class);
201
RpcController controller = control.createMock(RpcController.class);
202
BlockingService service =
203
ServiceWithNoOuter.newReflectiveBlockingService(impl);
205
MethodDescriptor fooMethod =
206
ServiceWithNoOuter.getDescriptor().findMethodByName("Foo");
207
MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance();
209
TestAllTypes expectedResponse = TestAllTypes.getDefaultInstance();
210
EasyMock.expect(impl.foo(EasyMock.same(controller), EasyMock.same(request)))
211
.andReturn(expectedResponse);
216
service.callBlockingMethod(fooMethod, controller, request);
217
assertEquals(expectedResponse, response);
222
public void testNoGenericServices() throws Exception {
223
// Non-services should be usable.
224
UnittestNoGenericServices.TestMessage message =
225
UnittestNoGenericServices.TestMessage.newBuilder()
227
.setExtension(UnittestNoGenericServices.testExtension, 456)
229
assertEquals(123, message.getA());
230
assertEquals(1, UnittestNoGenericServices.TestEnum.FOO.getNumber());
232
// Build a list of the class names nested in UnittestNoGenericServices.
233
String outerName = "google.protobuf.no_generic_services_test." +
234
"UnittestNoGenericServices";
235
Class<?> outerClass = Class.forName(outerName);
237
Set<String> innerClassNames = new HashSet<String>();
238
for (Class<?> innerClass : outerClass.getClasses()) {
239
String fullName = innerClass.getName();
240
// Figure out the unqualified name of the inner class.
241
// Note: Surprisingly, the full name of an inner class will be separated
242
// from the outer class name by a '$' rather than a '.'. This is not
243
// mentioned in the documentation for java.lang.Class. I don't want to
244
// make assumptions, so I'm just going to accept any character as the
246
assertTrue(fullName.startsWith(outerName));
247
innerClassNames.add(fullName.substring(outerName.length() + 1));
250
// No service class should have been generated.
251
assertTrue(innerClassNames.contains("TestMessage"));
252
assertTrue(innerClassNames.contains("TestEnum"));
253
assertFalse(innerClassNames.contains("TestService"));
255
// But descriptors are there.
256
FileDescriptor file = UnittestNoGenericServices.getDescriptor();
257
assertEquals(1, file.getServices().size());
258
assertEquals("TestService", file.getServices().get(0).getName());
259
assertEquals(1, file.getServices().get(0).getMethods().size());
261
file.getServices().get(0).getMethods().get(0).getName());
264
// =================================================================
267
* wrapsCallback() is an EasyMock argument predicate. wrapsCallback(c)
268
* matches a callback if calling that callback causes c to be called.
269
* In other words, c wraps the given callback.
271
private <Type extends Message> RpcCallback<Type> wrapsCallback(
272
MockCallback<?> callback) {
273
EasyMock.reportMatcher(new WrapsCallback(callback));
277
/** The parameter to wrapsCallback() must be a MockCallback. */
278
private static class MockCallback<Type extends Message>
279
implements RpcCallback<Type> {
280
private boolean called = false;
282
public boolean isCalled() { return called; }
284
public void reset() { called = false; }
285
public void run(Type message) { called = true; }
288
/** Implementation of the wrapsCallback() argument matcher. */
289
private static class WrapsCallback implements IArgumentMatcher {
290
private MockCallback<?> callback;
292
public WrapsCallback(MockCallback<?> callback) {
293
this.callback = callback;
296
@SuppressWarnings("unchecked")
297
public boolean matches(Object actual) {
298
if (!(actual instanceof RpcCallback)) {
301
RpcCallback actualCallback = (RpcCallback)actual;
304
actualCallback.run(null);
305
return callback.isCalled();
308
public void appendTo(StringBuffer buffer) {
309
buffer.append("wrapsCallback(mockCallback)");