-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure Files.createTempDirectory honors java.io.tmpdir system property
Signed-off-by: Arun Gopalpuri <[email protected]>
- Loading branch information
Showing
4 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...tion/tools/launchers/src/test/java/org/opensearch/tools/launchers/TempDirectoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.opensearch.tools.launchers; | ||
|
||
import com.carrotsearch.randomizedtesting.RandomizedTest; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.Assert.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class TempDirectoryTests extends LaunchersTestCase { | ||
|
||
@Rule | ||
public TemporaryFolder tempFolder = new TemporaryFolder(); | ||
|
||
private Path createdTempDir; | ||
|
||
@Before | ||
public void setUp() throws IOException{ | ||
System.setProperty("java.io.tmpdir", tempFolder.newFolder().toString()); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
if (createdTempDir != null && Files.exists(createdTempDir)) { | ||
Files.delete(createdTempDir); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMainWithArguments() { | ||
String[] args = {RandomizedTest.randomAsciiLettersOfLengthBetween(1, 10)}; | ||
assertThrows(IllegalArgumentException.class, () -> TempDirectory.main(args)); | ||
} | ||
|
||
@Test | ||
public void testMainOnWindows() throws IOException { | ||
System.setProperty("os.name", "Windows " + RandomizedTest.randomIntBetween(7, 10)); | ||
Launchers mockLaunchers = mock(Launchers.class); | ||
doNothing().when(mockLaunchers).outPrintln(any(String.class)); | ||
|
||
TempDirectory.main(new String[]{}); | ||
|
||
createdTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "opensearch"); | ||
verify(mockLaunchers, times(1)).outPrintln(createdTempDir.toString()); | ||
assert Files.exists(createdTempDir); | ||
} | ||
|
||
@Test | ||
public void testMainOnNonWindows() throws IOException { | ||
String [] osNames = {"Linux", "Mac OS X", "Unix"}; | ||
String osName = RandomizedTest.randomFrom(osNames); | ||
System.setProperty("os.name", osName); | ||
Launchers mockLaunchers = mock(Launchers.class); | ||
doNothing().when(mockLaunchers).outPrintln(any(String.class)); | ||
when(mockLaunchers.createTempDirectory(any(Path.class), any(String.class))).thenReturn(tempFolder.newFolder().toPath().resolve("opensearch-" + RandomizedTest.randomAsciiLettersOfLength(3))); | ||
|
||
TempDirectory.main(new String[]{}); | ||
|
||
createdTempDir = tempFolder.newFolder().toPath().resolve("opensearch-" + RandomizedTest.randomAsciiLettersOfLength(3)); | ||
verify(mockLaunchers, times(1)).outPrintln(createdTempDir.toString()); | ||
assert Files.exists(createdTempDir); | ||
} | ||
} |