Skip to content

Commit

Permalink
testing-proto: add test for @RpcMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
jbingham-google authored and carl-mastrangelo committed Aug 3, 2018
1 parent c42d5bb commit 52a7b62
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions testing-proto/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
project(':grpc-stub')
compileOnly libraries.javax_annotation
testCompile libraries.truth
testRuntime libraries.javax_annotation
}

configureProtoCompilation()
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
import static org.junit.Assert.assertTrue;

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.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -57,4 +74,91 @@ public void serviceMethodDescriotrs() {
public void generatedMethodsAreSampledToLocalTracing() throws Exception {
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();
}
}

0 comments on commit 52a7b62

Please sign in to comment.