|
| 1 | +package io.github.makbn.jlmap.element.menu; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import static org.assertj.core.api.Assertions.assertThat; |
| 6 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 7 | + |
| 8 | +/** |
| 9 | + * Unit tests for JLMenuItem. |
| 10 | + * Tests the menu item functionality. |
| 11 | + * |
| 12 | + * @author Matt Akbarian (@makbn) |
| 13 | + * @since 2.0.0 |
| 14 | + */ |
| 15 | +class JLMenuItemTest { |
| 16 | + |
| 17 | + // === Factory Method Tests === |
| 18 | + |
| 19 | + @Test |
| 20 | + void of_withIdAndText_shouldCreateMenuItem() { |
| 21 | + // When |
| 22 | + JLMenuItem item = JLMenuItem.of("edit", "Edit"); |
| 23 | + |
| 24 | + // Then |
| 25 | + assertThat(item).isNotNull(); |
| 26 | + assertThat(item.getId()).isEqualTo("edit"); |
| 27 | + assertThat(item.getText()).isEqualTo("Edit"); |
| 28 | + assertThat(item.getIcon()).isNull(); |
| 29 | + assertThat(item.isVisible()).isTrue(); |
| 30 | + assertThat(item.isEnabled()).isTrue(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void of_withIdTextAndIcon_shouldCreateMenuItemWithIcon() { |
| 35 | + // When |
| 36 | + JLMenuItem item = JLMenuItem.of("edit", "Edit", "https://example.com/icon.png"); |
| 37 | + |
| 38 | + // Then |
| 39 | + assertThat(item).isNotNull(); |
| 40 | + assertThat(item.getId()).isEqualTo("edit"); |
| 41 | + assertThat(item.getText()).isEqualTo("Edit"); |
| 42 | + assertThat(item.getIcon()).isEqualTo("https://example.com/icon.png"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void of_withNullId_shouldThrowException() { |
| 47 | + // When/Then |
| 48 | + assertThatThrownBy(() -> JLMenuItem.of(null, "Edit")) |
| 49 | + .isInstanceOf(NullPointerException.class); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void of_withNullText_shouldThrowException() { |
| 54 | + // When/Then |
| 55 | + assertThatThrownBy(() -> JLMenuItem.of("edit", null)) |
| 56 | + .isInstanceOf(NullPointerException.class); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void of_withBlankId_shouldAcceptBlankId() { |
| 61 | + // When |
| 62 | + JLMenuItem item = JLMenuItem.of("", "Edit"); |
| 63 | + |
| 64 | + // Then - Current implementation accepts blank ID |
| 65 | + // This documents the current behavior |
| 66 | + assertThat(item.getId()).isEmpty(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void of_withBlankText_shouldAcceptBlankText() { |
| 71 | + // When |
| 72 | + JLMenuItem item = JLMenuItem.of("edit", ""); |
| 73 | + |
| 74 | + // Then - Current implementation accepts blank text |
| 75 | + // This documents the current behavior |
| 76 | + assertThat(item.getText()).isEmpty(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void of_withWhitespaceId_shouldAcceptWhitespaceId() { |
| 81 | + // When |
| 82 | + JLMenuItem item = JLMenuItem.of(" ", "Edit"); |
| 83 | + |
| 84 | + // Then - Current implementation accepts whitespace ID |
| 85 | + // This documents the current behavior |
| 86 | + assertThat(item.getId()).isEqualTo(" "); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void of_withWhitespaceText_shouldAcceptWhitespaceText() { |
| 91 | + // When |
| 92 | + JLMenuItem item = JLMenuItem.of("edit", " "); |
| 93 | + |
| 94 | + // Then - Current implementation accepts whitespace text |
| 95 | + // This documents the current behavior |
| 96 | + assertThat(item.getText()).isEqualTo(" "); |
| 97 | + } |
| 98 | + |
| 99 | + // === Visibility Tests === |
| 100 | + |
| 101 | + @Test |
| 102 | + void isVisible_byDefault_shouldReturnTrue() { |
| 103 | + // Given |
| 104 | + JLMenuItem item = JLMenuItem.of("edit", "Edit"); |
| 105 | + |
| 106 | + // When/Then |
| 107 | + assertThat(item.isVisible()).isTrue(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void builder_withVisibleFalse_shouldCreateHiddenItem() { |
| 112 | + // When |
| 113 | + JLMenuItem item = JLMenuItem.builder() |
| 114 | + .id("edit") |
| 115 | + .text("Edit") |
| 116 | + .visible(false) |
| 117 | + .build(); |
| 118 | + |
| 119 | + // Then |
| 120 | + assertThat(item.isVisible()).isFalse(); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void toBuilder_withVisibleFalse_shouldCreateHiddenItem() { |
| 125 | + // Given |
| 126 | + JLMenuItem item = JLMenuItem.of("edit", "Edit"); |
| 127 | + |
| 128 | + // When |
| 129 | + JLMenuItem modifiedItem = item.toBuilder() |
| 130 | + .visible(false) |
| 131 | + .build(); |
| 132 | + |
| 133 | + // Then |
| 134 | + assertThat(modifiedItem.isVisible()).isFalse(); |
| 135 | + assertThat(item.isVisible()).isTrue(); // Original unchanged |
| 136 | + } |
| 137 | + |
| 138 | + // === Enabled Tests === |
| 139 | + |
| 140 | + @Test |
| 141 | + void isEnabled_byDefault_shouldReturnTrue() { |
| 142 | + // Given |
| 143 | + JLMenuItem item = JLMenuItem.of("edit", "Edit"); |
| 144 | + |
| 145 | + // When/Then |
| 146 | + assertThat(item.isEnabled()).isTrue(); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void builder_withEnabledFalse_shouldCreateDisabledItem() { |
| 151 | + // When |
| 152 | + JLMenuItem item = JLMenuItem.builder() |
| 153 | + .id("edit") |
| 154 | + .text("Edit") |
| 155 | + .enabled(false) |
| 156 | + .build(); |
| 157 | + |
| 158 | + // Then |
| 159 | + assertThat(item.isEnabled()).isFalse(); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void toBuilder_withEnabledFalse_shouldCreateDisabledItem() { |
| 164 | + // Given |
| 165 | + JLMenuItem item = JLMenuItem.of("edit", "Edit"); |
| 166 | + |
| 167 | + // When |
| 168 | + JLMenuItem modifiedItem = item.toBuilder() |
| 169 | + .enabled(false) |
| 170 | + .build(); |
| 171 | + |
| 172 | + // Then |
| 173 | + assertThat(modifiedItem.isEnabled()).isFalse(); |
| 174 | + assertThat(item.isEnabled()).isTrue(); // Original unchanged |
| 175 | + } |
| 176 | + |
| 177 | + // === Icon Tests === |
| 178 | + |
| 179 | + @Test |
| 180 | + void toBuilder_shouldAllowUpdatingIcon() { |
| 181 | + // Given |
| 182 | + JLMenuItem item = JLMenuItem.of("edit", "Edit"); |
| 183 | + |
| 184 | + // When |
| 185 | + JLMenuItem modifiedItem = item.toBuilder() |
| 186 | + .icon("https://example.com/new-icon.png") |
| 187 | + .build(); |
| 188 | + |
| 189 | + // Then |
| 190 | + assertThat(modifiedItem.getIcon()).isEqualTo("https://example.com/new-icon.png"); |
| 191 | + assertThat(item.getIcon()).isNull(); // Original unchanged |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + void toBuilder_withNullIcon_shouldAcceptNullIcon() { |
| 196 | + // Given |
| 197 | + JLMenuItem item = JLMenuItem.of("edit", "Edit", "old-icon.png"); |
| 198 | + |
| 199 | + // When |
| 200 | + JLMenuItem modifiedItem = item.toBuilder() |
| 201 | + .icon(null) |
| 202 | + .build(); |
| 203 | + |
| 204 | + // Then |
| 205 | + assertThat(modifiedItem.getIcon()).isNull(); |
| 206 | + } |
| 207 | + |
| 208 | + // === Text Tests === |
| 209 | + |
| 210 | + @Test |
| 211 | + void toBuilder_shouldAllowUpdatingText() { |
| 212 | + // Given |
| 213 | + JLMenuItem item = JLMenuItem.of("edit", "Edit"); |
| 214 | + |
| 215 | + // When |
| 216 | + JLMenuItem modifiedItem = item.toBuilder() |
| 217 | + .text("Edit Updated") |
| 218 | + .build(); |
| 219 | + |
| 220 | + // Then |
| 221 | + assertThat(modifiedItem.getText()).isEqualTo("Edit Updated"); |
| 222 | + assertThat(item.getText()).isEqualTo("Edit"); // Original unchanged |
| 223 | + } |
| 224 | + |
| 225 | + // === Equals and HashCode Tests === |
| 226 | + |
| 227 | + @Test |
| 228 | + void equals_withAllFieldsEqual_shouldReturnTrue() { |
| 229 | + // Given |
| 230 | + JLMenuItem item1 = JLMenuItem.of("edit", "Edit"); |
| 231 | + JLMenuItem item2 = JLMenuItem.of("edit", "Edit"); |
| 232 | + |
| 233 | + // When/Then |
| 234 | + assertThat(item1).isEqualTo(item2); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + void equals_withDifferentText_shouldReturnFalse() { |
| 239 | + // Given |
| 240 | + JLMenuItem item1 = JLMenuItem.of("edit", "Edit"); |
| 241 | + JLMenuItem item2 = JLMenuItem.of("edit", "Edit Different"); |
| 242 | + |
| 243 | + // When/Then - @Value uses all fields for equals |
| 244 | + assertThat(item1).isNotEqualTo(item2); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + void equals_withDifferentId_shouldReturnFalse() { |
| 249 | + // Given |
| 250 | + JLMenuItem item1 = JLMenuItem.of("edit", "Edit"); |
| 251 | + JLMenuItem item2 = JLMenuItem.of("delete", "Edit"); |
| 252 | + |
| 253 | + // When/Then |
| 254 | + assertThat(item1).isNotEqualTo(item2); |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + void hashCode_withAllFieldsEqual_shouldReturnSameHashCode() { |
| 259 | + // Given |
| 260 | + JLMenuItem item1 = JLMenuItem.of("edit", "Edit"); |
| 261 | + JLMenuItem item2 = JLMenuItem.of("edit", "Edit"); |
| 262 | + |
| 263 | + // When/Then |
| 264 | + assertThat(item1.hashCode()).isEqualTo(item2.hashCode()); |
| 265 | + } |
| 266 | + |
| 267 | + // === toString Tests === |
| 268 | + |
| 269 | + @Test |
| 270 | + void toString_shouldContainIdAndText() { |
| 271 | + // Given |
| 272 | + JLMenuItem item = JLMenuItem.of("edit", "Edit Marker"); |
| 273 | + |
| 274 | + // When |
| 275 | + String result = item.toString(); |
| 276 | + |
| 277 | + // Then |
| 278 | + assertThat(result).contains("edit"); |
| 279 | + assertThat(result).contains("Edit Marker"); |
| 280 | + } |
| 281 | + |
| 282 | + // === Immutability Tests === |
| 283 | + |
| 284 | + @Test |
| 285 | + void menuItem_shouldBeImmutable() { |
| 286 | + // Given |
| 287 | + JLMenuItem item = JLMenuItem.of("edit", "Edit"); |
| 288 | + String originalId = item.getId(); |
| 289 | + String originalText = item.getText(); |
| 290 | + |
| 291 | + // When - Create modified version using toBuilder |
| 292 | + JLMenuItem modifiedItem = item.toBuilder() |
| 293 | + .text("Updated") |
| 294 | + .icon("new-icon.png") |
| 295 | + .visible(false) |
| 296 | + .enabled(false) |
| 297 | + .build(); |
| 298 | + |
| 299 | + // Then - Original should be unchanged |
| 300 | + assertThat(item.getId()).isEqualTo(originalId); |
| 301 | + assertThat(item.getText()).isEqualTo(originalText); |
| 302 | + assertThat(item.isVisible()).isTrue(); |
| 303 | + assertThat(item.isEnabled()).isTrue(); |
| 304 | + assertThat(item.getIcon()).isNull(); |
| 305 | + |
| 306 | + // And modified should have new values |
| 307 | + assertThat(modifiedItem.getText()).isEqualTo("Updated"); |
| 308 | + assertThat(modifiedItem.getIcon()).isEqualTo("new-icon.png"); |
| 309 | + assertThat(modifiedItem.isVisible()).isFalse(); |
| 310 | + assertThat(modifiedItem.isEnabled()).isFalse(); |
| 311 | + } |
| 312 | + |
| 313 | + // === Null Parameter Tests for @NonNull Validation (Three-param method) === |
| 314 | + |
| 315 | + @Test |
| 316 | + void of_withNullIdAndIcon_shouldThrowException() { |
| 317 | + // When/Then |
| 318 | + assertThatThrownBy(() -> JLMenuItem.of(null, "Edit", "icon.png")) |
| 319 | + .isInstanceOf(NullPointerException.class); |
| 320 | + } |
| 321 | + |
| 322 | + @Test |
| 323 | + void of_withNullTextAndIcon_shouldThrowException() { |
| 324 | + // When/Then |
| 325 | + assertThatThrownBy(() -> JLMenuItem.of("edit", null, "icon.png")) |
| 326 | + .isInstanceOf(NullPointerException.class); |
| 327 | + } |
| 328 | + |
| 329 | + @Test |
| 330 | + void of_withNullIcon_shouldAcceptNullIcon() { |
| 331 | + // When |
| 332 | + JLMenuItem item = JLMenuItem.of("edit", "Edit", null); |
| 333 | + |
| 334 | + // Then |
| 335 | + assertThat(item.getIcon()).isNull(); |
| 336 | + } |
| 337 | + |
| 338 | + @Test |
| 339 | + void of_withBlankIcon_shouldAcceptBlankIcon() { |
| 340 | + // When |
| 341 | + JLMenuItem item = JLMenuItem.of("edit", "Edit", ""); |
| 342 | + |
| 343 | + // Then |
| 344 | + assertThat(item.getIcon()).isEmpty(); |
| 345 | + } |
| 346 | + |
| 347 | + @Test |
| 348 | + void of_withWhitespaceIcon_shouldAcceptWhitespaceIcon() { |
| 349 | + // When |
| 350 | + JLMenuItem item = JLMenuItem.of("edit", "Edit", " "); |
| 351 | + |
| 352 | + // Then |
| 353 | + assertThat(item.getIcon()).isEqualTo(" "); |
| 354 | + } |
| 355 | +} |
0 commit comments