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 org.junit.matchers.JUnitMatchers;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertThat;
16 import static org.junit.Assert.fail;
18 final class TestingExceptionHandler implements Thread.UncaughtExceptionHandler {
23 public void uncaughtException(Thread t, Throwable e) {
27 public void assertException(Class<? extends Exception> exType, String exMessageToContain) {
28 assertException(exMessageToContain, exType, exMessageToContain);
31 public void assertException(String failMessageSuffix, Class<? extends Exception> exType, String exMessageToContain) {
33 fail("Should fail to " + failMessageSuffix);
36 assertException(t, exType, exMessageToContain);
40 public void assertNoException() {
41 assertNull("No exception expected but was " + t, t);
44 private void assertException(Throwable t, Class<? extends Exception> exType, String exMessageToContain) {
45 assertEquals("Expected exception of type " + exType + " but was " + t, exType, t.getClass());
46 if(exMessageToContain!=null) {
47 assertThat(t.getMessage(), JUnitMatchers.containsString(exMessageToContain));
51 public void assertException(String failMessageSuffix, Class<? extends Exception> exType,
52 String exMessageToContain, Class<? extends Exception> nestedExType, String nestedExMessageToContain,
54 assertException(failMessageSuffix, exType, exMessageToContain);
55 assertNotNull("Expected nested exception in " + t, t.getCause());
56 assertException(getNestedException(t, nestedExDepth), nestedExType, nestedExMessageToContain);
59 private Throwable getNestedException(Throwable t, int nestedExDepth) {
62 while(t.getCause() != null) {
65 if(nestedExDepth == depth)
68 throw new IllegalArgumentException("Unable to get nested exception from " + t + " from depth " + nestedExDepth);