|
| 1 | +/* |
| 2 | + * SonarQube Java |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.java.test; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 25 | + |
| 26 | +class TestResourceScopeTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + void nested_scopes_close_only_their_own_resources_after_assertions() throws Exception { |
| 30 | + List<String> calls = new ArrayList<>(); |
| 31 | + try (TestResourceScope.Scope outer = TestResourceScope.open()) { |
| 32 | + TestResourceScope.retain(() -> calls.add("outer")); |
| 33 | + try (TestResourceScope.Scope inner = TestResourceScope.open()) { |
| 34 | + TestResourceScope.retain(() -> calls.add("inner")); |
| 35 | + assertThat(calls).isEmpty(); |
| 36 | + } |
| 37 | + assertThat(calls).containsExactly("inner"); |
| 38 | + } |
| 39 | + assertThat(calls).containsExactly("inner", "outer"); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void scope_closes_every_resource_in_reverse_order_even_when_cleanup_fails() throws Exception { |
| 44 | + TestResourceScope.Scope scope = TestResourceScope.open(); |
| 45 | + List<String> calls = new ArrayList<>(); |
| 46 | + IllegalStateException first = new IllegalStateException("first"); |
| 47 | + Exception second = new Exception("second"); |
| 48 | + TestResourceScope.retain(() -> calls.add("last")); |
| 49 | + TestResourceScope.retain(() -> { calls.add("second"); throw second; }); |
| 50 | + TestResourceScope.retain(() -> { calls.add("first"); throw first; }); |
| 51 | + assertThatThrownBy(scope::close).isSameAs(first).hasSuppressedException(second); |
| 52 | + scope.close(); |
| 53 | + assertThat(calls).containsExactly("first", "second", "last"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void scope_cleanup_attempts_every_resource_and_suppresses_errors() throws Exception { |
| 58 | + TestResourceScope.Scope scope = TestResourceScope.open(); |
| 59 | + List<String> calls = new ArrayList<>(); |
| 60 | + AssertionError failure = new AssertionError("cleanup"); |
| 61 | + TestResourceScope.retain(() -> calls.add("last")); |
| 62 | + TestResourceScope.retain(() -> { throw failure; }); |
| 63 | + Exception first = new Exception("first"); |
| 64 | + TestResourceScope.retain(() -> { throw first; }); |
| 65 | + |
| 66 | + assertThatThrownBy(scope::close).isSameAs(first).hasSuppressedException(failure); |
| 67 | + scope.close(); |
| 68 | + assertThat(calls).containsExactly("last"); |
| 69 | + try (TestResourceScope.Scope next = TestResourceScope.open()) { |
| 70 | + TestResourceScope.retain(() -> calls.add("next")); |
| 71 | + } |
| 72 | + assertThat(calls).containsExactly("last", "next"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void scope_cleanup_rethrows_first_error_after_attempting_every_resource() throws Exception { |
| 77 | + TestResourceScope.Scope scope = TestResourceScope.open(); |
| 78 | + List<String> calls = new ArrayList<>(); |
| 79 | + AssertionError first = new AssertionError("first"); |
| 80 | + Exception second = new Exception("second"); |
| 81 | + TestResourceScope.retain(() -> calls.add("last")); |
| 82 | + TestResourceScope.retain(() -> { throw second; }); |
| 83 | + TestResourceScope.retain(() -> { throw first; }); |
| 84 | + TestResourceScope.retain(() -> { throw first; }); |
| 85 | + |
| 86 | + assertThatThrownBy(scope::close).isSameAs(first).hasSuppressedException(second); |
| 87 | + scope.close(); |
| 88 | + assertThat(calls).containsExactly("last"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void missing_scope_fails_loudly_and_closes_the_unretained_fixture() { |
| 93 | + List<String> calls = new ArrayList<>(); |
| 94 | + assertThatThrownBy(() -> TestResourceScope.retain(() -> calls.add("closed"))) |
| 95 | + .isInstanceOf(IllegalStateException.class).hasMessageContaining("@TestResourceScope.Scoped"); |
| 96 | + assertThat(calls).containsExactly("closed"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void missing_scope_preserves_cleanup_exceptions_as_suppressed() { |
| 101 | + Exception failure = new Exception("cleanup failed"); |
| 102 | + assertThatThrownBy(() -> TestResourceScope.retain(() -> { throw failure; })) |
| 103 | + .isInstanceOf(IllegalStateException.class) |
| 104 | + .hasMessageContaining("@TestResourceScope.Scoped") |
| 105 | + .hasSuppressedException(failure); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void missing_scope_propagates_cleanup_errors() { |
| 110 | + AssertionError failure = new AssertionError("cleanup failed"); |
| 111 | + assertThatThrownBy(() -> TestResourceScope.retain(() -> { throw failure; })) |
| 112 | + .isSameAs(failure); |
| 113 | + } |
| 114 | +} |
0 commit comments