/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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 org.codehaus.activemq.store.bdb;

import com.sleepycat.je.*;
import junit.framework.TestCase;

/**
 * @version $Revision: 1.1 $
 */
public class OrderTest extends TestCase {
    protected Environment env;
    protected Database database;
    protected TransactionConfig transactionConfig = new TransactionConfig();
    protected DatabaseConfig config = BDbHelper.createDatabaseConfig();
    protected SecondaryCursor cursor;
    protected Transaction transaction;
    protected SecondaryDatabase secondaryDatabase;
    protected SecondaryConfig secondaryConfig = new SecondaryConfig();


    public void testOrder() throws Exception {
        doInsert("B", "b123");
        doInsert("D", "d123");
        doInsert("A", "a123");
        doInsert("C", "c123");

        // now lets iterate through
        DatabaseEntry keyEntry = new DatabaseEntry();
        DatabaseEntry valueEntry = new DatabaseEntry();
        DatabaseEntry counterEntry = new DatabaseEntry();
        assertStatusOK(cursor.getFirst(counterEntry, keyEntry, valueEntry, LockMode.DEFAULT));
        printKeyValue(counterEntry, keyEntry, valueEntry);
        while (true) {
            OperationStatus status = cursor.getNext(counterEntry, keyEntry, valueEntry, LockMode.DEFAULT);
            System.out.println("Status: " + status);

            if (status != OperationStatus.SUCCESS) {
                break;
            }
            printKeyValue(counterEntry, keyEntry, valueEntry);
        }
    }

    private void printKeyValue(DatabaseEntry counterEntry, DatabaseEntry keyEntry, DatabaseEntry valueEntry) {
        System.out.println("Got row with counter: " + BDbHelper.longFromBytes(counterEntry.getData())
                + " key: " + asString(keyEntry) + " and value: " + asString(valueEntry));
    }


    protected void doInsert(String key, String value) throws DatabaseException {
        assertStatusOK(database.put(transaction, asEntry(key), asEntry(value)));
    }

    protected void assertStatusOK(OperationStatus status) {
        assertEquals("Status should be OK", OperationStatus.SUCCESS, status);
    }

    protected DatabaseEntry asEntry(String key) {
        return new DatabaseEntry(key.getBytes());
    }

    protected String asString(DatabaseEntry entry) {
        return new String(entry.getData(), entry.getOffset(), entry.getSize());
    }

    protected void setUp() throws Exception {
        env = BDbQueueMessageContainerTest.createEnvironment();

        database = env.openDatabase(null, "orderTest", config);

        secondaryConfig.setKeyCreator(new StubSecondaryKeyGenerator());
        secondaryConfig.setAllowCreate(true);
        secondaryConfig.setAllowPopulate(true);
        secondaryConfig.setTransactional(true);
        secondaryDatabase = env.openSecondaryDatabase(null, "orderIndexTest", database, secondaryConfig);

        transaction = env.beginTransaction(null, null);
        cursor = secondaryDatabase.openSecondaryCursor(transaction, null);
    }

    protected void tearDown() throws Exception {
        /*
        transaction.commit();
        cursor.close();
        database.close();
        env.close();
        */
    }

}
