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


##########
maven-core/src/test/java/org/apache/maven/graph/DefaultGraphBuilderTest.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.graph;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.DefaultMavenExecutionResult;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.model.Build;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.building.ModelProblem;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.ProjectBuilder;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingResult;
+import org.codehaus.plexus.logging.Logger;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultGraphBuilderTest {
+
+    /**
+     * The warning about a plugin with extensions that is built in the same 
reactor is only for projects which are
+     * built, not for the ones excluded from the reactor (MNG-8092).
+     */
+    @Test
+    void warnsAboutExtensionInReactorOnlyWhenItIsBuilt() throws Exception {
+        Logger warnings = mock(Logger.class);
+
+        assertEquals(1, extensionWarnings(warnings, 
Collections.<String>emptyList()));
+        assertEquals(0, extensionWarnings(warnings, 
Collections.singletonList("org.apache.maven:test-0")));
+    }
+
+    private int extensionWarnings(Logger logger, List<String> 
excludedProjects) throws Exception {
+        Model pluginModel = new Model();
+        pluginModel.setGroupId("org.apache.maven");
+        pluginModel.setArtifactId("test-0");
+        pluginModel.setVersion("1");
+
+        Plugin plugin = new Plugin();
+        plugin.setGroupId("org.apache.maven");
+        plugin.setArtifactId("test-0");
+        plugin.setVersion("1");
+        plugin.setExtensions(true);
+        Build build = new Build();
+        build.addPlugin(plugin);
+        Model consumerModel = new Model();
+        consumerModel.setGroupId("org.apache.maven");
+        consumerModel.setArtifactId("test-1");
+        consumerModel.setVersion("1");
+        consumerModel.setBuild(build);
+
+        List<ProjectBuildingResult> results = new ArrayList<>();
+        for (Model model : Arrays.asList(pluginModel, consumerModel)) {
+            MavenProject project = new MavenProject(model);
+            ProjectBuildingResult result = mock(ProjectBuildingResult.class);
+            when(result.getProject()).thenReturn(project);
+            when(result.getProjectId()).thenReturn(project.getId());
+            
when(result.getProblems()).thenReturn(Collections.<ModelProblem>emptyList());
+            results.add(result);
+        }
+
+        ProjectBuilder projectBuilder = mock(ProjectBuilder.class);
+        when(projectBuilder.build(anyList(), anyBoolean(), 
any(ProjectBuildingRequest.class)))
+                .thenReturn(results);
+
+        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
+        request.setPom(new File("pom.xml"));
+        request.setExcludedProjects(excludedProjects);
+        MavenSession session = new MavenSession(null, null, request, new 
DefaultMavenExecutionResult());
+
+        DefaultGraphBuilder graphBuilder = new DefaultGraphBuilder();
+        graphBuilder.projectBuilder = projectBuilder;
+        graphBuilder.logger = logger;
+
+        Mockito.clearInvocations(logger);
+        assertFalse(graphBuilder.build(session).hasErrors());
+        return Mockito.mockingDetails(logger).getInvocations().stream()
+                .filter(invocation -> 
invocation.getMethod().getName().equals("warn"))
+                .mapToInt(invocation -> 1)
+                .sum();
+    }

Review Comment:
   💡 **Style:** `clearInvocations` + 
`mockingDetails().getInvocations().stream()...` is verbose and fragile (the 
method-name string `"warn"` is untyped). Prefer:
   
   ```suggestion
           Mockito.verify(logger, Mockito.times(1)).warn(Mockito.anyString());
           assertFalse(graphBuilder.build(session).hasErrors());
           return (int) Mockito.mockingDetails(logger).getInvocations().stream()
                   .filter(invocation -> 
invocation.getMethod().getName().equals("warn"))
                   .count();
   ```
   
   Or simpler still — call `extensionWarnings()` and then `verify(logger, 
times(N)).warn(anyString())` in the `@Test` method directly, rather than 
returning a count.
   
   This also avoids the need for `clearInvocations` between the two calls.



##########
maven-core/src/main/java/org/apache/maven/graph/DefaultGraphBuilder.java:
##########
@@ -61,8 +61,9 @@
 @Component(role = GraphBuilder.class, hint = GraphBuilder.HINT)
 public class DefaultGraphBuilder implements GraphBuilder {
 
+    // not private nor final, so that a test can capture what is logged
     @Requirement
-    private Logger logger;
+    Logger logger;

Review Comment:
   ⚠️ **Encapsulation weakening for test convenience.** Widening `logger` from 
`private` to package-private (with the accompanying comment) is a permanent 
production API surface change just to enable direct field assignment in the 
test. The original fix on `master` kept `Logger` as `private` (using SLF4J + an 
IT that didn't need field injection).
   
   Consider injecting via a setter or using the Plexus test harness so the 
field can stay `private`:
   
   ```suggestion
       @Requirement
       private Logger logger;
   ```
   (and remove the `// not private nor final` comment)



-- 
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