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<Object[]>(); 59 } 60 List<Object[]> classes = new ArrayList<Object[]>(); 61 for (ClassInfo classInfo : classInfos) { 62 Class<?> clazz = Class.forName(classInfo.getName(), false /*initialize*/, loader); 63 if (ServerBuilder.class.isAssignableFrom(clazz) && clazz != ServerBuilder.class) { 64 classes.add(new Object[]{clazz}); 65 } else if (ManagedChannelBuilder.class.isAssignableFrom(clazz) 66 && clazz != ManagedChannelBuilder.class) { 67 classes.add(new Object[]{clazz}); 68 } 69 } 70 Truth.assertWithMessage("Unable to find any builder classes").that(classes).isNotEmpty(); 71 return classes; 72 } 73 74 @Test serverBuilderHidesMethod_forPort()75 public void serverBuilderHidesMethod_forPort() throws Exception { 76 Assume.assumeTrue(ServerBuilder.class.isAssignableFrom(builderClass)); 77 Method method = builderClass.getMethod("forPort", int.class); 78 79 assertTrue(Modifier.isStatic(method.getModifiers())); 80 assertTrue(ServerBuilder.class.isAssignableFrom(method.getReturnType())); 81 assertSame(builderClass, method.getDeclaringClass()); 82 } 83 84 @Test channelBuilderHidesMethod_forAddress()85 public void channelBuilderHidesMethod_forAddress() throws Exception { 86 Assume.assumeTrue(ManagedChannelBuilder.class.isAssignableFrom(builderClass)); 87 Method method = builderClass.getMethod("forAddress", String.class, int.class); 88 89 assertTrue(Modifier.isStatic(method.getModifiers())); 90 assertTrue(ManagedChannelBuilder.class.isAssignableFrom(method.getReturnType())); 91 assertSame(builderClass, method.getDeclaringClass()); 92 } 93 94 @Test channelBuilderHidesMethod_forTarget()95 public void channelBuilderHidesMethod_forTarget() throws Exception { 96 Assume.assumeTrue(ManagedChannelBuilder.class.isAssignableFrom(builderClass)); 97 Method method = builderClass.getMethod("forTarget", String.class); 98 99 assertTrue(Modifier.isStatic(method.getModifiers())); 100 assertTrue(ManagedChannelBuilder.class.isAssignableFrom(method.getReturnType())); 101 assertSame(builderClass, method.getDeclaringClass()); 102 } 103 } 104