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

Code before:
    Integer exitCode =
        jibCli.execute("--target", "docker://jib-cli-image", "jar", jarFile.toString());

    assertThat(exitCode).isEqualTo(1);
    assertThat(stringWriter.toString())
        .isEqualTo(
            "[ERROR] The file path provided is for a directory. Please provide a path to a jar file: "
                + jarFile.toString()
                + "\n");
  }

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

    // Validate classpath before checking output
    assertThat(classPath).isNotNull();
    List<String> dependencies = Splitter.onPattern("\\s+").splitToList(classPath.trim());
    assertThat(dependencies).containsExactly("dependency1.jar", "directory/dependency2.jar");

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


Review comment (suggestion):
No need for splitting. I think it can be `assertThat(classPath).isEqualTo("dependency1.jar;directory/dependency2.jar")`, even without checking not null.

And just double-check that the main in `simplJar` does use classes in both `dependency1.jar` and `dependency2.jar`. 

Code after:

    assertThat(exitCode).isEqualTo(1);
    assertThat(stringWriter.toString())
        .isEqualTo(
            "[ERROR] The file path provided is for a directory. Please provide a path to a jar file: "
                + jarFile.toString()
                + "\n");
  }

  @Test
  public void testJar_toDocker() throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("jarTest/jarWithCp.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("--target", "docker://jib-cli-image", "jar", jarPath.toString());
    String output = new Command("docker", "run", "--rm", "jib-cli-image").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_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("noDependencyJar.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute("--target", "docker://cli-no-dep-jar", "jar", jarPath.toString());
    String output = new Command("docker", "run", "--rm", "cli-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");
    }
  }
}