Merge "Resolve Bug:593. Persister should communicate via OSGi SR instead of TCP."
[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 org.junit.matchers.JUnitMatchers;
11
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;
17
18 final class TestingExceptionHandler implements Thread.UncaughtExceptionHandler {
19
20     private Throwable t;
21
22     @Override
23     public void uncaughtException(Thread t, Throwable e) {
24         this.t = e;
25     }
26
27     public void assertException(Class<? extends Exception> exType, String exMessageToContain) {
28         assertException(exMessageToContain, exType, exMessageToContain);
29     }
30
31     public void assertException(String failMessageSuffix, Class<? extends Exception> exType, String exMessageToContain) {
32         if(t == null) {
33             fail("Should fail to " + failMessageSuffix);
34         }
35         else {
36             assertException(t, exType, exMessageToContain);
37         }
38     }
39
40     public void assertNoException() {
41         assertNull("No exception expected but was " + t, t);
42     }
43
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));
48         }
49     }
50
51     public void assertException(String failMessageSuffix, Class<? extends Exception> exType,
52             String exMessageToContain, Class<? extends Exception> nestedExType, String nestedExMessageToContain,
53             int nestedExDepth) {
54         assertException(failMessageSuffix, exType, exMessageToContain);
55         assertNotNull("Expected nested exception in " + t, t.getCause());
56         assertException(getNestedException(t, nestedExDepth), nestedExType, nestedExMessageToContain);
57     }
58
59     private Throwable getNestedException(Throwable t, int nestedExDepth) {
60
61         int depth = 0;
62         while(t.getCause() != null) {
63             t = t.getCause();
64             depth++;
65             if(nestedExDepth == depth)
66                 return t;
67         }
68         throw new IllegalArgumentException("Unable to get nested exception from " + t + " from depth " + nestedExDepth);
69     }
70 }