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(String failMessageSuffix, Class<? extends Exception> exType, String exMessageToContain) {
29 fail("Should fail to " + failMessageSuffix);
32 assertException(t, exType, exMessageToContain);
36 public void assertNoException() {
37 assertNull("No exception expected but was " + t, t);
40 private void assertException(Throwable t, Class<? extends Exception> exType, String exMessageToContain) {
41 assertEquals("Expected exception of type " + exType + " but was " + t, exType, t.getClass());
42 if(exMessageToContain!=null) {
43 assertThat(t.getMessage(), JUnitMatchers.containsString(exMessageToContain));
47 public void assertException(String failMessageSuffix, Class<? extends Exception> exType,
48 String exMessageToContain, Class<? extends Exception> nestedExType, String nestedExMessageToContain,
50 assertException(failMessageSuffix, exType, exMessageToContain);
51 assertNotNull("Expected nested exception in " + t, t.getCause());
52 assertException(getNestedException(t, nestedExDepth), nestedExType, nestedExMessageToContain);
55 private Throwable getNestedException(Throwable t, int nestedExDepth) {
58 while(t.getCause() != null) {
61 if(nestedExDepth == depth)
64 throw new IllegalArgumentException("Unable to get nested exception from " + t + " from depth " + nestedExDepth);