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 package com.jcabi.aspects;
31
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import org.hamcrest.MatcherAssert;
35 import org.hamcrest.Matchers;
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42
43 final class UnitedThrowTest {
44
45 @Test
46 void rethrowsDeclaredException() {
47 Assertions.assertThrows(
48 IOException.class,
49 () -> new UnitedThrowTest.Thrower().file()
50 );
51 }
52
53 @Test
54 void throwsDeclaredException() {
55 Assertions.assertThrows(
56 IOException.class,
57 () -> new UnitedThrowTest.Thrower().save()
58 );
59 }
60
61 @Test
62 void throwsConfiguredException() {
63 Assertions.assertThrows(
64 IOException.class,
65 () -> new UnitedThrowTest.Thrower().multiple()
66 );
67 }
68
69 @Test
70 void throwsIllegalStateException() {
71 Assertions.assertThrows(
72 IllegalStateException.class,
73 () -> new UnitedThrowTest.Thrower().def()
74 );
75 }
76
77 @Test
78 void throwsOriginalExceptionEncapsulatedInsideDeclared() {
79 try {
80 new UnitedThrowTest.Thrower().encapsulate();
81 } catch (final IOException ex) {
82 MatcherAssert.assertThat(
83 ex.getCause(),
84 Matchers.instanceOf(IllegalStateException.class)
85 );
86 }
87 }
88
89
90
91
92
93 private static final class Thrower {
94
95
96
97 @UnitedThrow
98 public void save() throws IOException {
99 throw new IllegalStateException();
100 }
101
102
103
104
105
106 @UnitedThrow
107 public void file() throws IOException {
108 throw new FileNotFoundException();
109 }
110
111
112
113
114
115 @UnitedThrow(IOException.class)
116 public void multiple() throws InterruptedException, IOException {
117 throw new IllegalStateException();
118 }
119
120
121
122
123 @UnitedThrow
124 public void def() {
125 throw new IllegalArgumentException();
126 }
127
128
129
130
131 @UnitedThrow(IOException.class)
132 public void encapsulate() throws IOException {
133 throw new IllegalStateException();
134 }
135 }
136 }