aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/squareup/javapoet
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com/squareup/javapoet')
-rw-r--r--src/test/java/com/squareup/javapoet/MethodSpecTest.java27
-rw-r--r--src/test/java/com/squareup/javapoet/ParameterSpecTest.java21
2 files changed, 46 insertions, 2 deletions
diff --git a/src/test/java/com/squareup/javapoet/MethodSpecTest.java b/src/test/java/com/squareup/javapoet/MethodSpecTest.java
index b768351..e180f20 100644
--- a/src/test/java/com/squareup/javapoet/MethodSpecTest.java
+++ b/src/test/java/com/squareup/javapoet/MethodSpecTest.java
@@ -15,6 +15,7 @@
*/
package com.squareup.javapoet;
+import com.google.testing.compile.Compilation;
import com.google.testing.compile.CompilationRule;
import java.io.Closeable;
import java.io.IOException;
@@ -30,15 +31,19 @@ import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
+import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
+import javax.tools.JavaFileObject;
+
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
-import static com.squareup.javapoet.MethodSpec.CONSTRUCTOR;
+import static com.google.testing.compile.CompilationSubject.assertThat;
+import static com.google.testing.compile.Compiler.javac;
import static com.squareup.javapoet.TestUtil.findFirst;
import static javax.lang.model.util.ElementFilter.methodsIn;
import static org.junit.Assert.fail;
@@ -247,6 +252,26 @@ public final class MethodSpecTest {
}
}
+ abstract static class AbstractClassWithPrivateAnnotation {
+
+ private @interface PrivateAnnotation{ }
+
+ abstract void foo(@PrivateAnnotation final String bar);
+ }
+
+ @Test public void overrideDoesNotCopyParameterAnnotations() {
+ TypeElement abstractTypeElement = getElement(AbstractClassWithPrivateAnnotation.class);
+ ExecutableElement fooElement = ElementFilter.methodsIn(abstractTypeElement.getEnclosedElements()).get(0);
+ ClassName implClassName = ClassName.get("com.squareup.javapoet", "Impl");
+ TypeSpec type = TypeSpec.classBuilder(implClassName)
+ .superclass(abstractTypeElement.asType())
+ .addMethod(MethodSpec.overriding(fooElement).build())
+ .build();
+ JavaFileObject jfo = JavaFile.builder(implClassName.packageName, type).build().toJavaFileObject();
+ Compilation compilation = javac().compile(jfo);
+ assertThat(compilation).succeeded();
+ }
+
@Test public void equalsAndHashCode() {
MethodSpec a = MethodSpec.constructorBuilder().build();
MethodSpec b = MethodSpec.constructorBuilder().build();
diff --git a/src/test/java/com/squareup/javapoet/ParameterSpecTest.java b/src/test/java/com/squareup/javapoet/ParameterSpecTest.java
index c3effca..561eebf 100644
--- a/src/test/java/com/squareup/javapoet/ParameterSpecTest.java
+++ b/src/test/java/com/squareup/javapoet/ParameterSpecTest.java
@@ -60,6 +60,25 @@ public class ParameterSpecTest {
assertThat(a.toString()).isEqualTo(b.toString());
}
+ @Test public void receiverParameterInstanceMethod() {
+ ParameterSpec.Builder builder = ParameterSpec.builder(int.class, "this");
+ assertThat(builder.build().name).isEqualTo("this");
+ }
+
+ @Test public void receiverParameterNestedClass() {
+ ParameterSpec.Builder builder = ParameterSpec.builder(int.class, "Foo.this");
+ assertThat(builder.build().name).isEqualTo("Foo.this");
+ }
+
+ @Test public void keywordName() {
+ try {
+ ParameterSpec.builder(int.class, "super");
+ fail();
+ } catch (Exception e) {
+ assertThat(e.getMessage()).isEqualTo("not a valid name: super");
+ }
+ }
+
@Test public void nullAnnotationsAddition() {
try {
ParameterSpec.builder(int.class, "foo").addAnnotations(null);
@@ -99,7 +118,7 @@ public class ParameterSpecTest {
VariableElement parameterElement = element.getParameters().get(0);
assertThat(ParameterSpec.get(parameterElement).toString())
- .isEqualTo("@javax.annotation.Nullable java.lang.String arg0");
+ .isEqualTo("java.lang.String arg0");
}
@Test public void addNonFinalModifier() {