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

Code before:
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.cloud.tools.jib.cli.cli2;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.tools.jib.Command;
import com.google.common.io.Resources;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;
import picocli.CommandLine;

public class JarCommandTest {

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

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

    assertThat(exitCode).isEqualTo(1);
    assertThat(sw.toString())
        .isEqualTo("[ERROR] The file path provided does not exist: unknown.jar\n");
  }

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

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

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

Review comment (suggestion):
The Google style doesn't allow 'sw'. I think we can go for
```suggestion
    StringWriter stringWriter = new StringWriter();
```

Code after:
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.cloud.tools.jib.cli.cli2;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.tools.jib.Command;
import com.google.common.io.Resources;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;
import picocli.CommandLine;

public class JarCommandTest {

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

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

    assertThat(exitCode).isEqualTo(1);
    assertThat(stringWriter.toString())
        .isEqualTo("[ERROR] The file path provided does not exist: unknown.jar\n");
  }

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

    Path jarFile = Paths.get("/");
    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");