2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.netconf.persist.impl.osgi;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.fail;
16 import org.junit.matchers.JUnitMatchers;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 final class TestingExceptionHandler implements Thread.UncaughtExceptionHandler {
22 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterTest.class);
27 public void uncaughtException(Thread t, Throwable e) {
28 logger.debug("Uncaught exception in thread {}", t, e);
32 public void assertException(Class<? extends Exception> exType, String exMessageToContain) {
33 assertException(exMessageToContain, exType, exMessageToContain);
36 public void assertException(String failMessageSuffix, Class<? extends Exception> exType, String exMessageToContain) {
38 fail("Should fail to " + failMessageSuffix);
41 assertException(t, exType, exMessageToContain);
45 public void assertNoException() {
46 assertNull("No exception expected but was " + t, t);
49 private void assertException(Throwable t, Class<? extends Exception> exType, String exMessageToContain) {
50 assertEquals("Expected exception of type " + exType + " but was " + t, exType, t.getClass());
51 if(exMessageToContain!=null) {
52 assertThat(t.getMessage(), JUnitMatchers.containsString(exMessageToContain));
56 public void assertException(String failMessageSuffix, Class<? extends Exception> exType,
57 String exMessageToContain, Class<? extends Exception> nestedExType, String nestedExMessageToContain,
59 assertException(failMessageSuffix, exType, exMessageToContain);
60 assertNotNull("Expected nested exception in " + t, t.getCause());
61 assertException(getNestedException(t, nestedExDepth), nestedExType, nestedExMessageToContain);
64 private Throwable getNestedException(Throwable t, int nestedExDepth) {
67 while(t.getCause() != null) {
70 if(nestedExDepth == depth)
73 throw new IllegalArgumentException("Unable to get nested exception from " + t + " from depth " + nestedExDepth);