<commit_before>/*
 * Copyright 2012-2015 Johns Hopkins University HLTCOE. All rights reserved.
 * See LICENSE in the project root directory.
 */
package acute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.GZIPOutputStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import edu.jhu.hlt.acute.archivers.tar.TarArchiver;
import edu.jhu.hlt.acute.iterators.tar.TarArchiveEntryByteIterator;
import edu.jhu.hlt.acute.iterators.tar.TarGzArchiveEntryByteIterator;

public class TarAPITest {

  String sOne = "foo.string";
  String sTwo = "qux.string";
  StringArchivable saOne = new StringArchivable(sOne);
  StringArchivable saTwo = new StringArchivable(sTwo);

  Path outPath;

  @Rule
  public TemporaryFolder tf = new TemporaryFolder();

  @Before
  public void setUp() throws Exception {
    outPath = tf.getRoot().toPath().resolve("test.tar");
  }

  @After
  public void tearDown() throws Exception {
  }

  @Test
  public void tarAPI () throws Exception {
    try (OutputStream os = Files.newOutputStream(outPath);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        TarArchiver archiver = new TarArchiver(bos);) {
      archiver.addEntry(saOne);
      archiver.addEntry(saTwo);
    }

    try (InputStream is = Files.newInputStream(outPath);
        BufferedInputStream bis = new BufferedInputStream(is);
        TarArchiveEntryByteIterator iter = new TarArchiveEntryByteIterator(bis);) {
      assertTrue(iter.hasNext());
      assertTrue(iter.hasNext());
      byte[] baOne = iter.next();
      assertEquals(sOne, new String(baOne));
      assertTrue(iter.hasNext());
      assertTrue(iter.hasNext());
      byte[] baTwo = iter.next();
      assertEquals(sTwo, new String(baTwo));
      assertFalse(iter.hasNext());
    }
  }

  @Test
  public void gzAPI () throws Exception {
    try (OutputStream os = Files.newOutputStream(outPath);
        GZIPOutputStream gos = new GZIPOutputStream(os, 65536);
        TarArchiver archiver = new TarArchiver(gos);) {
      archiver.addEntry(saOne);
      archiver.addEntry(saTwo);
    }

    try (InputStream is = Files.newInputStream(outPath);
        TarGzArchiveEntryByteIterator iter = new TarGzArchiveEntryByteIterator(is);) {
      assertTrue(iter.hasNext());
      assertTrue(iter.hasNext());
      byte[] baOne = iter.next();
      assertEquals(sOne, new String(baOne));
      assertTrue(iter.hasNext());
      assertTrue(iter.hasNext());
      byte[] baTwo = iter.next();
      assertEquals(sTwo, new String(baTwo));<commit_msg>Fix up tests.<commit_after>/*
 * Copyright 2012-2015 Johns Hopkins University HLTCOE. All rights reserved.
 * See LICENSE in the project root directory.
 */
package acute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import edu.jhu.hlt.acute.archivers.tar.TarArchiver;
import edu.jhu.hlt.acute.iterators.tar.TarArchiveEntryByteIterator;
import edu.jhu.hlt.acute.iterators.tar.TarGzArchiveEntryByteIterator;

public class TarAPITest {

  String sOne = "foo.string";
  String sTwo = "qux.string";
  StringArchivable saOne = new StringArchivable(sOne);
  StringArchivable saTwo = new StringArchivable(sTwo);

  Path outPath;

  @Rule
  public TemporaryFolder tf = new TemporaryFolder();

  @Before
  public void setUp() throws Exception {
    outPath = tf.getRoot().toPath().resolve("test.tar");
  }

  @After
  public void tearDown() throws Exception {
  }

  @Test
  public void tarAPI () throws Exception {
    try (OutputStream os = Files.newOutputStream(outPath);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        TarArchiver archiver = new TarArchiver(bos);) {
      archiver.addEntry(saOne);
      archiver.addEntry(saTwo);
    }

    try (InputStream is = Files.newInputStream(outPath);
        BufferedInputStream bis = new BufferedInputStream(is);
        TarArchiveEntryByteIterator iter = new TarArchiveEntryByteIterator(bis);) {
      assertTrue(iter.hasNext());
      assertTrue(iter.hasNext());
      byte[] baOne = iter.next();
      assertEquals(sOne, new String(baOne));
      assertTrue(iter.hasNext());
      assertTrue(iter.hasNext());
      byte[] baTwo = iter.next();
      assertEquals(sTwo, new String(baTwo));
      assertFalse(iter.hasNext());
    }
  }

  @Test
  public void gzAPI () throws Exception {
    try (OutputStream os = Files.newOutputStream(outPath);
        GzipCompressorOutputStream gos = new GzipCompressorOutputStream(os);
        TarArchiver archiver = new TarArchiver(gos);) {
      archiver.addEntry(saOne);
      archiver.addEntry(saTwo);
    }

    try (InputStream is = Files.newInputStream(outPath);
        TarGzArchiveEntryByteIterator iter = new TarGzArchiveEntryByteIterator(is);) {
      assertTrue(iter.hasNext());
      assertTrue(iter.hasNext());
      byte[] baOne = iter.next();
      assertEquals(sOne, new String(baOne));
      assertTrue(iter.hasNext());
      assertTrue(iter.hasNext());
      byte[] baTwo = iter.next();
      assertEquals(sTwo, new String(baTwo));
{'fraction_non_alphanumeric': 0.11402606310013717, 'fraction_numerical': 0.00360082304526749, 'mean_word_length': 3.9516129032258065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1540426', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1752, 'n_words': 382}