Repository: GoogleContainerTools/jib
File: jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/MavenProjectPropertiesExtensionTest.java (Java)

Code before:
            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"));
  }
}

Review comment (suggestion):
```diff
     FooExtension loadedExtension =
         new FooExtension(
             (buildPlan, properties, extraConfig, mavenData, logger) -> {
-               return buildPlan.toBuilder().setUser("user from extension").build();
+               return buildPlan.toBuilder().setBaseImage("my base image").build();
```
and verify that the base image hasn't changed.
```java
    Assert.assertEquals("scratch", extendedBuilder.toContainerBuildPlan().getBaseImage());
```
This rules out the case that `loadedExtension` runs first and then `injectExtension`, which makes the test succeed otherwise.

Code after:
            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().setBaseImage("loadedExtBaseImage").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());
    Assert.assertEquals("scratch", extendedBuilder.toContainerBuildPlan().getBaseImage());

    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"));
  }