|
| 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 io.github.ascopes.jct.assertions.StackTraceElementAssert; |
| 19 | +import org.junit.jupiter.api.DisplayName; |
| 20 | +import org.junit.jupiter.api.Nested; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.ValueSource; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | +import static org.mockito.Mockito.when; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link StackTraceElementAssert} tests. |
| 33 | + * |
| 34 | + * @author Ashley Scopes |
| 35 | + */ |
| 36 | +@DisplayName("StackTraceElementAssert tests") |
| 37 | +class StackTraceElementAssertTest { |
| 38 | + @DisplayName("StackTraceElementAssert#fileName tests") |
| 39 | + @Nested |
| 40 | + class FileNameTest { |
| 41 | + @DisplayName(".fileName() fails if the stack trace element is null") |
| 42 | + @Test |
| 43 | + void failsIfStackTraceElementIsNull() { |
| 44 | + // Given |
| 45 | + var assertions = new StackTraceElementAssert(null); |
| 46 | + |
| 47 | + // Then |
| 48 | + assertThatThrownBy(assertions::fileName) |
| 49 | + .isInstanceOf(AssertionError.class); |
| 50 | + } |
| 51 | + |
| 52 | + @DisplayName(".fileName() returns assertions on the file name") |
| 53 | + @Test |
| 54 | + void returnsAssertionsOnTheFileName() { |
| 55 | + // Given |
| 56 | + var element = mock(StackTraceElement.class); |
| 57 | + when(element.getFileName()).thenReturn("some-file-name.java"); |
| 58 | + |
| 59 | + var assertions = new StackTraceElementAssert(element); |
| 60 | + |
| 61 | + // Then |
| 62 | + assertThatCode(() -> assertions.fileName().isEqualTo("some-file-name.java")) |
| 63 | + .doesNotThrowAnyException(); |
| 64 | + |
| 65 | + assertThat(assertions.fileName().descriptionText()) |
| 66 | + .isEqualTo("file name"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @DisplayName("StackTraceElementAssert#lineNumber tests") |
| 71 | + @Nested |
| 72 | + class LineNumberTest { |
| 73 | + @DisplayName(".lineNumber() fails if the stack trace element is null") |
| 74 | + @Test |
| 75 | + void failsIfStackTraceElementIsNull() { |
| 76 | + // Given |
| 77 | + var assertions = new StackTraceElementAssert(null); |
| 78 | + |
| 79 | + // Then |
| 80 | + assertThatThrownBy(assertions::lineNumber) |
| 81 | + .isInstanceOf(AssertionError.class); |
| 82 | + } |
| 83 | + |
| 84 | + @DisplayName(".lineNumber() returns assertions on the line number") |
| 85 | + @Test |
| 86 | + void returnsAssertionsOnTheLineNumber() { |
| 87 | + // Given |
| 88 | + var element = mock(StackTraceElement.class); |
| 89 | + when(element.getLineNumber()).thenReturn(1_366_768); |
| 90 | + |
| 91 | + var assertions = new StackTraceElementAssert(element); |
| 92 | + |
| 93 | + // Then |
| 94 | + assertThatCode(() -> assertions.lineNumber().isEqualTo(1_366_768)) |
| 95 | + .doesNotThrowAnyException(); |
| 96 | + |
| 97 | + assertThat(assertions.lineNumber().descriptionText()) |
| 98 | + .isEqualTo("line number"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @DisplayName("StackTraceElementAssert#moduleName tests") |
| 103 | + @Nested |
| 104 | + class ModuleNameTest { |
| 105 | + @DisplayName(".moduleName() fails if the stack trace element is null") |
| 106 | + @Test |
| 107 | + void failsIfStackTraceElementIsNull() { |
| 108 | + // Given |
| 109 | + var assertions = new StackTraceElementAssert(null); |
| 110 | + |
| 111 | + // Then |
| 112 | + assertThatThrownBy(assertions::moduleName) |
| 113 | + .isInstanceOf(AssertionError.class); |
| 114 | + } |
| 115 | + |
| 116 | + @DisplayName(".moduleName() returns assertions on the module name") |
| 117 | + @Test |
| 118 | + void returnsAssertionsOnTheModuleName() { |
| 119 | + // Given |
| 120 | + var element = mock(StackTraceElement.class); |
| 121 | + when(element.getModuleName()).thenReturn("org.example.potatofarmer"); |
| 122 | + |
| 123 | + var assertions = new StackTraceElementAssert(element); |
| 124 | + |
| 125 | + // Then |
| 126 | + assertThatCode(() -> assertions.moduleName().isEqualTo("org.example.potatofarmer")) |
| 127 | + .doesNotThrowAnyException(); |
| 128 | + |
| 129 | + assertThat(assertions.moduleName().descriptionText()) |
| 130 | + .isEqualTo("module name"); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @DisplayName("StackTraceElementAssert#moduleVersion tests") |
| 135 | + @Nested |
| 136 | + class ModuleVersionTest { |
| 137 | + @DisplayName(".moduleVersion() fails if the stack trace element is null") |
| 138 | + @Test |
| 139 | + void failsIfStackTraceElementIsNull() { |
| 140 | + // Given |
| 141 | + var assertions = new StackTraceElementAssert(null); |
| 142 | + |
| 143 | + // Then |
| 144 | + assertThatThrownBy(assertions::moduleVersion) |
| 145 | + .isInstanceOf(AssertionError.class); |
| 146 | + } |
| 147 | + |
| 148 | + @DisplayName(".moduleVersion() returns assertions on the module version") |
| 149 | + @Test |
| 150 | + void returnsAssertionsOnTheModuleVersion() { |
| 151 | + // Given |
| 152 | + var element = mock(StackTraceElement.class); |
| 153 | + when(element.getModuleVersion()).thenReturn("v1.2.3.4"); |
| 154 | + |
| 155 | + var assertions = new StackTraceElementAssert(element); |
| 156 | + |
| 157 | + // Then |
| 158 | + assertThatCode(() -> assertions.moduleVersion().isEqualTo("v1.2.3.4")) |
| 159 | + .doesNotThrowAnyException(); |
| 160 | + |
| 161 | + assertThat(assertions.moduleVersion().descriptionText()) |
| 162 | + .isEqualTo("module version"); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @DisplayName("StackTraceElementAssert#classLoaderName tests") |
| 167 | + @Nested |
| 168 | + class ClassLoaderNameTest { |
| 169 | + @DisplayName(".classLoaderName() fails if the stack trace element is null") |
| 170 | + @Test |
| 171 | + void failsIfStackTraceElementIsNull() { |
| 172 | + // Given |
| 173 | + var assertions = new StackTraceElementAssert(null); |
| 174 | + |
| 175 | + // Then |
| 176 | + assertThatThrownBy(assertions::classLoaderName) |
| 177 | + .isInstanceOf(AssertionError.class); |
| 178 | + } |
| 179 | + |
| 180 | + @DisplayName(".classLoaderName() returns assertions on the class loader name") |
| 181 | + @Test |
| 182 | + void returnsAssertionsOnTheClassLoaderName() { |
| 183 | + // Given |
| 184 | + var element = mock(StackTraceElement.class); |
| 185 | + when(element.getClassLoaderName()).thenReturn("rubbish classloader"); |
| 186 | + |
| 187 | + var assertions = new StackTraceElementAssert(element); |
| 188 | + |
| 189 | + // Then |
| 190 | + assertThatCode(() -> assertions.classLoaderName().isEqualTo("rubbish classloader")) |
| 191 | + .doesNotThrowAnyException(); |
| 192 | + |
| 193 | + assertThat(assertions.classLoaderName().descriptionText()) |
| 194 | + .isEqualTo("class loader name"); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @DisplayName("StackTraceElementAssert#className tests") |
| 199 | + @Nested |
| 200 | + class ClassNameTest { |
| 201 | + @DisplayName(".className() fails if the stack trace element is null") |
| 202 | + @Test |
| 203 | + void failsIfStackTraceElementIsNull() { |
| 204 | + // Given |
| 205 | + var assertions = new StackTraceElementAssert(null); |
| 206 | + |
| 207 | + // Then |
| 208 | + assertThatThrownBy(assertions::className) |
| 209 | + .isInstanceOf(AssertionError.class); |
| 210 | + } |
| 211 | + |
| 212 | + @DisplayName(".className() returns assertions on the class name") |
| 213 | + @Test |
| 214 | + void returnsAssertionsOnTheClassName() { |
| 215 | + // Given |
| 216 | + var element = mock(StackTraceElement.class); |
| 217 | + when(element.getClassName()).thenReturn("org.example.NuclearBomb"); |
| 218 | + |
| 219 | + var assertions = new StackTraceElementAssert(element); |
| 220 | + |
| 221 | + // Then |
| 222 | + assertThatCode(() -> assertions.className().isEqualTo("org.example.NuclearBomb")) |
| 223 | + .doesNotThrowAnyException(); |
| 224 | + |
| 225 | + assertThat(assertions.className().descriptionText()) |
| 226 | + .isEqualTo("class name"); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + @DisplayName("StackTraceElementAssert#methodName tests") |
| 231 | + @Nested |
| 232 | + class MethodNameTest { |
| 233 | + @DisplayName(".methodName() fails if the stack trace element is null") |
| 234 | + @Test |
| 235 | + void failsIfStackTraceElementIsNull() { |
| 236 | + // Given |
| 237 | + var assertions = new StackTraceElementAssert(null); |
| 238 | + |
| 239 | + // Then |
| 240 | + assertThatThrownBy(assertions::methodName) |
| 241 | + .isInstanceOf(AssertionError.class); |
| 242 | + } |
| 243 | + |
| 244 | + @DisplayName(".methodName() returns assertions on the method name") |
| 245 | + @Test |
| 246 | + void returnsAssertionsOnTheMethodName() { |
| 247 | + // Given |
| 248 | + var element = mock(StackTraceElement.class); |
| 249 | + when(element.getMethodName()).thenReturn("explode"); |
| 250 | + |
| 251 | + var assertions = new StackTraceElementAssert(element); |
| 252 | + |
| 253 | + // Then |
| 254 | + assertThatCode(() -> assertions.methodName().isEqualTo("explode")) |
| 255 | + .doesNotThrowAnyException(); |
| 256 | + |
| 257 | + assertThat(assertions.methodName().descriptionText()) |
| 258 | + .isEqualTo("method name"); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + @DisplayName("StackTraceElementAssert#nativeMethod tests") |
| 263 | + @Nested |
| 264 | + class NativeMethodTest { |
| 265 | + @DisplayName(".nativeMethod() fails if the stack trace element is null") |
| 266 | + @Test |
| 267 | + void failsIfStackTraceElementIsNull() { |
| 268 | + // Given |
| 269 | + var assertions = new StackTraceElementAssert(null); |
| 270 | + |
| 271 | + // Then |
| 272 | + assertThatThrownBy(assertions::nativeMethod) |
| 273 | + .isInstanceOf(AssertionError.class); |
| 274 | + } |
| 275 | + |
| 276 | + @DisplayName(".nativeMethod() returns assertions on the native method") |
| 277 | + @ValueSource(booleans = {true, false}) |
| 278 | + @ParameterizedTest(name = "when isNativeMethod() returns {0}") |
| 279 | + void returnsAssertionsOnTheNativeMethod(boolean nativeMethod) { |
| 280 | + // Given |
| 281 | + var element = mock(StackTraceElement.class); |
| 282 | + when(element.isNativeMethod()).thenReturn(nativeMethod); |
| 283 | + |
| 284 | + var assertions = new StackTraceElementAssert(element); |
| 285 | + |
| 286 | + // Then |
| 287 | + assertThatCode(() -> assertions.nativeMethod().isEqualTo(nativeMethod)) |
| 288 | + .doesNotThrowAnyException(); |
| 289 | + |
| 290 | + assertThat(assertions.nativeMethod().descriptionText()) |
| 291 | + .isEqualTo("native method"); |
| 292 | + } |
| 293 | + } |
| 294 | +} |
0 commit comments