~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to protobuf/files/java/src/test/java/com/google/protobuf/ServiceTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
32
 
 
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;
44
 
 
45
 
import org.easymock.classextension.EasyMock;
46
 
import org.easymock.classextension.IMocksControl;
47
 
import org.easymock.IArgumentMatcher;
48
 
 
49
 
import java.util.HashSet;
50
 
import java.util.Set;
51
 
 
52
 
import junit.framework.TestCase;
53
 
 
54
 
/**
55
 
 * Tests services and stubs.
56
 
 *
57
 
 * @author kenton@google.com Kenton Varda
58
 
 */
59
 
public class ServiceTest extends TestCase {
60
 
  private IMocksControl control;
61
 
  private RpcController mockController;
62
 
 
63
 
  private final Descriptors.MethodDescriptor fooDescriptor =
64
 
    TestService.getDescriptor().getMethods().get(0);
65
 
  private final Descriptors.MethodDescriptor barDescriptor =
66
 
    TestService.getDescriptor().getMethods().get(1);
67
 
 
68
 
  @Override
69
 
  protected void setUp() throws Exception {
70
 
    super.setUp();
71
 
    control = EasyMock.createStrictControl();
72
 
    mockController = control.createMock(RpcController.class);
73
 
  }
74
 
 
75
 
  // =================================================================
76
 
 
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);
84
 
 
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));
89
 
    control.replay();
90
 
 
91
 
    mockService.callMethod(fooDescriptor, mockController,
92
 
                           fooRequest, fooCallback);
93
 
    mockService.callMethod(barDescriptor, mockController,
94
 
                           barRequest, barCallback);
95
 
    control.verify();
96
 
  }
97
 
 
98
 
  /** Tests Service.get{Request,Response}Prototype(). */
99
 
  public void testGetPrototype() throws Exception {
100
 
    TestService mockService = control.createMock(TestService.class);
101
 
 
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());
110
 
  }
111
 
 
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);
120
 
 
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));
133
 
    control.replay();
134
 
 
135
 
    stub.foo(mockController, fooRequest, fooCallback);
136
 
    stub.bar(mockController, barRequest, barCallback);
137
 
    control.verify();
138
 
  }
139
 
 
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);
148
 
 
149
 
    FooResponse fooResponse = FooResponse.newBuilder().build();
150
 
    BarResponse barResponse = BarResponse.newBuilder().build();
151
 
 
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);
162
 
    control.replay();
163
 
 
164
 
    assertSame(fooResponse, stub.foo(mockController, fooRequest));
165
 
    assertSame(barResponse, stub.bar(mockController, barRequest));
166
 
    control.verify();
167
 
  }
168
 
 
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);
174
 
 
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.
181
 
        fail();
182
 
      }
183
 
    };
184
 
    RpcCallback<TestAllTypes> specializedCallback =
185
 
        RpcUtil.specializeCallback(callback);
186
 
 
187
 
    impl.foo(EasyMock.same(controller), EasyMock.same(request),
188
 
        EasyMock.same(specializedCallback));
189
 
    EasyMock.expectLastCall();
190
 
 
191
 
    control.replay();
192
 
 
193
 
    service.callMethod(fooMethod, controller, request, callback);
194
 
 
195
 
    control.verify();
196
 
  }
197
 
 
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);
204
 
 
205
 
    MethodDescriptor fooMethod =
206
 
        ServiceWithNoOuter.getDescriptor().findMethodByName("Foo");
207
 
    MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance();
208
 
 
209
 
    TestAllTypes expectedResponse = TestAllTypes.getDefaultInstance();
210
 
    EasyMock.expect(impl.foo(EasyMock.same(controller), EasyMock.same(request)))
211
 
        .andReturn(expectedResponse);
212
 
 
213
 
    control.replay();
214
 
 
215
 
    Message response =
216
 
        service.callBlockingMethod(fooMethod, controller, request);
217
 
    assertEquals(expectedResponse, response);
218
 
 
219
 
    control.verify();
220
 
  }
221
 
 
222
 
  public void testNoGenericServices() throws Exception {
223
 
    // Non-services should be usable.
224
 
    UnittestNoGenericServices.TestMessage message =
225
 
      UnittestNoGenericServices.TestMessage.newBuilder()
226
 
        .setA(123)
227
 
        .setExtension(UnittestNoGenericServices.testExtension, 456)
228
 
        .build();
229
 
    assertEquals(123, message.getA());
230
 
    assertEquals(1, UnittestNoGenericServices.TestEnum.FOO.getNumber());
231
 
 
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);
236
 
 
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
245
 
      //   separator.
246
 
      assertTrue(fullName.startsWith(outerName));
247
 
      innerClassNames.add(fullName.substring(outerName.length() + 1));
248
 
    }
249
 
 
250
 
    // No service class should have been generated.
251
 
    assertTrue(innerClassNames.contains("TestMessage"));
252
 
    assertTrue(innerClassNames.contains("TestEnum"));
253
 
    assertFalse(innerClassNames.contains("TestService"));
254
 
 
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());
260
 
    assertEquals("Foo",
261
 
        file.getServices().get(0).getMethods().get(0).getName());
262
 
  }
263
 
 
264
 
  // =================================================================
265
 
 
266
 
  /**
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.
270
 
   */
271
 
  private <Type extends Message> RpcCallback<Type> wrapsCallback(
272
 
      MockCallback<?> callback) {
273
 
    EasyMock.reportMatcher(new WrapsCallback(callback));
274
 
    return null;
275
 
  }
276
 
 
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;
281
 
 
282
 
    public boolean isCalled() { return called; }
283
 
 
284
 
    public void reset() { called = false; }
285
 
    public void run(Type message) { called = true; }
286
 
  }
287
 
 
288
 
  /** Implementation of the wrapsCallback() argument matcher. */
289
 
  private static class WrapsCallback implements IArgumentMatcher {
290
 
    private MockCallback<?> callback;
291
 
 
292
 
    public WrapsCallback(MockCallback<?> callback) {
293
 
      this.callback = callback;
294
 
    }
295
 
 
296
 
    @SuppressWarnings("unchecked")
297
 
    public boolean matches(Object actual) {
298
 
      if (!(actual instanceof RpcCallback)) {
299
 
        return false;
300
 
      }
301
 
      RpcCallback actualCallback = (RpcCallback)actual;
302
 
 
303
 
      callback.reset();
304
 
      actualCallback.run(null);
305
 
      return callback.isCalled();
306
 
    }
307
 
 
308
 
    public void appendTo(StringBuffer buffer) {
309
 
      buffer.append("wrapsCallback(mockCallback)");
310
 
    }
311
 
  }
312
 
}