1 /* 2 * Copyright 2020 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 ForwardingServerBuilder}. 33 */ 34 @RunWith(JUnit4.class) 35 public class ForwardingServerBuilderTest { 36 private final ServerBuilder<?> mockDelegate = mock(ServerBuilder.class); 37 private final ForwardingServerBuilder<?> testServerBuilder = new TestBuilder(); 38 39 private final class TestBuilder extends ForwardingServerBuilder<TestBuilder> { 40 @Override delegate()41 protected ServerBuilder<?> delegate() { 42 return mockDelegate; 43 } 44 } 45 46 @Test allMethodsForwarded()47 public void allMethodsForwarded() throws Exception { 48 ForwardingTestUtil.testMethodsForwarded( 49 ServerBuilder.class, 50 mockDelegate, 51 testServerBuilder, 52 Collections.<Method>emptyList()); 53 } 54 55 @Test allBuilderMethodsReturnThis()56 public void allBuilderMethodsReturnThis() throws Exception { 57 for (Method method : ServerBuilder.class.getDeclaredMethods()) { 58 if (Modifier.isStatic(method.getModifiers()) 59 || Modifier.isPrivate(method.getModifiers()) 60 || Modifier.isFinal(method.getModifiers())) { 61 continue; 62 } 63 if (method.getName().equals("build")) { 64 continue; 65 } 66 Class<?>[] argTypes = method.getParameterTypes(); 67 Object[] args = new Object[argTypes.length]; 68 for (int i = 0; i < argTypes.length; i++) { 69 args[i] = Defaults.defaultValue(argTypes[i]); 70 } 71 72 Object returnedValue = method.invoke(testServerBuilder, args); 73 74 assertThat(returnedValue).isSameInstanceAs(testServerBuilder); 75 } 76 } 77 78 @Test buildReturnsDelegateBuildByDefault()79 public void buildReturnsDelegateBuildByDefault() { 80 Server server = mock(Server.class); 81 doReturn(server).when(mockDelegate).build(); 82 83 assertThat(testServerBuilder.build()).isSameInstanceAs(server); 84 } 85 } 86