|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 - 2023, the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.ascopes.jct.tests.unit.assertions; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import io.github.ascopes.jct.assertions.ModuleContainerGroupAssert; |
| 27 | +import io.github.ascopes.jct.containers.ModuleContainerGroup; |
| 28 | +import io.github.ascopes.jct.containers.PackageContainerGroup; |
| 29 | +import io.github.ascopes.jct.filemanagers.ModuleLocation; |
| 30 | +import java.util.Map; |
| 31 | +import javax.tools.StandardLocation; |
| 32 | +import org.junit.jupiter.api.DisplayName; |
| 33 | +import org.junit.jupiter.api.Nested; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +/** |
| 37 | + * {@link ModuleContainerGroupAssert} tests. |
| 38 | + * |
| 39 | + * @author Ashley Scopes |
| 40 | + */ |
| 41 | +@DisplayName("ModuleContainerGroupAssert tests") |
| 42 | +class ModuleContainerGroupAssertTest { |
| 43 | + |
| 44 | + @DisplayName("ModuleContainerGroupAssert#moduleExists tests") |
| 45 | + @Nested |
| 46 | + class ModuleExistsTest { |
| 47 | + |
| 48 | + @DisplayName(".moduleExists(...) fails if the module name is null") |
| 49 | + @Test |
| 50 | + void moduleExistsFailsIfModuleNameIsNull() { |
| 51 | + // Given |
| 52 | + var assertions = new ModuleContainerGroupAssert(mock()); |
| 53 | + |
| 54 | + // Then |
| 55 | + assertThatThrownBy(() -> assertions.moduleExists(null)) |
| 56 | + .isInstanceOf(NullPointerException.class); |
| 57 | + } |
| 58 | + |
| 59 | + @DisplayName(".moduleExists(...) fails if the container group is null") |
| 60 | + @Test |
| 61 | + void moduleExistsFailsIfContainerGroupIsNull() { |
| 62 | + // Given |
| 63 | + var assertions = new ModuleContainerGroupAssert(null); |
| 64 | + |
| 65 | + // Then |
| 66 | + assertThatThrownBy(() -> assertions.moduleExists("something")) |
| 67 | + .isInstanceOf(AssertionError.class); |
| 68 | + } |
| 69 | + |
| 70 | + @DisplayName(".moduleExists(...) fails with fuzzy suggestions when the module does not exist") |
| 71 | + @Test |
| 72 | + void moduleExistsFailsWithFuzzySuggestionsWhenTheModuleDoesNotExist() { |
| 73 | + // Given |
| 74 | + var moduleContainerGroup = mock(ModuleContainerGroup.class); |
| 75 | + when(moduleContainerGroup.getModule(any())).thenReturn(null); |
| 76 | + when(moduleContainerGroup.getModules()).thenReturn(Map.of( |
| 77 | + module("foo.baz"), mock(), |
| 78 | + module("foo.bork"), mock(), |
| 79 | + module("org.example"), mock() |
| 80 | + )); |
| 81 | + |
| 82 | + var assertions = new ModuleContainerGroupAssert(moduleContainerGroup); |
| 83 | + |
| 84 | + // Then |
| 85 | + assertThatThrownBy(() -> assertions.moduleExists("foo.bar")) |
| 86 | + .message() |
| 87 | + .isEqualTo(String.join( |
| 88 | + "\n", |
| 89 | + "No module matching foo.bar was found. Maybe you meant:", |
| 90 | + " - foo.baz", |
| 91 | + " - foo.bork" |
| 92 | + )); |
| 93 | + |
| 94 | + verify(moduleContainerGroup).getModule("foo.bar"); |
| 95 | + verify(moduleContainerGroup).getModules(); |
| 96 | + verifyNoMoreInteractions(moduleContainerGroup); |
| 97 | + } |
| 98 | + |
| 99 | + @DisplayName(".moduleExists(...) returns assertions on the module when it exists") |
| 100 | + @Test |
| 101 | + void moduleExistsReturnsAssertionsOnTheModuleWhenItExists() { |
| 102 | + // Given |
| 103 | + var moduleContainerGroup = mock(ModuleContainerGroup.class); |
| 104 | + var packageContainerGroup = mock(PackageContainerGroup.class); |
| 105 | + when(moduleContainerGroup.getModule(any())).thenReturn(packageContainerGroup); |
| 106 | + |
| 107 | + var assertions = new ModuleContainerGroupAssert(moduleContainerGroup); |
| 108 | + |
| 109 | + // Then |
| 110 | + assertThatCode(() -> assertions.moduleExists("foo.bar").isSameAs(packageContainerGroup)) |
| 111 | + .doesNotThrowAnyException(); |
| 112 | + |
| 113 | + verify(moduleContainerGroup).getModule("foo.bar"); |
| 114 | + verifyNoMoreInteractions(moduleContainerGroup); |
| 115 | + } |
| 116 | + |
| 117 | + private ModuleLocation module(String name) { |
| 118 | + return new ModuleLocation(StandardLocation.MODULE_SOURCE_PATH, name); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @DisplayName("ModuleContainerGroupAssert#moduleDoesNotExist tests") |
| 123 | + @Nested |
| 124 | + class ModuleDoesNotExistTest { |
| 125 | + |
| 126 | + @DisplayName(".moduleDoesNotExist(...) fails if the module name is null") |
| 127 | + @Test |
| 128 | + void moduleDoesNotExistFailsIfModuleNameIsNull() { |
| 129 | + // Given |
| 130 | + var assertions = new ModuleContainerGroupAssert(mock()); |
| 131 | + |
| 132 | + // Then |
| 133 | + assertThatThrownBy(() -> assertions.moduleDoesNotExist(null)) |
| 134 | + .isInstanceOf(NullPointerException.class); |
| 135 | + } |
| 136 | + |
| 137 | + @DisplayName(".moduleDoesNotExist(...) fails if the container group is null") |
| 138 | + @Test |
| 139 | + void moduleDoesNotExistFailsIfContainerGroupIsNull() { |
| 140 | + // Given |
| 141 | + var assertions = new ModuleContainerGroupAssert(null); |
| 142 | + |
| 143 | + // Then |
| 144 | + assertThatThrownBy(() -> assertions.moduleDoesNotExist("something")) |
| 145 | + .isInstanceOf(AssertionError.class); |
| 146 | + } |
| 147 | + |
| 148 | + @DisplayName(".moduleDoesNotExist(...) fails if the module exists") |
| 149 | + @Test |
| 150 | + void moduleDoesNotExistFailsIfModuleExists() { |
| 151 | + // Given |
| 152 | + var moduleContainerGroup = mock(ModuleContainerGroup.class); |
| 153 | + when(moduleContainerGroup.getModule(any())).thenReturn(mock()); |
| 154 | + var assertions = new ModuleContainerGroupAssert(moduleContainerGroup); |
| 155 | + |
| 156 | + // Then |
| 157 | + assertThatThrownBy(() -> assertions.moduleDoesNotExist("foo.bar")) |
| 158 | + .isInstanceOf(AssertionError.class) |
| 159 | + .hasMessage("Found unexpected module foo.bar"); |
| 160 | + } |
| 161 | + |
| 162 | + @DisplayName(".moduleDoesNotExist(...) succeeds if the module does not exist") |
| 163 | + @Test |
| 164 | + void moduleDoesNotExistSucceedsIfTheModuleDoesNotExist() { |
| 165 | + // Given |
| 166 | + var moduleContainerGroup = mock(ModuleContainerGroup.class); |
| 167 | + when(moduleContainerGroup.getModule(any())).thenReturn(null); |
| 168 | + var assertions = new ModuleContainerGroupAssert(moduleContainerGroup); |
| 169 | + |
| 170 | + // Then |
| 171 | + assertThatCode(() -> assertions.moduleDoesNotExist("foo.bar").isSameAs(moduleContainerGroup)) |
| 172 | + .doesNotThrowAnyException(); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments