1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.store.bdb;
19
20 import com.sleepycat.je.*;
21 import junit.framework.TestCase;
22
23 /***
24 * @version $Revision: 1.2 $
25 */
26 public class OrderTest extends TestCase {
27 protected Environment env;
28 protected Database database;
29 protected TransactionConfig transactionConfig = new TransactionConfig();
30 protected DatabaseConfig config = BDbHelper.createDatabaseConfig();
31 protected SecondaryCursor cursor;
32 protected Transaction transaction;
33 protected SecondaryDatabase secondaryDatabase;
34 protected SecondaryConfig secondaryConfig = new SecondaryConfig();
35
36
37 public void testOrder() throws Exception {
38 doInsert("B", "b123");
39 doInsert("D", "d123");
40 doInsert("A", "a123");
41 doInsert("C", "c123");
42
43
44 DatabaseEntry keyEntry = new DatabaseEntry();
45 DatabaseEntry valueEntry = new DatabaseEntry();
46 DatabaseEntry counterEntry = new DatabaseEntry();
47 assertStatusOK(cursor.getFirst(counterEntry, keyEntry, valueEntry, LockMode.DEFAULT));
48 printKeyValue(counterEntry, keyEntry, valueEntry);
49 while (true) {
50 OperationStatus status = cursor.getNext(counterEntry, keyEntry, valueEntry, LockMode.DEFAULT);
51 System.out.println("Status: " + status);
52
53 if (status != OperationStatus.SUCCESS) {
54 break;
55 }
56 printKeyValue(counterEntry, keyEntry, valueEntry);
57 }
58 }
59
60 private void printKeyValue(DatabaseEntry counterEntry, DatabaseEntry keyEntry, DatabaseEntry valueEntry) {
61 System.out.println("Got row with counter: " + BDbHelper.longFromBytes(counterEntry.getData())
62 + " key: " + asString(keyEntry) + " and value: " + asString(valueEntry));
63 }
64
65
66 protected void doInsert(String key, String value) throws DatabaseException {
67 assertStatusOK(database.put(transaction, asEntry(key), asEntry(value)));
68 }
69
70 protected void assertStatusOK(OperationStatus status) {
71 assertEquals("Status should be OK", OperationStatus.SUCCESS, status);
72 }
73
74 protected DatabaseEntry asEntry(String key) {
75 return new DatabaseEntry(key.getBytes());
76 }
77
78 protected String asString(DatabaseEntry entry) {
79 return new String(entry.getData(), entry.getOffset(), entry.getSize());
80 }
81
82 protected void setUp() throws Exception {
83 env = BDbQueueMessageContainerTest.createEnvironment();
84
85 database = env.openDatabase(null, "orderTest", config);
86
87 secondaryConfig.setKeyCreator(new StubSecondaryKeyGenerator());
88 secondaryConfig.setAllowCreate(true);
89 secondaryConfig.setAllowPopulate(true);
90 secondaryConfig.setTransactional(true);
91 secondaryDatabase = env.openSecondaryDatabase(null, "orderIndexTest", database, secondaryConfig);
92
93 transaction = env.beginTransaction(null, null);
94 cursor = secondaryDatabase.openSecondaryCursor(transaction, null);
95 }
96
97 protected void tearDown() throws Exception {
98
99
100
101
102
103
104 }
105
106 }