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

Code before:
    Path jarFile = Paths.get(Resources.getResource("jarTest/jarWithCp.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("--target", "docker://jib-cli-image", "jar", jarFile.toString());
    String output = new Command("docker", "run", "--rm", "jib-cli-image").run();

    assertThat(exitCode).isEqualTo(0);
    assertThat(output).isEqualTo("HelloWorld");
  }

  @Test
  public void testJar_packagedMode_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarFile = Paths.get(Resources.getResource("jarTest/jarWithCp.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute(
                "--target", "docker://jib-cli-image", "jar", jarFile.toString(), "--mode=packaged");
    String output = new Command("docker", "run", "--rm", "jib-cli-image").run();

    assertThat(exitCode).isEqualTo(0);
    assertThat(output).isEqualTo("HelloWorld");
  }

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

    Path jarFile = Paths.get(Resources.getResource("jarTest/jarWithCp.jar").toURI());
    Integer exitCode =
        jibCli.execute(
            "--target", "docker://jib-cli-image", "jar", jarFile.toString(), "--mode=unknown");

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


Review comment (suggestion):
```suggestion
  public void testJar_unknownMode() throws URISyntaxException {
```

Code after:
  public void testJar_explodedMode_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("jarTest/jarWithCp.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("--target", "docker://exploded-jar", "jar", jarPath.toString());
    String output = new Command("docker", "run", "--rm", "exploded-jar").run();
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      String classPath =
          jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);

      assertThat(classPath).isEqualTo("dependency1.jar directory/dependency2.jar");
      assertThat(exitCode).isEqualTo(0);
      assertThat(output).isEqualTo("Hello World");
    }
  }

  @Test
  public void testNoDependencyJar_explodedMode_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("noDependencyJar.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("--target", "docker://exploded-no-dep-jar", "jar", jarPath.toString());
    String output = new Command("docker", "run", "--rm", "exploded-no-dep-jar").run();
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      String classPath =
          jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);

      assertThat(classPath).isNull();
      assertThat(exitCode).isEqualTo(0);
      assertThat(output).isEqualTo("Hello World");
    }
  }

  @Test
  public void testJar_packagedMode_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("jarTest/jarWithCp.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute(
                "--target", "docker://packaged-jar", "jar", jarPath.toString(), "--mode=packaged");
    String output = new Command("docker", "run", "--rm", "packaged-jar").run();
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      String classPath =
          jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);

      assertThat(classPath).isEqualTo("dependency1.jar directory/dependency2.jar");
      assertThat(exitCode).isEqualTo(0);
      assertThat(output).isEqualTo("Hello World");