package thredds.filesystem;

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

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class TestMFileOS {

  @Parameterized.Parameters(name = "{0}")
  static public List<Integer> getTestParameters() {
    return Arrays.asList(0, 1, 60000, 100000);
  }

  @Parameterized.Parameter()
  public int expectedSize;

  @Test
  public void shouldWriteFileToStream() throws IOException {
    final File file = createTemporaryFile(expectedSize);
    final MFileOS mFile = new MFileOS(file);
    final long length = mFile.getLength();
    assertThat(length).isEqualTo(expectedSize);

    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    mFile.writeToStream(outputStream);
    assertThat(outputStream.size()).isEqualTo(expectedSize);
    assertThat(outputStream.toByteArray()).isEqualTo(Files.readAllBytes(file.toPath()));
  }

  private File createTemporaryFile(int size) throws IOException {
    final File tempFile = File.createTempFile("TestMFileOS-", ".tmp");
    tempFile.deleteOnExit();

    byte[] bytes = new byte[size];
    new Random().nextBytes(bytes);
    Files.write(tempFile.toPath(), bytes);

    return tempFile;
  }
}

@@ -1158,16 +1158,888 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void shouldWritePartialFileToStream() throws IOException %7B%0A    final File file = createTemporaryFile(expectedSize);%0A    final MFileOS mFile = new MFileOS(file);%0A    final long length = mFile.getLength();%0A    assertThat(length).isEqualTo(expectedSize);%0A%0A    final int offset = 42;%0A    final int maxBytes = 100;%0A%0A    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();%0A    mFile.writeToStream(outputStream, offset, maxBytes);%0A%0A    final int startPosition = Math.min(offset, expectedSize);%0A    final int endPosition = Math.min(offset + maxBytes, expectedSize);%0A%0A    assertThat(outputStream.size()).isEqualTo(Math.max(0, endPosition - startPosition));%0A%0A    final byte%5B%5D partialFile = Arrays.copyOfRange(Files.readAllBytes(file.toPath()), startPosition, endPosition);%0A    assertThat(outputStream.toByteArray()).isEqualTo(partialFile);%0A  %7D%0A%0A
   privat

Add test for writing byte range to stream