File tree Expand file tree Collapse file tree
main/java/com/retailsvc/http/internal
test/java/com/retailsvc/http/internal Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .retailsvc .http .internal ;
2+
3+ import com .retailsvc .http .ExceptionHandler ;
4+ import com .sun .net .httpserver .Filter ;
5+ import com .sun .net .httpserver .HttpExchange ;
6+ import java .io .IOException ;
7+
8+ public final class ExceptionFilter extends Filter {
9+ private final ExceptionHandler handler ;
10+
11+ public ExceptionFilter (ExceptionHandler handler ) {
12+ this .handler = handler ;
13+ }
14+
15+ @ Override
16+ public void doFilter (HttpExchange exchange , Chain chain ) throws IOException {
17+ try {
18+ chain .doFilter (exchange );
19+ } catch (RuntimeException | IOException t ) {
20+ handler .handle (exchange , t );
21+ }
22+ }
23+
24+ @ Override
25+ public String description () {
26+ return "Exception filter" ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package com .retailsvc .http .internal ;
2+
3+ import com .retailsvc .http .ExceptionHandler ;
4+ import com .retailsvc .http .NotFoundException ;
5+ import com .sun .net .httpserver .Filter ;
6+ import com .sun .net .httpserver .HttpExchange ;
7+ import org .junit .jupiter .api .Test ;
8+ import org .mockito .Mockito ;
9+
10+ class ExceptionFilterTest {
11+ @ Test
12+ void delegatesToExceptionHandler () throws Exception {
13+ HttpExchange ex = Mockito .mock (HttpExchange .class );
14+ ExceptionHandler handler = Mockito .mock (ExceptionHandler .class );
15+ Filter f = new ExceptionFilter (handler );
16+ Filter .Chain chain = Mockito .mock (Filter .Chain .class );
17+ Mockito .doThrow (new NotFoundException ("x" )).when (chain ).doFilter (ex );
18+ f .doFilter (ex , chain );
19+ Mockito .verify (handler ).handle (Mockito .eq (ex ), Mockito .any (NotFoundException .class ));
20+ }
21+
22+ @ Test
23+ void passThroughOnSuccess () throws Exception {
24+ HttpExchange ex = Mockito .mock (HttpExchange .class );
25+ ExceptionHandler handler = Mockito .mock (ExceptionHandler .class );
26+ Filter f = new ExceptionFilter (handler );
27+ Filter .Chain chain = Mockito .mock (Filter .Chain .class );
28+ f .doFilter (ex , chain );
29+ Mockito .verify (chain ).doFilter (ex );
30+ Mockito .verifyNoInteractions (handler );
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments