1
1
package io .specto .hoverfly .junit .core ;
2
2
3
- import org .apache .commons .io .FileUtils ;
4
- import org .slf4j .Logger ;
5
- import org .slf4j .LoggerFactory ;
3
+ import static io .specto .hoverfly .junit .core .SystemConfigFactory .OsName .WINDOWS ;
4
+ import static java .nio .file .attribute .PosixFilePermission .OWNER_EXECUTE ;
5
+ import static java .nio .file .attribute .PosixFilePermission .OWNER_READ ;
6
+ import static java .util .Arrays .asList ;
6
7
7
8
import java .io .File ;
8
9
import java .io .IOException ;
9
- import java .net . URL ;
10
+ import java .io . InputStream ;
10
11
import java .nio .file .Files ;
11
12
import java .nio .file .Path ;
12
13
import java .nio .file .Paths ;
14
+ import java .nio .file .StandardCopyOption ;
15
+ import java .util .Comparator ;
13
16
import java .util .HashSet ;
14
-
15
- import static io .specto .hoverfly .junit .core .HoverflyUtils .findResourceOnClasspath ;
16
- import static io .specto .hoverfly .junit .core .SystemConfigFactory .OsName .WINDOWS ;
17
- import static java .nio .file .attribute .PosixFilePermission .OWNER_EXECUTE ;
18
- import static java .nio .file .attribute .PosixFilePermission .OWNER_READ ;
19
- import static java .util .Arrays .asList ;
17
+ import org .slf4j .Logger ;
18
+ import org .slf4j .LoggerFactory ;
20
19
21
20
/**
22
21
* Manage temporary files for running hoverfly
@@ -36,22 +35,24 @@ void purge() {
36
35
return ;
37
36
}
38
37
try {
39
- FileUtils .deleteDirectory (tempDirectory .toFile ());
38
+ Files .walk (tempDirectory )
39
+ .sorted (Comparator .reverseOrder ())
40
+ .map (Path ::toFile )
41
+ .forEach (File ::delete );
42
+ tempDirectory = null ;
40
43
} catch (IOException e ) {
41
44
LOGGER .warn ("Failed to delete hoverfly binary, will try again on JVM shutdown." , e );
42
45
}
43
-
44
46
}
45
47
46
48
/**
47
49
* Copy classpath resource to hoverfly temporary directory
48
50
*/
49
51
Path copyClassPathResource (String resourcePath , String targetName ) {
50
- URL sourceUrl = HoverflyUtils .findResourceOnClasspath (resourcePath );
51
52
52
53
Path targetPath = getOrCreateTempDirectory ().resolve (targetName );
53
- try {
54
- FileUtils . copyURLToFile ( sourceUrl , targetPath . toFile () );
54
+ try ( InputStream resourceAsStream = HoverflyUtils . getClasspathResourceAsStream ( resourcePath )) {
55
+ Files . copy ( resourceAsStream , targetPath , StandardCopyOption . REPLACE_EXISTING );
55
56
} catch (IOException e ) {
56
57
throw new IllegalStateException ("Failed to copy classpath resource " + resourcePath , e );
57
58
}
@@ -66,13 +67,13 @@ Path copyClassPathResource(String resourcePath, String targetName) {
66
67
Path copyHoverflyBinary (SystemConfig systemConfig ) {
67
68
String binaryName = systemConfig .getHoverflyBinaryName ();
68
69
LOGGER .info ("Selecting the following binary based on the current operating system: {}" , binaryName );
69
- final URL sourceUrl = findResourceOnClasspath (HOVERFLY_BINARIES_ROOT_PATH + binaryName );
70
- final Path targetPath = getOrCreateTempDirectory ().resolve (binaryName );
70
+ Path targetPath = getOrCreateTempDirectory ().resolve (binaryName );
71
71
LOGGER .info ("Storing binary in temporary directory {}" , targetPath );
72
- final File targetFile = targetPath . toFile ();
73
- try {
74
- FileUtils . copyURLToFile ( sourceUrl , targetFile );
72
+
73
+ try ( InputStream resourceAsStream = HoverflyUtils . getClasspathResourceAsStream ( HOVERFLY_BINARIES_ROOT_PATH + binaryName )) {
74
+ Files . copy ( resourceAsStream , targetPath , StandardCopyOption . REPLACE_EXISTING );
75
75
if (systemConfig .getOsName () == WINDOWS ) {
76
+ final File targetFile = targetPath .toFile ();
76
77
targetFile .setExecutable (true );
77
78
targetFile .setReadable (true );
78
79
targetFile .setWritable (true );
0 commit comments