Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.util.List;

import org.apache.maven.execution.ProjectDependencyGraph;
import org.apache.maven.project.MavenProject;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class FilteredProjectDependencyGraphTest {

@Mock
private ProjectDependencyGraph projectDependencyGraph;

private final MavenProject aProject = createProject("A");

private final MavenProject bProject = createProject("B");

private final MavenProject cProject = createProject("C");

@ParameterizedTest
@ValueSource(booleans = {true, false})
void downstreamProjectsShouldBeCached(boolean transitive) {
FilteredProjectDependencyGraph graph =
new FilteredProjectDependencyGraph(projectDependencyGraph, List.of(aProject));

Copy link
Contributor

@Pankraz76 Pankraz76 May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
verify(projectDependencyGraph, times(0)).getDownstreamProjects(bProject, transitive);

this could be kind of increment, but not mandatory.

when(projectDependencyGraph.getDownstreamProjects(bProject, transitive)).thenReturn(List.of(cProject));

graph.getDownstreamProjects(bProject, transitive);
graph.getDownstreamProjects(bProject, transitive);

verify(projectDependencyGraph).getDownstreamProjects(bProject, transitive);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does assert that only one call is done ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly

}

@ParameterizedTest
@ValueSource(booleans = {true, false})
void upstreamProjectsShouldBeCached(boolean transitive) {
FilteredProjectDependencyGraph graph =
new FilteredProjectDependencyGraph(projectDependencyGraph, List.of(aProject));

when(projectDependencyGraph.getUpstreamProjects(bProject, transitive)).thenReturn(List.of(cProject));

graph.getUpstreamProjects(bProject, transitive);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could pass this test by only caching first 2 calls. Making random loop would break this mutated impl.

graph.getUpstreamProjects(bProject, transitive);

verify(projectDependencyGraph).getUpstreamProjects(bProject, transitive);
}

private static MavenProject createProject(String id) {
MavenProject result = new MavenProject();
result.setGroupId("org.apache");
result.setArtifactId(id);
result.setVersion("1.2");
return result;
}
}