in maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/stubs/SessionMock.java [90:405]
public static InternalSession getMockSession(LocalRepository localRepository) {
InternalSession session = mock(InternalSession.class);
//
// RepositoryFactory
//
RepositoryFactory repositoryFactory = mock(RepositoryFactory.class);
when(session.createRemoteRepository(anyString(), anyString())).thenAnswer(iom -> {
String id = iom.getArgument(0, String.class);
String url = iom.getArgument(1, String.class);
return session.getService(RepositoryFactory.class).createRemote(id, url);
});
when(session.createRemoteRepository(any()))
.thenAnswer(iom -> repositoryFactory.createRemote(iom.getArgument(0, Repository.class)));
when(repositoryFactory.createRemote(any(Repository.class))).thenAnswer(iom -> {
Repository repository = iom.getArgument(0, Repository.class);
return repositoryFactory.createRemote(repository.getId(), repository.getUrl());
});
when(repositoryFactory.createRemote(anyString(), anyString())).thenAnswer(iom -> {
String id = iom.getArgument(0, String.class);
String url = iom.getArgument(1, String.class);
RemoteRepository remoteRepository =
mock(RemoteRepository.class, withSettings().lenient());
when(remoteRepository.getId()).thenReturn(id);
when(remoteRepository.getUrl()).thenReturn(url);
when(remoteRepository.getProtocol()).thenReturn(URI.create(url).getScheme());
return remoteRepository;
});
when(session.getService(RepositoryFactory.class)).thenReturn(repositoryFactory);
//
// VersionParser
//
VersionParser versionParser =
new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme()));
when(session.parseVersion(any()))
.thenAnswer(iom -> versionParser.parseVersion(iom.getArgument(0, String.class)));
when(session.getService(VersionParser.class)).thenReturn(versionParser);
//
// LocalRepositoryManager
//
LocalRepositoryManager localRepositoryManager = mock(LocalRepositoryManager.class);
when(session.getPathForLocalArtifact(any(Artifact.class)))
.then(iom -> localRepositoryManager.getPathForLocalArtifact(
session, session.getLocalRepository(), iom.getArgument(0, Artifact.class)));
when(session.getPathForRemoteArtifact(any(), any()))
.thenAnswer(iom -> localRepositoryManager.getPathForRemoteArtifact(
session,
session.getLocalRepository(),
iom.getArgument(0, RemoteRepository.class),
iom.getArgument(1, Artifact.class)));
when(localRepositoryManager.getPathForLocalArtifact(any(), any(), any()))
.thenAnswer(iom -> {
LocalRepository localRepo = iom.getArgument(1, LocalRepository.class);
Artifact artifact = iom.getArgument(2, Artifact.class);
return localRepo.getPath().resolve(getPathForArtifact(artifact, true));
});
when(session.getService(LocalRepositoryManager.class)).thenReturn(localRepositoryManager);
//
// ArtifactInstaller
//
ArtifactInstaller artifactInstaller = mock(ArtifactInstaller.class);
doAnswer(iom -> {
artifactInstaller.install(
ArtifactInstallerRequest.build(session, iom.getArgument(0, Collection.class)));
return null;
})
.when(session)
.installArtifacts(any(Collection.class));
doAnswer(iom -> {
artifactInstaller.install(ArtifactInstallerRequest.build(
session, Arrays.asList(iom.getArgument(0, ProducedArtifact[].class))));
return null;
})
.when(session)
.installArtifacts(any(ProducedArtifact[].class));
doAnswer(iom -> {
artifactInstaller.install(ArtifactInstallerRequest.build(
iom.getArgument(0, Session.class), iom.getArgument(1, Collection.class)));
return null;
})
.when(artifactInstaller)
.install(any(Session.class), ArgumentMatchers.<Collection<ProducedArtifact>>any());
when(session.getService(ArtifactInstaller.class)).thenReturn(artifactInstaller);
//
// ArtifactDeployer
//
ArtifactDeployer artifactDeployer = mock(ArtifactDeployer.class);
doAnswer(iom -> {
artifactDeployer.deploy(ArtifactDeployerRequest.build(
iom.getArgument(0, Session.class),
iom.getArgument(1, RemoteRepository.class),
Arrays.asList(iom.getArgument(2, ProducedArtifact[].class))));
return null;
})
.when(session)
.deployArtifact(any(), any());
doAnswer(iom -> {
artifactDeployer.deploy(ArtifactDeployerRequest.build(
iom.getArgument(0, Session.class),
iom.getArgument(1, RemoteRepository.class),
iom.getArgument(2, Collection.class)));
return null;
})
.when(artifactDeployer)
.deploy(any(), any(), any());
when(session.getService(ArtifactDeployer.class)).thenReturn(artifactDeployer);
//
// ArtifactManager
//
ArtifactManager artifactManager = mock(ArtifactManager.class);
Map<Artifact, Path> paths = new HashMap<>();
doAnswer(iom -> {
paths.put(iom.getArgument(0), iom.getArgument(1));
return null;
})
.when(artifactManager)
.setPath(any(), any());
doAnswer(iom -> Optional.ofNullable(paths.get(iom.getArgument(0, Artifact.class))))
.when(artifactManager)
.getPath(any());
doAnswer(iom -> artifactManager.getPath(iom.getArgument(0, Artifact.class)))
.when(session)
.getArtifactPath(any());
when(session.getService(ArtifactManager.class)).thenReturn(artifactManager);
//
// ProjectManager
//
ProjectManager projectManager = mock(ProjectManager.class);
Map<Project, Collection<Artifact>> attachedArtifacts = new HashMap<>();
doAnswer(iom -> {
Project project = iom.getArgument(1, Project.class);
String type = iom.getArgument(2, String.class);
Path path = iom.getArgument(3, Path.class);
ProducedArtifact artifact = session.createProducedArtifact(
project.getGroupId(), project.getArtifactId(), project.getVersion(), null, null, type);
artifactManager.setPath(artifact, path);
attachedArtifacts
.computeIfAbsent(project, p -> new ArrayList<>())
.add(artifact);
return null;
})
.when(projectManager)
.attachArtifact(same(session), any(Project.class), any(), any());
doAnswer(iom -> {
Project project = iom.getArgument(0, Project.class);
ProducedArtifact artifact = iom.getArgument(1, ProducedArtifact.class);
Path path = iom.getArgument(2, Path.class);
artifactManager.setPath(artifact, path);
attachedArtifacts
.computeIfAbsent(project, p -> new ArrayList<>())
.add(artifact);
return null;
})
.when(projectManager)
.attachArtifact(any(Project.class), any(ProducedArtifact.class), any(Path.class));
when(projectManager.getAttachedArtifacts(any()))
.then(iom ->
attachedArtifacts.computeIfAbsent(iom.getArgument(0, Project.class), p -> new ArrayList<>()));
when(projectManager.getAllArtifacts(any())).then(iom -> {
Project project = iom.getArgument(0, Project.class);
List<Artifact> result = new ArrayList<>();
result.addAll(project.getArtifacts());
result.addAll(attachedArtifacts.computeIfAbsent(project, p -> new ArrayList<>()));
return result;
});
when(session.getService(ProjectManager.class)).thenReturn(projectManager);
//
// ArtifactFactory
//
ArtifactFactory artifactFactory = mock(ArtifactFactory.class);
when(artifactFactory.create(any())).then(iom -> {
ArtifactFactoryRequest request = iom.getArgument(0, ArtifactFactoryRequest.class);
String classifier = request.getClassifier();
String extension = request.getExtension();
String type = request.getType();
if (classifier == null) {
classifier = "";
}
if (extension == null) {
extension = type != null ? type : "";
}
return new ArtifactStub(
request.getGroupId(), request.getArtifactId(), classifier, request.getVersion(), extension);
});
when(artifactFactory.createProduced(any())).then(iom -> {
ArtifactFactoryRequest request = iom.getArgument(0, ArtifactFactoryRequest.class);
String classifier = request.getClassifier();
String extension = request.getExtension();
String type = request.getType();
if (classifier == null) {
classifier = "";
}
if (extension == null) {
extension = type != null ? type : "";
}
return new ProducedArtifactStub(
request.getGroupId(), request.getArtifactId(), classifier, request.getVersion(), extension);
});
when(session.createArtifact(any(), any(), any(), any(), any(), any())).thenAnswer(iom -> {
String groupId = iom.getArgument(0, String.class);
String artifactId = iom.getArgument(1, String.class);
String version = iom.getArgument(2, String.class);
String classifier = iom.getArgument(3, String.class);
String extension = iom.getArgument(4, String.class);
String type = iom.getArgument(5, String.class);
return session.getService(ArtifactFactory.class)
.create(ArtifactFactoryRequest.builder()
.session(session)
.groupId(groupId)
.artifactId(artifactId)
.version(version)
.classifier(classifier)
.extension(extension)
.type(type)
.build());
});
when(session.createArtifact(any(), any(), any(), any())).thenAnswer(iom -> {
String groupId = iom.getArgument(0, String.class);
String artifactId = iom.getArgument(1, String.class);
String version = iom.getArgument(2, String.class);
String extension = iom.getArgument(3, String.class);
return session.getService(ArtifactFactory.class)
.create(ArtifactFactoryRequest.builder()
.session(session)
.groupId(groupId)
.artifactId(artifactId)
.version(version)
.extension(extension)
.build());
});
when(session.createProducedArtifact(any(), any(), any(), any(), any(), any()))
.thenAnswer(iom -> {
String groupId = iom.getArgument(0, String.class);
String artifactId = iom.getArgument(1, String.class);
String version = iom.getArgument(2, String.class);
String classifier = iom.getArgument(3, String.class);
String extension = iom.getArgument(4, String.class);
String type = iom.getArgument(5, String.class);
return session.getService(ArtifactFactory.class)
.createProduced(ArtifactFactoryRequest.builder()
.session(session)
.groupId(groupId)
.artifactId(artifactId)
.version(version)
.classifier(classifier)
.extension(extension)
.type(type)
.build());
});
when(session.createProducedArtifact(any(), any(), any(), any())).thenAnswer(iom -> {
String groupId = iom.getArgument(0, String.class);
String artifactId = iom.getArgument(1, String.class);
String version = iom.getArgument(2, String.class);
String extension = iom.getArgument(3, String.class);
return session.getService(ArtifactFactory.class)
.createProduced(ArtifactFactoryRequest.builder()
.session(session)
.groupId(groupId)
.artifactId(artifactId)
.version(version)
.extension(extension)
.build());
});
when(session.getService(ArtifactFactory.class)).thenReturn(artifactFactory);
//
// ProjectBuilder
//
ProjectBuilder projectBuilder = mock(ProjectBuilder.class);
when(projectBuilder.build(any(ProjectBuilderRequest.class))).then(iom -> {
ProjectBuilderRequest request = iom.getArgument(0, ProjectBuilderRequest.class);
ProjectBuilderResult result = mock(ProjectBuilderResult.class);
Model model = new MavenStaxReader().read(request.getSource().get().openStream());
ProjectStub projectStub = new ProjectStub();
projectStub.setModel(model);
ProducedArtifactStub artifactStub = new ProducedArtifactStub(
model.getGroupId(), model.getArtifactId(), "", model.getVersion(), model.getPackaging());
if (!"pom".equals(model.getPackaging())) {
projectStub.setMainArtifact(artifactStub);
}
when(result.getProject()).thenReturn(Optional.of(projectStub));
return result;
});
when(session.getService(ProjectBuilder.class)).thenReturn(projectBuilder);
//
// ModelXmlFactory
//
when(session.getService(ModelXmlFactory.class)).thenReturn(new DefaultModelXmlFactory());
//
// Lookup
//
when(session.getService(Lookup.class)).thenReturn(LookupStub.EMPTY);
//
// Other
//
Properties sysProps = new Properties();
Properties usrProps = new Properties();
doReturn(sysProps).when(session).getSystemProperties();
doReturn(usrProps).when(session).getUserProperties();
when(session.getLocalRepository()).thenReturn(localRepository);
when(session.getData()).thenReturn(new TestSessionData());
when(session.withLocalRepository(any()))
.thenAnswer(iom -> getMockSession(iom.getArgument(0, LocalRepository.class)));
return session;
}