/*
 * Copyright (c) 2014-2015, Qiao
 *
 * 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 info.breezes.orm;

import android.annotation.TargetApi;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.test.AndroidTestCase;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

import junit.framework.TestResult;

import java.io.File;
import java.util.ArrayList;

/**
 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
 */
public class OrmTest extends AndroidTestCase {

    String TAG = "orm.test";

    TestOrmSQLiteHelper helper;
    boolean over;

    @Override
    public void run(TestResult result) {
        long st = System.currentTimeMillis();
        super.run(result);
        Log.d(TAG, getName() + ":" + (System.currentTimeMillis() - st));
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        Log.d(TAG, "setUp");
        OrmConfig.Debug = false;
        helper = new TestOrmSQLiteHelper(getContext(), "test.db", 1);
    }

    @SmallTest
    public void testACreateTable() throws Exception {
        SQLiteDatabase database = helper.getCurrentDatabase(true);
        assertNotNull(database);
    }

    @SmallTest
    public void testBInsert500() throws Exception {
        for (int i = 0; i < 500; i++) {
            Employee employee = new Employee();
            helper.insert(employee);
        }
    }

    @SmallTest
    public void testCInsertAll500() throws Exception {
        ArrayList<Employee> employees = new ArrayList<Employee>();
        for (int i = 0; i < 500; i++) {
            Employee employee = new Employee();
            employees.add(employee);
        }
        helper.insertAll(employees.toArray());
    }

    @SmallTest
    public void testDCount() throws Exception {
        int count = helper.query(Employee.class).execute().size();
        Log.d(TAG, "Count:" + count);
    }


    @SmallTest
    public void testESelect500ToList() throws Exception {
        ArrayList<Employee> employees = helper.query(Employee.class).limit(0, 500).execute().toList();
        if (employees.size() < 1) {
            fail("query to list error.");
        } else {
            Log.d(TAG, "ListSize:" + employees.size());
        }
    }


    @SmallTest
    public void testFDeleteAll() throws Exception {
        helper.clear(Employee.class);
        over = true;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void tearDown() throws Exception {
        Log.d(TAG, "tearDown");
        if (over) {
            Log.d(TAG, "Clean.");
            File file = new File(helper.getCurrentDatabase(false).getPath());
            helper.getCurrentDatabase(false).close();
            SQLiteDatabase.deleteDatabase(file);
        }
        super.tearDown();
    }
}
@@ -622,77 +622,8 @@
 m;%0A%0A
-import android.annotation.TargetApi;%0Aimport android.app.Application;%0A
 impo
@@ -731,105 +731,8 @@
 se;%0A
-import android.test.ApplicationTestCase;%0Aimport android.test.suitebuilder.annotation.MediumTest;%0A
 impo
@@ -2921,16 +2921,18 @@
  %7D%0A%0A    
+//
 @TargetA
@@ -2966,16 +2966,16 @@
 Y_BEAN)%0A
-
     @Ove
@@ -3077,24 +3077,52 @@
 if (over) %7B%0A
+            helper.close();%0A
             
@@ -3291,44 +3291,185 @@
     
-SQLiteDatabase.deleteDatabase(file);
+if(Build.VERSION.SDK_INT%3E= Build.VERSION_CODES.JELLY_BEAN) %7B%0A                SQLiteDatabase.deleteDatabase(file);%0A            %7Delse%7B%0A                file.delete();%0A            %7D
 %0A   

update test case