From 3f16c5486793c8fadbf5f198afb216dd31a7d6a2 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 11 Jun 2022 11:55:58 +0200 Subject: [PATCH] Create test util to write data to temp file --- .../test/java/org/pgpainless/cli/TestUtils.java | 12 ++++++++++++ .../cli/commands/GenerateCertCmdTest.java | 14 +++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pgpainless-cli/src/test/java/org/pgpainless/cli/TestUtils.java b/pgpainless-cli/src/test/java/org/pgpainless/cli/TestUtils.java index 16e1937c..8fca7381 100644 --- a/pgpainless-cli/src/test/java/org/pgpainless/cli/TestUtils.java +++ b/pgpainless-cli/src/test/java/org/pgpainless/cli/TestUtils.java @@ -11,6 +11,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -40,6 +41,17 @@ public class TestUtils { return dir; } + public static File writeTempFile(File tempDir, byte[] value) throws IOException { + File tempFile = new File(tempDir, randomString(10)); + tempFile.createNewFile(); + tempFile.deleteOnExit(); + FileOutputStream fileOutputStream = new FileOutputStream(tempFile); + fileOutputStream.write(value); + fileOutputStream.flush(); + fileOutputStream.close(); + return tempFile; + } + private static String randomString(int length) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { diff --git a/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/GenerateCertCmdTest.java b/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/GenerateCertCmdTest.java index 8534decb..63afc39f 100644 --- a/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/GenerateCertCmdTest.java +++ b/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/GenerateCertCmdTest.java @@ -11,12 +11,9 @@ import static org.pgpainless.cli.TestUtils.ARMOR_PRIVATE_KEY_HEADER_BYTES; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Arrays; import com.ginsberg.junit.exit.FailOnSystemExit; @@ -27,6 +24,7 @@ import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.cli.PGPainlessCLI; +import org.pgpainless.cli.TestUtils; import org.pgpainless.key.info.KeyInfo; import org.pgpainless.key.info.KeyRingInfo; import org.pgpainless.key.protection.UnlockSecretKey; @@ -61,14 +59,8 @@ public class GenerateCertCmdTest { PrintStream orig = System.out; try { // Write password to file - Path tempDir = Files.createTempDirectory("genkey"); - File passwordFile = new File(tempDir.toFile(), "password"); - passwordFile.createNewFile(); - passwordFile.deleteOnExit(); - FileOutputStream fileOut = new FileOutputStream(passwordFile); - fileOut.write("sw0rdf1sh".getBytes(StandardCharsets.UTF_8)); - fileOut.flush(); - fileOut.close(); + File tempDir = TestUtils.createTempDirectory(); + File passwordFile = TestUtils.writeTempFile(tempDir, "sw0rdf1sh".getBytes(StandardCharsets.UTF_8)); ByteArrayOutputStream out = new ByteArrayOutputStream(); System.setOut(new PrintStream(out));