1212import java .util .Properties ;
1313import java .util .UUID ;
1414import org .junit .jupiter .api .Test ;
15+ import org .junit .jupiter .params .ParameterizedTest ;
16+ import org .junit .jupiter .params .provider .ValueSource ;
1517
1618public class TempLocationManagerTest {
17- @ Test
18- void testDefault () throws Exception {
19+ @ ParameterizedTest
20+ @ ValueSource (strings = {"" , "test1" })
21+ void testDefault (String subPath ) throws Exception {
1922 TempLocationManager tempLocationManager = TempLocationManager .getInstance (true );
20- Path tempDir = tempLocationManager .getTempDir ();
23+ Path tempDir = tempLocationManager .getTempDir (Paths . get ( subPath ) );
2124 assertNotNull (tempDir );
2225 assertTrue (Files .exists (tempDir ));
2326 assertTrue (Files .isDirectory (tempDir ));
2427 assertTrue (Files .isWritable (tempDir ));
2528 assertTrue (Files .isReadable (tempDir ));
2629 assertTrue (Files .isExecutable (tempDir ));
27- assertTrue (tempDir .endsWith ("pid_" + PidHelper .getPid ()));
30+ assertTrue (tempDir .toString (). contains ("pid_" + PidHelper .getPid ()));
2831 }
2932
30- @ Test
31- void testFromConfig () throws Exception {
33+ @ ParameterizedTest
34+ @ ValueSource (strings = {"" , "test1" })
35+ void testFromConfig (String subPath ) throws Exception {
3236 Path myDir =
3337 Files .createTempDirectory (
3438 "ddprof-test-" ,
3539 PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("rwx------" )));
40+ myDir .toFile ().deleteOnExit ();
3641 Properties props = new Properties ();
3742 props .put (ProfilingConfig .PROFILING_TEMP_DIR , myDir .toString ());
3843 ConfigProvider configProvider = ConfigProvider .withPropertiesOverride (props );
3944 TempLocationManager tempLocationManager = new TempLocationManager (configProvider );
40- Path tempDir = tempLocationManager .getTempDir ();
45+ Path tempDir = tempLocationManager .getTempDir (Paths . get ( subPath ) );
4146 assertNotNull (tempDir );
4247 assertTrue (tempDir .toString ().startsWith (myDir .toString ()));
4348 }
@@ -58,29 +63,42 @@ void testFromConfigNotWritable() throws Exception {
5863 Files .createTempDirectory (
5964 "ddprof-test-" ,
6065 PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("r-x------" )));
66+ myDir .toFile ().deleteOnExit ();
6167 Properties props = new Properties ();
6268 props .put (ProfilingConfig .PROFILING_TEMP_DIR , myDir .toString ());
6369 ConfigProvider configProvider = ConfigProvider .withPropertiesOverride (props );
6470 TempLocationManager tempLocationManager = new TempLocationManager (configProvider );
6571 assertThrows (IllegalStateException .class , tempLocationManager ::getTempDir );
6672 }
6773
68- @ Test
69- void testCleanup () throws Exception {
74+ @ ParameterizedTest
75+ @ ValueSource (strings = {"" , "test1" })
76+ void testCleanup (String subPath ) throws Exception {
7077 Path myDir =
7178 Files .createTempDirectory (
7279 "ddprof-test-" ,
7380 PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("rwx------" )));
81+ myDir .toFile ().deleteOnExit ();
7482 Properties props = new Properties ();
7583 props .put (ProfilingConfig .PROFILING_TEMP_DIR , myDir .toString ());
84+ props .put (
85+ ProfilingConfig .PROFILING_UPLOAD_PERIOD ,
86+ "0" ); // to force immediate cleanup; must be a string value!
7687 ConfigProvider configProvider = ConfigProvider .withPropertiesOverride (props );
7788 TempLocationManager tempLocationManager = new TempLocationManager (configProvider );
78- Path tempDir = tempLocationManager .getTempDir ();
89+ Path tempDir = tempLocationManager .getTempDir (Paths . get ( subPath ) );
7990 assertNotNull (tempDir );
8091
8192 // fake temp location
82- Path fakeTempDir = tempDir .getParent ().resolve ("pid_00000" );
93+ Path fakeTempDir = tempDir .getParent ();
94+ while (fakeTempDir != null && !fakeTempDir .endsWith ("ddprof" )) {
95+ fakeTempDir = fakeTempDir .getParent ();
96+ }
97+ fakeTempDir = fakeTempDir .resolve ("pid_0000" );
8398 Files .createDirectories (fakeTempDir );
99+ Path tmpFile = Files .createFile (fakeTempDir .resolve ("test.txt" ));
100+ tmpFile .toFile ().deleteOnExit (); // make sure this is deleted at exit
101+ fakeTempDir .toFile ().deleteOnExit (); // also this one
84102 tempLocationManager .cleanup (false );
85103 // fake temp location should be deleted
86104 // real temp location should be kept
0 commit comments