|
17 | 17 |
|
18 | 18 |
|
19 | 19 |
|
20 | | -import org.culturegraph.mf.framework.DefaultObjectReceiver; |
21 | | -import org.junit.Assert; |
| 20 | +import static org.hamcrest.CoreMatchers.containsString; |
| 21 | +import static org.hamcrest.CoreMatchers.is; |
| 22 | +import static org.junit.Assert.assertThat; |
| 23 | +import static org.mockito.Matchers.anyString; |
| 24 | +import static org.mockito.Mockito.doThrow; |
| 25 | +import static org.mockito.Mockito.times; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | + |
| 28 | +import org.apache.log4j.Appender; |
| 29 | +import org.apache.log4j.Level; |
| 30 | +import org.apache.log4j.Logger; |
| 31 | +import org.apache.log4j.spi.LoggingEvent; |
| 32 | +import org.culturegraph.mf.framework.ObjectReceiver; |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.Before; |
22 | 35 | import org.junit.Test; |
| 36 | +import org.mockito.ArgumentCaptor; |
| 37 | +import org.mockito.Captor; |
| 38 | +import org.mockito.Mock; |
| 39 | +import org.mockito.MockitoAnnotations; |
23 | 40 |
|
24 | 41 |
|
25 | 42 | /** |
26 | 43 | * Tests {@link ObjectExceptionCatcher}. |
27 | | - * |
| 44 | + * |
28 | 45 | * @author Christoph Böhme |
29 | 46 | */ |
30 | 47 | public final class ObjectExceptionCatcherTest { |
31 | 48 |
|
32 | | - private static final String OBJECT = "Test Object"; |
33 | | - |
| 49 | + private static final String OBJECT_STRING = "TEST OBJECT REPRESENTATION"; |
| 50 | + private static final String EXCEPTION_MESSAGE = "TEST EXCEPTION MESSSAGE"; |
| 51 | + |
34 | 52 | /** |
35 | 53 | * A special exception to make sure the test |
36 | | - * is not passed accidentally on a different |
| 54 | + * is not passed accidentally on a different |
37 | 55 | * exception. |
38 | 56 | */ |
39 | | - protected static final class ModuleException |
| 57 | + private static final class TestException |
40 | 58 | extends RuntimeException { |
41 | 59 |
|
42 | | - private static final long serialVersionUID = 1L; |
43 | | - } |
44 | | - |
45 | | - /** |
46 | | - * A module whose {@code process()} method always throws |
47 | | - * an exception. |
48 | | - * |
49 | | - * @param <T> object type |
50 | | - */ |
51 | | - protected static final class FailingModule<T> |
52 | | - extends DefaultObjectReceiver<T> { |
53 | | - |
54 | | - @Override |
55 | | - public void process(final T obj) { |
56 | | - throw new ModuleException(); |
| 60 | + private static final long serialVersionUID = 1L; |
| 61 | + |
| 62 | + public TestException(final String msg) { |
| 63 | + super(msg); |
57 | 64 | } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + private ObjectExceptionCatcher<String> systemUnderTest; |
| 69 | + |
| 70 | + @Mock |
| 71 | + private ObjectReceiver<String> exceptionThrowingModule; |
| 72 | + |
| 73 | + @Mock |
| 74 | + private Appender logAppender; |
| 75 | + @Captor |
| 76 | + private ArgumentCaptor<LoggingEvent> loggingEventCaptor; |
| 77 | + |
| 78 | + @Before |
| 79 | + public void setup() { |
| 80 | + MockitoAnnotations.initMocks(this); |
| 81 | + doThrow(new TestException(EXCEPTION_MESSAGE)) |
| 82 | + .when(exceptionThrowingModule).process(anyString()); |
| 83 | + |
| 84 | + final Logger logger = Logger.getLogger(ObjectExceptionCatcher.class); |
| 85 | + logger.addAppender(logAppender); |
| 86 | + |
| 87 | + systemUnderTest = new ObjectExceptionCatcher<String>(); |
| 88 | + systemUnderTest.setReceiver(exceptionThrowingModule); |
58 | 89 | } |
59 | | - |
60 | | - @Test(expected=ModuleException.class) |
61 | | - public void testSetup() { |
62 | | - final FailingModule<String> failingModule = new FailingModule<String>(); |
63 | | - |
64 | | - failingModule.process(OBJECT); |
65 | | - failingModule.closeStream(); |
| 90 | + |
| 91 | + @After |
| 92 | + public void cleanup() { |
| 93 | + final Logger logger = Logger.getLogger(ObjectExceptionCatcher.class); |
| 94 | + logger.removeAppender(logAppender); |
66 | 95 | } |
67 | | - |
| 96 | + |
68 | 97 | @Test |
69 | | - public void testCatcher() { |
70 | | - final ObjectExceptionCatcher<String> catcher = new ObjectExceptionCatcher<String>(); |
71 | | - final FailingModule<String> failingModule = new FailingModule<String>(); |
72 | | - |
73 | | - catcher.setReceiver(failingModule); |
74 | | - |
75 | | - try { |
76 | | - catcher.process(OBJECT); |
77 | | - catcher.closeStream(); |
78 | | - } catch (final ModuleException e) { |
79 | | - Assert.fail(e.toString()); |
80 | | - } |
| 98 | + public void shouldCatchAndLogException() { |
| 99 | + systemUnderTest.process(OBJECT_STRING); |
| 100 | + |
| 101 | + verify(logAppender).doAppend(loggingEventCaptor.capture()); |
| 102 | + final LoggingEvent loggingEvent = loggingEventCaptor.getValue(); |
| 103 | + assertThat(loggingEvent.getLevel(), is(Level.ERROR)); |
| 104 | + assertThat(loggingEvent.getRenderedMessage(), containsString(EXCEPTION_MESSAGE)); |
| 105 | + assertThat(loggingEvent.getRenderedMessage(), containsString(OBJECT_STRING)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void shouldLogStackTraceIfConfigured() { |
| 110 | + systemUnderTest.setLogStackTrace(true); |
| 111 | + |
| 112 | + systemUnderTest.process(OBJECT_STRING); |
| 113 | + |
| 114 | + verify(logAppender, times(2)).doAppend(loggingEventCaptor.capture()); |
| 115 | + final LoggingEvent loggingEvent = loggingEventCaptor.getValue(); |
| 116 | + assertThat(loggingEvent.getLevel(), is(Level.ERROR)); |
| 117 | + assertThat(loggingEvent.getRenderedMessage(), containsString("Stack Trace")); |
81 | 118 | } |
82 | 119 |
|
83 | 120 | } |
0 commit comments