• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.stub;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.mock;
25 
26 import io.grpc.CallOptions;
27 import io.grpc.Channel;
28 import java.util.concurrent.Executor;
29 import javax.annotation.Nullable;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnit;
34 import org.mockito.junit.MockitoRule;
35 
36 /** Standard unit tests for AbstractStub and its subclasses. */
37 abstract class BaseAbstractStubTest<T extends AbstractStub<T>> {
38   @Rule public final MockitoRule mocks = MockitoJUnit.rule();
39 
40   @Mock
41   Channel channel;
42 
create(Channel channel)43   T create(Channel channel) {
44     return create(channel, CallOptions.DEFAULT);
45   }
46 
create(@ullable Channel channel, @Nullable CallOptions callOptions)47   abstract T create(@Nullable Channel channel, @Nullable CallOptions callOptions);
48 
49   @Test
callOptionsMustNotBeNull()50   public void callOptionsMustNotBeNull() {
51     try {
52       create(channel, null);
53       fail("NullPointerException expected");
54     } catch (NullPointerException npe) {
55       // expected
56     }
57   }
58 
59   @Test
channelMustNotBeNull2()60   public void channelMustNotBeNull2() {
61     try {
62       create(null, CallOptions.DEFAULT);
63       fail("NullPointerException expected");
64     } catch (NullPointerException npe) {
65       // expected
66     }
67   }
68 
69   @Test
withWaitForReady()70   public void withWaitForReady() {
71     T stub = create(channel);
72     CallOptions callOptions = stub.getCallOptions();
73     assertFalse(callOptions.isWaitForReady());
74 
75     stub = stub.withWaitForReady();
76     callOptions = stub.getCallOptions();
77     assertTrue(callOptions.isWaitForReady());
78   }
79 
80   @Test
withExecutor()81   public void withExecutor() {
82     T stub = create(channel);
83     CallOptions callOptions = stub.getCallOptions();
84 
85     assertNull(callOptions.getExecutor());
86 
87     Executor executor = mock(Executor.class);
88     stub = stub.withExecutor(executor);
89     callOptions = stub.getCallOptions();
90 
91     assertEquals(callOptions.getExecutor(), executor);
92   }
93 }
94