1 /*
2 * Copyright (c) 2012-2024, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
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 * Tests for {@link UnitedThrow}.
41 * @since 0.0.0
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 * Class for testing UnitedThrow.
91 * @since 0.0.0
92 */
93 private static final class Thrower {
94 /**
95 * Test method.
96 */
97 @UnitedThrow
98 public void save() throws IOException {
99 throw new IllegalStateException();
100 }
101
102 /**
103 * Test method.
104 * @throws IOException In case of exception.
105 */
106 @UnitedThrow
107 public void file() throws IOException {
108 throw new FileNotFoundException();
109 }
110
111 /**
112 * Test method.
113 * @checkstyle ThrowsCountCheck (3 lines)
114 */
115 @UnitedThrow(IOException.class)
116 public void multiple() throws InterruptedException, IOException {
117 throw new IllegalStateException();
118 }
119
120 /**
121 * Test method.
122 */
123 @UnitedThrow
124 public void def() {
125 throw new IllegalArgumentException();
126 }
127
128 /**
129 * Test method.
130 */
131 @UnitedThrow(IOException.class)
132 public void encapsulate() throws IOException {
133 throw new IllegalStateException();
134 }
135 }
136 }