Repository: GoogleContainerTools/jib
File: jib-core/src/test/java/com/google/cloud/tools/jib/tar/TarStreamBuilderTest.java (Java)

Code before:

  @Test
  public void testToBlob_tarArchiveEntriesWithCompression() throws IOException {
    setUpWithTarEntries();
    verifyBlobWithCompression();
  }

  @Test
  public void testToBlob_stringsWithCompression() throws IOException {
    setUpWithStrings();
    verifyBlobWithCompression();
  }

  @Test
  public void testToBlob_stringsAndTarArchiveEntriesWithCompression() throws IOException {
    setUpWithStringsAndTarEntries();
    verifyBlobWithCompression();
  }

  @Test
  public void testToBlob_multiByte() throws IOException {
    testTarStreamBuilder.addByteEntry("日本語".getBytes(StandardCharsets.UTF_8), "test", creationTime);
    testTarStreamBuilder.addByteEntry(
        "asdf".getBytes(StandardCharsets.UTF_8), "crepecake", creationTime);
    testTarStreamBuilder.addBlobEntry(
        Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib", creationTime);

    // Writes the BLOB and captures the output.
    ByteArrayOutputStream tarByteOutputStream = new ByteArrayOutputStream();
    OutputStream compressorStream = new GZIPOutputStream(tarByteOutputStream);
    testTarStreamBuilder.writeAsTarArchiveTo(compressorStream);

    // Rearrange the output into input for verification.
    ByteArrayInputStream byteArrayInputStream =
        new ByteArrayInputStream(tarByteOutputStream.toByteArray());
    InputStream tarByteInputStream = new GZIPInputStream(byteArrayInputStream);
    TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(tarByteInputStream);

    // Verify multi-byte characters are written/read correctly
    TarArchiveEntry headerFile = tarArchiveInputStream.getNextTarEntry();
    Assert.assertEquals("test", headerFile.getName());
    Assert.assertEquals(
        "日本語", new String(ByteStreams.toByteArray(tarArchiveInputStream), StandardCharsets.UTF_8));

    headerFile = tarArchiveInputStream.getNextTarEntry();
    Assert.assertEquals("crepecake", headerFile.getName());
    Assert.assertEquals(
        "asdf", new String(ByteStreams.toByteArray(tarArchiveInputStream), StandardCharsets.UTF_8));

    headerFile = tarArchiveInputStream.getNextTarEntry();
    Assert.assertEquals("jib", headerFile.getName());

Review comment (suggestion):
In tests, just do
```suggestion
        Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib", Instant.EPOCH);
```

Code after:
  }

  @Test
  public void testToBlob_tarArchiveEntriesWithCompression() throws IOException {
    setUpWithTarEntries();
    verifyBlobWithCompression();
  }

  @Test
  public void testToBlob_stringsWithCompression() throws IOException {
    setUpWithStrings();
    verifyBlobWithCompression();
  }

  @Test
  public void testToBlob_stringsAndTarArchiveEntriesWithCompression() throws IOException {
    setUpWithStringsAndTarEntries();
    verifyBlobWithCompression();
  }

  @Test
  public void testToBlob_multiByte() throws IOException {
    Instant modificationTime = Instant.ofEpochMilli(1618041179516L);
    Instant timeFromTarArchiveEntry = modificationTime.truncatedTo(SECONDS);

    testTarStreamBuilder.addByteEntry(
        "日本語".getBytes(StandardCharsets.UTF_8), "test", modificationTime);
    testTarStreamBuilder.addByteEntry(
        "asdf".getBytes(StandardCharsets.UTF_8), "crepecake", modificationTime);
    testTarStreamBuilder.addBlobEntry(
        Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib", modificationTime);

    // Writes the BLOB and captures the output.
    ByteArrayOutputStream tarByteOutputStream = new ByteArrayOutputStream();
    OutputStream compressorStream = new GZIPOutputStream(tarByteOutputStream);
    testTarStreamBuilder.writeAsTarArchiveTo(compressorStream);

    // Rearrange the output into input for verification.
    ByteArrayInputStream byteArrayInputStream =
        new ByteArrayInputStream(tarByteOutputStream.toByteArray());
    InputStream tarByteInputStream = new GZIPInputStream(byteArrayInputStream);
    TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(tarByteInputStream);

    // Verify multi-byte characters are written/read correctly
    TarArchiveEntry headerFile = tarArchiveInputStream.getNextTarEntry();
    Assert.assertEquals("test", headerFile.getName());
    Assert.assertEquals(
        "日本語", new String(ByteStreams.toByteArray(tarArchiveInputStream), StandardCharsets.UTF_8));

    headerFile = tarArchiveInputStream.getNextTarEntry();
    Assert.assertEquals("crepecake", headerFile.getName());