com.google.testing.compile.Compilation Java Examples

The following examples show how to use com.google.testing.compile.Compilation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestProcessor.java    From android-state with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testTypeNotSupported() {
    JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestTypeNotSupported"), ""
            + "package com.evernote.android.state.test;\n"
            + "\n"
            + "import com.evernote.android.state.State;\n"
            + "\n"
            + "public class TestTypeNotSupported {\n"
            + "    @State\n"
            + "    public Other test;\n"
            + "\n"
            + "    public static final class Other {}\n"
            + "}\n");

    Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject);
    assertThat(compilation).failed();
    assertThat(compilation)
            .hadErrorContaining("Don't know how to put com.evernote.android.state.test.TestTypeNotSupported.Other into a bundle")
            .inFile(javaFileObject)
            .onLine(7)
            .atColumn(18);
}
 
Example #2
Source File: AutoValueCompilationTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void noMultidimensionalPrimitiveArrays() {
  JavaFileObject javaFileObject =
      JavaFileObjects.forSourceLines(
          "foo.bar.Baz",
          "package foo.bar;",
          "",
          "import com.google.auto.value.AutoValue;",
          "",
          "@AutoValue",
          "public abstract class Baz {",
          "  public abstract int[][] ints();",
          "",
          "  public static Baz create(int[][] ints) {",
          "    return new AutoValue_Baz(ints);",
          "  }",
          "}");
  Compilation compilation =
      javac().withProcessors(new AutoValueProcessor()).compile(javaFileObject);
  assertThat(compilation)
      .hadErrorContaining(
          "@AutoValue class cannot define an array-valued property "
              + "unless it is a primitive array")
      .inFile(javaFileObject)
      .onLineContaining("int[][] ints()");
}
 
Example #3
Source File: AnalyzeWithDefinedComponentsTest.java    From deptective with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailUponAnalyzeWithPackageMatchedByMultipleComponents() throws Exception {
    Compilation compilation = Compiler.javac()
            .withOptions(
                    TestOptions.deptectiveOptions(
                            Options.MODE, "ANALYZE",
                            Options.COMPONENTS,
                            "foo1:org.moditect.deptective.plugintest.packagecontainedtwiceanalyze.foo;" +
                                    "foo2:org.moditect.deptective.plugintest.packagecontainedtwiceanalyze.foo"
                    )
            )
            .compile(forTestClass(Foo.class));

    assertThat(compilation).failed();
    assertThat(compilation).hadNoteCount(0);
    assertThat(compilation).hadErrorContaining(
            "Multiple components match package org.moditect.deptective.plugintest.packagecontainedtwiceanalyze.foo: foo1, foo2"
    );
}
 
Example #4
Source File: IntentBuilderGeneratorTest.java    From dart with Apache License 2.0 6 votes vote down vote up
@Test
public void intentBuilderGenerator_should_fail_when_extraIsInvalidType() {
  JavaFileObject source =
      JavaFileObjects.forSourceString(
          "test.navigation.TestNavigationModel",
          Joiner.on('\n')
              .join(
                  "package test.navigation;",
                  "import dart.BindExtra;",
                  "public class TestNavigationModel {",
                  "    @BindExtra Object extra;",
                  "}"));

  Compilation compilation = javac().withProcessors(hensonProcessor()).compile(source);
  assertThat(compilation)
      .hadErrorContaining(
          "The fields of class annotated with @DartModel must be primitive, Serializable or Parcelable (test.navigation.TestNavigationModel.extra).");
}
 
Example #5
Source File: AutoAnnotationErrorsTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtraParameters() {
  JavaFileObject testSource =
      JavaFileObjects.forSourceLines(
          "com.foo.Test",
          "package com.foo;",
          "",
          "import com.example.TestAnnotation;",
          "import com.google.auto.value.AutoAnnotation;",
          "",
          "class Test {",
          "  @AutoAnnotation static TestAnnotation newTestAnnotation(int value, int other) {",
          "    return new AutoAnnotation_Test_newTestAnnotation(value);",
          "  }",
          "}");
  Compilation compilation =
      javac()
          .withProcessors(new AutoAnnotationProcessor())
          .compile(TEST_ANNOTATION, testSource);
  assertThat(compilation)
      .hadErrorContaining(
          "method parameter 'other' must have the same name as a member of "
              + "com.example.TestAnnotation")
      .inFile(testSource)
      .onLineContaining("newTestAnnotation(int value, int other)");
}
 
Example #6
Source File: NativeImageConfigGeneratorProcessorTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testOptionDisableProxyVerbose() {
    NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
    Compilation compilation =
            javac()
                    .withProcessors(processor)
                    .withOptions("-A" + ProxyConfigGen.OPTION_DISABLE, "-A" + AbstractGenerator.OPTION_VERBOSE) // no value
                    .compile(JavaFileObjects.forResource(
                            "picocli/examples/subcommands/ParentCommandDemo.java"));

    assertThat(compilation).succeeded();
    String[][] allParams = {
            { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true" , "true" },
            { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true" , "true" },
            { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "false", "true" },
    };
    expectGeneratedWithNotes(compilation, allParams);
}
 
Example #7
Source File: IntentBuilderGeneratorTest.java    From dart with Apache License 2.0 6 votes vote down vote up
@Test
public void intentBuilderGenerator_should_fail_when_parentIsNotANavigationModel() {
  JavaFileObject source =
      JavaFileObjects.forSourceString(
          "test.navigation.TestNavigationModel",
          Joiner.on('\n')
              .join(
                  "package test.navigation;",
                  "import dart.BindExtra;",
                  "public class TestNavigationModel extends SuperClass {",
                  "    @BindExtra String extra;",
                  "}",
                  "class SuperClass {",
                  "}"));

  Compilation compilation = javac().withProcessors(hensonProcessor()).compile(source);
  assertThat(compilation)
      .hadErrorContaining(
          "DartModel test.navigation.TestNavigationModel parent does not have an IntentBuilder. Is test.navigation.SuperClass annotated with @DartModel or contains @BindExtra fields?");
}
 
Example #8
Source File: ControllerTest.java    From nalu with Apache License 2.0 6 votes vote down vote up
@Test
void testControllerWithComposite01() {
  Compilation compilation = javac().withProcessors(new NaluProcessor())
                                   .compile(Arrays.asList(JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/controllerWithComposite01/ControllerWithComposite01.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/MockContext.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/MockShell.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/controllerWithComposite01/IComponent01.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/controllerWithComposite01/Component01.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/controllerWithComposite01/composite/CompositeController01.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/controllerWithComposite01/composite/ICompositeComponent01.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/controllerWithComposite01/composite/CompositeComponent01.java")));
  CompilationSubject.assertThat(compilation)
                    .succeeded();
  CompilationSubject.assertThat(compilation)
                    .generatedSourceFile("com/github/nalukit/nalu/processor/common/ui/controllerWithComposite01/ControllerWithComposite01CreatorImpl")
                    .hasSourceEquivalentTo(JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/controllerWithComposite01/ControllerWithComposite01CreatorImpl.java"));
}
 
Example #9
Source File: AutoAnnotationErrorsTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoesNotReturnAnnotation() {
  JavaFileObject testSource =
      JavaFileObjects.forSourceLines(
          "com.foo.Test",
          "package com.foo;",
          "",
          "import com.google.auto.value.AutoAnnotation;",
          "",
          "class Test {",
          "  @AutoAnnotation static String newString(int value) {",
          "    return new AutoAnnotation_Test_newString(value);",
          "  }",
          "}");
  Compilation compilation =
      javac()
          .withProcessors(new AutoAnnotationProcessor())
          .compile(TEST_ANNOTATION, testSource);
  assertThat(compilation)
      .hadErrorContaining("must be an annotation type, not java.lang.String")
      .inFile(testSource)
      .onLineContaining("static String newString(int value)");
}
 
Example #10
Source File: EncodableProcessorTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void compile_withGenericClass_ShouldWarnAboutPotentialProblems() {
  Compilation result =
      javac()
          .withProcessors(new EncodableProcessor())
          .compile(
              JavaFileObjects.forSourceLines(
                  "GenericClass",
                  "import com.google.firebase.encoders.annotations.Encodable;",
                  "@Encodable public class GenericClass<T, U> {",
                  "public T getT() { return null; }",
                  "public U getU() { return null; }",
                  "}"));

  assertThat(result).hadWarningContaining("GenericClass<T,U> is a generic type");
  assertThat(result)
      .generatedSourceFile("AutoGenericClassEncoder")
      .hasSourceEquivalentTo(
          JavaFileObjects.forResource("ExpectedGenericsEncoderWithUnknownType.java"));
}
 
Example #11
Source File: AutoValueCompilationTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void testAbstractWithParams() {
  JavaFileObject javaFileObject =
      JavaFileObjects.forSourceLines(
          "foo.bar.Baz",
          "package foo.bar;",
          "import com.google.auto.value.AutoValue;",
          "@AutoValue",
          "public abstract class Baz {",
          "  public abstract int foo(int bar);",
          "}");
  Compilation compilation =
      javac().withProcessors(new AutoValueProcessor()).compile(javaFileObject);
  assertThat(compilation).failed();
  assertThat(compilation)
      .hadWarningContaining(
          "Abstract method is neither a property getter nor a Builder converter")
      .inFile(javaFileObject)
      .onLineContaining("int foo(int bar)");
}
 
Example #12
Source File: IntegrationTest.java    From dataenum with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCopyDocFromCaseSourceToJavadocCommentOnFactoryMethod() {
  Compilation compilation =
      javac()
          .withOptions("-implicit:class")
          .withProcessors(new DataEnumProcessor())
          .compile(JavaFileObjects.forResource("javadoc/Javadoc_dataenum.java"));

  assertThat(compilation).succeededWithoutWarnings();
  assertThat(compilation)
      .generatedSourceFile("javadoc.Javadoc")
      .contentsAsUtf8String()
      .contains(
          "/**\n"
              + "   * Some documentation about this case.\n"
              + "   *\n"
              + "   * @return a {@link Documented} (see {@link Javadoc_dataenum#Documented} for source)\n"
              + "   */\n");
}
 
Example #13
Source File: AutoValueCompilationTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void annotationOnEnum() {
  JavaFileObject javaFileObject =
      JavaFileObjects.forSourceLines(
          "foo.bar.Baz",
          "package foo.bar;",
          "",
          "import com.google.auto.value.AutoValue;",
          "",
          "@AutoValue",
          "public enum Baz {}");
  Compilation compilation =
      javac().withProcessors(new AutoValueProcessor()).compile(javaFileObject);
  assertThat(compilation)
      .hadErrorContaining("AutoValue only applies to classes")
      .inFile(javaFileObject)
      .onLineContaining("enum Baz");
}
 
Example #14
Source File: IntentBuilderGeneratorTest.java    From dart with Apache License 2.0 6 votes vote down vote up
@Test
public void intentBuilderGenerator_should_fail_when_extraKeyIsInvalid() {
  JavaFileObject source =
      JavaFileObjects.forSourceString(
          "test.navigation.TestNavigationModel",
          Joiner.on('\n')
              .join(
                  "package test.navigation;",
                  "import dart.BindExtra;",
                  "public class TestNavigationModel {",
                  "    @BindExtra(\"my.key\") String extra;",
                  "}"));

  Compilation compilation = javac().withProcessors(hensonProcessor()).compile(source);
  assertThat(compilation)
      .hadErrorContaining(
          "@BindExtra key has to be a valid java variable identifier (test.navigation.TestNavigationModel#extra).");
}
 
Example #15
Source File: AutoOneOfCompilationTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void noKindGetter() {
  JavaFileObject javaFileObject =
      JavaFileObjects.forSourceLines(
          "foo.bar.Pet",
          "package foo.bar;",
          "",
          "import com.google.auto.value.AutoOneOf;",
          "",
          "@AutoOneOf(Pet.Kind.class)",
          "public abstract class Pet {",
          "  public enum Kind {DOG, CAT}",
          "  public abstract String dog();",
          "  public abstract String cat();",
          "}");
  Compilation compilation =
      javac().withProcessors(new AutoOneOfProcessor()).compile(javaFileObject);
  assertThat(compilation)
      .hadErrorContaining(
          "foo.bar.Pet must have a no-arg abstract method returning foo.bar.Pet.Kind")
      .inFile(javaFileObject)
      .onLineContaining("class Pet");
}
 
Example #16
Source File: NativeImageConfigGeneratorProcessorTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateReflectConfigIssue850MissingMixin() {
    NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
    Compilation compilation =
            javac()
                    .withProcessors(processor)
                    .withOptions("-A" + OPTION_PROJECT + "=issue850")
                    .compile(JavaFileObjects.forSourceLines(
                            "picocli.issue850missingmixin.App",
                            slurp("/picocli/issue850missingmixin/App.java")),
                            JavaFileObjects.forSourceLines(
                                    "picocli.issue850missingmixin.InitCommand",
                                    slurp("/picocli/issue850missingmixin/InitCommand.java")),
                            JavaFileObjects.forSourceLines(
                                    "picocli.issue850missingmixin.ProviderMixin",
                                    slurp("/picocli/issue850missingmixin/ProviderMixin.java"))
                            );
    assertThat(compilation).succeeded();
    assertThat(compilation)
            .generatedFile(StandardLocation.CLASS_OUTPUT, "META-INF/native-image/picocli-generated/issue850/reflect-config.json")
            .contentsAsUtf8String().isEqualTo(slurp("/picocli/issue850missingmixin/issue850-reflect-config.json"));
}
 
Example #17
Source File: Issue777Test.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue777() {
    Compilation compilation = compareCommandYamlDump(slurp("/picocli/issue777/MutuallyExclusiveOptionsDemo.yaml"),
            JavaFileObjects.forResource("picocli/issue777/MutuallyExclusiveOptionsDemo.java"));

    assertThat(compilation).succeeded();
}
 
Example #18
Source File: ControllerTest.java    From nalu with Apache License 2.0 5 votes vote down vote up
@Test
void testAcceptAnnotation01() {
  Compilation compilation = javac().withProcessors(new NaluProcessor())
                                   .compile(Arrays.asList(JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/MockShell.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/controller/acceptAnnotation01/AcceptAnnotation01Controller.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/controller/acceptAnnotation01/IComponent01.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/controller/acceptAnnotation01/Component01.java")));
  CompilationSubject.assertThat(compilation)
                    .failed();
  CompilationSubject.assertThat(compilation)
                    .hadErrorContaining("Nalu-Processor: controller >>com.github.nalukit.nalu.processor.controller.acceptAnnotation01.AcceptAnnotation01Controller<< - @AcceptParameter with value >>parameter03<< is not represented in the route as parameter");
}
 
Example #19
Source File: ConsistenceTest.java    From nalu with Apache License 2.0 5 votes vote down vote up
@Test
void testStartRouteDoesNotExists() {
  Compilation compilation = javac().withProcessors(new NaluProcessor())
                                   .compile(Arrays.asList(JavaFileObjects.forResource("com/github/nalukit/nalu/processor/consistence/startRouteDoesNotExist/StartRouteDoesNotExistApplication.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/component03/Controller03.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/component03/IComponent03.java"),
                                                          JavaFileObjects.forResource("com/github/nalukit/nalu/processor/common/ui/component03/Component03.java")));
  CompilationSubject.assertThat(compilation)
                    .failed();
  CompilationSubject.assertThat(compilation)
                    .hadErrorContaining("Nalu-Processor: The shell of the startRoute >>mockShell<< does not exist!");
}
 
Example #20
Source File: ExtraPropertyProcessorTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void test2() {
  Compilation result =
      javac()
          .withProcessors(new ExtraPropertyProcessor())
          .compile(
              JavaFileObjects.forSourceLines(
                  "com.example.MyAnnotation",
                  "package com.example;",
                  "import com.google.firebase.encoders.annotations.ExtraProperty;",
                  "@ExtraProperty",
                  "public @interface MyAnnotation {",
                  "int intVal();",
                  "long longVal();",
                  "boolean boolVal();",
                  "short shortVal();",
                  "float floatVal();",
                  "double doubleVal();",
                  "double[] doubleArrayVal();",
                  "String strVal() default \"default\";",
                  "MyEnum enumVal() default MyEnum.VALUE1;",
                  "enum MyEnum { VALUE1, VALUE2 }",
                  "}"));

  assertThat(result).succeededWithoutWarnings();
  assertThat(result)
      .generatedSourceFile("com/example/AtMyAnnotation")
      .hasSourceEquivalentTo(JavaFileObjects.forResource("ExpectedAtMyAnnotation.java"));
}
 
Example #21
Source File: IntegrationTest.java    From dataenum with Apache License 2.0 5 votes vote down vote up
@Test
public void varargValueEnum() throws Exception {
  Compilation compilation =
      javac()
          .withProcessors(new DataEnumProcessor())
          .compile(JavaFileObjects.forResource("VarargValue_dataenum.java"));
  assertThat(compilation).failed();
  assertThat(compilation).hadErrorCount(1);
  assertThat(compilation).hadErrorContaining("Vararg parameters not permitted");
}
 
Example #22
Source File: AutoValueCompilationTest.java    From auto with Apache License 2.0 5 votes vote down vote up
@Test
public void autoValueBuilderOnEnum() {
  JavaFileObject javaFileObject =
      JavaFileObjects.forSourceLines(
          "foo.bar.Baz",
          "package foo.bar;",
          "",
          "import com.google.auto.value.AutoValue;",
          "",
          "@AutoValue",
          "public abstract class Baz {",
          "  abstract int foo();",
          "",
          "  static Builder builder() {",
          "    return null;",
          "  }",
          "",
          "  @AutoValue.Builder",
          "  public enum Builder {}",
          "}");
  Compilation compilation =
      javac()
          .withProcessors(new AutoValueProcessor(), new AutoValueBuilderProcessor())
          .compile(javaFileObject);
  assertThat(compilation)
      .hadErrorContaining("can only apply to a class or an interface")
      .inFile(javaFileObject)
      .onLineContaining("public enum Builder");
}
 
Example #23
Source File: GremlinDslProcessorTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCompileRemoteDslTraversal() {
    final Compilation compilation = javac()
            .withProcessors(new GremlinDslProcessor())
            .compile(JavaFileObjects.forResource(GremlinDsl.class.getResource("SocialTraversalDsl.java")),
                    JavaFileObjects.forResource(GremlinDsl.class.getResource("RemoteDslTraversal.java")));

    try {
        final ClassLoader cl = new JavaFileObjectClassLoader(compilation.generatedFiles());
        final Class cls = cl.loadClass("org.apache.tinkerpop.gremlin.process.traversal.dsl.RemoteDslTraversal");
        cls.getConstructor().newInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #24
Source File: MethodSpecTest.java    From javapoet with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #25
Source File: BasicPluginTest.java    From deptective with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAllowAccessToJavaLangAutomatically() {
    Compilation compilation = Compiler.javac()
            .withOptions(TestOptions.deptectiveOptions(Options.CONFIG_FILE, getConfigFileOption()))
            .compile(forTestClass(FooWithoutErrors.class));
    assertThat(compilation).succeeded();
}
 
Example #26
Source File: WitchProcessorTest.java    From Witch-Android with Apache License 2.0 5 votes vote down vote up
@Test
public void errorInvalidBindWhen() {
    Compilation compilation = compile("ErrorInvalidBindWhen.java");
    assertThat(compilation).hadErrorContaining("ErrorInvalidBindWhen");
    assertThat(compilation).hadErrorContaining("text(android.view.View) has invalid value \"sometimes\" for @BindWhen");
    assertThat(compilation).hadErrorContaining("Valid values are");
    assertThat(compilation).hadErrorContaining("BindWhen.NOT_SAME");
    assertThat(compilation).hadErrorContaining("BindWhen.NOT_EQUALS");
    assertThat(compilation).hadErrorContaining("BindWhen.ALWAYS");
    assertThat(compilation).hadErrorContaining("BindWhen.ONCE");
    assertThat(compilation).hadErrorContaining(String.format(readMore));
    printErrors(compilation);
}
 
Example #27
Source File: EncodableProcessorTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void compile_withNestedAutoValueInSamePackage_shouldRegisterGeneratedSubclass() {
  Compilation result =
      javac()
          .withProcessors(new AutoValueProcessor(), new EncodableProcessor())
          .compile(
              JavaFileObjects.forSourceLines(
                  "Foo",
                  "import com.google.firebase.encoders.annotations.Encodable;",
                  "import com.google.auto.value.AutoValue;",
                  "@Encodable @AutoValue public abstract class Foo {",
                  "public abstract Bar getBar();",
                  "@AutoValue public abstract static class Bar {",
                  "public abstract Baz getBaz();",
                  "@AutoValue public abstract static class Baz {",
                  "public abstract String getField();",
                  "}",
                  "}",
                  "}"));

  StringSubject compiled =
      assertThat(result).generatedSourceFile("AutoFooEncoder").contentsAsUtf8String();

  compiled.contains("cfg.registerEncoder(AutoValue_Foo.class");
  compiled.contains("cfg.registerEncoder(AutoValue_Foo_Bar.class");
  compiled.contains("cfg.registerEncoder(AutoValue_Foo_Bar_Baz.class");
}
 
Example #28
Source File: ExtensionTest.java    From auto with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyErrorShouldNotCrash() {
  JavaFileObject autoValueClass = JavaFileObjects.forSourceLines(
      "test.Test",
      "package test;",
      "import com.google.auto.value.AutoValue;",
      "import java.util.List;",
      "",
      "@AutoValue",
      "public abstract class Test {",
      "  abstract Integer property();",
      "  abstract List<String> listProperty();",
      "",
      "  @AutoValue.Builder",
      "  public interface Builder {",
      "    Builder property(Integer property);",
      "    Builder listProperty(List<String> listProperty);",
      "    Builder listProperty(Integer listPropertyValues);",
      "    Test build();",
      "  }",
      "}");
  // We don't actually expect the extension to be invoked. Previously it was, and that led to a
  // NullPointerException when calling .setters() in the checker.
  ContextChecker checker =
      context -> {
        assertThat(context.builder()).isPresent();
        assertThat(context.builder().get().setters()).isEmpty();
      };
  ContextCheckingExtension extension = new ContextCheckingExtension(checker);
  Compilation compilation =
      javac()
          .withProcessors(new AutoValueProcessor(ImmutableList.of(extension)))
          .compile(autoValueClass);
  assertThat(compilation)
      .hadErrorContaining("Parameter type java.lang.Integer of setter method")
      .inFile(autoValueClass)
      .onLineContaining("Builder listProperty(Integer listPropertyValues)");
}
 
Example #29
Source File: AutoValueCompilationTest.java    From auto with Apache License 2.0 5 votes vote down vote up
@Test
public void autoValueBuilderWrongTypeGetter() {
  JavaFileObject javaFileObject =
      JavaFileObjects.forSourceLines(
          "foo.bar.Baz",
          "package foo.bar;",
          "",
          "import com.google.auto.value.AutoValue;",
          "",
          "@AutoValue",
          "public abstract class Baz<T, U> {",
          "  abstract T blim();",
          "  abstract U blam();",
          "",
          "  @AutoValue.Builder",
          "  public interface Builder<T, U> {",
          "    Builder<T, U> blim(T x);",
          "    Builder<T, U> blam(U x);",
          "    T blim();",
          "    T blam();",
          "    Baz<T, U> build();",
          "  }",
          "}");
  Compilation compilation =
      javac()
          .withProcessors(new AutoValueProcessor(), new AutoValueBuilderProcessor())
          .compile(javaFileObject);
  assertThat(compilation)
      .hadErrorContaining(
          "Method matches a property of foo.bar.Baz but has return type T instead of U")
      .inFile(javaFileObject)
      .onLineContaining("T blam()");
}
 
Example #30
Source File: NativeImageConfigGeneratorProcessorTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateReflectConfigArgGroup() {
    NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
    Compilation compilation =
            javac()
                    .withProcessors(processor)
                    .withOptions("-A" + OPTION_PROJECT + "=issue793")
                    .compile(JavaFileObjects.forSourceLines(
                            "picocli.issue793.Issue793",
                            slurp("/picocli/issue793/Issue793.java")));
    assertThat(compilation).succeeded();
    assertThat(compilation)
            .generatedFile(StandardLocation.CLASS_OUTPUT, "META-INF/native-image/picocli-generated/issue793/reflect-config.json")
            .contentsAsUtf8String().isEqualTo(slurp("/picocli/issue793/issue793-reflect-config.json"));
}