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

Code before:
  public void testErrorLogging_directoryGiven() {
    StringWriter stringWriter = new StringWriter();
    CommandLine jibCli = new CommandLine(new JibCli()).setErr(new PrintWriter(stringWriter));

    Path jarFile = Paths.get("/");
    Integer exitCode =
        jibCli.execute("jar", "--target", "docker://jib-cli-image", 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: "
                + jarFile.toString()
                + "\n");
  }

  @Test
  public void testStandardJar_explodedMode_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("jarTest/standard/jarWithCp.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute(
                "jar",
                "--target",
                "docker://" + dockerHost + ":5000/exploded-jar",
                "--from=gcr.io/google-appengine/openjdk:8",
                jarPath.toString());
    try {
      new Command("docker", "run", "--rm", dockerHost + ":5000/exploded-jar", "--network=host")
          .run();
      assertThat(exitCode).isEqualTo(0);
    } catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
    }

    // 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 testNoDependencyStandardJar_explodedMode_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("jarTest/standard/noDependencyJar.jar").toURI());

Review comment (performance):
Hmm... I have a doubt. Since it's directly pushing to a Docker daemon, I think the name shouldn't matter. Also, `docker run` isn't supposed to make a network connection, since the image should already exist in the daemon cache. But I'm not sure what's going on.

Code after:
  public void testErrorLogging_directoryGiven() {
    StringWriter stringWriter = new StringWriter();
    CommandLine jibCli = new CommandLine(new JibCli()).setErr(new PrintWriter(stringWriter));

    Path jarFile = Paths.get("/");
    Integer exitCode =
        jibCli.execute("jar", "--target", "docker://jib-cli-image", 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: "
                + jarFile.toString()
                + "\n");
  }

  @Test
  public void testStandardJar_explodedMode_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("jarTest/standard/jarWithCp.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute(
                "jar",
                "--from",
                "eclipse-temurin:8-jdk-focal",
                "--target",
                "docker://exploded-jar",
                jarPath.toString());
    String output =
        new Command("docker", "run", "--rm", "exploded-jar", "--privileged", "--network=host")
            .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 testNoDependencyStandardJar_explodedMode_toDocker()
      throws IOException, InterruptedException, URISyntaxException {
    Path jarPath = Paths.get(Resources.getResource("jarTest/standard/noDependencyJar.jar").toURI());
    Integer exitCode =
        new CommandLine(new JibCli())
            .execute(
                "jar",