/*
 * Copyright 2008-2009 LinkedIn, Inc
 * 
 * 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 voldemort.store.bdb;

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

import org.apache.commons.io.FileDeleteStrategy;

import voldemort.TestUtils;
import voldemort.store.StorageEngine;
import voldemort.store.AbstractStorageEngineTest;
import voldemort.utils.ByteArray;
import voldemort.versioning.VectorClock;
import voldemort.versioning.Versioned;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

public class BdbStorageEngineTest extends AbstractStorageEngineTest {

    private Environment environment;
    private Database database;
    private File tempDir;
    private BdbStorageEngine store;
    private DatabaseConfig databaseConfig;
    private Random random;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        this.random = new Random();
        EnvironmentConfig environmentConfig = new EnvironmentConfig();
        environmentConfig = new EnvironmentConfig();
        environmentConfig.setTxnNoSync(true);
        environmentConfig.setAllowCreate(true);
        environmentConfig.setTransactional(true);
        this.tempDir = TestUtils.createTempDir();
        this.environment = new Environment(this.tempDir, environmentConfig);
        this.databaseConfig = new DatabaseConfig();
        databaseConfig.setAllowCreate(true);
        databaseConfig.setTransactional(true);
        databaseConfig.setSortedDuplicates(true);
        this.database = environment.openDatabase(null, "test", databaseConfig);
        this.store = new BdbStorageEngine("test", this.environment, this.database);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        try {
            database.close();
            environment.close();
        } finally {
            FileDeleteStrategy.FORCE.delete(tempDir);
        }
    }

    @Override
    public StorageEngine<ByteArray, byte[]> getStorageEngine() {
        return store;
    }

    public void testMakeKey(byte[] key, VectorClock clock) {
        byte[] keyBytes = BdbStorageEngine.makeKey(key, clock);
        assertTrue("Invalid key returned",
                   TestUtils.bytesEqual(key, BdbStorageEngine.getObjKey(keyBytes)));
        assertEquals("Invalid clock returned", clock, BdbStorageEngine.getVersion(keyBytes));
    }

    public void testMakeKey() {
        testMakeKey("".getBytes(), TestUtils.getClock());
        testMakeKey("abc".getBytes(), null);
        testMakeKey("hello-there".getBytes(), TestUtils.getClock(1, 1, 2, 3, 4));
    }

    public void testPersistence() throws Exception {
        StorageEngine<ByteArray, byte[]> eng = getStorageEngine();
        eng.put(new ByteArray("abc".getBytes()), new Versioned<byte[]>("cdef".getBytes()));
        eng.close();
        this.database = environment.openDatabase(null, "test", databaseConfig);
        eng = new BdbStorageEngine("test", this.environment, this.database);
        List<Versioned<byte[]>> vals = eng.get(new ByteArray("abc".getBytes()));
        assertEquals(1, vals.size());
        TestUtils.bytesEqual("cdef".getBytes(), vals.get(0).getValue());
    }

    public void testEquals() {
        String name = "someName";
        assertEquals(new BdbStorageEngine(name, environment, database),
                     new BdbStorageEngine(name, environment, database));
    }

    public void testNullConstructorParameters() {
        try {
            new BdbStorageEngine(null, environment, database);
        } catch(IllegalArgumentException e) {
            return;
        }
        fail("No exception thrown for null name.");
        try {
            new BdbStorageEngine("name", null, database);
        } catch(IllegalArgumentException e) {
            return;
        }
        fail("No exception thrown for null environment.");
        try {
            new BdbStorageEngine("name", environment, null);
        } catch(IllegalArgumentException e) {
            return;
        }
        fail("No exception thrown for null database.");
    }

}

@@ -672,33 +672,8 @@
 ist;
-%0Aimport java.util.Random;
 %0A%0Aim
@@ -771,16 +771,24 @@
 t.store.
+Abstract
 StorageE
@@ -792,16 +792,20 @@
 geEngine
+Test
 ;%0Aimport
@@ -817,32 +817,24 @@
 emort.store.
-Abstract
 StorageEngin
@@ -826,36 +826,32 @@
 re.StorageEngine
-Test
 ;%0Aimport voldemo
@@ -1353,35 +1353,8 @@
 fig;
-%0A    private Random random;
 %0A%0A  
@@ -1438,44 +1438,8 @@
 ();%0A
-        this.random = new Random();%0A
     

Remove random field, it's never used.