KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > utilint > ExceptionListenerTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: ExceptionListenerTest.java,v 1.5 2006/12/04 19:23:29 mark Exp $
7  */

8
9 package com.sleepycat.je.utilint;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.je.DatabaseException;
17 import com.sleepycat.je.DbInternal;
18 import com.sleepycat.je.Environment;
19 import com.sleepycat.je.EnvironmentConfig;
20 import com.sleepycat.je.ExceptionEvent;
21 import com.sleepycat.je.ExceptionListener;
22 import com.sleepycat.je.dbi.EnvironmentImpl;
23 import com.sleepycat.je.util.TestUtils;
24
25 public class ExceptionListenerTest extends TestCase {
26
27     private File JavaDoc envHome;
28
29     private volatile boolean exceptionSeenCalled = false;
30
31     private DaemonThread dt = null;
32
33
34     public ExceptionListenerTest() {
35         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
36     }
37
38     public void setUp()
39         throws IOException JavaDoc {
40
41         TestUtils.removeLogFiles("Setup", envHome, false);
42     }
43     
44     public void tearDown()
45         throws Exception JavaDoc {
46
47         TestUtils.removeLogFiles("TearDown", envHome, false);
48     }
49
50     public void testExceptionListener()
51     throws Exception JavaDoc {
52
53     EnvironmentConfig envConfig = new EnvironmentConfig();
54     envConfig.setExceptionListener(new MyExceptionListener());
55     envConfig.setAllowCreate(true);
56     Environment env = new Environment(envHome, envConfig);
57     EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
58     dt = new MyDaemonThread(0, Environment.CLEANER_NAME, envImpl);
59     dt.stifleExceptionChatter = true;
60     dt.addToQueue(new Object JavaDoc());
61     dt.runOrPause(true);
62     while (!dt.isShutdownRequested()) {
63         Thread.yield();
64     }
65     assertTrue("ExceptionListener apparently not called",
66            exceptionSeenCalled);
67     }
68
69     private class MyDaemonThread extends DaemonThread {
70     MyDaemonThread(long waitTime, String JavaDoc name, EnvironmentImpl env) {
71         super(waitTime, name, env);
72     }
73
74     protected void onWakeup()
75         throws DatabaseException {
76
77         throw new RuntimeException JavaDoc("test exception listener");
78     }
79     }
80
81     private class MyExceptionListener implements ExceptionListener {
82     public void exceptionThrown(ExceptionEvent event) {
83             assertEquals("daemonName should be CLEANER_NAME",
84              Environment.CLEANER_NAME,
85                          event.getThreadName());
86         dt.requestShutdown();
87         exceptionSeenCalled = true;
88     }
89     }
90 }
91
Popular Tags