gnodet-bot commented on code in PR #13941:
URL: https://github.com/apache/maven/pull/13941#discussion_r4522453610


##########
impl/maven-core/src/test/java/org/apache/maven/internal/impl/EventSpyImplTest.java:
##########
@@ -191,6 +193,75 @@ void capturesExecutionDetailsWhenEventIsCreated() {
         assertSame(failure, event.getException().orElseThrow());
     }
 
+    @Test
+    void autoDiscoversListenerImplementingSpiService() throws Exception {
+        class SpiListener implements ExecutionListener, SpiService {
+            int startedCalls;
+
+            @Override
+            public void sessionStarted(ExecutionEvent event) {
+                startedCalls++;
+            }
+        }
+
+        SpiListener spiListener = new SpiListener();
+        SpiService nonListenerSpi = new SpiService() {};
+        Lookup lookup = mock(Lookup.class);
+        
when(lookup.lookupList(SpiService.class)).thenReturn(List.of(nonListenerSpi, 
spiListener));

Review Comment:
   🧪 **Missing test coverage for `catch (LookupException)` branch:** this stub 
uses `thenReturn()` which never throws, so the new `catch (LookupException e)` 
block in `AbstractSession.getListeners()` is never reached by this test. Add a 
companion test where `lookupList()` throws `LookupException` and verify the 
session still constructs successfully with zero listeners.



##########
impl/maven-core/src/test/java/org/apache/maven/internal/impl/EventSpyImplTest.java:
##########
@@ -191,6 +193,75 @@ void capturesExecutionDetailsWhenEventIsCreated() {
         assertSame(failure, event.getException().orElseThrow());
     }
 
+    @Test
+    void autoDiscoversListenerImplementingSpiService() throws Exception {
+        class SpiListener implements ExecutionListener, SpiService {
+            int startedCalls;
+
+            @Override
+            public void sessionStarted(ExecutionEvent event) {
+                startedCalls++;
+            }
+        }
+
+        SpiListener spiListener = new SpiListener();
+        SpiService nonListenerSpi = new SpiService() {};
+        Lookup lookup = mock(Lookup.class);
+        
when(lookup.lookupList(SpiService.class)).thenReturn(List.of(nonListenerSpi, 
spiListener));
+
+        DefaultRepositorySystemSession resolver = new 
DefaultRepositorySystemSession(h -> false);
+        MavenSession maven = new MavenSession(null, resolver, new 
DefaultMavenExecutionRequest(), null);
+        DefaultSession session = new DefaultSession(maven, 
mock(RepositorySystem.class), List.of(), null, lookup, null);
+        maven.setSession(session);
+        resolver.getData().set(InternalSession.class, session);
+
+        assertEquals(1, session.getListeners().size());
+        assertTrue(session.getListeners().contains(spiListener));
+
+        Session derived = session.withRemoteRepositories(List.of());
+        assertEquals(1, derived.getListeners().size());
+        assertTrue(derived.getListeners().contains(spiListener));
+
+        org.apache.maven.execution.ExecutionEvent event = 
mock(org.apache.maven.execution.ExecutionEvent.class);
+        when(event.getSession()).thenReturn(maven);
+        
when(event.getType()).thenReturn(org.apache.maven.execution.ExecutionEvent.Type.SessionStarted);
+        new EventSpyImpl().onEvent(event);
+
+        assertEquals(1, spiListener.startedCalls);
+    }
+
+    @Test
+    void autoDiscoversRepositoryListenerImplementingSpiService() {
+        class SpiRepoListener implements RepositoryListener, SpiService {
+            int resolvingCalls;
+
+            @Override
+            public void artifactResolving(RepositoryEvent event) {
+                resolvingCalls++;
+            }
+        }
+
+        SpiRepoListener spiListener = new SpiRepoListener();
+        Lookup lookup = mock(Lookup.class);
+        
when(lookup.lookupList(SpiService.class)).thenReturn(List.of(spiListener));

Review Comment:
   🧪 **Same missing coverage:** `thenReturn(List.of(spiListener))` never 
throws, so this test also skips the `catch (LookupException e)` path. A test 
that stubs `lookupList()` to throw would close this gap for the 
`RepositoryListener` path as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to