package com.couchbase.cblite.testapp.tests;

import com.couchbase.cblite.CBLDatabase;
import com.couchbase.cblite.CBLManager;

import junit.framework.Assert;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class Server extends CBLiteTestCase {

    public void testServer() {

        //to ensure this test is easily repeatable we will explicitly remove
        //any stale foo.cblite
        CBLDatabase old = manager.getExistingDatabase("foo");
        if(old != null) {
            old.delete();
        }

        CBLDatabase db = manager.getDatabase("foo");
        Assert.assertNotNull(db);
        Assert.assertEquals("foo", db.getName());
        Assert.assertTrue(db.getPath().startsWith(getServerPath()));
        Assert.assertFalse(db.exists());

        Assert.assertEquals(db, manager.getDatabase("foo"));

        // because foo doesn't exist yet
        List<String> databaseNames = manager.getAllDatabaseNames();
        Assert.assertTrue(!databaseNames.contains("foo"));

        Assert.assertTrue(db.open());
        Assert.assertTrue(db.exists());

        databaseNames = manager.getAllDatabaseNames();
        Assert.assertTrue(databaseNames.contains("foo"));

        db.close();
        db.delete();

    }

    public void testUpgradeOldDatabaseFiles() throws Exception {


        String directoryName = "test-directory-" + System.currentTimeMillis();
        String normalFilesDir = getInstrumentation().getContext().getFilesDir().getAbsolutePath();
        String fakeFilesDir = String.format("%s/%s", normalFilesDir, directoryName);

        File directory = new File(fakeFilesDir);
        if(!directory.exists()) {
            boolean result = directory.mkdir();
            if(!result) {
                throw new IOException("Unable to create directory " + directory);
            }
        }
        File oldTouchDbFile = new File(directory, String.format("old%s", CBLManager.DATABASE_SUFFIX_OLD));
        oldTouchDbFile.createNewFile();
        File newCbLiteFile = new File(directory, String.format("new%s", CBLManager.DATABASE_SUFFIX));
        newCbLiteFile.createNewFile();

        File migratedOldFile = new File(directory, String.format("old%s", CBLManager.DATABASE_SUFFIX));

        Assert.assertTrue(migratedOldFile.exists());
        Assert.assertTrue(oldTouchDbFile.exists() == false);
        Assert.assertTrue(newCbLiteFile.exists());


    }

}

@@ -1322,18 +1322,16 @@
 ption %7B%0A
-%0A%0A
         
@@ -2238,24 +2238,792 @@
 E_SUFFIX));%0A
+        migratedOldFile.createNewFile();%0A        super.stopCBLite();%0A        manager = new CBLManager(getInstrumentation().getContext(), directoryName);%0A%0A        Assert.assertTrue(migratedOldFile.exists());%0A        //cannot rename old.touchdb in old.cblite, old.cblite already exists%0A        Assert.assertTrue(oldTouchDbFile.exists());%0A        Assert.assertTrue(newCbLiteFile.exists());%0A%0A        File dir=new File(getInstrumentation().getContext().getFilesDir(), directoryName);%0A        Assert.assertEquals(3, dir.listFiles().length);%0A%0A        super.stopCBLite();%0A        migratedOldFile.delete();%0A        manager = new CBLManager(getInstrumentation().getContext(), directoryName);%0A%0A        //rename old.touchdb in old.cblite, previous old.cblite already doesn't exist
 %0A        Ass
@@ -3176,16 +3176,157 @@
 sts());%0A
+        dir=new File(getInstrumentation().getContext().getFilesDir(), directoryName);%0A        Assert.assertEquals(2, dir.listFiles().length);
 %0A%0A    %7D%0A

fix testUpgradeOldDatabaseFiles we can't rename old.touchdb in old.cblite if old.cblite already exists