OLD | NEW |
| (Empty) |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // http://code.google.com/p/protobuf/ | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
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 | |
14 // distribution. | |
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. | |
18 // | |
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. | |
30 | |
31 package com.google.protobuf.test; | |
32 import com.google.protobuf.*; | |
33 | |
34 import com.google.protobuf.Descriptors.FileDescriptor; | |
35 import com.google.protobuf.Descriptors.MethodDescriptor; | |
36 import google.protobuf.no_generic_services_test.UnittestNoGenericServices; | |
37 import protobuf_unittest.MessageWithNoOuter; | |
38 import protobuf_unittest.ServiceWithNoOuter; | |
39 import protobuf_unittest.UnittestProto.TestAllTypes; | |
40 import protobuf_unittest.UnittestProto.TestService; | |
41 import protobuf_unittest.UnittestProto.FooRequest; | |
42 import protobuf_unittest.UnittestProto.FooResponse; | |
43 import protobuf_unittest.UnittestProto.BarRequest; | |
44 import protobuf_unittest.UnittestProto.BarResponse; | |
45 | |
46 import org.easymock.classextension.EasyMock; | |
47 import org.easymock.classextension.IMocksControl; | |
48 import org.easymock.IArgumentMatcher; | |
49 | |
50 import java.util.HashSet; | |
51 import java.util.Set; | |
52 | |
53 import junit.framework.TestCase; | |
54 | |
55 /** | |
56 * Tests services and stubs. | |
57 * | |
58 * @author kenton@google.com Kenton Varda | |
59 */ | |
60 public class ServiceTest extends TestCase { | |
61 private IMocksControl control; | |
62 private RpcController mockController; | |
63 | |
64 private final Descriptors.MethodDescriptor fooDescriptor = | |
65 TestService.getDescriptor().getMethods().get(0); | |
66 private final Descriptors.MethodDescriptor barDescriptor = | |
67 TestService.getDescriptor().getMethods().get(1); | |
68 | |
69 @Override | |
70 protected void setUp() throws Exception { | |
71 super.setUp(); | |
72 control = EasyMock.createStrictControl(); | |
73 mockController = control.createMock(RpcController.class); | |
74 } | |
75 | |
76 // ================================================================= | |
77 | |
78 /** Tests Service.callMethod(). */ | |
79 public void testCallMethod() throws Exception { | |
80 FooRequest fooRequest = FooRequest.newBuilder().build(); | |
81 BarRequest barRequest = BarRequest.newBuilder().build(); | |
82 MockCallback<Message> fooCallback = new MockCallback<Message>(); | |
83 MockCallback<Message> barCallback = new MockCallback<Message>(); | |
84 TestService mockService = control.createMock(TestService.class); | |
85 | |
86 mockService.foo(EasyMock.same(mockController), EasyMock.same(fooRequest), | |
87 this.<FooResponse>wrapsCallback(fooCallback)); | |
88 mockService.bar(EasyMock.same(mockController), EasyMock.same(barRequest), | |
89 this.<BarResponse>wrapsCallback(barCallback)); | |
90 control.replay(); | |
91 | |
92 mockService.callMethod(fooDescriptor, mockController, | |
93 fooRequest, fooCallback); | |
94 mockService.callMethod(barDescriptor, mockController, | |
95 barRequest, barCallback); | |
96 control.verify(); | |
97 } | |
98 | |
99 /** Tests Service.get{Request,Response}Prototype(). */ | |
100 public void testGetPrototype() throws Exception { | |
101 TestService mockService = control.createMock(TestService.class); | |
102 | |
103 assertSame(mockService.getRequestPrototype(fooDescriptor), | |
104 FooRequest.getDefaultInstance()); | |
105 assertSame(mockService.getResponsePrototype(fooDescriptor), | |
106 FooResponse.getDefaultInstance()); | |
107 assertSame(mockService.getRequestPrototype(barDescriptor), | |
108 BarRequest.getDefaultInstance()); | |
109 assertSame(mockService.getResponsePrototype(barDescriptor), | |
110 BarResponse.getDefaultInstance()); | |
111 } | |
112 | |
113 /** Tests generated stubs. */ | |
114 public void testStub() throws Exception { | |
115 FooRequest fooRequest = FooRequest.newBuilder().build(); | |
116 BarRequest barRequest = BarRequest.newBuilder().build(); | |
117 MockCallback<FooResponse> fooCallback = new MockCallback<FooResponse>(); | |
118 MockCallback<BarResponse> barCallback = new MockCallback<BarResponse>(); | |
119 RpcChannel mockChannel = control.createMock(RpcChannel.class); | |
120 TestService stub = TestService.newStub(mockChannel); | |
121 | |
122 mockChannel.callMethod( | |
123 EasyMock.same(fooDescriptor), | |
124 EasyMock.same(mockController), | |
125 EasyMock.same(fooRequest), | |
126 EasyMock.same(FooResponse.getDefaultInstance()), | |
127 this.<Message>wrapsCallback(fooCallback)); | |
128 mockChannel.callMethod( | |
129 EasyMock.same(barDescriptor), | |
130 EasyMock.same(mockController), | |
131 EasyMock.same(barRequest), | |
132 EasyMock.same(BarResponse.getDefaultInstance()), | |
133 this.<Message>wrapsCallback(barCallback)); | |
134 control.replay(); | |
135 | |
136 stub.foo(mockController, fooRequest, fooCallback); | |
137 stub.bar(mockController, barRequest, barCallback); | |
138 control.verify(); | |
139 } | |
140 | |
141 /** Tests generated blocking stubs. */ | |
142 public void testBlockingStub() throws Exception { | |
143 FooRequest fooRequest = FooRequest.newBuilder().build(); | |
144 BarRequest barRequest = BarRequest.newBuilder().build(); | |
145 BlockingRpcChannel mockChannel = | |
146 control.createMock(BlockingRpcChannel.class); | |
147 TestService.BlockingInterface stub = | |
148 TestService.newBlockingStub(mockChannel); | |
149 | |
150 FooResponse fooResponse = FooResponse.newBuilder().build(); | |
151 BarResponse barResponse = BarResponse.newBuilder().build(); | |
152 | |
153 EasyMock.expect(mockChannel.callBlockingMethod( | |
154 EasyMock.same(fooDescriptor), | |
155 EasyMock.same(mockController), | |
156 EasyMock.same(fooRequest), | |
157 EasyMock.same(FooResponse.getDefaultInstance()))).andReturn(fooResponse); | |
158 EasyMock.expect(mockChannel.callBlockingMethod( | |
159 EasyMock.same(barDescriptor), | |
160 EasyMock.same(mockController), | |
161 EasyMock.same(barRequest), | |
162 EasyMock.same(BarResponse.getDefaultInstance()))).andReturn(barResponse); | |
163 control.replay(); | |
164 | |
165 assertSame(fooResponse, stub.foo(mockController, fooRequest)); | |
166 assertSame(barResponse, stub.bar(mockController, barRequest)); | |
167 control.verify(); | |
168 } | |
169 | |
170 public void testNewReflectiveService() { | |
171 ServiceWithNoOuter.Interface impl = | |
172 control.createMock(ServiceWithNoOuter.Interface.class); | |
173 RpcController controller = control.createMock(RpcController.class); | |
174 Service service = ServiceWithNoOuter.newReflectiveService(impl); | |
175 | |
176 MethodDescriptor fooMethod = | |
177 ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); | |
178 MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance(); | |
179 RpcCallback<Message> callback = new RpcCallback<Message>() { | |
180 public void run(Message parameter) { | |
181 // No reason this should be run. | |
182 fail(); | |
183 } | |
184 }; | |
185 RpcCallback<TestAllTypes> specializedCallback = | |
186 RpcUtil.specializeCallback(callback); | |
187 | |
188 impl.foo(EasyMock.same(controller), EasyMock.same(request), | |
189 EasyMock.same(specializedCallback)); | |
190 EasyMock.expectLastCall(); | |
191 | |
192 control.replay(); | |
193 | |
194 service.callMethod(fooMethod, controller, request, callback); | |
195 | |
196 control.verify(); | |
197 } | |
198 | |
199 public void testNewReflectiveBlockingService() throws ServiceException { | |
200 ServiceWithNoOuter.BlockingInterface impl = | |
201 control.createMock(ServiceWithNoOuter.BlockingInterface.class); | |
202 RpcController controller = control.createMock(RpcController.class); | |
203 BlockingService service = | |
204 ServiceWithNoOuter.newReflectiveBlockingService(impl); | |
205 | |
206 MethodDescriptor fooMethod = | |
207 ServiceWithNoOuter.getDescriptor().findMethodByName("Foo"); | |
208 MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance(); | |
209 | |
210 TestAllTypes expectedResponse = TestAllTypes.getDefaultInstance(); | |
211 EasyMock.expect(impl.foo(EasyMock.same(controller), EasyMock.same(request))) | |
212 .andReturn(expectedResponse); | |
213 | |
214 control.replay(); | |
215 | |
216 Message response = | |
217 service.callBlockingMethod(fooMethod, controller, request); | |
218 assertEquals(expectedResponse, response); | |
219 | |
220 control.verify(); | |
221 } | |
222 | |
223 public void testNoGenericServices() throws Exception { | |
224 // Non-services should be usable. | |
225 UnittestNoGenericServices.TestMessage message = | |
226 UnittestNoGenericServices.TestMessage.newBuilder() | |
227 .setA(123) | |
228 .setExtension(UnittestNoGenericServices.testExtension, 456) | |
229 .build(); | |
230 assertEquals(123, message.getA()); | |
231 assertEquals(1, UnittestNoGenericServices.TestEnum.FOO.getNumber()); | |
232 | |
233 // Build a list of the class names nested in UnittestNoGenericServices. | |
234 String outerName = "google.protobuf.no_generic_services_test." + | |
235 "UnittestNoGenericServices"; | |
236 Class<?> outerClass = Class.forName(outerName); | |
237 | |
238 Set<String> innerClassNames = new HashSet<String>(); | |
239 for (Class<?> innerClass : outerClass.getClasses()) { | |
240 String fullName = innerClass.getName(); | |
241 // Figure out the unqualified name of the inner class. | |
242 // Note: Surprisingly, the full name of an inner class will be separated | |
243 // from the outer class name by a '$' rather than a '.'. This is not | |
244 // mentioned in the documentation for java.lang.Class. I don't want to | |
245 // make assumptions, so I'm just going to accept any character as the | |
246 // separator. | |
247 assertTrue(fullName.startsWith(outerName)); | |
248 | |
249 if (!Service.class.isAssignableFrom(innerClass) && | |
250 !Message.class.isAssignableFrom(innerClass) && | |
251 !ProtocolMessageEnum.class.isAssignableFrom(innerClass)) { | |
252 // Ignore any classes not generated by the base code generator. | |
253 continue; | |
254 } | |
255 | |
256 innerClassNames.add(fullName.substring(outerName.length() + 1)); | |
257 } | |
258 | |
259 // No service class should have been generated. | |
260 assertTrue(innerClassNames.contains("TestMessage")); | |
261 assertTrue(innerClassNames.contains("TestEnum")); | |
262 assertFalse(innerClassNames.contains("TestService")); | |
263 | |
264 // But descriptors are there. | |
265 FileDescriptor file = UnittestNoGenericServices.getDescriptor(); | |
266 assertEquals(1, file.getServices().size()); | |
267 assertEquals("TestService", file.getServices().get(0).getName()); | |
268 assertEquals(1, file.getServices().get(0).getMethods().size()); | |
269 assertEquals("Foo", | |
270 file.getServices().get(0).getMethods().get(0).getName()); | |
271 } | |
272 | |
273 // ================================================================= | |
274 | |
275 /** | |
276 * wrapsCallback() is an EasyMock argument predicate. wrapsCallback(c) | |
277 * matches a callback if calling that callback causes c to be called. | |
278 * In other words, c wraps the given callback. | |
279 */ | |
280 private <Type extends Message> RpcCallback<Type> wrapsCallback( | |
281 MockCallback<?> callback) { | |
282 EasyMock.reportMatcher(new WrapsCallback(callback)); | |
283 return null; | |
284 } | |
285 | |
286 /** The parameter to wrapsCallback() must be a MockCallback. */ | |
287 private static class MockCallback<Type extends Message> | |
288 implements RpcCallback<Type> { | |
289 private boolean called = false; | |
290 | |
291 public boolean isCalled() { return called; } | |
292 | |
293 public void reset() { called = false; } | |
294 public void run(Type message) { called = true; } | |
295 } | |
296 | |
297 /** Implementation of the wrapsCallback() argument matcher. */ | |
298 private static class WrapsCallback implements IArgumentMatcher { | |
299 private MockCallback<?> callback; | |
300 | |
301 public WrapsCallback(MockCallback<?> callback) { | |
302 this.callback = callback; | |
303 } | |
304 | |
305 @SuppressWarnings("unchecked") | |
306 public boolean matches(Object actual) { | |
307 if (!(actual instanceof RpcCallback)) { | |
308 return false; | |
309 } | |
310 RpcCallback actualCallback = (RpcCallback)actual; | |
311 | |
312 callback.reset(); | |
313 actualCallback.run(null); | |
314 return callback.isCalled(); | |
315 } | |
316 | |
317 public void appendTo(StringBuffer buffer) { | |
318 buffer.append("wrapsCallback(mockCallback)"); | |
319 } | |
320 } | |
321 } | |
OLD | NEW |