Migrade JUnitMatchers references
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / osgi / TestingExceptionHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.persist.impl.osgi;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.fail;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 final class TestingExceptionHandler implements Thread.UncaughtExceptionHandler {
21
22     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterTest.class);
23
24     private Throwable t;
25
26     @Override
27     public void uncaughtException(Thread t, Throwable e) {
28         logger.debug("Uncaught exception in thread {}", t, e);
29         this.t = e;
30     }
31
32     public void assertException(Class<? extends Exception> exType, String exMessageToContain) {
33         assertException(exMessageToContain, exType, exMessageToContain);
34     }
35
36     public void assertException(String failMessageSuffix, Class<? extends Exception> exType, String exMessageToContain) {
37         if(t == null) {
38             fail("Should fail to " + failMessageSuffix);
39         }
40         else {
41             assertException(t, exType, exMessageToContain);
42         }
43     }
44
45     public void assertNoException() {
46         assertNull("No exception expected but was " + t, t);
47     }
48
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(), containsString(exMessageToContain));
53         }
54     }
55
56     public void assertException(String failMessageSuffix, Class<? extends Exception> exType,
57             String exMessageToContain, Class<? extends Exception> nestedExType, String nestedExMessageToContain,
58             int nestedExDepth) {
59         assertException(failMessageSuffix, exType, exMessageToContain);
60         assertNotNull("Expected nested exception in " + t, t.getCause());
61         assertException(getNestedException(t, nestedExDepth), nestedExType, nestedExMessageToContain);
62     }
63
64     private Throwable getNestedException(Throwable t, int nestedExDepth) {
65
66         int depth = 0;
67         while(t.getCause() != null) {
68             t = t.getCause();
69             depth++;
70             if(nestedExDepth == depth)
71                 return t;
72         }
73         throw new IllegalArgumentException("Unable to get nested exception from " + t + " from depth " + nestedExDepth);
74     }
75 }