MavenInvokerRequester extends IArtifactRequester { @Override public Optional<File> requestFile(Coordinate mavenCoordinate, Path targetDirectory, ClassifierInformation classifierInformation) { File expectedJarFile = getExpectedJarFile(mavenCoordinate, targetDirectory, classifierInformation); if (expectedJarFile.exists()) { LOGGER.debug("The file " + expectedJarFile + " already exists and won't be downloaded again"); return Optional.of(expectedJarFile); } LOGGER.debug("Requesting artifact with id " + mavenCoordinate.toString()); boolean requestSuccessful = callMavenInvoker(mavenCoordinate, targetDirectory, classifierInformation.classifier); String jarType = classifierInformation.isSource ? "sources jar" : classifierInformation.classifier + " jar"; if (!requestSuccessful) { LOGGER.warn("Failed to find " + jarType + ": Artifact " + mavenCoordinate.toString() + " not found in repo."); return Optional.empty(); } else if (!expectedJarFile.exists()) { LOGGER.warn("Failed to find " + jarType + ": Maven call succeeded but Artifact was not generated in the expected place."); return Optional.empty(); } return Optional.of(getExpectedJarFile(mavenCoordinate, targetDirectory, classifierInformation)); } MavenInvokerRequester(File basedir);  MavenInvokerRequester(File basedir, URL sourceRepositoryUrl);  MavenInvokerRequester(File basedir, DefaultInvoker defaultInvoker, Optional<URL> sourceRepositoryUrl); @Override Optional<File> requestFile(Coordinate mavenCoordinate, Path targetDirectory, ClassifierInformation classifierInformation); static final String POM_FILENAME; }
@Test public void requestFileTestThatRequestIsComposedCorrectly() throws Exception { Path targetDirectory = temporaryFolder.newFolder("target").toPath(); final String expectedJarBaseName = mir.getExpectedJarBaseName(mavenCoordinate, ClassifierInformation.DEFAULT_JAR); File expectedJarFile = new File(targetDirectory.toFile(), expectedJarBaseName); Mockito.when(defaultInvokerMock.execute(ArgumentMatchers.any(InvocationRequest.class))) .then((Answer<InvocationResult>) invocationOnMock -> { new FileOutputStream(expectedJarFile).close(); return getDummyInvocationResult(0); }); Optional<File> resultFile = mir.requestFile(mavenCoordinate, targetDirectory, ClassifierInformation.DEFAULT_JAR); Mockito.verify(defaultInvokerMock).execute(captor.capture()); assertThat(resultFile.get()).isEqualTo(expectedJarFile); InvocationRequest invocationRequest = captor.getValue(); Collection<String> goals = invocationRequest.getGoals(); assertThat(goals).isNotEmpty(); assertThat(goals).filteredOn(s -> s.contains(mavenCoordinate.getNamespace())).hasSize(1); assertThat(goals).filteredOn(s -> s.contains(mavenCoordinate.getName())).hasSize(1); assertThat(goals).filteredOn(s -> s.contains(mavenCoordinate.getVersion())).hasSize(1); assertThat(goals).filteredOn(s -> s.contains(targetDirectory.toString())).hasSize(1); }
@Test public void requestFileRecognizesNonExistingArtifactReturningAnEmptyOptional() throws IOException, MavenInvocationException { Path targetDirectory = temporaryFolder.newFolder("target").toPath(); Mockito.when(defaultInvokerMock.execute(ArgumentMatchers.any(InvocationRequest.class))) .thenReturn(getDummyInvocationResult(0)); Optional<File> requestResult = mir.requestFile(mavenCoordinate, targetDirectory, ClassifierInformation.DEFAULT_JAR); assertThat(requestResult).isEmpty(); }
@Test public void requestFileOnNonzeroReturnCodeReturnsEmptyOptional() throws IOException, MavenInvocationException { Path targetDirectory = temporaryFolder.newFolder("target").toPath(); final String expectedJarBaseName = mir.getExpectedJarBaseName(mavenCoordinate, ClassifierInformation.DEFAULT_JAR); File expectedJarFile = new File(targetDirectory.toFile(), expectedJarBaseName); Mockito.when(defaultInvokerMock.execute(ArgumentMatchers.any(InvocationRequest.class))) .then((Answer<InvocationResult>) invocationOnMock -> { new FileOutputStream(expectedJarFile).close(); return getDummyInvocationResult(1); }); Optional<File> requestResult = mir.requestFile(mavenCoordinate, targetDirectory, ClassifierInformation.DEFAULT_JAR); assertThat(requestResult).isEmpty(); }