mthmulders commented on code in PR #701:
URL: https://github.com/apache/maven/pull/701#discussion_r917915165


##########
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( getMavenProject( 
"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( getMavenProject( 
"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 = getMavenProject( "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();
+        final MavenProject mavenProject = getMavenProject( "maven-core" );
+        mavenProject.setFile( new File( tempProjectDir, "some-file.xml" ) );
+
+        final boolean result = sut.isMatchingProject( mavenProject, selector, 
tempDir );
+
+        tempProjectDir.delete();
+        assertThat( result, is( true ) );
+    }
+
+    @Test
+    void getOptionalProjectsBySelectorsReturnsMatches()
+    {
+        final HashSet<String> selectors = new HashSet<>();
+        selectors.add( ":maven-core" );
+        selectors.add( ":optional" );
+
+        final MavenProject mavenProject = getMavenProject( "maven-core" );
+        final List<MavenProject> listOfProjects = Collections.singletonList( 
mavenProject );
+
+        final Set<MavenProject> optionalProjectsBySelectors =
+                sut.getOptionalProjectsBySelectors( mavenExecutionRequest, 
listOfProjects, selectors );
+
+        assertThat( optionalProjectsBySelectors.size(), is( 1 ) );
+        assertThat( optionalProjectsBySelectors, contains( mavenProject ) );
+    }
+
+    @Test
+    void getRequiredProjectsBySelectorsThrowsMavenExecutionException()
+    {
+        final HashSet<String> selectors = new HashSet<>();
+        selectors.add( ":maven-core" );
+        selectors.add( ":required" );
+
+        final MavenProject mavenProject = getMavenProject( "maven-core" );
+        final List<MavenProject> listOfProjects = Collections.singletonList( 
mavenProject );
+
+        final MavenExecutionException exception = assertThrows( 
MavenExecutionException.class,
+                () -> sut.getRequiredProjectsBySelectors( 
mavenExecutionRequest, listOfProjects, selectors ) );
+        assertThat( exception.getMessage(), containsString( "Could not find" ) 
);
+        assertThat( exception.getMessage(), containsString( ":required" ) );
+    }
+
+    @Test
+    void getRequiredProjectsBySelectorsReturnsProject() throws 
MavenExecutionException
+    {
+        final HashSet<String> selectors = new HashSet<>();
+        selectors.add( ":maven-core" );
+
+        final MavenProject mavenProject = getMavenProject( "maven-core" );
+        final List<MavenProject> listOfProjects = Collections.singletonList( 
mavenProject );
+
+        final Set<MavenProject> requiredProjectsBySelectors =
+                sut.getRequiredProjectsBySelectors( mavenExecutionRequest, 
listOfProjects, selectors );
+
+        assertThat( requiredProjectsBySelectors.size(), is( 1 ) );
+        assertThat( requiredProjectsBySelectors, contains( mavenProject ) );
+    }
+
+    @Test
+    void getRequiredProjectsBySelectorsReturnsProjectWithChildProjects() 
throws MavenExecutionException
+    {
+        when( mavenExecutionRequest.isRecursive() ).thenReturn( true );
+
+        final HashSet<String> selectors = new HashSet<>();
+        selectors.add( ":maven-core" );
+
+        final MavenProject mavenProject = getMavenProject( "maven-core" );
+        final MavenProject child = getMavenProject( "maven-core-child" );
+        mavenProject.setCollectedProjects( Collections.singletonList( child ) 
);
+        final List<MavenProject> listOfProjects = Collections.singletonList( 
mavenProject );
+
+        final Set<MavenProject> requiredProjectsBySelectors =
+                sut.getRequiredProjectsBySelectors( mavenExecutionRequest, 
listOfProjects, selectors );
+
+        assertThat( requiredProjectsBySelectors.size(), is( 2 ) );
+        assertThat( requiredProjectsBySelectors, contains( mavenProject, child 
) );
+    }
+
+    @Test
+    void getOptionalProjectsBySelectorsReturnsProjectWithChildProjects()
+    {
+        when( mavenExecutionRequest.isRecursive() ).thenReturn( true );
+
+        final HashSet<String> selectors = new HashSet<>();
+        selectors.add( ":maven-core" );
+
+        final MavenProject mavenProject = getMavenProject( "maven-core" );
+        final MavenProject child = getMavenProject( "maven-core-child" );
+        mavenProject.setCollectedProjects( Collections.singletonList( child ) 
);
+        final List<MavenProject> listOfProjects = Collections.singletonList( 
mavenProject );
+
+        final Set<MavenProject> optionalProjectsBySelectors =
+                sut.getOptionalProjectsBySelectors( mavenExecutionRequest, 
listOfProjects, selectors );
+
+        assertThat( optionalProjectsBySelectors.size(), is( 2 ) );
+        assertThat( optionalProjectsBySelectors, contains( mavenProject, child 
) );
+    }
+
+    private MavenProject getMavenProject( String artifactId )

Review Comment:
   I'd suggest naming this method `createMavenProject` so it's clear that this 
creates an object that you will use only in the test. Reading the first few 
test methods confused me a bit...



##########
maven-core/src/main/java/org/apache/maven/graph/ProjectSelector.java:
##########
@@ -0,0 +1,144 @@
+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.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+public final class ProjectSelector

Review Comment:
   Thanks!



-- 
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: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to