-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathExceptionsTests.java
More file actions
120 lines (92 loc) · 4.15 KB
/
ExceptionsTests.java
File metadata and controls
120 lines (92 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package dmg.util;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import java.io.IOException;
import java.net.SocketException;
import java.util.List;
import org.ietf.jgss.GSSException;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;
/**
* Tests for utility methods in Exceptions.
*/
public class ExceptionsTests {
private List<ILoggingEvent> _log;
@Before
public void setup() {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
context.reset();
ListAppender<ILoggingEvent> appender = new ListAppender<>();
appender.setContext(context);
appender.setName("appender");
appender.start();
_log = appender.list;
Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
logger.addAppender(appender);
logger.setLevel(Level.WARN);
}
@Test
public void shouldWrapWithCauseInSimpleCase() {
IOException cause = new IOException("Something went wrong");
IOException wrapped = Exceptions.wrap("Wrapped message", cause, IOException.class);
assertThat(wrapped, is(notNullValue()));
assertThat(wrapped.getMessage(), is(equalTo("Wrapped message: Something went wrong")));
assertThat(wrapped.getCause(), is(cause));
assertThat(wrapped.getClass(), is(equalTo(IOException.class)));
assertThat(_log, is(empty()));
}
@Test
public void shouldWrapWithCauseInBroaderContext() {
IOException cause = new IOException("Something went wrong");
Exception wrapped = Exceptions.wrap("Wrapped message", cause, Exception.class);
assertThat(wrapped, is(notNullValue()));
assertThat(wrapped.getMessage(), is(equalTo("Wrapped message: Something went wrong")));
assertThat(wrapped.getCause(), is(cause));
assertThat(wrapped.getClass(), is(equalTo(IOException.class)));
assertThat(_log, is(empty()));
}
@Test
public void shouldWapWithMessageIfExceptionHasNoStringThrowableConstructor() {
// Note: SocketException has no (String,Throwable) constructor, but has
// a (String) constructor.
SocketException cause = new SocketException("Something went wrong");
Exception wrapped = Exceptions.wrap("Wrapped message", cause, SocketException.class);
assertThat(wrapped, is(notNullValue()));
assertThat(wrapped.getMessage(), is(equalTo("Wrapped message: Something went wrong")));
//assertThat(wrapped.getCause(), is(nullValue()));
assertThat(wrapped.getClass(), is(equalTo(SocketException.class)));
assertThat(_log, is(empty()));
}
@Test
public void shouldUseBroaderExceptionIfCannotWrap() {
// Note: GSSException has neither a (String,Throwable) constructor, nor
// a (String) constructor.
GSSException cause = new GSSException(GSSException.BAD_MECH);
Exception wrapped = Exceptions.wrap("Wrapped message", cause, Exception.class);
assertThat(wrapped, is(notNullValue()));
assertThat(wrapped.getMessage(), is(equalTo("Wrapped message: " + cause.getMessage())));
assertThat(wrapped.getCause(), is(cause));
assertThat(wrapped.getClass(), is(equalTo(Exception.class)));
assertThat(_log, is(empty()));
}
@Test
public void shouldUseCauseIfCannotWrap() {
// Note: GSSException has neither a (String,Throwable) constructor, nor
// a (String) constructor.
GSSException cause = new GSSException(GSSException.BAD_MECH);
GSSException wrapped = Exceptions.wrap("Wrapped message", cause, GSSException.class);
assertThat(wrapped, is(cause));
assertThat(_log, is(not(empty())));
}
}