コード例 #1
0
  @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");
  }
コード例 #2
0
  @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");
  }
コード例 #3
0
  @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");
  }
コード例 #4
0
  @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);
  }