Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.IntStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.file.AbstractTempDirTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -42,7 +41,7 @@
/**
* Tests {@code DeferredFileOutputStream}.
*/
public class DeferredFileOutputStreamTest {
public class DeferredFileOutputStreamTest extends AbstractTempDirTest {

public static IntStream data() {
return IntStream.of(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096);
Expand All @@ -65,28 +64,21 @@ public static IntStream data() {
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
public void testAboveThreshold(final int initialBufferSize) throws IOException {
final File testFile = new File("testAboveThreshold.dat");
final File testFile = Files.createTempFile(tempDirPath, "testAboveThreshold", "dat").toFile();

// Ensure that the test starts from a clean base.
testFile.delete();

// @formatter:off
final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
try (final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
.setThreshold(testBytes.length - 5)
.setBufferSize(initialBufferSize)
.setOutputFile(testFile)
.get();
// @formatter:on
dfos.write(testBytes, 0, testBytes.length);
dfos.close();
assertFalse(dfos.isInMemory());
assertNull(dfos.getData());
assertEquals(testFile.length(), dfos.getByteCount());

verifyResultFile(testFile);

// Ensure that the test starts from a clean base.
testFile.delete();
.get()) {
dfos.write(testBytes, 0, testBytes.length);
dfos.close();
assertFalse(dfos.isInMemory());
assertNull(dfos.getData());
assertEquals(testFile.length(), dfos.getByteCount());

verifyResultFile(testFile);
}
}

/**
Expand All @@ -97,25 +89,24 @@ public void testAboveThreshold(final int initialBufferSize) throws IOException {
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
public void testAboveThresholdGetInputStream(final int initialBufferSize, final @TempDir Path tempDir) throws IOException {
final File testFile = tempDir.resolve("testAboveThreshold.dat").toFile();
final File testFile = Files.createTempFile(tempDirPath, "testAboveThreshold", "dat").toFile();

// @formatter:off
final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
try (final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
.setThreshold(testBytes.length - 5)
.setBufferSize(initialBufferSize)
.setOutputFile(testFile)
.get();
// @formatter:on
dfos.write(testBytes, 0, testBytes.length);
dfos.close();
assertFalse(dfos.isInMemory());
assertEquals(testFile.length(), dfos.getByteCount());
.get()) {
dfos.write(testBytes, 0, testBytes.length);
dfos.close();
assertFalse(dfos.isInMemory());
assertEquals(testFile.length(), dfos.getByteCount());

try (InputStream is = dfos.toInputStream()) {
assertArrayEquals(testBytes, IOUtils.toByteArray(is));
}
try (InputStream is = dfos.toInputStream()) {
assertArrayEquals(testBytes, IOUtils.toByteArray(is));
}

verifyResultFile(testFile);
verifyResultFile(testFile);
}
}

/**
Expand Down Expand Up @@ -196,15 +187,14 @@ public void testTempFileAboveThreshold(final int initialBufferSize) throws IOExc

final String prefix = "commons-io-test";
final String suffix = ".out";
final Path tempDir = Paths.get("target");
// @formatter:off
final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
.setThreshold(testBytes.length - 5)
.setBufferSize(initialBufferSize)
.setPrefix(prefix)
.setSuffix(suffix)
.setDirectory(tempDir)
.setDirectory(tempDir.toFile())
.setDirectory(tempDirFile)
.setDirectory(tempDirPath.toFile())
.get();
// @formatter:on
assertNull(dfos.getFile(), "Check File is null-A");
Expand All @@ -218,12 +208,9 @@ public void testTempFileAboveThreshold(final int initialBufferSize) throws IOExc
assertTrue(dfos.getFile().exists(), "Check file exists");
assertTrue(dfos.getFile().getName().startsWith(prefix), "Check prefix");
assertTrue(dfos.getFile().getName().endsWith(suffix), "Check suffix");
assertEquals(tempDir, dfos.getPath().getParent(), "Check dir");
assertEquals(tempDirPath, dfos.getPath().getParent(), "Check dir");

verifyResultFile(dfos.getFile());

// Delete the temporary file.
dfos.getFile().delete();
}

/**
Expand All @@ -236,32 +223,30 @@ public void testTempFileAboveThresholdPrefixOnly(final int initialBufferSize) th

final String prefix = "commons-io-test";
final String suffix = null;
final Path tempDir = null;
// @formatter:off
final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
.setThreshold(testBytes.length - 5)
.setBufferSize(initialBufferSize)
.setPrefix(prefix)
.setSuffix(suffix)
.setDirectory(tempDir)
.setDirectory((Path) null)
.get();
// @formatter:on
assertNull(dfos.getFile(), "Check File is null-A");
assertNull(dfos.getPath(), "Check Path is null-A");
dfos.write(testBytes, 0, testBytes.length);
dfos.close();
assertFalse(dfos.isInMemory());
assertNull(dfos.getData());
assertEquals(testBytes.length, dfos.getByteCount());
assertNotNull(dfos.getFile(), "Check file not null");
assertTrue(dfos.getFile().exists(), "Check file exists");
assertTrue(dfos.getFile().getName().startsWith(prefix), "Check prefix");
assertTrue(dfos.getFile().getName().endsWith(".tmp"), "Check suffix"); // ".tmp" is default

verifyResultFile(dfos.getFile());

// Delete the temporary file.
dfos.getFile().delete();
try {
assertNull(dfos.getFile(), "Check File is null-A");
assertNull(dfos.getPath(), "Check Path is null-A");
dfos.write(testBytes, 0, testBytes.length);
dfos.close();
assertFalse(dfos.isInMemory());
assertNull(dfos.getData());
assertEquals(testBytes.length, dfos.getByteCount());
assertNotNull(dfos.getFile(), "Check file not null");
assertTrue(dfos.getFile().exists(), "Check file exists");
assertTrue(dfos.getFile().getName().startsWith(prefix), "Check prefix");
assertTrue(dfos.getFile().getName().endsWith(".tmp"), "Check suffix"); // ".tmp" is default
verifyResultFile(dfos.getFile());
} finally {
// Delete the temporary file.
dfos.getFile().delete();
}
}

/**
Expand All @@ -274,8 +259,7 @@ public void testTempFileBelowThreshold(final int initialBufferSize) throws IOExc

final String prefix = "commons-io-test";
final String suffix = ".out";
final File tempDir = FileUtils.current();
final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, prefix, suffix, tempDir);
final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, prefix, suffix, tempDirFile);
assertNull(dfos.getFile(), "Check File is null-A");
assertNull(dfos.getPath(), "Check Path is null-A");
dfos.write(testBytes, 0, testBytes.length);
Expand All @@ -294,8 +278,7 @@ public void testTempFileBelowThreshold(final int initialBufferSize) throws IOExc
public void testTempFileError() throws Exception {
final String prefix = null;
final String suffix = ".out";
final File tempDir = FileUtils.current();
assertThrows(NullPointerException.class, () -> new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir));
assertThrows(NullPointerException.class, () -> new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDirFile));
}

/**
Expand All @@ -306,10 +289,7 @@ public void testTempFileError() throws Exception {
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
public void testThresholdReached(final int initialBufferSize) throws IOException {
final File testFile = new File("testThresholdReached.dat");

// Ensure that the test starts from a clean base.
testFile.delete();
final File testFile = Files.createTempFile(tempDirPath, "testThresholdReached", "dat").toFile();

// @formatter:off
final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
Expand All @@ -328,9 +308,6 @@ public void testThresholdReached(final int initialBufferSize) throws IOException
assertEquals(testBytes.length, dfos.getByteCount());

verifyResultFile(testFile);

// Ensure that the test starts from a clean base.
testFile.delete();
}

/**
Expand All @@ -339,10 +316,8 @@ public void testThresholdReached(final int initialBufferSize) throws IOException
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
public void testWriteToLarge(final int initialBufferSize) throws IOException {
final File testFile = new File("testWriteToFile.dat");
final File testFile = Files.createTempFile(tempDirPath, "testWriteToFile", "dat").toFile();
final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
// Ensure that the test starts from a clean base.
testFile.delete();

final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder().setThreshold(testBytes.length / 2).setOutputFile(testFile).get();
dfos.write(testBytes);
Expand All @@ -358,7 +333,6 @@ public void testWriteToLarge(final int initialBufferSize) throws IOException {
final byte[] copiedBytes = baos.toByteArray();
assertArrayEquals(testBytes, copiedBytes);
verifyResultFile(testFile);
testFile.delete();
}

/**
Expand All @@ -367,10 +341,8 @@ public void testWriteToLarge(final int initialBufferSize) throws IOException {
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
public void testWriteToLargeCtor(final int initialBufferSize) throws IOException {
final File testFile = new File("testWriteToFile.dat");
final File testFile = Files.createTempFile(tempDirPath, "testWriteToFile", "dat").toFile();
final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
// Ensure that the test starts from a clean base.
testFile.delete();

final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length / 2, testFile);
dfos.write(testBytes);
Expand All @@ -386,7 +358,6 @@ public void testWriteToLargeCtor(final int initialBufferSize) throws IOException
final byte[] copiedBytes = baos.toByteArray();
assertArrayEquals(testBytes, copiedBytes);
verifyResultFile(testFile);
testFile.delete();
}

/**
Expand All @@ -396,15 +367,12 @@ public void testWriteToLargeCtor(final int initialBufferSize) throws IOException
@ParameterizedTest(name = "initialBufferSize = {0}")
@MethodSource("data")
public void testWriteToSmall(final int initialBufferSize) throws IOException {
final File testFile = new File("testWriteToMem.dat");
final File testFile = Files.createTempFile(tempDirPath, "testWriteToMem", "dat").toFile();
final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
// Ensure that the test starts from a clean base.
testFile.delete();

final DeferredFileOutputStream dfos = new DeferredFileOutputStream(testBytes.length * 2, initialBufferSize, testFile);
dfos.write(testBytes);

assertFalse(testFile.exists());
assertTrue(dfos.isInMemory());

assertThrows(IOException.class, () -> dfos.writeTo(baos));
Expand All @@ -414,8 +382,6 @@ public void testWriteToSmall(final int initialBufferSize) throws IOException {
dfos.writeTo(baos);
final byte[] copiedBytes = baos.toByteArray();
assertArrayEquals(testBytes, copiedBytes);

testFile.delete();
}

/**
Expand Down