All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.greenrobot.dao.test.AbstractDaoTest Maven / Gradle / Ivy

There is a newer version: 3.0.0-beta3
Show newest version
/*
 * Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.greenrobot.dao.test;

import java.lang.reflect.Method;

import android.database.sqlite.SQLiteDatabase;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.DaoLog;
import de.greenrobot.dao.InternalUnitTestDaoAccess;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.database.Database;
import de.greenrobot.dao.identityscope.IdentityScope;

/**
 * Base class for DAO related testing. Prepares an in-memory DB and DAO.
 * 
 * @author Markus
 * 
 * @param 
 *            DAO class
 * @param 
 *            Entity type of the DAO
 * @param 
 *            Key type of the DAO
 */
public abstract class AbstractDaoTest, T, K> extends DbTest {

    protected final Class daoClass;
    protected D dao;
    protected InternalUnitTestDaoAccess daoAccess;
    protected Property pkColumn;
    protected IdentityScope identityScopeForDao;

    public AbstractDaoTest(Class daoClass) {
        this(daoClass, true);
    }

    public AbstractDaoTest(Class daoClass, boolean inMemory) {
        super(inMemory);
        this.daoClass = daoClass;
    }

    public void setIdentityScopeBeforeSetUp(IdentityScope identityScope) {
        this.identityScopeForDao = identityScope;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        try {
            setUpTableForDao();
            daoAccess = new InternalUnitTestDaoAccess(db, (Class>) daoClass, identityScopeForDao);
            dao = (D) daoAccess.getDao();
        } catch (Exception e) {
            throw new RuntimeException("Could not prepare DAO Test", e);
        }
    }

    protected void setUpTableForDao() throws Exception {
        try {
            Method createTableMethod = daoClass.getMethod("createTable", Database.class, boolean.class);
            createTableMethod.invoke(null, db, false);
        } catch (NoSuchMethodException e) {
            DaoLog.i("No createTable method");
        }
    }

    protected void clearIdentityScopeIfAny() {
        if (identityScopeForDao != null) {
            identityScopeForDao.clear();
            DaoLog.d("Identity scope cleared");
        } else {
            DaoLog.d("No identity scope to clear");
        }
    }

    protected void logTableDump() {
        logTableDump(dao.getTablename());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy