1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 */
7
8/*
9 * Alexg 23-4-06
10 * Based on scr016 TestCallback test application.
11 *
12 * Simple tests for DbErrorHandler, DbFeedbackHandler, DbPanicHandler
13 */
14
15package com.sleepycat.db.test;
16
17import org.junit.Before;
18import org.junit.After;
19import org.junit.AfterClass;
20import org.junit.BeforeClass;
21import org.junit.Test;
22import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.fail;
24
25import java.io.File;
26import java.io.FileNotFoundException;
27import com.sleepycat.db.*;
28
29import com.sleepycat.db.DatabaseException;
30
31import com.sleepycat.db.test.TestUtils;
32
33public class CallbackTest
34    implements FeedbackHandler, PanicHandler, ErrorHandler {
35
36    public static final String CALLBACKTEST_DBNAME  =  "callbacktest.db";
37
38    int callback_count = 0;
39    boolean callback_throws = false;
40
41    @BeforeClass public static void ClassInit() {
42	    TestUtils.loadConfig(null);
43        TestUtils.check_file_removed(TestUtils.getDBFileName(CALLBACKTEST_DBNAME), true, true);
44    }
45
46    @AfterClass public static void ClassShutdown() {
47        TestUtils.check_file_removed(TestUtils.getDBFileName(CALLBACKTEST_DBNAME), true, true);
48    }
49
50    @Before public void PerTestInit()
51        throws Exception {
52	    TestUtils.removeall(true, true, TestUtils.BASETEST_DBDIR, TestUtils.getDBFileName(CALLBACKTEST_DBNAME));
53    }
54
55    @After public void PerTestShutdown()
56        throws Exception {
57	    TestUtils.removeall(true, true, TestUtils.BASETEST_DBDIR, TestUtils.getDBFileName(CALLBACKTEST_DBNAME));
58    }
59
60    /*
61     * Test creating a new database.
62     */
63    @Test public void test1()
64        throws DatabaseException, FileNotFoundException
65    {
66        TestUtils.debug_level = 2;
67        EnvironmentConfig envc = new EnvironmentConfig();
68        envc.setAllowCreate(true);
69        envc.setInitializeCache(true);
70        envc.setTransactional(true);
71        envc.setInitializeLocking(true);
72        envc.setCacheSize(64 * 1024);
73        envc.setFeedbackHandler(this);
74        envc.setPanicHandler(this);
75        envc.setErrorHandler(this);
76    	Environment dbEnv = new Environment(TestUtils.BASETEST_DBFILE, envc);
77
78        // set up a transaction DB.
79        DatabaseConfig dbConfig = new DatabaseConfig();
80        dbConfig.setType(DatabaseType.BTREE);
81        dbConfig.setAllowCreate(true);
82        Database db = dbEnv.openDatabase(null, CALLBACKTEST_DBNAME, null, dbConfig);
83
84        DatabaseEntry key1 = new DatabaseEntry("key".getBytes());
85        DatabaseEntry data1 = new DatabaseEntry("data".getBytes());
86        // populate was doing some more than this (validating that not retrieving things that were not added)
87        db.putNoOverwrite(null, key1, data1);
88//        TestUtil.populate(db);
89
90        CheckpointConfig cpcfg = new CheckpointConfig();
91        cpcfg.setForce(true);
92        dbEnv.checkpoint(cpcfg);
93
94        try {
95            dbConfig.setBtreeComparator(null);
96        }
97        catch (IllegalArgumentException dbe)
98        {
99            TestUtils.DEBUGOUT(1, "got expected exception: " + dbe);
100            // ignore
101        }
102
103        /*
104        // Pretend we crashed, and reopen the environment
105        db = null;
106        dbenv = null;
107
108        dbenv = new DbEnv(0);
109        dbenv.setFeedbackHandler(this);
110        dbenv.open(".", Db.DB_INIT_LOCK | Db.DB_INIT_MPOOL | Db.DB_INIT_LOG
111                   | Db.DB_INIT_TXN | Db.DB_RECOVER, 0);
112        */
113
114        dbEnv.panic(true);
115        try {
116            DatabaseEntry key = new DatabaseEntry("foo".getBytes());
117            DatabaseEntry data = new DatabaseEntry();
118            db.get(null, key, data, null);
119        }
120        catch (DatabaseException dbe2)
121        {
122            TestUtils.DEBUGOUT(2, "got expected exception: " + dbe2);
123            // ignore
124        }
125
126    }
127
128    /*
129     * FeedbackHandler interface implementation.
130     */
131    public void recoveryFeedback(Environment dbenv, int percent)
132    {
133        TestUtils.DEBUGOUT(2, "recoveryFeedback callback invoked. percent: " + percent);
134    }
135    public void upgradeFeedback(Database db, int percent)
136    {
137        TestUtils.DEBUGOUT(2, "upgradeFeedback callback invoked. percent: " + percent);
138    }
139    public void verifyFeedback(Database db, int percent)
140    {
141        TestUtils.DEBUGOUT(2, "verifyFeedback callback invoked. percent: " + percent);
142    }
143
144    /*
145     * Panic handler interface implementation.
146     */
147    public void panic(Environment dbenv, DatabaseException e)
148    {
149        TestUtils.DEBUGOUT(2, "panic callback invoked. exception: " + e);
150    }
151
152     /*
153      * Error handler interface implementation.
154      */
155    public void error(Environment dbenv, String errpfx, String msg)
156    {
157        TestUtils.DEBUGOUT(2, "error callback invoked, errpfx: \"" + errpfx + "\", msg: \"" + msg + "\"");
158    }
159}
160