mirror of https://github.com/grpc/grpc-java.git
testing-proto: add test for @RpcMethod
This commit is contained in:
parent
c42d5bbf2c
commit
52a7b62a52
|
@ -14,6 +14,7 @@ dependencies {
|
||||||
project(':grpc-stub')
|
project(':grpc-stub')
|
||||||
compileOnly libraries.javax_annotation
|
compileOnly libraries.javax_annotation
|
||||||
testCompile libraries.truth
|
testCompile libraries.truth
|
||||||
|
testRuntime libraries.javax_annotation
|
||||||
}
|
}
|
||||||
|
|
||||||
configureProtoCompilation()
|
configureProtoCompilation()
|
||||||
|
|
|
@ -24,6 +24,23 @@ import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import io.grpc.MethodDescriptor;
|
import io.grpc.MethodDescriptor;
|
||||||
|
import io.grpc.stub.annotations.RpcMethod;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.annotation.processing.AbstractProcessor;
|
||||||
|
import javax.annotation.processing.RoundEnvironment;
|
||||||
|
import javax.lang.model.element.Element;
|
||||||
|
import javax.lang.model.element.TypeElement;
|
||||||
|
import javax.lang.model.type.MirroredTypeException;
|
||||||
|
import javax.tools.JavaCompiler;
|
||||||
|
import javax.tools.JavaCompiler.CompilationTask;
|
||||||
|
import javax.tools.JavaFileObject;
|
||||||
|
import javax.tools.StandardJavaFileManager;
|
||||||
|
import javax.tools.ToolProvider;
|
||||||
|
import org.junit.Assume;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
@ -57,4 +74,91 @@ public class SimpleServiceTest {
|
||||||
public void generatedMethodsAreSampledToLocalTracing() throws Exception {
|
public void generatedMethodsAreSampledToLocalTracing() throws Exception {
|
||||||
assertTrue(SimpleServiceGrpc.getUnaryRpcMethod().isSampledToLocalTracing());
|
assertTrue(SimpleServiceGrpc.getUnaryRpcMethod().isSampledToLocalTracing());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class AnnotationProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
|
private boolean processedClass = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> getSupportedAnnotationTypes() {
|
||||||
|
return Collections.singleton(RpcMethod.class.getCanonicalName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void verifyRpcMethodAnnotation(
|
||||||
|
MethodDescriptor<SimpleRequest, SimpleResponse> descriptor, RpcMethod annotation) {
|
||||||
|
assertEquals(descriptor.getFullMethodName(), annotation.fullMethodName());
|
||||||
|
assertEquals(descriptor.getType(), annotation.methodType());
|
||||||
|
|
||||||
|
// Class objects may not be available at runtime, handle MirroredTypeException if it occurs
|
||||||
|
try {
|
||||||
|
assertEquals(SimpleRequest.class, annotation.requestType());
|
||||||
|
} catch (MirroredTypeException e) {
|
||||||
|
assertEquals(SimpleRequest.class.getCanonicalName(), e.getTypeMirror().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
assertEquals(SimpleResponse.class, annotation.responseType());
|
||||||
|
} catch (MirroredTypeException e) {
|
||||||
|
assertEquals(SimpleResponse.class.getCanonicalName(), e.getTypeMirror().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized boolean process(
|
||||||
|
Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||||
|
for (Element rootElement : roundEnv.getRootElements()) {
|
||||||
|
if (!rootElement.asType().toString().equals(SimpleServiceGrpc.class.getCanonicalName())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, RpcMethod> methodToAnnotation = new HashMap<String, RpcMethod>();
|
||||||
|
for (Element enclosedElement : rootElement.getEnclosedElements()) {
|
||||||
|
RpcMethod annotation = enclosedElement.getAnnotation(RpcMethod.class);
|
||||||
|
if (annotation != null) {
|
||||||
|
methodToAnnotation.put(enclosedElement.getSimpleName().toString(), annotation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
verifyRpcMethodAnnotation(
|
||||||
|
SimpleServiceGrpc.getUnaryRpcMethod(), methodToAnnotation.get("getUnaryRpcMethod"));
|
||||||
|
verifyRpcMethodAnnotation(
|
||||||
|
SimpleServiceGrpc.getServerStreamingRpcMethod(),
|
||||||
|
methodToAnnotation.get("getServerStreamingRpcMethod"));
|
||||||
|
verifyRpcMethodAnnotation(
|
||||||
|
SimpleServiceGrpc.getClientStreamingRpcMethod(),
|
||||||
|
methodToAnnotation.get("getClientStreamingRpcMethod"));
|
||||||
|
verifyRpcMethodAnnotation(
|
||||||
|
SimpleServiceGrpc.getBidiStreamingRpcMethod(),
|
||||||
|
methodToAnnotation.get("getBidiStreamingRpcMethod"));
|
||||||
|
|
||||||
|
processedClass = true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRpcMethodAnnotations() throws Exception {
|
||||||
|
File grpcJavaFile =
|
||||||
|
new File("./src/generated/main/grpc/io/grpc/testing/protobuf/SimpleServiceGrpc.java");
|
||||||
|
Assume.assumeTrue(grpcJavaFile.exists());
|
||||||
|
|
||||||
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
||||||
|
AnnotationProcessor processor = new AnnotationProcessor();
|
||||||
|
Iterable<? extends JavaFileObject> obs = fileManager.getJavaFileObjects(grpcJavaFile);
|
||||||
|
|
||||||
|
CompilationTask task =
|
||||||
|
compiler.getTask(
|
||||||
|
null,
|
||||||
|
fileManager,
|
||||||
|
null,
|
||||||
|
Collections.singleton("-proc:only"),
|
||||||
|
Collections.singleton(SimpleServiceGrpc.class.getCanonicalName()),
|
||||||
|
obs);
|
||||||
|
task.setProcessors(Collections.singleton(processor));
|
||||||
|
assertTrue(task.call());
|
||||||
|
assertTrue(processor.processedClass);
|
||||||
|
fileManager.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue