|
33 | 33 | import java.util.concurrent.ConcurrentHashMap;
|
34 | 34 | import java.util.function.Supplier;
|
35 | 35 |
|
| 36 | +import java.util.Set; |
36 | 37 | import org.apache.maven.api.Artifact;
|
37 | 38 | import org.apache.maven.api.LocalRepository;
|
38 | 39 | import org.apache.maven.api.ProducedArtifact;
|
|
70 | 71 |
|
71 | 72 | import static org.mockito.ArgumentMatchers.any;
|
72 | 73 | import static org.mockito.ArgumentMatchers.anyString;
|
73 |
| -import static org.mockito.ArgumentMatchers.argThat; |
74 | 74 | import static org.mockito.ArgumentMatchers.same;
|
75 | 75 | import static org.mockito.Mockito.doAnswer;
|
76 | 76 | import static org.mockito.Mockito.doReturn;
|
|
126 | 126 | * <p><strong>Service Behavior:</strong> The {@link Session#getService(Class)} method
|
127 | 127 | * throws {@link NoSuchElementException} for unknown services by default, matching
|
128 | 128 | * the behavior of the real Session implementation. This ensures consistent behavior
|
129 |
| - * between tests and production code.</p> |
| 129 | + * between tests and production code. The implementation uses Mockito's {@code argThat} |
| 130 | + * matcher to selectively apply the default behavior only to services that are not |
| 131 | + * explicitly configured, providing a clean and maintainable approach.</p> |
130 | 132 | *
|
131 | 133 | * @see InternalSession
|
132 | 134 | * @see LocalRepository
|
@@ -154,7 +156,7 @@ public static InternalSession getMockSession(LocalRepository localRepository) {
|
154 | 156 | when(session.createRemoteRepository(anyString(), anyString())).thenAnswer(iom -> {
|
155 | 157 | String id = iom.getArgument(0, String.class);
|
156 | 158 | String url = iom.getArgument(1, String.class);
|
157 |
| - return session.getService(RepositoryFactory.class).createRemote(id, url); |
| 159 | + return repositoryFactory.createRemote(id, url); |
158 | 160 | });
|
159 | 161 | when(session.createRemoteRepository(any()))
|
160 | 162 | .thenAnswer(iom -> repositoryFactory.createRemote(iom.getArgument(0, Repository.class)));
|
@@ -461,29 +463,43 @@ public static InternalSession getMockSession(LocalRepository localRepository) {
|
461 | 463 |
|
462 | 464 | // Set up default behavior for getService to throw NoSuchElementException for unknown services
|
463 | 465 | // This matches the behavior of the real Session implementation
|
464 |
| - // We use doAnswer to set up a fallback that only applies when no specific stub is found |
| 466 | + // We use doAnswer with argThat to only apply to services not explicitly configured above |
465 | 467 | doAnswer(invocation -> {
|
466 | 468 | Class<?> serviceClass = invocation.getArgument(0, Class.class);
|
467 | 469 | throw new NoSuchElementException(serviceClass.getName());
|
468 | 470 | })
|
469 | 471 | .when(session)
|
470 |
| - .getService(argThat(serviceClass -> |
| 472 | + .getService(ArgumentMatchers.argThat(serviceClass -> |
471 | 473 | // Only throw for service classes that are NOT explicitly configured above
|
472 |
| - !serviceClass.equals(RepositoryFactory.class) |
473 |
| - && !serviceClass.equals(VersionParser.class) |
474 |
| - && !serviceClass.equals(LocalRepositoryManager.class) |
475 |
| - && !serviceClass.equals(ArtifactInstaller.class) |
476 |
| - && !serviceClass.equals(ArtifactDeployer.class) |
477 |
| - && !serviceClass.equals(ArtifactManager.class) |
478 |
| - && !serviceClass.equals(ProjectManager.class) |
479 |
| - && !serviceClass.equals(ArtifactFactory.class) |
480 |
| - && !serviceClass.equals(ProjectBuilder.class) |
481 |
| - && !serviceClass.equals(ModelXmlFactory.class) |
482 |
| - && !serviceClass.equals(Lookup.class))); |
| 474 | + !isConfiguredService(serviceClass))); |
483 | 475 |
|
484 | 476 | return session;
|
485 | 477 | }
|
486 | 478 |
|
| 479 | + /** |
| 480 | + * List of service classes explicitly configured in the mock session. |
| 481 | + */ |
| 482 | + private static final Set<Class<?>> CONFIGURED_SERVICES = Set.of( |
| 483 | + RepositoryFactory.class, |
| 484 | + VersionParser.class, |
| 485 | + LocalRepositoryManager.class, |
| 486 | + ArtifactInstaller.class, |
| 487 | + ArtifactDeployer.class, |
| 488 | + ArtifactManager.class, |
| 489 | + ProjectManager.class, |
| 490 | + ArtifactFactory.class, |
| 491 | + ProjectBuilder.class, |
| 492 | + ModelXmlFactory.class, |
| 493 | + Lookup.class |
| 494 | + ); |
| 495 | + |
| 496 | + /** |
| 497 | + * Checks if a service class is explicitly configured in the mock session. |
| 498 | + */ |
| 499 | + private static boolean isConfiguredService(Class<?> serviceClass) { |
| 500 | + return CONFIGURED_SERVICES.contains(serviceClass); |
| 501 | + } |
| 502 | + |
487 | 503 | /**
|
488 | 504 | * Creates a mock session with enhanced getService behavior that throws NoSuchElementException
|
489 | 505 | * for unknown services.
|
|
0 commit comments