@Before
  public void setup() {
    Messager messager = new TestMessager();
    elements = rule.getElements();
    processingEnvironment = new TestProcessingEnvironment(messager, elements, rule.getTypes());

    parcelable =
        JavaFileObjects.forSourceString(
            "android.os.Parcelable",
            ""
                + "package android.os;\n"
                + "public interface Parcelable {\n"
                + "public interface Creator<T> {\n"
                + "  public T createFromParcel(Parcel source);\n"
                + "  public T[] newArray(int size);\n"
                + "}"
                + "int describeContents();\n"
                + "void writeToParcel(Parcel in, int flags);\n"
                + "}\n");
    parcel =
        JavaFileObjects.forSourceString(
            "android.os.Parcel",
            ""
                + "package android.os;\n"
                + "public interface Parcel {\n"
                + "Object readValue(ClassLoader cl);\n"
                + "void writeValue(Object o);\n"
                + "}");
  }
Esempio n. 2
0
  @Test
  public void testTable() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import Table;",
                    "import Key;",
                    "@Table",
                    "public class Test {",
                    "  @Key long id;",
                    "  String name;",
                    "}"));
    JavaFileObject expectedSource =
        JavaFileObjects.forSourceString(
            "test/Test$$ViewBinder",
            Joiner.on('\n')
                .join(
                    "// Generated code from Cying-ORM. Do not modify!",
                    "package test;",
                    "import android.content.ContentValues;",
                    "import android.database.Cursor;",
                    "import BaseDao;",
                    "public class Test$$Dao extends BaseDao<Test> {",
                    "    private static String SQL=\"CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT);\"",
                    "    static {",
                    "       saveSQL(SQL);",
                    "    }",
                    "    @Override protected Test cursorToEntity(Cursor cursor) {",
                    "        Test entity=new Test();",
                    "        entity.id=cursor.getLong(cursor.getColumnIndex(\"id\"));",
                    "        entity.name=cursor.getString(cursor.getColumnIndex(\"name\"));",
                    "        return entity;",
                    "    }",
                    "    @Override protected ContentValues entityToValues(Test entity) {",
                    "        ContentValues values=new ContentValues();",
                    "        values.put(\"name\",entity.name)",
                    "        return values;",
                    "    }",
                    "    @Override public String getTableName() {",
                    "        return \"test\";",
                    "    }",
                    "    @Override public String getTableSQL() { return SQL; }",
                    "    @Override public String getIdentityName() {",
                    "        return \"id\";",
                    "    }",
                    "    @Override public long getIdentity(Test entity) {",
                    "        return entity.id;",
                    "    }",
                    "}"));

    Truth.assertAbout(javaSource())
        .that(source)
        .processedWith(new ORMProcessor())
        .compilesWithoutError()
        .and()
        .generatesSources(expectedSource);
  }
  @Test
  public void methodWithMultipleIds() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import android.app.Activity;",
                    "import android.view.View;",
                    "import butterknife.OnClick;",
                    "public class Test extends Activity {",
                    "  @OnClick({1, 2, 3}) void click() {}",
                    "}"));

    JavaFileObject expectedSource =
        JavaFileObjects.forSourceString(
            "test/Test$$ViewInjector",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import android.view.View;",
                    "import butterknife.ButterKnife.Finder;",
                    "import butterknife.ButterKnife.Injector;",
                    "public class Test$$ViewInjector<T extends test.Test> implements Injector<T> {",
                    "  @Override public void inject(final Finder finder, final T target, Object source) {",
                    "    View view;",
                    "    view = finder.findRequiredView(source, 1, \"method 'click'\");",
                    "    view.setOnClickListener(new butterknife.internal.DebouncingOnClickListener() {",
                    "      @Override public void doClick(android.view.View p0) {",
                    "        target.click();",
                    "      }",
                    "    });",
                    "    view = finder.findRequiredView(source, 2, \"method 'click'\");",
                    "    view.setOnClickListener(new butterknife.internal.DebouncingOnClickListener() {",
                    "      @Override public void doClick(android.view.View p0) {",
                    "        target.click();",
                    "      }",
                    "    });",
                    "    view = finder.findRequiredView(source, 3, \"method 'click'\");",
                    "    view.setOnClickListener(new butterknife.internal.DebouncingOnClickListener() {",
                    "      @Override public void doClick(android.view.View p0) {",
                    "        target.click();",
                    "      }",
                    "    });",
                    "  }",
                    "  @Override public void reset(T target) {",
                    "  }",
                    "}"));

    ASSERT
        .about(javaSource())
        .that(source)
        .processedWith(butterknifeProcessors())
        .compilesWithoutError()
        .and()
        .generatesSources(expectedSource);
  }
  @Test
  public void shouldCompileWithoutError() throws Exception {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "LocalStorage",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import de.ad.sharp.api.SharedPreference;",
                    "import java.util.Date;",
                    "@SharedPreference public interface LocalStorage {",
                    "   int getIntPreference();",
                    "   void setIntPreference(int value);",
                    "   long getLongPreference();",
                    "   void setLongPreference(long value);",
                    "   float getFloatPreference();",
                    "   void setFloatPreference(float value);",
                    "   boolean isBooleanPreference();",
                    "   void setBooleanPreference(boolean value);",
                    "   String getStringPreference();",
                    "   void setStringPreference(String value);",
                    "   Date getDatePreference();",
                    "   void setDatePreference(Date value);",
                    "void reset();",
                    "}"));

    JavaFileObject expectedSource =
        JavaFileObjects.forSourceString(
            "LocalStorageImpl",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import de.ad.sharp.api.SharedPreference;",
                    "@SharedPreference public interface LocalStorage {",
                    "   int getIntPreference();",
                    "   void setIntPreference(int value);",
                    "   long getLongPreference();",
                    "   void setLongPreference(long value);",
                    "   float getFloatPreference();",
                    "   void setFloatPreference(float value);",
                    "   boolean getBooleanPreference();",
                    "   void setBooleanPreference(boolean value);",
                    "   String getStringPreference();",
                    "   void setStringPreference(String value);",
                    "}"));

    assertAbout(javaSource())
        .that(source)
        .processedWith(new SharedPreferenceProcessor())
        .compilesWithoutError();
    // http://stackoverflow.com/questions/32394726/assertionerror-when-unit-testing-an-annotation-processor-with-compile-testing
    // .and().generatesSources(expectedSource);
  }
  @Test
  public void wrong2ndOnUpgradeParam() {
    JavaFileObject helperClass =
        JavaFileObjects.forSourceString(
            "com.example.MyDatabaseHelper",
            Joiner.on('\n')
                .join(
                    "package com.example;",
                    "",
                    "import android.database.sqlite.SQLiteDatabase;",
                    "import com.j256.ormlite.support.ConnectionSource;",
                    "import eu.f3rog.ormlite.helper.Helper;",
                    "import eu.f3rog.ormlite.helper.OnUpgrade;",
                    "",
                    "@Helper(",
                    "   name = \"my_database.db\",",
                    "   tables = {}",
                    ")",
                    "public class MyDatabaseHelper {",
                    "",
                    "   @OnUpgrade(to = 1)",
                    "   public void up(SQLiteDatabase database, Object o) {}",
                    "",
                    "}"));

    Truth.assert_()
        .about(JavaSourcesSubjectFactory.javaSources())
        .that(files(helperClass))
        .processedWith(new eu.f3rog.ormlite.helper.compiler.HelperProcessor())
        .failsToCompile()
        .withErrorContaining("must have 2nd parameter of type");
  }
  @Test
  public void failsIfMoreThanOneParameter() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import android.app.Activity;",
                    "import android.view.View;",
                    "import butterknife.OnClick;",
                    "public class Test extends Activity {",
                    "  @OnClick(1)",
                    "  public void doStuff(View thing, View otherThing) {",
                    "  }",
                    "}"));

    ASSERT
        .about(javaSource())
        .that(source)
        .processedWith(butterknifeProcessors())
        .failsToCompile()
        .withErrorContaining(
            "@OnClick methods can have at most 1 parameter(s). (test.Test.doStuff)")
        .in(source)
        .onLine(7);
  }
  @Test
  public void failsIfStatic() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import android.app.Activity;",
                    "import butterknife.OnClick;",
                    "public class Test extends Activity {",
                    "  @OnClick(1)",
                    "  public static void doStuff() {",
                    "  }",
                    "}"));

    ASSERT
        .about(javaSource())
        .that(source)
        .processedWith(butterknifeProcessors())
        .failsToCompile()
        .withErrorContaining("@OnClick methods must not be private or static. (test.Test.doStuff)")
        .in(source)
        .onLine(6);
  }
  @Test
  public void failsIfHasDuplicateIds() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import android.app.Activity;",
                    "import butterknife.OnClick;",
                    "public class Test extends Activity {",
                    "  @OnClick({1, 2, 3, 1})",
                    "  void doStuff() {",
                    "  }",
                    "}"));

    ASSERT
        .about(javaSource())
        .that(source)
        .processedWith(butterknifeProcessors())
        .failsToCompile()
        .withErrorContaining(
            "@OnClick annotation for method contains duplicate ID 1. (test.Test.doStuff)")
        .in(source)
        .onLine(6);
  }
  @Test
  public void missingEmptyConstructor() {
    JavaFileObject helperClass =
        JavaFileObjects.forSourceString(
            "com.example.MyDatabaseHelper",
            Joiner.on('\n')
                .join(
                    "package com.example;",
                    "",
                    "import eu.f3rog.ormlite.helper.Helper;",
                    "import eu.f3rog.ormlite.helper.OnUpgrade;",
                    "",
                    "@Helper(",
                    "   name = \"my_database.db\",",
                    "   tables = {}",
                    ")",
                    "public class MyDatabaseHelper {",
                    "",
                    "   public MyDatabaseHelper(int i) { }",
                    "",
                    "}"));

    Truth.assert_()
        .about(JavaSourcesSubjectFactory.javaSources())
        .that(files(helperClass))
        .processedWith(new eu.f3rog.ormlite.helper.compiler.HelperProcessor())
        .failsToCompile()
        .withErrorContaining("must have empty constructor");
  }
Esempio n. 10
0
  @Test
  public void testPrivateModelValues() {
    final String content =
        ""
            + "package com.yella;\n"
            + "import com.ngandroid.lib.annotations.NgModel;\n"
            + "import com.ngandroid.lib.annotations.NgScope;\n\n"
            + "@NgScope    \n"
            + "class PrivateScope {\n"
            + "   @NgModel\n"
            + "   Model model;"
            + "}\n"
            + "class Model {\n"
            + "   private void setX(int x){\n"
            + "   }\n"
            + "   private int getX(){\n"
            + "       return 0;\n"
            + "   }\n"
            + "}";

    JavaFileObject fileObject = JavaFileObjects.forSourceString("com.yella.PrivateScope", content);

    ASSERT
        .about(javaSource())
        .that(fileObject)
        .processedWith(
            Collections.singletonList(
                new NgProcessor("ng-processor/src/test/resources/emptylayout")))
        .failsToCompile()
        .withErrorContaining("Unable to access field")
        .in(fileObject)
        .onLine(10);
  }
Esempio n. 11
0
  @Test
  public void testPrivateUsedMethod() {
    final String content =
        ""
            + "package com.yella;\n"
            + "import com.ngandroid.lib.annotations.NgModel;\n"
            + "import com.ngandroid.lib.annotations.NgScope;\n\n"
            + "@NgScope    \n"
            + "class PrivateScope {\n"
            + "   private void method(){\n"
            + "   }\n"
            + "}";

    JavaFileObject fileObject = JavaFileObjects.forSourceString("com.yella.PrivateScope", content);

    ASSERT
        .about(javaSource())
        .that(fileObject)
        .processedWith(
            Collections.singletonList(
                new NgProcessor("ng-processor/src/test/resources/test_private_method")))
        .failsToCompile()
        .withErrorContaining("is not accessible")
        .in(fileObject)
        .onLine(7);
  }
  @Test
  public void shouldCompileDefaultSharedPreferenceWithoutError() throws Exception {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "LocalStorage",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import de.ad.sharp.api.DefaultSharedPreference;",
                    "@DefaultSharedPreference public interface LocalStorage {",
                    "   int getIntPreference();",
                    "   void setIntPreference(int value);",
                    "   long getLongPreference();",
                    "   void setLongPreference(long value);",
                    "   float getFloatPreference();",
                    "   void setFloatPreference(float value);",
                    "   boolean isBooleanPreference();",
                    "   void setBooleanPreference(boolean value);",
                    "   String getStringPreference();",
                    "   void setStringPreference(String value);",
                    "void reset();",
                    "}"));

    assertAbout(javaSource())
        .that(source)
        .processedWith(new SharedPreferenceProcessor())
        .compilesWithoutError();
  }
 private JavaFileObject classFile(String packageName, String className) {
   return JavaFileObjects.forSourceString(
       String.format("%s.%s", packageName, className),
       Joiner.on('\n')
           .join(
               String.format("package %s;", packageName),
               String.format("public class %s {}", className)));
 }
  @Test
  public void missingDatabaseTableAnnotation() {
    JavaFileObject helperClass =
        JavaFileObjects.forSourceString(
            "com.example.MyDatabaseHelper",
            Joiner.on('\n')
                .join(
                    "package com.example;",
                    "",
                    "import com.example.model.Missing;",
                    "import eu.f3rog.ormlite.helper.Helper;",
                    "import eu.f3rog.ormlite.helper.OnUpgrade;",
                    "",
                    "@Helper(",
                    "   name = \"my_database.db\",",
                    "   tables = {",
                    "       Missing.class",
                    "   }",
                    ")",
                    "public class MyDatabaseHelper {",
                    "",
                    "}"));

    JavaFileObject table =
        JavaFileObjects.forSourceString(
            "com.example.model.Missing",
            Joiner.on('\n')
                .join("package com.example.model;", "", "public class Missing {", "", "}"));

    Truth.assert_()
        .about(JavaSourcesSubjectFactory.javaSources())
        .that(files(table, helperClass))
        .processedWith(new eu.f3rog.ormlite.helper.compiler.HelperProcessor())
        .failsToCompile()
        .withErrorContaining("@Helper tables must contain classes annotated with @DatabaseTable");
  }
 private JavaFileObject tableClass1() {
   return JavaFileObjects.forSourceString(
       "com.example.model.TableClass1",
       Joiner.on('\n')
           .join(
               "package com.example.model;",
               "",
               "import com.example.model.BaseClass;",
               "import com.j256.ormlite.table.DatabaseTable;",
               "",
               "@DatabaseTable",
               "public class TableClass1 extends BaseClass {",
               "",
               "}"));
 }
 private JavaFileObject baseClass() {
   return JavaFileObjects.forSourceString(
       "com.example.model.BaseClass",
       Joiner.on('\n')
           .join(
               "package com.example.model;",
               "",
               "import com.j256.ormlite.field.DatabaseField;",
               "",
               "public class BaseClass {",
               "",
               "    @DatabaseField(id = true)",
               "    private String id;",
               "",
               "}"));
 }
  @Test
  public void shouldFailWithIllegalResetReturnType() throws Exception {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "LocalStorage",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import de.ad.sharp.api.SharedPreference;",
                    "@SharedPreference public interface LocalStorage {",
                    "   boolean reset();",
                    "}"));

    assertAbout(javaSource())
        .that(source)
        .processedWith(new SharedPreferenceProcessor())
        .failsToCompile()
        .withErrorContaining(SharedPreferenceImpl.ILLEGAL_RESET_RETURN_TYPE);
  }
  @Test
  public void shouldFailWithIllegalSetterParameterCount() throws Exception {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "LocalStorage",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import de.ad.sharp.api.SharedPreference;",
                    "@SharedPreference public interface LocalStorage {",
                    "   void setIntPreference();",
                    "}"));

    assertAbout(javaSource())
        .that(source)
        .processedWith(new SharedPreferenceProcessor())
        .failsToCompile()
        .withErrorContaining(SharedPreferenceImpl.ILLEGAL_SETTER_PARAMETER_COUNT);
  }
  @Test
  public void shouldFailWithIllegalBooleanGetterMessageName() throws Exception {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "LocalStorage",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import de.ad.sharp.api.SharedPreference;",
                    "@SharedPreference public interface LocalStorage {",
                    "   boolean getIntPreference();",
                    "}"));

    assertAbout(javaSource())
        .that(source)
        .processedWith(new SharedPreferenceProcessor())
        .failsToCompile()
        .withErrorContaining(SharedPreferenceImpl.ILLEGAL_BOOLEAN_GETTER_MESSAGE_NAME);
  }
  @Test
  public void shouldFailWithIllegalTypeError() throws Exception {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "LocalStorage",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import de.ad.sharp.api.SharedPreference;",
                    "@SharedPreference public abstract class LocalStorage {",
                    "   public void getIntPreference();",
                    "}"));

    assertAbout(javaSource())
        .that(source)
        .processedWith(new SharedPreferenceProcessor())
        .failsToCompile()
        .withErrorContaining(SharedPreferenceImpl.ILLEGAL_TYPE);
  }
 @Override
 public void run() {
   String source =
       Joiner.on('\n')
           .join(
               ImmutableList.of(
                   "package foo.bar;",
                   "import " + AutoJson.class.getName() + ";",
                   "@AutoJson abstract class Test {",
                   "  abstract int baz();",
                   "  static Test create(int baz) {",
                   "    return new AutoJson_Test(baz);",
                   "  }",
                   "}"));
   assert_()
       .about(javaSource())
       .that(JavaFileObjects.forSourceString("foo.bar.Test", source))
       .processedWith(new AutoJsonProcessor())
       .compilesWithoutError();
 }
  @Test
  public void shouldFailWithIllegalNoGetterForSetter() throws Exception {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "LocalStorage",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import de.ad.sharp.api.SharedPreference;",
                    "@SharedPreference public interface LocalStorage {",
                    "   void setIntPreference(int value);",
                    "}"));

    String expectedError =
        String.format(SharedPreferenceImpl.ILLEGAL_NO_GETTER_FOR_SETTER, "setIntPreference");

    assertAbout(javaSource())
        .that(source)
        .processedWith(new SharedPreferenceProcessor())
        .failsToCompile()
        .withErrorContaining(expectedError);
  }
  @Test
  public void failsInAndroidPackage() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package android.test;",
                    "import butterknife.OnClick;",
                    "public class Test {",
                    "  @OnClick(1) void doStuff() {}",
                    "}"));

    ASSERT
        .about(javaSource())
        .that(source)
        .processedWith(butterknifeProcessors())
        .failsToCompile()
        .withErrorContaining(
            "@OnClick-annotated class incorrectly in Android framework package. (android.test.Test)")
        .in(source)
        .onLine(4);
  }
Esempio n. 24
0
  @Test
  public void testPrivateUnusedMethod() {
    final String content =
        ""
            + "package com.yella;\n"
            + "import com.ngandroid.lib.annotations.NgModel;\n"
            + "import com.ngandroid.lib.annotations.NgScope;\n\n"
            + "@NgScope    \n"
            + "class PrivateScope {\n"
            + "   private void method(){\n"
            + "   }\n"
            + "}";

    ASSERT
        .about(javaSource())
        .that(JavaFileObjects.forSourceString("com.yella.PrivateScope", content))
        .processedWith(
            Collections.singletonList(
                new NgProcessor("ng-processor/src/test/resources/emptylayout")))
        .compilesWithoutError()
        .and()
        .generatesFileNamed(SOURCE_OUTPUT, "com.yella", "PrivateScope$$NgScope.java");
  }
  @Test
  public void methodVisibility() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import android.app.Activity;",
                    "import android.view.View;",
                    "import butterknife.OnClick;",
                    "public class Test extends Activity {",
                    "  @OnClick(1) public void thing1() {}",
                    "  @OnClick(2) void thing2() {}",
                    "  @OnClick(3) protected void thing3() {}",
                    "}"));

    ASSERT
        .about(javaSource())
        .that(source)
        .processedWith(butterknifeProcessors())
        .compilesWithoutError();
  }
  @Test
  public void failsIfParameterNotView() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import android.app.Activity;",
                    "import butterknife.OnClick;",
                    "public class Test extends Activity {",
                    "  @OnClick(1)",
                    "  public void doStuff(String thing) {",
                    "  }",
                    "}"));

    ASSERT
        .about(javaSource())
        .that(source)
        .processedWith(butterknifeProcessors())
        .failsToCompile()
        .withErrorContaining(
            Joiner.on('\n')
                .join(
                    "Unable to match @OnClick method arguments. (test.Test.doStuff)",
                    "  ",
                    "    Parameter #1: java.lang.String",
                    "      did not match any listener parameters",
                    "  ",
                    "  Methods may have up to 1 parameter(s):",
                    "  ",
                    "    android.view.View",
                    "  ",
                    "  These may be listed in any order but will be searched for from top to bottom."))
        .in(source)
        .onLine(6);
  }
  @Test
  public void failsIfInInterface() {
    JavaFileObject source =
        JavaFileObjects.forSourceString(
            "test.Test",
            Joiner.on('\n')
                .join(
                    "package test;",
                    "import butterknife.OnClick;",
                    "public interface Test {",
                    "  @OnClick(1)",
                    "  void doStuff();",
                    "}"));

    ASSERT
        .about(javaSource())
        .that(source)
        .processedWith(butterknifeProcessors())
        .failsToCompile()
        .withErrorContaining(
            "@OnClick methods may only be contained in classes. (test.Test.doStuff)")
        .in(source)
        .onLine(3);
  }
Esempio n. 28
0
  @Test
  public void unbindingThroughAbstractChild() {
    JavaFileObject source1 =
        JavaFileObjects.forSourceString(
            "test.Test",
            ""
                + "package test;\n"
                + "import android.app.Activity;\n"
                + "import butterknife.OnClick;\n"
                + "public class Test extends Activity {\n"
                + "  @OnClick(1) void doStuff1() { }\n"
                + "}");

    JavaFileObject source2 =
        JavaFileObjects.forSourceString(
            "test.TestOne",
            "" + "package test;\n" + "public abstract class TestOne extends Test {\n" + "}");

    JavaFileObject source3 =
        JavaFileObjects.forSourceString(
            "test.TestTwo",
            ""
                + "package test;\n"
                + "import butterknife.OnClick;\n"
                + "class TestTwo extends TestOne {\n"
                + "  @OnClick(1) void doStuff2() { }\n"
                + "}");

    JavaFileObject binding1Source =
        JavaFileObjects.forSourceString(
            "test/Test_ViewBinding",
            ""
                + "package test;\n"
                + "import android.support.annotation.CallSuper;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.view.View;\n"
                + "import butterknife.Unbinder;\n"
                + "import butterknife.internal.DebouncingOnClickListener;\n"
                + "import butterknife.internal.Utils;\n"
                + "import java.lang.IllegalStateException;\n"
                + "import java.lang.Override;\n"
                + "public class Test_ViewBinding implements Unbinder {\n"
                + "  private Test target;\n"
                + "  private View view1;\n"
                + "  @UiThread\n"
                + "  public Test_ViewBinding(final Test target, View source) {\n"
                + "    this.target = target;\n"
                + "    View view;\n"
                + "    view = Utils.findRequiredView(source, 1, \"method 'doStuff1'\");\n"
                + "    view1 = view;\n"
                + "    view.setOnClickListener(new DebouncingOnClickListener() {\n"
                + "      @Override\n"
                + "      public void doClick(View p0) {\n"
                + "        target.doStuff1();\n"
                + "      }\n"
                + "    });\n"
                + "  }\n"
                + "  @Override\n"
                + "  @CallSuper\n"
                + "  public void unbind() {\n"
                + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n"
                + "    target = null;\n"
                + "    view1.setOnClickListener(null);\n"
                + "    view1 = null;\n"
                + "  }\n"
                + "}");

    JavaFileObject binding2Source =
        JavaFileObjects.forSourceString(
            "test/TestTwo_ViewBinding",
            ""
                + "package test;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.view.View;\n"
                + "import butterknife.internal.DebouncingOnClickListener;\n"
                + "import butterknife.internal.Utils;\n"
                + "import java.lang.IllegalStateException;\n"
                + "import java.lang.Override;\n"
                + "public class TestTwo_ViewBinding extends Test_ViewBinding {\n"
                + "  private TestTwo target;\n"
                + "  private View view1;\n"
                + "  @UiThread\n"
                + "  public TestTwo_ViewBinding(final TestTwo target, View source) {\n"
                + "    super(target, source);\n"
                + "    this.target = target;\n"
                + "    View view;\n"
                + "    view = Utils.findRequiredView(source, 1, \"method 'doStuff2'\");\n"
                + "    view1 = view;\n"
                + "    view.setOnClickListener(new DebouncingOnClickListener() {\n"
                + "      @Override\n"
                + "      public void doClick(View p0) {\n"
                + "        target.doStuff2();\n"
                + "      }\n"
                + "    });\n"
                + "  }\n"
                + "  @Override\n"
                + "  public void unbind() {\n"
                + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n"
                + "    target = null;\n"
                + "    view1.setOnClickListener(null);\n"
                + "    view1 = null;\n"
                + "    super.unbind();\n"
                + "  }\n"
                + "}");

    assertAbout(javaSources())
        .that(asList(source1, source2, source3))
        .withCompilerOptions("-Xlint:-processing")
        .processedWith(new ButterKnifeProcessor())
        .compilesWithoutWarnings()
        .and()
        .generatesSources(binding1Source, binding2Source);
  }
  @Test
  public void customUpgrade() {
    JavaFileObject helperClass =
        JavaFileObjects.forSourceString(
            "com.example.WhateverDatabase",
            Joiner.on('\n')
                .join(
                    "package com.example;",
                    "",
                    "import android.database.sqlite.SQLiteDatabase;",
                    "import com.example.model.TableClass1;",
                    "import com.j256.ormlite.support.ConnectionSource;",
                    "import eu.f3rog.ormlite.helper.Helper;",
                    "import eu.f3rog.ormlite.helper.OnUpgrade;",
                    "",
                    "@Helper(",
                    "   version = 3,",
                    "   name = \"whatever\",",
                    "   tables = {",
                    "       TableClass1.class",
                    "   }",
                    ")",
                    "public class WhateverDatabase {",
                    "",
                    "   @OnUpgrade(from = 1, to = 3)",
                    "   public void up1to2(SQLiteDatabase database, ConnectionSource connectionSource) {",
                    "   }",
                    "",
                    "   @OnUpgrade(from = 2, to = 3)",
                    "   public void up2to3(SQLiteDatabase database, ConnectionSource connectionSource) {",
                    "   }",
                    "",
                    "   @OnUpgrade(to = 4)",
                    "   public void upto4(SQLiteDatabase database, ConnectionSource connectionSource) {",
                    "   }",
                    "",
                    "   @OnUpgrade(to = 5)",
                    "   public void upto5(SQLiteDatabase database, ConnectionSource connectionSource) {",
                    "   }",
                    "",
                    "   @OnUpgrade(to = 6)",
                    "   public void upto6(SQLiteDatabase database, ConnectionSource connectionSource) {",
                    "   }",
                    "",
                    "}"));

    JavaFileObject expectedFile =
        JavaFileObjects.forSourceString(
            "com.example.WhateverDatabaseHelper",
            Joiner.on('\n')
                .join(
                    "package com.example;",
                    "",
                    "import android.content.Context;",
                    "import android.database.sqlite.SQLiteDatabase;",
                    "import android.util.Log;",
                    "import com.example.model.TableClass1;",
                    "import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;",
                    "import com.j256.ormlite.dao.Dao;",
                    "import com.j256.ormlite.support.ConnectionSource;",
                    "import com.j256.ormlite.table.TableUtils;",
                    "import java.lang.Override;",
                    "import java.lang.String;",
                    "import java.sql.SQLException;",
                    "",
                    "public final class WhateverDatabaseHelper extends OrmLiteSqliteOpenHelper {",
                    "",
                    "   private Dao<TableClass1, String> mTableClass1Dao;",
                    "",
                    "   public Gen_MyDatabaseHelper(Context context) {",
                    "       super(context, \"whatever.db\", null, 3);",
                    "   }",
                    "",
                    "   @Override",
                    "   public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {",
                    "       Log.i(WhateverDatabase.class.getName(), \"onCreate\");",
                    "       try {",
                    "               TableUtils.createTable(connectionSource, TableClass1.class);",
                    "       }",
                    "       catch (SQLException e) {",
                    "           Log.e(WhateverDatabase.class.getName(), \"Can't create database tables.\", e);",
                    "           throw new RuntimeException(e);",
                    "       }",
                    "   }",
                    "",
                    "   @Override",
                    "   public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {",
                    "       WhateverDatabase upgradeHelper = new WhateverDatabase();",
                    "       int version = oldVersion;",
                    "       if (version == 1) {",
                    "           upgradeHelper.up1to2(database, connectionSource);",
                    "           version = 3;",
                    "       }",
                    "       if (version == 2) {",
                    "           upgradeHelper.up2to3(database, connectionSource);",
                    "           version = 3;",
                    "       }",
                    "       if (version <= 4) {",
                    "            upgradeHelper.upto4(database, connectionSource);",
                    "            version = 4;",
                    "       }",
                    "       if (version <= 5) {",
                    "           upgradeHelper.upto5(database, connectionSource);",
                    "           version = 5;",
                    "       }",
                    "       if (version <= 6) {",
                    "           upgradeHelper.upto6(database, connectionSource);",
                    "           version = 6;",
                    "       }",
                    "   }",
                    "",
                    "   public void clearTables() {",
                    "       Log.i(WhateverDatabase.class.getName(), \"clearTables\");",
                    "       try {",
                    "           connectionSource.getReadWriteConnection();",
                    "           TableUtils.clearTable(connectionSource, TableClass1.class);",
                    "       }",
                    "       catch (SQLException e) {",
                    "           Log.e(WhateverDatabase.class.getName(), \"Can't clear database tables.\", e);",
                    "       }",
                    "   }",
                    "",
                    "   @Override",
                    "   public void close() {",
                    "       super.close();",
                    "       mTableClass1Dao = null;",
                    "   }",
                    "",
                    "   public Dao<TableClass1, String> getTableClass1Dao() throws SQLException {",
                    "       if (mTableClass1Dao == null)  {",
                    "           mTableClass1Dao = getDao(TableClass1.class);",
                    "       }",
                    "       return mTableClass1Dao;",
                    "   }",
                    "",
                    "}"));

    Truth.assert_()
        .about(JavaSourcesSubjectFactory.javaSources())
        .that(files(helperClass))
        .processedWith(new eu.f3rog.ormlite.helper.compiler.HelperProcessor())
        .compilesWithoutError()
        .and()
        .generatesSources(expectedFile);
  }
Esempio n. 30
0
  @Test
  public void fullIntegration() {
    JavaFileObject sourceA =
        JavaFileObjects.forSourceString(
            "test.A",
            ""
                + "package test;\n"
                + "import android.support.annotation.ColorInt;\n"
                + "import android.view.View;\n"
                + "import butterknife.BindColor;\n"
                + "import butterknife.ButterKnife;\n"
                + "public class A {\n"
                + "  @BindColor(android.R.color.black) @ColorInt int blackColor;\n"
                + "  public A(View view) {\n"
                + "    ButterKnife.bind(this, view);\n"
                + "  }\n"
                + "}\n");

    JavaFileObject sourceB =
        JavaFileObjects.forSourceString(
            "test.B",
            ""
                + "package test;\n"
                + "import android.support.annotation.ColorInt;\n"
                + "import android.view.View;\n"
                + "import butterknife.BindColor;\n"
                + "import butterknife.ButterKnife;\n"
                + "public class B extends A {\n"
                + "  @BindColor(android.R.color.white) @ColorInt int whiteColor;\n"
                + "  public B(View view) {\n"
                + "    super(view);\n"
                + "    ButterKnife.bind(this, view);\n"
                + "  }\n"
                + "}\n");

    JavaFileObject sourceC =
        JavaFileObjects.forSourceString(
            "test.C",
            ""
                + "package test;\n"
                + "import android.support.annotation.ColorInt;\n"
                + "import android.view.View;\n"
                + "import butterknife.BindView;\n"
                + "import butterknife.BindColor;\n"
                + "import butterknife.ButterKnife;\n"
                + "public class C extends B {\n"
                + "  @BindColor(android.R.color.transparent) @ColorInt int transparentColor;\n"
                + "  @BindView(android.R.id.button1) View button1;\n"
                + "  public C(View view) {\n"
                + "    super(view);\n"
                + "    ButterKnife.bind(this, view);\n"
                + "  }\n"
                + "}\n");

    JavaFileObject sourceD =
        JavaFileObjects.forSourceString(
            "test.D",
            ""
                + "package test;\n"
                + "import android.support.annotation.ColorInt;\n"
                + "import android.view.View;\n"
                + "import butterknife.BindColor;\n"
                + "import butterknife.ButterKnife;\n"
                + "public class D extends C {\n"
                + "  @BindColor(android.R.color.darker_gray) @ColorInt int grayColor;\n"
                + "  public D(View view) {\n"
                + "    super(view);\n"
                + "    ButterKnife.bind(this, view);\n"
                + "  }\n"
                + "}\n");

    JavaFileObject sourceE =
        JavaFileObjects.forSourceString(
            "test.E",
            ""
                + "package test;\n"
                + "import android.support.annotation.ColorInt;\n"
                + "import android.view.View;\n"
                + "import butterknife.BindColor;\n"
                + "import butterknife.ButterKnife;\n"
                + "public class E extends C {\n"
                + "  @BindColor(android.R.color.background_dark) @ColorInt int backgroundDarkColor;\n"
                + "  public E(View view) {\n"
                + "    super(view);\n"
                + "    ButterKnife.bind(this, view);\n"
                + "  }\n"
                + "}\n");

    JavaFileObject sourceF =
        JavaFileObjects.forSourceString(
            "test.F",
            ""
                + "package test;\n"
                + "import android.support.annotation.ColorInt;\n"
                + "import android.view.View;\n"
                + "import butterknife.BindColor;\n"
                + "import butterknife.ButterKnife;\n"
                + "public class F extends D {\n"
                + "  @BindColor(android.R.color.background_light) @ColorInt int backgroundLightColor;\n"
                + "  public F(View view) {\n"
                + "    super(view);\n"
                + "    ButterKnife.bind(this, view);\n"
                + "  }\n"
                + "}\n");

    JavaFileObject sourceG =
        JavaFileObjects.forSourceString(
            "test.G",
            ""
                + "package test;\n"
                + "import android.support.annotation.ColorInt;\n"
                + "import android.view.View;\n"
                + "import butterknife.BindView;\n"
                + "import butterknife.BindColor;\n"
                + "import butterknife.ButterKnife;\n"
                + "import butterknife.OnClick;\n"
                + "public class G extends E {\n"
                + "  @BindColor(android.R.color.darker_gray) @ColorInt int grayColor;\n"
                + "  @BindView(android.R.id.button2) View button2;\n"
                + "  public G(View view) {\n"
                + "    super(view);\n"
                + "    ButterKnife.bind(this, view);\n"
                + "  }\n"
                + "  @OnClick(android.R.id.content) public void onClick() {\n"
                + "  }\n"
                + "}\n");

    JavaFileObject sourceH =
        JavaFileObjects.forSourceString(
            "test.H",
            ""
                + "package test;\n"
                + "import android.support.annotation.ColorInt;\n"
                + "import android.view.View;\n"
                + "import butterknife.BindView;\n"
                + "import butterknife.BindColor;\n"
                + "import butterknife.ButterKnife;\n"
                + "public class H extends G {\n"
                + "  @BindColor(android.R.color.primary_text_dark) @ColorInt int grayColor;\n"
                + "  @BindView(android.R.id.button3) View button3;\n"
                + "  public H(View view) {\n"
                + "    super(view);\n"
                + "    ButterKnife.bind(this, view);\n"
                + "  }\n"
                + "}\n");

    JavaFileObject bindingASource =
        JavaFileObjects.forSourceString(
            "test/A_ViewBinding",
            ""
                + "// Generated code from Butter Knife. Do not modify!\n"
                + "package test;\n"
                + "import android.content.Context;\n"
                + "import android.support.annotation.CallSuper;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.support.v4.content.ContextCompat;\n"
                + "import android.view.View;\n"
                + "import butterknife.Unbinder;\n"
                + "import java.lang.Deprecated;\n"
                + "import java.lang.Override;\n"
                + "public class A_ViewBinding implements Unbinder {\n"
                + "  /**\n"
                + "   * @deprecated Use {@link #Test_ViewBinding(A, Context)} for direct creation.\n"
                + "   *     Only present for runtime invocation through {@code ButterKnife.bind()}.\n"
                + "   */\n"
                + "  @Deprecated\n"
                + "  @UiThread\n"
                + "  public Test_ViewBinding(A target, View source) {\n"
                + "    this(target, source.getContext());\n"
                + "  }\n"
                + "  @UiThread\n"
                + "  public A_ViewBinding(A target, Context context) {\n"
                + "    target.blackColor = ContextCompat.getColor(context, android.R.color.black);\n"
                + "  }\n"
                + "  @Override\n"
                + "  @CallSuper\n"
                + "  public void unbind() {\n"
                + "  }\n"
                + "}");

    JavaFileObject bindingBSource =
        JavaFileObjects.forSourceString(
            "test/B_ViewBinding",
            ""
                + "// Generated code from Butter Knife. Do not modify!\n"
                + "package test;\n"
                + "import android.content.Context;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.support.v4.content.ContextCompat;\n"
                + "import android.view.View;\n"
                + "import java.lang.Deprecated;\n"
                + "public class B_ViewBinding extends A_ViewBinding {\n"
                + "  /**\n"
                + "   * @deprecated Use {@link #Test_ViewBinding(B, Context)} for direct creation.\n"
                + "   *     Only present for runtime invocation through {@code ButterKnife.bind()}.\n"
                + "   */\n"
                + "  @Deprecated\n"
                + "  @UiThread\n"
                + "  public Test_ViewBinding(B target, View source) {\n"
                + "    this(target, source.getContext());\n"
                + "  }\n"
                + "  @UiThread\n"
                + "  public B_ViewBinding(B target, Context context) {\n"
                + "    super(target, context);\n"
                + "    target.whiteColor = ContextCompat.getColor(context, android.R.color.white);\n"
                + "  }\n"
                + "}");

    JavaFileObject bindingCSource =
        JavaFileObjects.forSourceString(
            "test/C_ViewBinding",
            ""
                + "// Generated code from Butter Knife. Do not modify!\n"
                + "package test;\n"
                + "import android.content.Context;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.support.v4.content.ContextCompat;\n"
                + "import android.view.View;\n"
                + "import butterknife.internal.Utils;\n"
                + "import java.lang.IllegalStateException;\n"
                + "import java.lang.Override;\n"
                + "public class C_ViewBinding extends B_ViewBinding {\n"
                + "  private C target;\n"
                + "  @UiThread\n"
                + "  public C_ViewBinding(C target, View source) {\n"
                + "    super(target, source.getContext());\n"
                + "    this.target = target;\n"
                + "    target.button1 = Utils.findRequiredView(source, android.R.id.button1, \"field 'button1'\");\n"
                + "    Context context = source.getContext();\n"
                + "    target.transparentColor = ContextCompat.getColor(context, android.R.color.transparent);\n"
                + "  }\n"
                + "  @Override\n"
                + "  public void unbind() {\n"
                + "    C target = this.target;\n"
                + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n"
                + "    this.target = null;\n"
                + "    target.button1 = null;\n"
                + "    super.unbind();\n"
                + "  }\n"
                + "}");

    JavaFileObject bindingDSource =
        JavaFileObjects.forSourceString(
            "test/D_ViewBinding",
            ""
                + "package test;\n"
                + "import android.content.Context;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.support.v4.content.ContextCompat;\n"
                + "import android.view.View;\n"
                + "public class D_ViewBinding extends C_ViewBinding {\n"
                + "  @UiThread\n"
                + "  public D_ViewBinding(D target, View source) {\n"
                + "    super(target, source);\n"
                + "    Context context = source.getContext();\n"
                + "    target.grayColor = ContextCompat.getColor(context, android.R.color.darker_gray);\n"
                + "  }\n"
                + "}");

    JavaFileObject bindingESource =
        JavaFileObjects.forSourceString(
            "test/E_ViewBinding",
            ""
                + "package test;\n"
                + "import android.content.Context;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.support.v4.content.ContextCompat;\n"
                + "import android.view.View;\n"
                + "public class E_ViewBinding extends C_ViewBinding {\n"
                + "  @UiThread\n"
                + "  public E_ViewBinding(E target, View source) {\n"
                + "    super(target, source);\n"
                + "    Context context = source.getContext();\n"
                + "    target.backgroundDarkColor = ContextCompat.getColor(context, android.R.color.background_dark);\n"
                + "  }\n"
                + "}");

    JavaFileObject bindingFSource =
        JavaFileObjects.forSourceString(
            "test/F_ViewBinding",
            ""
                + "package test;\n"
                + "import android.content.Context;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.support.v4.content.ContextCompat;\n"
                + "import android.view.View;\n"
                + "public class F_ViewBinding extends D_ViewBinding {\n"
                + "  @UiThread\n"
                + "  public F_ViewBinding(F target, View source) {\n"
                + "    super(target, source);\n"
                + "    Context context = source.getContext();\n"
                + "    target.backgroundLightColor = ContextCompat.getColor(context, android.R.color.background_light);\n"
                + "  }\n"
                + "}");

    JavaFileObject bindingGSource =
        JavaFileObjects.forSourceString(
            "test/G_ViewBinding",
            ""
                + "package test;\n"
                + "import android.content.Context;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.support.v4.content.ContextCompat;\n"
                + "import android.view.View;\n"
                + "import butterknife.internal.DebouncingOnClickListener;\n"
                + "import butterknife.internal.Utils;\n"
                + "import java.lang.IllegalStateException;\n"
                + "import java.lang.Override;\n"
                + "public class G_ViewBinding extends E_ViewBinding {\n"
                + "  private G target;\n"
                + "  private View view16908290;\n"
                + "  @UiThread\n"
                + "  public G_ViewBinding(final G target, View source) {\n"
                + "    super(target, source);\n"
                + "    this.target = target;\n"
                + "    View view;\n"
                + "    target.button2 = Utils.findRequiredView(source, android.R.id.button2, \"field 'button2'\");\n"
                + "    view = Utils.findRequiredView(source, android.R.id.content, \"method 'onClick'\");\n"
                + "    view16908290 = view;\n"
                + "    view.setOnClickListener(new DebouncingOnClickListener() {\n"
                + "      @Override\n"
                + "      public void doClick(View p0) {\n"
                + "        target.onClick();\n"
                + "      }\n"
                + "    });\n"
                + "    Context context = source.getContext();\n"
                + "    target.grayColor = ContextCompat.getColor(context, android.R.color.darker_gray);\n"
                + "  }\n"
                + "  @Override\n"
                + "  public void unbind() {\n"
                + "    G target = this.target;\n"
                + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n"
                + "    this.target = null\n"
                + "    target.button2 = null;\n"
                + "    view16908290.setOnClickListener(null);\n"
                + "    view16908290 = null;\n"
                + "    super.unbind();\n"
                + "  }\n"
                + "}");

    JavaFileObject bindingHSource =
        JavaFileObjects.forSourceString(
            "test/H_ViewBinding",
            ""
                + "package test;\n"
                + "import android.content.Context;\n"
                + "import android.support.annotation.UiThread;\n"
                + "import android.support.v4.content.ContextCompat;\n"
                + "import android.view.View;\n"
                + "import butterknife.internal.Utils;\n"
                + "import java.lang.IllegalStateException;\n"
                + "import java.lang.Override;\n"
                + "public class H_ViewBinding extends G_ViewBinding {\n"
                + "  private H target;\n"
                + "  @UiThread\n"
                + "  public H_ViewBinding(H target, View source) {\n"
                + "    super(target, source);\n"
                + "    this.target = target;\n"
                + "    target.button3 = Utils.findRequiredView(source, android.R.id.button3, \"field 'button3'\");\n"
                + "    Context context = source.getContext();\n"
                + "    target.grayColor = ContextCompat.getColor(context, android.R.color.primary_text_dark);\n"
                + "  }\n"
                + "  @Override\n"
                + "  public void unbind() {\n"
                + "    H target = this.target;\n"
                + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n"
                + "    this.target = null;\n"
                + "    target.button3 = null;\n"
                + "    super.unbind();\n"
                + "  }\n"
                + "}");

    assertAbout(javaSources())
        .that(asList(sourceA, sourceB, sourceC, sourceD, sourceE, sourceF, sourceG, sourceH))
        .withCompilerOptions("-Xlint:-processing")
        .processedWith(new ButterKnifeProcessor())
        .compilesWithoutWarnings()
        .and()
        .generatesSources(
            bindingASource,
            bindingBSource,
            bindingCSource,
            bindingDSource,
            bindingESource,
            bindingFSource,
            bindingGSource,
            bindingHSource);
  }