3030import java .io .File ;
3131import java .io .IOException ;
3232import java .io .OutputStream ;
33- import java .net .URI ;
3433import java .net .URISyntaxException ;
35- import java .net .URL ;
3634import java .nio .charset .StandardCharsets ;
3735import java .nio .file .DirectoryStream ;
38- import java .nio .file .FileSystem ;
3936import java .nio .file .FileSystems ;
4037import java .nio .file .Files ;
4138import java .nio .file .LinkOption ;
4542import java .nio .file .attribute .FileTime ;
4643import java .nio .file .attribute .PosixFileAttributes ;
4744import java .util .GregorianCalendar ;
48- import java .util .HashMap ;
4945import java .util .Iterator ;
50- import java .util .Map ;
5146
5247import org .apache .commons .io .FileUtils ;
53- import org .apache .commons .io .FilenameUtils ;
5448import org .apache .commons .io .filefilter .NameFileFilter ;
5549import org .apache .commons .io .test .TestUtils ;
5650import org .apache .commons .lang3 .ArrayUtils ;
@@ -68,10 +62,6 @@ class PathUtilsTest extends AbstractTempDirTest {
6862
6963 private static final byte [] BYTE_ARRAY_FIXTURE = STRING_FIXTURE .getBytes (StandardCharsets .UTF_8 );
7064
71- private static final String TEST_JAR_NAME = "test.jar" ;
72-
73- private static final String TEST_JAR_PATH = "src/test/resources/org/apache/commons/io/test.jar" ;
74-
7565 private static final String PATH_FIXTURE = "NOTICE.txt" ;
7666
7767 private Path current () {
@@ -86,117 +76,10 @@ private Path getNonExistentPath() {
8676 return Paths .get ("/does not exist/for/certain" );
8777 }
8878
89- private FileSystem openArchive (final Path p , final boolean createNew ) throws IOException {
90- if (createNew ) {
91- final Map <String , String > env = new HashMap <>();
92- env .put ("create" , "true" );
93- final URI fileUri = p .toAbsolutePath ().toUri ();
94- final URI uri = URI .create ("jar:" + fileUri .toASCIIString ());
95- return FileSystems .newFileSystem (uri , env , null );
96- }
97- return FileSystems .newFileSystem (p , (ClassLoader ) null );
98- }
99-
10079 private void setLastModifiedMillis (final Path file , final long millis ) throws IOException {
10180 Files .setLastModifiedTime (file , FileTime .fromMillis (millis ));
10281 }
10382
104- @ Test
105- void testCopyDirectoryForDifferentFilesystemsWithAbsolutePath () throws IOException {
106- final Path archivePath = Paths .get (TEST_JAR_PATH );
107- try (FileSystem archive = openArchive (archivePath , false )) {
108- // relative jar -> absolute dir
109- Path sourceDir = archive .getPath ("dir1" );
110- PathUtils .copyDirectory (sourceDir , tempDirPath );
111- assertTrue (Files .exists (tempDirPath .resolve ("f1" )));
112-
113- // absolute jar -> absolute dir
114- sourceDir = archive .getPath ("/next" );
115- PathUtils .copyDirectory (sourceDir , tempDirPath );
116- assertTrue (Files .exists (tempDirPath .resolve ("dir" )));
117- }
118- }
119-
120- @ Test
121- void testCopyDirectoryForDifferentFilesystemsWithAbsolutePathReverse () throws IOException {
122- try (FileSystem archive = openArchive (tempDirPath .resolve (TEST_JAR_NAME ), true )) {
123- // absolute dir -> relative jar
124- Path targetDir = archive .getPath ("target" );
125- Files .createDirectory (targetDir );
126- final Path sourceDir = Paths .get ("src/test/resources/org/apache/commons/io/dirs-2-file-size-2" ).toAbsolutePath ();
127- PathUtils .copyDirectory (sourceDir , targetDir );
128- assertTrue (Files .exists (targetDir .resolve ("dirs-a-file-size-1" )));
129-
130- // absolute dir -> absolute jar
131- targetDir = archive .getPath ("/" );
132- PathUtils .copyDirectory (sourceDir , targetDir );
133- assertTrue (Files .exists (targetDir .resolve ("dirs-a-file-size-1" )));
134- }
135- }
136-
137- @ Test
138- void testCopyDirectoryForDifferentFilesystemsWithRelativePath () throws IOException {
139- final Path archivePath = Paths .get (TEST_JAR_PATH );
140- try (FileSystem archive = openArchive (archivePath , false );
141- FileSystem targetArchive = openArchive (tempDirPath .resolve (TEST_JAR_NAME ), true )) {
142- final Path targetDir = targetArchive .getPath ("targetDir" );
143- Files .createDirectory (targetDir );
144- // relative jar -> relative dir
145- Path sourceDir = archive .getPath ("next" );
146- PathUtils .copyDirectory (sourceDir , targetDir );
147- assertTrue (Files .exists (targetDir .resolve ("dir" )));
148-
149- // absolute jar -> relative dir
150- sourceDir = archive .getPath ("/dir1" );
151- PathUtils .copyDirectory (sourceDir , targetDir );
152- assertTrue (Files .exists (targetDir .resolve ("f1" )));
153- }
154- }
155-
156- @ Test
157- void testCopyDirectoryForDifferentFilesystemsWithRelativePathReverse () throws IOException {
158- try (FileSystem archive = openArchive (tempDirPath .resolve (TEST_JAR_NAME ), true )) {
159- // relative dir -> relative jar
160- Path targetDir = archive .getPath ("target" );
161- Files .createDirectory (targetDir );
162- final Path sourceDir = Paths .get ("src/test/resources/org/apache/commons/io/dirs-2-file-size-2" );
163- PathUtils .copyDirectory (sourceDir , targetDir );
164- assertTrue (Files .exists (targetDir .resolve ("dirs-a-file-size-1" )));
165-
166- // relative dir -> absolute jar
167- targetDir = archive .getPath ("/" );
168- PathUtils .copyDirectory (sourceDir , targetDir );
169- assertTrue (Files .exists (targetDir .resolve ("dirs-a-file-size-1" )));
170- }
171- }
172-
173- @ Test
174- void testCopyFile () throws IOException {
175- final Path sourceFile = Paths .get ("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin" );
176- final Path targetFile = PathUtils .copyFileToDirectory (sourceFile , tempDirPath );
177- assertTrue (Files .exists (targetFile ));
178- assertEquals (Files .size (sourceFile ), Files .size (targetFile ));
179- }
180-
181- @ Test
182- void testCopyFileTwoFileSystem () throws IOException {
183- try (FileSystem archive = openArchive (Paths .get (TEST_JAR_PATH ), false )) {
184- final Path sourceFile = archive .getPath ("next/dir/test.log" );
185- final Path targetFile = PathUtils .copyFileToDirectory (sourceFile , tempDirPath );
186- assertTrue (Files .exists (targetFile ));
187- assertEquals (Files .size (sourceFile ), Files .size (targetFile ));
188- }
189- }
190-
191- @ Test
192- void testCopyURL () throws IOException {
193- final Path sourceFile = Paths .get ("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin" );
194- final URL url = new URL ("file:///" + FilenameUtils .getPath (sourceFile .toAbsolutePath ().toString ()) + sourceFile .getFileName ());
195- final Path targetFile = PathUtils .copyFileToDirectory (url , tempDirPath );
196- assertTrue (Files .exists (targetFile ));
197- assertEquals (Files .size (sourceFile ), Files .size (targetFile ));
198- }
199-
20083 @ Test
20184 void testCreateDirectoriesAlreadyExists () throws IOException {
20285 assertEquals (tempDirPath .getParent (), PathUtils .createParentDirectories (tempDirPath ));
0 commit comments