1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 */ 7 8 9package com.sleepycat.db.test; 10 11import org.junit.After; 12import org.junit.AfterClass; 13import org.junit.Before; 14import org.junit.BeforeClass; 15import org.junit.Ignore; 16import org.junit.Test; 17import static org.junit.Assert.assertEquals; 18import static org.junit.Assert.fail; 19 20import com.sleepycat.db.*; 21 22import java.io.File; 23import java.io.FileNotFoundException; 24import java.io.IOException; 25 26import com.sleepycat.db.test.TestUtils; 27public class HashCompareTest { 28 public static final String HASHCOMPARETEST_DBNAME = "hashcomparetest.db"; 29 @BeforeClass public static void ClassInit() { 30 TestUtils.loadConfig(null); 31 TestUtils.check_file_removed(TestUtils.getDBFileName(HASHCOMPARETEST_DBNAME), true, true); 32 TestUtils.removeall(true, true, TestUtils.BASETEST_DBDIR, TestUtils.getDBFileName(HASHCOMPARETEST_DBNAME)); 33 } 34 35 @AfterClass public static void ClassShutdown() { 36 TestUtils.check_file_removed(TestUtils.getDBFileName(HASHCOMPARETEST_DBNAME), true, true); 37 TestUtils.removeall(true, true, TestUtils.BASETEST_DBDIR, TestUtils.getDBFileName(HASHCOMPARETEST_DBNAME)); 38 } 39 40 @Before public void PerTestInit() 41 throws Exception { 42 TestUtils.check_file_removed(TestUtils.getDBFileName(HASHCOMPARETEST_DBNAME), true, true); 43 java.io.File dbfile = new File(HASHCOMPARETEST_DBNAME); 44 dbfile.delete(); 45 } 46 47 @After public void PerTestShutdown() 48 throws Exception { 49 } 50 /* 51 * Test case implementations. 52 * To disable a test mark it with @Ignore 53 * To set a timeout(ms) notate like: @Test(timeout=1000) 54 * To indicate an expected exception notate like: (expected=Exception) 55 */ 56 57 @Test public void test1() 58 throws DatabaseException, FileNotFoundException 59 { 60 runTest(DatabaseType.HASH); 61 } 62 63 @Test public void test2() 64 throws DatabaseException, FileNotFoundException 65 { 66 runTest(DatabaseType.BTREE); 67 } 68 69 public void runTest(DatabaseType type) 70 throws DatabaseException, FileNotFoundException 71 { 72 int i; 73 DatabaseConfig conf = new DatabaseConfig(); 74 conf.setErrorStream(TestUtils.getErrorStream()); 75 conf.setErrorPrefix("HashCompareTest"); 76 conf.setType(type); 77 if (type == DatabaseType.HASH) { 78 conf.setHashComparator(new HashComparator()); 79 } else 80 conf.setBtreeComparator(new BtreeComparator()); 81 conf.setAllowCreate(true); 82 83 Database db = new Database(HASHCOMPARETEST_DBNAME, null, conf); 84 85 DatabaseEntry key = new DatabaseEntry(); 86 DatabaseEntry data = new DatabaseEntry("world".getBytes()); 87 for (i = 0; i < 100; i++) { 88 key.setData((new String("key"+i)).getBytes()); 89 db.put(null, key, data); 90 } 91 i = 0; 92 Cursor dbc; 93 dbc = db.openCursor(null, CursorConfig.DEFAULT); 94 while (dbc.getNext(key, data, LockMode.DEFAULT) == 95 OperationStatus.SUCCESS) { 96 ++i; 97 } 98// System.out.println("retrieved " + i + " entries"); 99 dbc.close(); 100 db.close(); 101 102 } 103} 104 105class HashComparator implements java.util.Comparator 106{ 107 public int compare(Object o1, Object o2) { 108// System.out.println("Comparing: " + o1 + ":"+o2); 109 String s1, s2; 110 s1 = new String((byte[])o1); 111 s2 = new String((byte[])o2); 112 return s1.compareToIgnoreCase(s2); 113 } 114} 115 116class BtreeComparator implements java.util.Comparator 117{ 118 public int compare(Object o1, Object o2) { 119 //System.out.println("Comparing: " + o1 + ":"+o2); 120 String s1, s2; 121 s1 = new String((byte[])o1); 122 s2 = new String((byte[])o2); 123 return s1.compareToIgnoreCase(s2); 124 } 125} 126