Use junit temporaryFolder instead of using deleteOnExit
Original commit: elastic/x-pack-elasticsearch@b437df66f5
This commit is contained in:
parent
9b30af3958
commit
3bf4d2659e
|
@ -11,12 +11,12 @@ import org.elasticsearch.license.TestUtils;
|
||||||
import org.elasticsearch.license.core.ESLicense;
|
import org.elasticsearch.license.core.ESLicense;
|
||||||
import org.elasticsearch.license.core.ESLicenses;
|
import org.elasticsearch.license.core.ESLicenses;
|
||||||
import org.elasticsearch.license.licensor.tools.LicenseGeneratorTool;
|
import org.elasticsearch.license.licensor.tools.LicenseGeneratorTool;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -31,6 +31,9 @@ import static org.junit.Assert.fail;
|
||||||
|
|
||||||
public class LicenseGenerationToolTests extends AbstractLicensingTestBase {
|
public class LicenseGenerationToolTests extends AbstractLicensingTestBase {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
LicenseSpec inputLicenseSpec = generateRandomLicenseSpec();
|
LicenseSpec inputLicenseSpec = generateRandomLicenseSpec();
|
||||||
|
@ -59,10 +62,8 @@ public class LicenseGenerationToolTests extends AbstractLicensingTestBase {
|
||||||
public void testWithLicenseFile() throws Exception {
|
public void testWithLicenseFile() throws Exception {
|
||||||
LicenseSpec inputLicenseSpec = generateRandomLicenseSpec();
|
LicenseSpec inputLicenseSpec = generateRandomLicenseSpec();
|
||||||
|
|
||||||
Path tempFilePath = Files.createTempFile("license_spec", "json");
|
File tempFile = temporaryFolder.newFile("license_spec.json");
|
||||||
File tempFile = tempFilePath.toFile();
|
|
||||||
FileUtils.write(tempFile, generateESLicenseSpecString(Arrays.asList(inputLicenseSpec)));
|
FileUtils.write(tempFile, generateESLicenseSpecString(Arrays.asList(inputLicenseSpec)));
|
||||||
tempFile.deleteOnExit();
|
|
||||||
|
|
||||||
String[] args = new String[6];
|
String[] args = new String[6];
|
||||||
args[0] = "--licenseFile";
|
args[0] = "--licenseFile";
|
||||||
|
@ -124,9 +125,8 @@ public class LicenseGenerationToolTests extends AbstractLicensingTestBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String runLicenseGenerationTool(String[] args) throws Exception {
|
private String runLicenseGenerationTool(String[] args) throws Exception {
|
||||||
File temp = File.createTempFile("temp", ".out");
|
File temp = temporaryFolder.newFile("license_generator.out");
|
||||||
temp.deleteOnExit();
|
|
||||||
try (FileOutputStream outputStream = new FileOutputStream(temp)) {
|
try (FileOutputStream outputStream = new FileOutputStream(temp)) {
|
||||||
LicenseGeneratorTool.run(args, outputStream);
|
LicenseGeneratorTool.run(args, outputStream);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,13 +16,13 @@ import org.elasticsearch.license.TestUtils;
|
||||||
import org.elasticsearch.license.core.ESLicense;
|
import org.elasticsearch.license.core.ESLicense;
|
||||||
import org.elasticsearch.license.core.ESLicenses;
|
import org.elasticsearch.license.core.ESLicenses;
|
||||||
import org.elasticsearch.license.licensor.tools.LicenseVerificationTool;
|
import org.elasticsearch.license.licensor.tools.LicenseVerificationTool;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween;
|
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween;
|
||||||
|
@ -34,6 +34,9 @@ import static org.junit.Assert.fail;
|
||||||
|
|
||||||
public class LicenseVerificationToolTests extends AbstractLicensingTestBase {
|
public class LicenseVerificationToolTests extends AbstractLicensingTestBase {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMissingCLTArgs() throws Exception {
|
public void testMissingCLTArgs() throws Exception {
|
||||||
ESLicense singedLicense = generateSignedLicense(randomRealisticUnicodeOfCodepointLengthBetween(5, 15),
|
ESLicense singedLicense = generateSignedLicense(randomRealisticUnicodeOfCodepointLengthBetween(5, 15),
|
||||||
|
@ -114,9 +117,7 @@ public class LicenseVerificationToolTests extends AbstractLicensingTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String dumpLicense(ESLicense license) throws Exception {
|
private String dumpLicense(ESLicense license) throws Exception {
|
||||||
Path tempFilePath = Files.createTempFile("license_spec", "json");
|
File tempFile = temporaryFolder.newFile("licenses.json");
|
||||||
File tempFile = tempFilePath.toFile();
|
|
||||||
tempFile.deleteOnExit();
|
|
||||||
try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
|
try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
|
||||||
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, outputStream);
|
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, outputStream);
|
||||||
ESLicenses.toXContent(Collections.singletonList(license), builder, ToXContent.EMPTY_PARAMS);
|
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 {
|
private String runLicenseVerificationTool(String[] args) throws IOException {
|
||||||
File temp = File.createTempFile("temp", ".out");
|
File tempFile = temporaryFolder.newFile("licence_verification.out");
|
||||||
temp.deleteOnExit();
|
try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
|
||||||
try (FileOutputStream outputStream = new FileOutputStream(temp)) {
|
|
||||||
LicenseVerificationTool.run(args, outputStream);
|
LicenseVerificationTool.run(args, outputStream);
|
||||||
}
|
}
|
||||||
return FileUtils.readFileToString(temp);
|
return FileUtils.readFileToString(tempFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue