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 org.junit.Assert.assertSame; 20 import static org.junit.Assert.assertTrue; 21 22 import com.google.common.reflect.ClassPath; 23 import com.google.common.reflect.ClassPath.ClassInfo; 24 import com.google.common.truth.Truth; 25 import java.lang.reflect.Method; 26 import java.lang.reflect.Modifier; 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.List; 30 import org.junit.Assume; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.junit.runners.Parameterized; 34 import org.junit.runners.Parameterized.Parameter; 35 import org.junit.runners.Parameterized.Parameters; 36 37 /** 38 * Tests that Channel and Server builders properly hide the static constructors. 39 * 40 * <p>This test does nothing on Java 9. 41 */ 42 @RunWith(Parameterized.class) 43 public class ChannelAndServerBuilderTest { 44 45 @Parameter 46 public Class<?> builderClass; 47 48 /** 49 * Javadoc. 50 */ 51 @Parameters(name = "class={0}") params()52 public static Collection<Object[]> params() throws Exception { 53 ClassLoader loader = ChannelAndServerBuilderTest.class.getClassLoader(); 54 Collection<ClassInfo> classInfos = 55 ClassPath.from(loader).getTopLevelClassesRecursive("io.grpc"); 56 // Java 9 doesn't expose the URLClassLoader, which breaks searching through the classpath 57 if (classInfos.isEmpty()) { 58 return new ArrayList<>(); 59 } 60 List<Object[]> classes = new ArrayList<>(); 61 for (ClassInfo classInfo : classInfos) { 62 String className = classInfo.getName(); 63 if (className.contains("io.grpc.netty.shaded.io.netty")) { 64 continue; 65 } 66 Class<?> clazz = Class.forName(className, false /*initialize*/, loader); 67 if (ServerBuilder.class.isAssignableFrom(clazz) && clazz != ServerBuilder.class) { 68 classes.add(new Object[]{clazz}); 69 } else if (ManagedChannelBuilder.class.isAssignableFrom(clazz) 70 && clazz != ManagedChannelBuilder.class) { 71 classes.add(new Object[]{clazz}); 72 } 73 } 74 Truth.assertWithMessage("Unable to find any builder classes").that(classes).isNotEmpty(); 75 return classes; 76 } 77 78 @Test serverBuilderHidesMethod_forPort()79 public void serverBuilderHidesMethod_forPort() throws Exception { 80 Assume.assumeTrue(ServerBuilder.class.isAssignableFrom(builderClass)); 81 Method method = builderClass.getMethod("forPort", int.class); 82 83 assertTrue(Modifier.isStatic(method.getModifiers())); 84 assertTrue(ServerBuilder.class.isAssignableFrom(method.getReturnType())); 85 assertSame(builderClass, method.getDeclaringClass()); 86 } 87 88 @Test channelBuilderHidesMethod_forAddress()89 public void channelBuilderHidesMethod_forAddress() throws Exception { 90 Assume.assumeTrue(ManagedChannelBuilder.class.isAssignableFrom(builderClass)); 91 Method method = builderClass.getMethod("forAddress", String.class, int.class); 92 93 assertTrue(Modifier.isStatic(method.getModifiers())); 94 assertTrue(ManagedChannelBuilder.class.isAssignableFrom(method.getReturnType())); 95 assertSame(builderClass, method.getDeclaringClass()); 96 } 97 98 @Test channelBuilderHidesMethod_forTarget()99 public void channelBuilderHidesMethod_forTarget() throws Exception { 100 Assume.assumeTrue(ManagedChannelBuilder.class.isAssignableFrom(builderClass)); 101 Method method = builderClass.getMethod("forTarget", String.class); 102 103 assertTrue(Modifier.isStatic(method.getModifiers())); 104 assertTrue(ManagedChannelBuilder.class.isAssignableFrom(method.getReturnType())); 105 assertSame(builderClass, method.getDeclaringClass()); 106 } 107 } 108