Java Code Examples for com.google.testing.compile.JavaFileObjects#forSourceString()
The following examples show how to use
com.google.testing.compile.JavaFileObjects#forSourceString() .
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: RelaxedFactoryForClassContainingMethodsTest.java From toothpick with Apache License 2.0 | 6 votes |
@Test public void testRelaxedFactoryCreationForInjectedMethod_shouldFail_WhenMethodParameterIsInvalidProvider() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestRelaxedFactoryCreationForInjectMethod", Joiner.on('\n') .join( // "package test;", // "import javax.inject.Inject;", // "import javax.inject.Provider;", // "public class TestRelaxedFactoryCreationForInjectMethod {", // " @Inject void m(Provider foo) {}", // "}", // " class Foo {}")); assert_() .about(javaSource()) .that(source) .processedWith(ProcessorTestUtilities.factoryAndMemberInjectorProcessors()) .failsToCompile() .withErrorContaining( "Parameter foo in method/constructor test.TestRelaxedFactoryCreationForInjectMethod#m is not a valid javax.inject.Provider."); }
Example 2
Source File: MethodMemberInjectorTest.java From toothpick with Apache License 2.0 | 6 votes |
@Test public void testMethodInjection_shouldFail_whenInjectedMethodParameterIsInvalidLazy() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestMethodInjection", Joiner.on('\n') .join( // "package test;", // "import javax.inject.Inject;", // "import toothpick.Lazy;", // "public class TestMethodInjection {", // " @Inject", // " public void m(Lazy foo) {}", // "}", // "class Foo {}" // )); assert_() .about(javaSource()) .that(source) .processedWith(memberInjectorProcessors()) .failsToCompile() .withErrorContaining( "Parameter foo in method/constructor test.TestMethodInjection#m is not a valid toothpick.Lazy."); }
Example 3
Source File: PostmanProcessorTest.java From postman with MIT License | 6 votes |
@Test public void testFinalFieldRaisesWarning() throws Exception { //language=JAVA final String sourceString = "package test;\n" + "import com.workday.postman.annotations.Parceled;\n" + "\n" + " @Parceled\n" + " public class TestClass {\n" + " final String myString = \"The final word.\";\n" + " }"; JavaFileObject source = JavaFileObjects.forSourceString("test.TestClass", sourceString); assertAbout(javaSource()).that(source) .processedWith(new PostmanProcessor()) .compilesWithoutError() .withWarningContaining( "Parceler will ignore final field myString.") .in(source) .onLine(6); }
Example 4
Source File: AkatsukiProcessorSourceNameTest.java From Akatsuki with Apache License 2.0 | 6 votes |
private static GeneratedClassWithName staticInnerClass(String... classes) { // maybe (String first, String...more) ? if (classes.length == 0) throw new IllegalArgumentException(""); String[] generatedClasses = new String[classes.length]; TypeSpec.Builder lastBuilder = null; for (int i = classes.length - 1; i >= 0; i--) { String clazz = classes[i]; final Builder currentBuilder = TypeSpec.classBuilder(clazz) .addModifiers((i == 0 ? Modifier.PUBLIC : Modifier.STATIC)) .addField(field(STRING_TYPE, clazz.toLowerCase(), Retained.class)); if (lastBuilder != null) { currentBuilder.addType(lastBuilder.build()); } // we generate static inner class names eg A.B -> A$B generatedClasses[i] = IntStream.range(0, i + 1).boxed().map(n -> classes[n]) .collect(Collectors.joining("$")); lastBuilder = currentBuilder; } final JavaFile file = JavaFile.builder(TEST_PACKAGE, lastBuilder.build()).build(); final JavaFileObject object = JavaFileObjects .forSourceString(TEST_PACKAGE + "." + classes[0], file.toString()); return new GeneratedClassWithName(object, generatedClasses); }
Example 5
Source File: MethodMemberInjectorTest.java From toothpick with Apache License 2.0 | 6 votes |
@Test public void testMethodInjection_shouldFail_whenInjectedMethodParameterIsInvalidProvider() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestMethodInjection", Joiner.on('\n') .join( // "package test;", // "import javax.inject.Inject;", // "import javax.inject.Provider;", // "public class TestMethodInjection {", // " @Inject", // " public void m(Provider foo) {}", // "}", // "class Foo {}" // )); assert_() .about(javaSource()) .that(source) .processedWith(memberInjectorProcessors()) .failsToCompile() .withErrorContaining( "Parameter foo in method/constructor test.TestMethodInjection#m is not a valid javax.inject.Provider."); }
Example 6
Source File: FactoryTest.java From toothpick with Apache License 2.0 | 6 votes |
@Test public void testPrivateConstructor() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestPrivateConstructor", Joiner.on('\n') .join( // "package test;", // "import javax.inject.Inject;", // "public class TestPrivateConstructor {", // " @Inject private TestPrivateConstructor() {}", // "}" // )); assert_() .about(javaSource()) .that(source) .processedWith(ProcessorTestUtilities.factoryProcessors()) .failsToCompile() .withErrorContaining( "@Inject constructors must not be private in class test.TestPrivateConstructor"); }
Example 7
Source File: PaperParcelProcessorTest.java From paperparcel with Apache License 2.0 | 6 votes |
@Test public void intersectionFieldTypeTest() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", Joiner.on('\n').join( "package test;", "import android.os.Parcel;", "import android.os.Parcelable;", "import paperparcel.PaperParcel;", "import java.io.Serializable;", "@PaperParcel", "public final class Test<T extends Number & Serializable> implements Parcelable {", " public T value;", " public int describeContents() {", " return 0;", " }", " public void writeToParcel(Parcel dest, int flags) {", " }", "}" )); assertAbout(javaSource()).that(source) .processedWith(new PaperParcelProcessor()) .failsToCompile() .withErrorContaining(ErrorMessages.FIELD_TYPE_IS_INTERSECTION_TYPE) .in(source) .onLine(8); }
Example 8
Source File: RelaxedFactoryForClassContainingFieldsTest.java From toothpick with Apache License 2.0 | 6 votes |
@Test public void testRelaxedFactoryCreationForInjectedField_shouldWorkButNoFactoryIsProduced_whenTypeHasAPrivateDefaultConstructor() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestRelaxedFactoryCreationForInjectField", Joiner.on('\n') .join( // "package test;", // "import javax.inject.Inject;", // "public class TestRelaxedFactoryCreationForInjectField {", // " @Inject Foo foo;", // " private TestRelaxedFactoryCreationForInjectField() {}", // "}", // "class Foo {}")); assertThatCompileWithoutErrorButNoFactoryIsCreated( source, "test", "TestRelaxedFactoryCreationForInjectField"); }
Example 9
Source File: PaperParcelProcessorTest.java From paperparcel with Apache License 2.0 | 5 votes |
@Test public void failIfNestedAdapterIsEnclosedInNonPublicClass() { JavaFileObject source = JavaFileObjects.forSourceString("test.TestOuter", Joiner.on('\n').join( "package test;", "import android.os.Parcel;", "import paperparcel.Adapter;", "import paperparcel.ProcessorConfig;", "import paperparcel.TypeAdapter;", "class TestOuter {", " @ProcessorConfig(adapters = @Adapter(TestInner.class))", " public class TestInner implements TypeAdapter<Integer> {", " public Integer readFromParcel(Parcel in) {", " return null;", " }", " public void writeToParcel(Integer value, Parcel dest, int flags) {", " }", " }", "}" )); assertAbout(javaSource()).that(source) .processedWith(new PaperParcelProcessor()) .failsToCompile() .withErrorContaining(ErrorMessages.ADAPTER_VISIBILITY_RESTRICTED) .in(source) .onLine(8); }
Example 10
Source File: MixedTest.java From abtestgen with Apache License 2.0 | 5 votes |
@Test public void testResourceTest() throws Exception { JavaFileObject sourceObj = JavaFileObjects.forSourceString("test.Test", sourceClass); JavaFileObject genObj = JavaFileObjects.forSourceString("test/Test$$Test1", generatedClass); assertAbout(javaSource()).that(sourceObj) .processedWith(new ABTestProcessor()) .compilesWithoutError() .and() .generatesSources(genObj); }
Example 11
Source File: AutoReducerGeneratorTest.java From reductor with Apache License 2.0 | 5 votes |
@Test public void testGeneratedReducerForSinglePayloadActionHandler() { JavaFileObject source = JavaFileObjects.forSourceString("test.FoobarReducer", "// Generated by com.yheriatovych.reductor.processor.ReductorAnnotationProcessor (https://github.com/Yarikx/reductor)\n" + "package test;" + "\n" + "import com.yheriatovych.reductor.Reducer;\n" + "import com.yheriatovych.reductor.annotations.AutoReducer;\n" + "\n" + "@AutoReducer\n" + "public abstract class FoobarReducer implements Reducer<String>{\n" + " @AutoReducer.Action(\"ACTION_1\")\n" + " String append(String state, int number) {\n" + " return state + number;\n" + " }\n" + "}"); JavaFileObject generatedPojo = JavaFileObjects.forSourceString("test.FoobarReducerImpl", "// Generated by com.yheriatovych.reductor.processor.ReductorAnnotationProcessor (https://github.com/Yarikx/reductor)\n" + "package test;" + "\n" + "import com.yheriatovych.reductor.Action;\n" + "\n" + "class FoobarReducerImpl extends FoobarReducer {\n" + " @Override\n" + " public String reduce(String state, Action action) {\n" + " switch (action.type) {\n" + " case \"ACTION_1\":\n" + " return append(state, (int) action.getValue(0));\n" + " default:\n" + " return state;\n" + " }\n" + " }\n" + "}"); assertAbout(javaSource()).that(source) .withCompilerOptions("-Xlint:-processing") .processedWith(new ReductorAnnotationProcessor()) .compilesWithoutWarnings() .and() .generatesSources(generatedPojo); }
Example 12
Source File: AutoValueCursorExtensionTest.java From auto-value-cursor with Apache License 2.0 | 5 votes |
@Test public void columnName() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import com.gabrielittner.auto.value.cursor.ColumnName;\n" + "import com.google.auto.value.AutoValue;\n" + "import android.database.Cursor;\n" + "@AutoValue public abstract class Test {\n" + " public static Test blah(Cursor cursor) { return null; }\n" + " public abstract int a();\n" + " @ColumnName(\"column_b\") public abstract String b();\n" + "}\n"); JavaFileObject expected = JavaFileObjects.forSourceString("test.AutoValue_Test", "" + "package test;\n" + "import android.database.Cursor;\n" + "import java.lang.String;\n" + "final class AutoValue_Test extends $AutoValue_Test {\n" + " AutoValue_Test(int a, String b) {\n" + " super(a, b);\n" + " }\n" + " static AutoValue_Test createFromCursor(Cursor cursor) {\n" + " int a = cursor.getInt(cursor.getColumnIndexOrThrow(\"a\"));\n" + " String b = cursor.getString(cursor.getColumnIndexOrThrow(\"column_b\"));\n" + " return new AutoValue_Test(a, b);\n" + " }\n" + "}\n"); assertAbout(javaSources()) .that(Collections.singletonList(source)) .processedWith(new AutoValueProcessor()) .compilesWithoutError() .and() .generatesSources(expected); }
Example 13
Source File: AutoValueCursorExtensionTest.java From auto-value-cursor with Apache License 2.0 | 5 votes |
private JavaFileObject func1() { return JavaFileObjects.forSourceString( "rx.functions.Func1", "" + "package rx.functions;\n" + "public interface Func1<T, R> {\n" + " R call(T t);\n" + "}\n"); }
Example 14
Source File: PaperParcelProcessorTest.java From paperparcel with Apache License 2.0 | 4 votes |
@Test public void preferVisibleConstructorOverReflectionTest() { JavaFileObject reflectAnnotation = JavaFileObjects.forSourceString("test.Reflect", Joiner.on('\n').join( "package test;", "public @interface Reflect {}" )); JavaFileObject source = JavaFileObjects.forSourceString("test.Test", Joiner.on('\n').join( "package test;", "import android.os.Parcel;", "import android.os.Parcelable;", "import paperparcel.PaperParcel;", "import paperparcel.ProcessorConfig;", "import java.util.List;", "@ProcessorConfig(options = @PaperParcel.Options(reflectAnnotations = Reflect.class))", "@PaperParcel", "public final class Test implements Parcelable {", " private int value1;", " private int value2;", " @Reflect private Test(int value1, int value2) {", " }", " Test(int value1) {", " }", " public int value1() {", " return this.value1;", " }", " public int value2() {", " return this.value2;", " }", " public void value2(int value2) {", " }", " public int describeContents() {", " return 0;", " }", " public void writeToParcel(Parcel dest, int flags) {", " }", "}" )); JavaFileObject expected = JavaFileObjects.forSourceString("test/PaperParcelTest", Joiner.on('\n').join( "package test;", "import android.os.Parcel;", "import android.os.Parcelable;", "import androidx.annotation.NonNull;", "final class PaperParcelTest {", " @NonNull", " static final Parcelable.Creator<Test> CREATOR = new Parcelable.Creator<Test>() {", " @Override", " public Test createFromParcel(Parcel in) {", " int value1 = in.readInt();", " int value2 = in.readInt();", " Test data = new Test(value1);", " data.value2(value2);", " return data;", " }", " @Override", " public Test[] newArray(int size) {", " return new Test[size];", " }", " };", " private PaperParcelTest() {", " }", " static void writeToParcel(@NonNull Test data, @NonNull Parcel dest, int flags) {", " dest.writeInt(data.value1());", " dest.writeInt(data.value2());", " }", "}" )); assertAbout(javaSources()).that(Arrays.asList(source, reflectAnnotation)) .processedWith(new PaperParcelProcessor()) .compilesWithoutError() .and() .generatesSources(expected); }
Example 15
Source File: TestProcessor.java From android-state with Eclipse Public License 1.0 | 4 votes |
@Test public void testPrivateClassBundler() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestPrivateClassBundler"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import android.os.Bundle;\n" + "\n" + "import com.evernote.android.state.Bundler;\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestPrivateClassBundler {\n" + " @State(MyBundler.class)\n" + " private Data mData2;\n" + "\n" + " public Data getData2() {\n" + " return mData2;\n" + " }\n" + "\n" + " public void setData2(Data data2) {\n" + " mData2 = data2;\n" + " }\n" + "\n" + " public static final class Data {\n" + " private int int1;\n" + " private int int2;\n" + " }\n" + "\n" + " private static final class MyBundler implements Bundler<Data> {\n" + " @Override\n" + " public void put(String key, Data value, Bundle bundle) {\n" + " bundle.putInt(key + \"1\", value.int1);\n" + " bundle.putInt(key + \"2\", value.int2);\n" + " }\n" + "\n" + " @Override\n" + " public Data get(String key, Bundle bundle) {\n" + " Data data = new Data();\n" + " data.int1 = bundle.getInt(key + \"1\");\n" + " data.int2 = bundle.getInt(key + \"2\");\n" + " return data;\n" + " }\n" + " }\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Class must not be private") .inFile(javaFileObject) .onLine(25) .atColumn(26); }
Example 16
Source File: PaperParcelProcessorTest.java From paperparcel with Apache License 2.0 | 4 votes |
@Test public void overrideDefaultAdapterTest() { JavaFileObject typeAdapter = JavaFileObjects.forSourceString("test.MyIntegerAdapter", Joiner.on('\n').join( "package test;", "import paperparcel.TypeAdapter;", "import android.os.Parcel;", "import paperparcel.internal.Utils;", "public class MyIntegerAdapter implements TypeAdapter<Integer> {", " public Integer readFromParcel(Parcel in) {", " return null;", " }", " public void writeToParcel(Integer value, Parcel dest, int flags) {", " }", "}" )); JavaFileObject source = JavaFileObjects.forSourceString("test.Test", Joiner.on('\n').join( "package test;", "import android.os.Parcel;", "import android.os.Parcelable;", "import paperparcel.Adapter;", "import paperparcel.PaperParcel;", "import paperparcel.ProcessorConfig;", "@ProcessorConfig(adapters = @Adapter(MyIntegerAdapter.class))", "@PaperParcel", "public class Test implements Parcelable {", " public Integer value;", " @Override", " public int describeContents() {", " return 0;", " }", " @Override", " public void writeToParcel(Parcel dest, int flags) {", " }", "}" )); JavaFileObject expected = JavaFileObjects.forSourceString("test/PaperParcelTest", Joiner.on('\n').join( "package test;", "import android.os.Parcel;", "import android.os.Parcelable;", "import androidx.annotation.NonNull;", "import paperparcel.TypeAdapter;", "import paperparcel.internal.Utils;", "final class PaperParcelTest {", " static final TypeAdapter<Integer> MY_INTEGER_ADAPTER = new MyIntegerAdapter();", " @NonNull", " static final Parcelable.Creator<Test> CREATOR = new Parcelable.Creator<Test>() {", " @Override", " public Test createFromParcel(Parcel in) {", " Integer value = Utils.readNullable(in, PaperParcelTest.MY_INTEGER_ADAPTER);", " Test data = new Test();", " data.value = value;", " return data;", " }", " @Override", " public Test[] newArray(int size) {", " return new Test[size];", " }", " };", " private PaperParcelTest() {", " }", " static void writeToParcel(@NonNull Test data, @NonNull Parcel dest, int flags) {", " Utils.writeNullable(data.value, dest, flags, PaperParcelTest.MY_INTEGER_ADAPTER);", " }", "}" )); assertAbout(javaSources()).that(Arrays.asList(typeAdapter, source)) .processedWith(new PaperParcelProcessor()) .compilesWithoutError() .and() .generatesSources(expected); }
Example 17
Source File: FieldMemberInjectorTest.java From toothpick with Apache License 2.0 | 4 votes |
@Test public void testNamedFieldInjection_whenUsingQualifierAnnotation() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestFieldInjection", Joiner.on('\n') .join( // "package test;", // "import javax.inject.Inject;", // "import javax.inject.Named;", // "import javax.inject.Qualifier;", // "public class TestFieldInjection {", // " @Inject @Bar Foo foo;", // " public TestFieldInjection() {}", // "}", // "class Foo {}", // "@Qualifier", // "@interface Bar {}" // )); JavaFileObject expectedSource = JavaFileObjects.forSourceString( "test/TestFieldInjection__MemberInjector", Joiner.on('\n') .join( // "package test;", // "", // "import java.lang.Override;", // "import toothpick.MemberInjector;", // "import toothpick.Scope;", // "", // "public final class TestFieldInjection__MemberInjector implements MemberInjector<TestFieldInjection> {", // " @Override", // " public void inject(TestFieldInjection target, Scope scope) {", // " target.foo = scope.getInstance(Foo.class, \"test.Bar\");", // " }", // "}" // )); assert_() .about(javaSource()) .that(source) .processedWith(memberInjectorProcessors()) .compilesWithoutError() .and() .generatesSources(expectedSource); }
Example 18
Source File: DaggerReflectCompilerTest.java From dagger-reflect with Apache License 2.0 | 4 votes |
@Test public void factory() { JavaFileObject component = JavaFileObjects.forSourceString( "example.TestComponent", "" + "package example;\n" + "\n" + "import dagger.Component;\n" + "\n" + "@Component\n" + "interface TestComponent {\n" + " @Component.Factory\n" + " interface Factory {\n" + " }\n" + "}\n"); JavaFileObject expected = JavaFileObjects.forSourceString( "example.DaggerTestComponent", "" + "package example;\n" + "\n" + "import dagger.reflect.DaggerReflect;\n" + "import java.lang.AssertionError;\n" + generatedAnnotationImport + "\n" + "@Generated(\n" + " value = \"dagger.reflect.compiler.DaggerReflectCompiler\",\n" + " comments = \"https://github.com/JakeWharton/dagger-reflect\"\n" + ")\n" + "public final class DaggerTestComponent {\n" + " private DaggerTestComponent() {\n" + " throw new AssertionError();\n" + " }\n" + " public static TestComponent create() {\n" + " return DaggerReflect.create(TestComponent.class);\n" + " }\n" + " public static TestComponent.Factory factory() {\n" + " return DaggerReflect.factory(TestComponent.Factory.class);\n" + " }\n" + "}\n"); assertAbout(javaSource()) .that(component) .processedWith(new DaggerReflectCompiler()) .compilesWithoutError() .and() .generatesSources(expected); }
Example 19
Source File: DaggerReflectCompilerTest.java From dagger-reflect with Apache License 2.0 | 4 votes |
@Test public void builder() { JavaFileObject component = JavaFileObjects.forSourceString( "example.TestComponent", "" + "package example;\n" + "\n" + "import dagger.Component;\n" + "\n" + "@Component\n" + "interface TestComponent {\n" + " @Component.Builder\n" + " interface Builder {\n" + " }\n" + "}\n"); JavaFileObject expected = JavaFileObjects.forSourceString( "example.DaggerTestComponent", "" + "package example;\n" + "\n" + "import dagger.reflect.DaggerReflect;\n" + "import java.lang.AssertionError;\n" + generatedAnnotationImport + "\n" + "@Generated(\n" + " value = \"dagger.reflect.compiler.DaggerReflectCompiler\",\n" + " comments = \"https://github.com/JakeWharton/dagger-reflect\"\n" + ")\n" + "public final class DaggerTestComponent {\n" + " private DaggerTestComponent() {\n" + " throw new AssertionError();\n" + " }\n" + " public static TestComponent create() {\n" + " return DaggerReflect.create(TestComponent.class);\n" + " }\n" + " public static TestComponent.Builder builder() {\n" + " return DaggerReflect.builder(TestComponent.Builder.class);\n" + " }\n" + "}\n"); assertAbout(javaSource()) .that(component) .processedWith(new DaggerReflectCompiler()) .compilesWithoutError() .and() .generatesSources(expected); }
Example 20
Source File: FactoryTest.java From toothpick with Apache License 2.0 | 4 votes |
@Test public void testInjectedConstructorInPackageClass_shouldWork() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestConstructorInPackageClass", Joiner.on('\n') .join( // "package test;", // "import javax.inject.Inject;", // "class TestConstructorInPackageClass {", // " @Inject public TestConstructorInPackageClass() {}", // "}" // )); JavaFileObject expectedSource = JavaFileObjects.forSourceString( "test/TestConstructorInPackageClass__Factory", Joiner.on('\n') .join( // "package test;", // "import java.lang.Override;", // "import toothpick.Factory;", // "import toothpick.Scope;", // "", // "public final class TestConstructorInPackageClass__Factory implements Factory<TestConstructorInPackageClass> {", // " @Override", // " public TestConstructorInPackageClass createInstance(Scope scope) {", // " TestConstructorInPackageClass testConstructorInPackageClass = new TestConstructorInPackageClass();", // " return testConstructorInPackageClass;", // " }", // " @Override", // " public Scope getTargetScope(Scope scope) {", // " return scope;", // " }", // " @Override", // " public boolean hasScopeAnnotation() {", // " return false;", // " }", // " @Override", // " public boolean hasSingletonAnnotation() {", // " return false;", // " }", // " @Override", // " public boolean hasReleasableAnnotation() {", // " return false;", // " }", // " @Override", // " public boolean hasProvidesSingletonAnnotation() {", // " return false;", // " }", // " @Override", // " public boolean hasProvidesReleasableAnnotation() {", // " return false;", // " }", // "}" // )); assert_() .about(javaSource()) .that(source) .processedWith(ProcessorTestUtilities.factoryProcessors()) .compilesWithoutError() .and() .generatesSources(expectedSource); }