1 /* 2 * Copyright 2017 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; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.mock; 22 23 import com.google.common.base.Defaults; 24 import java.lang.reflect.Method; 25 import java.lang.reflect.Modifier; 26 import java.util.Collections; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 /** 32 * Unit tests for {@link ForwardingChannelBuilder}. 33 */ 34 @RunWith(JUnit4.class) 35 public class ForwardingChannelBuilderTest { 36 private final ManagedChannelBuilder<?> mockDelegate = mock(ManagedChannelBuilder.class); 37 38 private final ForwardingChannelBuilder<?> testChannelBuilder = new TestBuilder(); 39 40 private final class TestBuilder extends ForwardingChannelBuilder<TestBuilder> { 41 @Override delegate()42 protected ManagedChannelBuilder<?> delegate() { 43 return mockDelegate; 44 } 45 } 46 47 @Test allMethodsForwarded()48 public void allMethodsForwarded() throws Exception { 49 ForwardingTestUtil.testMethodsForwarded( 50 ManagedChannelBuilder.class, 51 mockDelegate, 52 testChannelBuilder, 53 Collections.<Method>emptyList(), 54 new ForwardingTestUtil.ArgumentProvider() { 55 @Override 56 public Object get(Method method, int argPos, Class<?> clazz) { 57 if (method.getName().equals("maxInboundMetadataSize")) { 58 assertThat(argPos).isEqualTo(0); 59 return 1; // an arbitrary positive number 60 } 61 return null; 62 } 63 }); 64 } 65 66 @Test allBuilderMethodsReturnThis()67 public void allBuilderMethodsReturnThis() throws Exception { 68 for (Method method : ManagedChannelBuilder.class.getDeclaredMethods()) { 69 if (Modifier.isStatic(method.getModifiers()) || Modifier.isPrivate(method.getModifiers())) { 70 continue; 71 } 72 if (method.getName().equals("build")) { 73 continue; 74 } 75 Class<?>[] argTypes = method.getParameterTypes(); 76 Object[] args = new Object[argTypes.length]; 77 for (int i = 0; i < argTypes.length; i++) { 78 args[i] = Defaults.defaultValue(argTypes[i]); 79 } 80 if (method.getName().equals("maxInboundMetadataSize")) { 81 args[0] = 1; // an arbitrary positive number 82 } 83 84 Object returnedValue = method.invoke(testChannelBuilder, args); 85 86 assertThat(returnedValue).isSameInstanceAs(testChannelBuilder); 87 } 88 } 89 90 @Test buildReturnsDelegateBuildByDefault()91 public void buildReturnsDelegateBuildByDefault() { 92 ManagedChannel mockChannel = mock(ManagedChannel.class); 93 doReturn(mockChannel).when(mockDelegate).build(); 94 95 assertThat(testChannelBuilder.build()).isSameInstanceAs(mockChannel); 96 } 97 } 98