[ 
https://issues.apache.org/jira/browse/MNG-7443?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17570506#comment-17570506
 ] 

ASF GitHub Bot commented on MNG-7443:
-------------------------------------

michael-o commented on code in PR #701:
URL: https://github.com/apache/maven/pull/701#discussion_r928279271


##########
maven-core/src/test/java/org/apache/maven/graph/ProjectSelectorTest.java:
##########
@@ -0,0 +1,221 @@
+package org.apache.maven.graph;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.MavenExecutionException;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.project.MavenProject;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EmptySource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ProjectSelectorTest
+{
+    private final ProjectSelector sut = new ProjectSelector();
+    private final MavenExecutionRequest mavenExecutionRequest = mock( 
MavenExecutionRequest.class );
+
+    @Test
+    void getBaseDirectoryFromRequestWhenDirectoryIsNullReturnNull()
+    {
+        when( mavenExecutionRequest.getBaseDirectory() ).thenReturn( null );
+
+        final File baseDirectoryFromRequest = sut.getBaseDirectoryFromRequest( 
mavenExecutionRequest );
+
+        assertThat( baseDirectoryFromRequest, nullValue() );
+    }
+
+    @Test
+    void getBaseDirectoryFromRequestWhenDirectoryIsValidReturnFile()
+    {
+        when( mavenExecutionRequest.getBaseDirectory() ).thenReturn( 
"path/to/file" );
+
+        final File baseDirectoryFromRequest = sut.getBaseDirectoryFromRequest( 
mavenExecutionRequest );
+
+        assertThat( baseDirectoryFromRequest, notNullValue() );
+        assertThat( baseDirectoryFromRequest.getPath(), is( new File( 
"path/to/file" ).getPath() ) );
+    }
+
+    @ParameterizedTest
+    @ValueSource( strings = {":wrong-selector", "wrong-selector"} )
+    @EmptySource
+    void isMatchingProjectNoMatchOnSelectorReturnsFalse( String selector )
+    {
+        final boolean result = sut.isMatchingProject( 
createMavenProject("maven-core" ), selector, null );
+        assertThat( result, is( false ) );
+    }
+
+    @ParameterizedTest
+    @ValueSource( strings = {":maven-core", "org.apache.maven:maven-core"} )
+    void isMatchingProjectMatchOnSelectorReturnsTrue( String selector )
+    {
+        final boolean result = sut.isMatchingProject( 
createMavenProject("maven-core" ), selector, null );
+        assertThat( result, is( true ) );
+    }
+
+    @Test
+    void isMatchingProjectMatchOnFileReturnsTrue() throws IOException
+    {
+        final File tempFile = File.createTempFile( "maven-core-unit-test-pom", 
".xml" );
+        final String selector = tempFile.getName();
+        final MavenProject mavenProject = createMavenProject("maven-core" );
+        mavenProject.setFile( tempFile );
+
+        final boolean result = sut.isMatchingProject( mavenProject, selector, 
tempFile.getParentFile() );
+
+        tempFile.delete();
+        assertThat( result, is( true ) );
+    }
+
+    @Test
+    void isMatchingProjectMatchOnDirectoryReturnsTrue()
+    {
+        String selector = "maven-core";
+        final File tempDir = new File( System.getProperty( "java.io.tmpdir" ) 
);
+        final File tempProjectDir = new File( tempDir, "maven-core" );
+        tempProjectDir.mkdir();

Review Comment:
   JUnit provides `@TempDir` as method injection.



##########
maven-core/src/main/java/org/apache/maven/graph/ProjectSelector.java:
##########
@@ -0,0 +1,160 @@
+package org.apache.maven.graph;
+
+/*
+ * 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.
+ */
+
+import org.apache.maven.MavenExecutionException;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.project.MavenProject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * Utility class to extract {@link MavenProject} from the project graph during 
the execution phase based on optional or
+ * required selectors.
+ */
+public final class ProjectSelector
+{
+    private static final Logger LOGGER = LoggerFactory.getLogger( 
ProjectSelector.class );
+
+    public Set<MavenProject> getRequiredProjectsBySelectors( 
MavenExecutionRequest request, List<MavenProject> projects,
+                                                             Set<String> 
projectSelectors )
+            throws MavenExecutionException
+    {
+        Set<MavenProject> selectedProjects = new LinkedHashSet<>();
+        File baseDirectory = getBaseDirectoryFromRequest( request );
+        for ( String selector : projectSelectors )
+        {
+            Optional<MavenProject> optSelectedProject =
+                    findOptionalProjectBySelector( projects, baseDirectory, 
selector );
+            if ( !optSelectedProject.isPresent() )
+            {
+                String message = "Could not find the selected project in the 
reactor: " + selector;
+                throw new MavenExecutionException( message, request.getPom() );
+            }
+
+            MavenProject selectedProject = optSelectedProject.get();
+
+            selectedProjects.add( selectedProject );
+            selectedProjects.addAll( getChildProjects( selectedProject, 
request ) );
+        }
+
+        return selectedProjects;
+    }
+
+    public Set<MavenProject> getOptionalProjectsBySelectors( 
MavenExecutionRequest request, List<MavenProject> projects,
+                                                             Set<String> 
projectSelectors )
+    {
+        Set<MavenProject> resolvedOptionalProjects = new LinkedHashSet<>();
+        Set<String> unresolvedOptionalSelectors = new HashSet<>();
+        File baseDirectory = getBaseDirectoryFromRequest( request );
+        for ( String selector : projectSelectors )
+        {
+            Optional<MavenProject> optSelectedProject =
+                    findOptionalProjectBySelector( projects, baseDirectory, 
selector );
+            if ( optSelectedProject.isPresent() )
+            {
+                resolvedOptionalProjects.add( optSelectedProject.get() );
+                resolvedOptionalProjects.addAll( getChildProjects( 
optSelectedProject.get(), request ) );
+            }
+            else
+            {
+                unresolvedOptionalSelectors.add( selector );
+            }
+        }
+
+        if ( !unresolvedOptionalSelectors.isEmpty() )
+        {
+            String message = String.format( "The requested optional projects 
[%s] do not exist.",
+                    String.join( ",", unresolvedOptionalSelectors ) );
+            LOGGER.info( message );

Review Comment:
   I think you can safely use the to string representation of the collection 
and use regular SLF4J placeholders here





> Consistent logging between optional projects and optional profiles
> ------------------------------------------------------------------
>
>                 Key: MNG-7443
>                 URL: https://issues.apache.org/jira/browse/MNG-7443
>             Project: Maven
>          Issue Type: Improvement
>          Components: Core, Logging
>    Affects Versions: 4.0.0-alpha-1
>            Reporter: Giovanni van der Schelde
>            Priority: Minor
>         Attachments: example.png
>
>
> Maven 4 introduces optional profiles and optional projects. However, the 
> feedback provided to the user on whether a project or profile has been 
> skipped is inconsistent between the two (see image attached). 
> For profiles, it will be logged twice: before and after the build.
> For projects, it will be logged once: before the build.
> The idea would be to log the information for skipped optional projects after 
> the build as well.
> !example.png!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to