1 8 9 package com.sleepycat.je.utilint; 10 11 import java.io.File ; 12 import java.io.IOException ; 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 envHome; 28 29 private volatile boolean exceptionSeenCalled = false; 30 31 private DaemonThread dt = null; 32 33 34 public ExceptionListenerTest() { 35 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 36 } 37 38 public void setUp() 39 throws IOException { 40 41 TestUtils.removeLogFiles("Setup", envHome, false); 42 } 43 44 public void tearDown() 45 throws Exception { 46 47 TestUtils.removeLogFiles("TearDown", envHome, false); 48 } 49 50 public void testExceptionListener() 51 throws Exception { 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 ()); 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 name, EnvironmentImpl env) { 71 super(waitTime, name, env); 72 } 73 74 protected void onWakeup() 75 throws DatabaseException { 76 77 throw new RuntimeException ("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 |