diff --git a/src/test/java/org/elasticsearch/license/licensor/LicenseGenerationToolTests.java b/src/test/java/org/elasticsearch/license/licensor/LicenseGenerationToolTests.java index a6c5e0b2b91..642c0ee21cf 100644 --- a/src/test/java/org/elasticsearch/license/licensor/LicenseGenerationToolTests.java +++ b/src/test/java/org/elasticsearch/license/licensor/LicenseGenerationToolTests.java @@ -11,12 +11,12 @@ import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.licensor.tools.LicenseGeneratorTool; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.FileOutputStream; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.Set; @@ -31,6 +31,9 @@ import static org.junit.Assert.fail; public class LicenseGenerationToolTests extends AbstractLicensingTestBase { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Test public void testSimple() throws Exception { LicenseSpec inputLicenseSpec = generateRandomLicenseSpec(); @@ -59,10 +62,8 @@ public class LicenseGenerationToolTests extends AbstractLicensingTestBase { public void testWithLicenseFile() throws Exception { LicenseSpec inputLicenseSpec = generateRandomLicenseSpec(); - Path tempFilePath = Files.createTempFile("license_spec", "json"); - File tempFile = tempFilePath.toFile(); + File tempFile = temporaryFolder.newFile("license_spec.json"); FileUtils.write(tempFile, generateESLicenseSpecString(Arrays.asList(inputLicenseSpec))); - tempFile.deleteOnExit(); String[] args = new String[6]; args[0] = "--licenseFile"; @@ -124,9 +125,8 @@ public class LicenseGenerationToolTests extends AbstractLicensingTestBase { } } - private static String runLicenseGenerationTool(String[] args) throws Exception { - File temp = File.createTempFile("temp", ".out"); - temp.deleteOnExit(); + private String runLicenseGenerationTool(String[] args) throws Exception { + File temp = temporaryFolder.newFile("license_generator.out"); try (FileOutputStream outputStream = new FileOutputStream(temp)) { LicenseGeneratorTool.run(args, outputStream); } diff --git a/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationToolTests.java b/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationToolTests.java index 677153a6e8c..159ccd80afd 100644 --- a/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationToolTests.java +++ b/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationToolTests.java @@ -16,13 +16,13 @@ import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.licensor.tools.LicenseVerificationTool; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.*; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween; @@ -34,6 +34,9 @@ import static org.junit.Assert.fail; public class LicenseVerificationToolTests extends AbstractLicensingTestBase { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Test public void testMissingCLTArgs() throws Exception { ESLicense singedLicense = generateSignedLicense(randomRealisticUnicodeOfCodepointLengthBetween(5, 15), @@ -114,9 +117,7 @@ public class LicenseVerificationToolTests extends AbstractLicensingTestBase { } private String dumpLicense(ESLicense license) throws Exception { - Path tempFilePath = Files.createTempFile("license_spec", "json"); - File tempFile = tempFilePath.toFile(); - tempFile.deleteOnExit(); + File tempFile = temporaryFolder.newFile("licenses.json"); try (FileOutputStream outputStream = new FileOutputStream(tempFile)) { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, outputStream); ESLicenses.toXContent(Collections.singletonList(license), builder, ToXContent.EMPTY_PARAMS); @@ -126,12 +127,11 @@ public class LicenseVerificationToolTests extends AbstractLicensingTestBase { } - private static String runLicenseVerificationTool(String[] args) throws IOException { - File temp = File.createTempFile("temp", ".out"); - temp.deleteOnExit(); - try (FileOutputStream outputStream = new FileOutputStream(temp)) { + private String runLicenseVerificationTool(String[] args) throws IOException { + File tempFile = temporaryFolder.newFile("licence_verification.out"); + try (FileOutputStream outputStream = new FileOutputStream(tempFile)) { LicenseVerificationTool.run(args, outputStream); } - return FileUtils.readFileToString(temp); + return FileUtils.readFileToString(tempFile); } }