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

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

Review comment (bug):
Nit: I'd put this before `docker run` so that you don't run `docker run` if `jib jar` fails for some reason.

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