Repository: GoogleContainerTools/jib
File: jib-cli/src/integration-test/java/com/google/cloud/tools/jib/cli/cli2/JarCommandTest.java (Java)

Code before:
    assertThat(exitCode).isEqualTo(2);
    assertThat(stringWriter.toString())
        .contains(
            "Invalid value for option '--mode': expected one of [exploded, packaged] (case-sensitive) but was 'unknown'");
  }

  @Test
  public void testSpringBootLayeredJar_explodedMode() throws IOException, InterruptedException {
    springBootProjectLayered.build("clean", "bootJar");
    Path jarParentPath =
        springBootProjectLayered.getProjectRoot().toAbsolutePath().resolve("build").resolve("libs");
    Path jarPath = jarParentPath.resolve("spring-boot-layered.jar");

    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("jar", "--target", "docker://spring-boot-jar-layered", jarPath.toString());
    String output =
        new Command("docker", "run", "--rm", "--detach", "-p8080:8080", "spring-boot-jar-layered")
            .run();

    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      assertThat(jarFile.getEntry("BOOT-INF/layers.idx")).isNotNull();
      assertThat(getContent(new URL("http://localhost:8080"))).isEqualTo("Hello world");
      assertThat(exitCode).isEqualTo(0);

      new Command("docker", "stop", output.trim()).run();
    }
  }

  @Test
  public void testSpringBootNonLayeredJar_explodedMode() throws IOException, InterruptedException {
    springBootProjectNonLayered.build("clean", "bootJar");
    Path jarParentPath =
        springBootProjectNonLayered
            .getProjectRoot()
            .toAbsolutePath()
            .resolve("build")
            .resolve("libs");
    Path jarPath = jarParentPath.resolve("spring-boot-nonlayered.jar");

    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("jar", "--target", "docker://spring-boot-jar", jarPath.toString());
    String output =
        new Command("docker", "run", "--rm", "--detach", "-p8080:8080", "spring-boot-jar").run();

    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      assertThat(jarFile.getEntry("BOOT-INF/layers.idx")).isNull();
      assertThat(getContent(new URL("http://localhost:8080"))).isEqualTo("Hello world");
      assertThat(exitCode).isEqualTo(0);


Review comment (bug):
This will not run if one of the assertions above fail or some code throws an exception. Put it in `finally`, or `@After public tearDown()` as done in other tests.

Code after:

  @Test
  public void testJar_unknownMode() {
    StringWriter stringWriter = new StringWriter();
    CommandLine jibCli = new CommandLine(new JibCli()).setErr(new PrintWriter(stringWriter));

    Integer exitCode =
        jibCli.execute(
            "jar", "--target", "docker://jib-cli-image", "ignored.jar", "--mode=unknown");

    assertThat(exitCode).isEqualTo(2);
    assertThat(stringWriter.toString())
        .contains(
            "Invalid value for option '--mode': expected one of [exploded, packaged] (case-sensitive) but was 'unknown'");
  }

  @Test
  public void testSpringBootLayeredJar_explodedMode() throws IOException, InterruptedException {
    springBootProjectLayered.build("clean", "bootJar");
    Path jarParentPath = springBootProjectLayered.getProjectRoot().resolve("build").resolve("libs");
    Path jarPath = jarParentPath.resolve("spring-boot-layered.jar");

    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("jar", "--target", "docker://spring-boot-jar-layered", jarPath.toString());
    assertThat(exitCode).isEqualTo(0);

    String output =
        new Command("docker", "run", "--rm", "--detach", "-p8080:8080", "spring-boot-jar-layered")
            .run();
    containerName = output.trim();
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {

      assertThat(jarFile.getEntry("BOOT-INF/layers.idx")).isNotNull();
      assertThat(getContent(new URL("http://localhost:8080"))).isEqualTo("Hello world");
    }
  }

  @Test
  public void testSpringBootNonLayeredJar_explodedMode() throws IOException, InterruptedException {
    springBootProjectNonLayered.build("clean", "bootJar");
    Path jarParentPath =
        springBootProjectNonLayered.getProjectRoot().resolve("build").resolve("libs");
    Path jarPath = jarParentPath.resolve("spring-boot-nonlayered.jar");

    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("jar", "--target", "docker://spring-boot-jar", jarPath.toString());
    assertThat(exitCode).isEqualTo(0);

    String output =