11package io .specto .hoverfly .junit .core ;
22
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 ;
67
78import java .io .File ;
89import java .io .IOException ;
9- import java .net . URL ;
10+ import java .io . InputStream ;
1011import java .nio .file .Files ;
1112import java .nio .file .Path ;
1213import java .nio .file .Paths ;
14+ import java .nio .file .StandardCopyOption ;
15+ import java .util .Comparator ;
1316import 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 ;
2019
2120/**
2221 * Manage temporary files for running hoverfly
@@ -36,22 +35,24 @@ void purge() {
3635 return ;
3736 }
3837 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 ;
4043 } catch (IOException e ) {
4144 LOGGER .warn ("Failed to delete hoverfly binary, will try again on JVM shutdown." , e );
4245 }
43-
4446 }
4547
4648 /**
4749 * Copy classpath resource to hoverfly temporary directory
4850 */
4951 Path copyClassPathResource (String resourcePath , String targetName ) {
50- URL sourceUrl = HoverflyUtils .findResourceOnClasspath (resourcePath );
5152
5253 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 );
5556 } catch (IOException e ) {
5657 throw new IllegalStateException ("Failed to copy classpath resource " + resourcePath , e );
5758 }
@@ -66,13 +67,13 @@ Path copyClassPathResource(String resourcePath, String targetName) {
6667 Path copyHoverflyBinary (SystemConfig systemConfig ) {
6768 String binaryName = systemConfig .getHoverflyBinaryName ();
6869 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 );
7171 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 );
7575 if (systemConfig .getOsName () == WINDOWS ) {
76+ final File targetFile = targetPath .toFile ();
7677 targetFile .setExecutable (true );
7778 targetFile .setReadable (true );
7879 targetFile .setWritable (true );
0 commit comments