Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanocke committed Feb 20, 2021
1 parent 0f49919 commit 6b6d340
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,20 @@ public static MavenProjectProperties getForProject(
Supplier<List<JibMavenPluginExtension<?>>> extensionLoader =
() -> {
List<JibMavenPluginExtension<?>> extensions = new ArrayList<>();
// Add the injected extensions at first to prefer them over the ones from JDK service
// loader.
// Extensions might support both approaches (injection and JDK service loader) at the same
// time for compatibility reasons.
extensions.addAll(injectedExtensions);
for (JibMavenPluginExtension<?> extension :
ServiceLoader.load(JibMavenPluginExtension.class)) {
extensions.add(extension);
}
return extensions;
};
return new MavenProjectProperties(
jibPluginDescriptor, project, session, log, tempDirectoryProvider, extensionLoader);
jibPluginDescriptor,
project,
session,
log,
tempDirectoryProvider,
injectedExtensions,
extensionLoader);
}

/**
Expand Down Expand Up @@ -221,6 +222,7 @@ static Optional<String> getChildValue(@Nullable Xpp3Dom dom, String... childNode
private final SingleThreadedExecutor singleThreadedExecutor = new SingleThreadedExecutor();
private final ConsoleLogger consoleLogger;
private final TempDirectoryProvider tempDirectoryProvider;
private final Collection<JibMavenPluginExtension<?>> injectedExtensions;
private final Supplier<List<JibMavenPluginExtension<?>>> extensionLoader;

@VisibleForTesting
Expand All @@ -230,11 +232,13 @@ static Optional<String> getChildValue(@Nullable Xpp3Dom dom, String... childNode
MavenSession session,
Log log,
TempDirectoryProvider tempDirectoryProvider,
Collection<JibMavenPluginExtension<?>> injectedExtensions,
Supplier<List<JibMavenPluginExtension<?>>> extensionLoader) {
this.jibPluginDescriptor = jibPluginDescriptor;
this.project = project;
this.session = session;
this.tempDirectoryProvider = tempDirectoryProvider;
this.injectedExtensions = injectedExtensions;
this.extensionLoader = extensionLoader;
ConsoleLoggerBuilder consoleLoggerBuilder =
(isProgressFooterEnabled(session)
Expand Down Expand Up @@ -605,7 +609,12 @@ public JibContainerBuilder runPluginExtensions(
return jibContainerBuilder;
}

List<JibMavenPluginExtension<?>> loadedExtensions = extensionLoader.get();
// Add the injected extensions at first to prefer them over the ones from JDK service
// loader.
// Extensions might support both approaches (injection and JDK service loader) at the same
// time for compatibility reasons.
List<JibMavenPluginExtension<?>> loadedExtensions = new ArrayList<>(injectedExtensions);
loadedExtensions.addAll(extensionLoader.get());
JibMavenPluginExtension<?> extension = null;
ContainerBuildPlan buildPlan = jibContainerBuilder.toContainerBuildPlan();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void testDefaults() {
Assert.assertEquals("exploded", testPluginConfiguration.getContainerizingMode());
Assert.assertEquals("EPOCH_PLUS_SECOND", testPluginConfiguration.getFilesModificationTime());
Assert.assertEquals("EPOCH", testPluginConfiguration.getCreationTime());
Assert.assertTrue(testPluginConfiguration.getInjectedPluginExtensions().isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public void setUp() {
mockMavenSession,
mockLog,
mockTempDirectoryProvider,
Collections.emptyList(),
() -> loadedExtensions);
}

Expand Down Expand Up @@ -450,4 +451,76 @@ public void testRunPluginExtensions_runtimeExceptionFromExtension() {
Assert.assertEquals("extension crashed: buggy extension", ex.getMessage());
}
}

@Test
public void testRunPluginExtensions_injected() throws JibPluginExtensionException {
FooExtension injectedExtension =
new FooExtension(
(buildPlan, properties, extraConfig, mavenData, logger) -> {
logger.log(LogLevel.ERROR, "awesome error from my extension");
return buildPlan.toBuilder().setUser("user from extension").build();
});

mavenProjectProperties =
new MavenProjectProperties(
mockJibPluginDescriptor,
mockMavenProject,
mockMavenSession,
mockLog,
mockTempDirectoryProvider,
Arrays.asList(injectedExtension),
() -> Collections.emptyList());

JibContainerBuilder extendedBuilder =
mavenProjectProperties.runPluginExtensions(
Arrays.asList(new FooExtensionConfig()), containerBuilder);
Assert.assertEquals("user from extension", extendedBuilder.toContainerBuildPlan().getUser());

mavenProjectProperties.waitForLoggingThread();
Mockito.verify(mockLog).error("awesome error from my extension");
Mockito.verify(mockLog)
.info(
Mockito.startsWith(
"Running extension: com.google.cloud.tools.jib.maven.MavenProjectProperties"));
}

@Test
public void testRunPluginExtensions_preferInjectionOverServiceLoader()
throws JibPluginExtensionException {
FooExtension injectedExtension =
new FooExtension(
(buildPlan, properties, extraConfig, mavenData, logger) -> {
logger.log(LogLevel.ERROR, "awesome error from my extension");
return buildPlan.toBuilder().setUser("user from injected extension").build();
});

FooExtension loadedExtension =
new FooExtension(
(buildPlan, properties, extraConfig, mavenData, logger) -> {
return buildPlan.toBuilder().setUser("user from extension").build();
});

mavenProjectProperties =
new MavenProjectProperties(
mockJibPluginDescriptor,
mockMavenProject,
mockMavenSession,
mockLog,
mockTempDirectoryProvider,
Arrays.asList(injectedExtension),
() -> Arrays.asList(loadedExtension));

JibContainerBuilder extendedBuilder =
mavenProjectProperties.runPluginExtensions(
Arrays.asList(new FooExtensionConfig()), containerBuilder);
Assert.assertEquals(
"user from injected extension", extendedBuilder.toContainerBuildPlan().getUser());

mavenProjectProperties.waitForLoggingThread();
Mockito.verify(mockLog).error("awesome error from my extension");
Mockito.verify(mockLog)
.info(
Mockito.startsWith(
"Running extension: com.google.cloud.tools.jib.maven.MavenProjectProperties"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ public void setUp() throws IOException, URISyntaxException {
mockMavenSession,
mockLog,
mockTempDirectoryProvider,
Collections.emptyList(),
mockExtensionLoader);

Path outputPath = getResource("maven/application/output");
Expand Down

0 comments on commit 6b6d340

Please sign in to comment.