2222import static org .junit .jupiter .api .Assertions .assertTrue ;
2323
2424import java .io .IOException ;
25+ import java .net .URI ;
2526import java .nio .file .DirectoryStream ;
27+ import java .nio .file .FileSystem ;
28+ import java .nio .file .FileSystems ;
2629import java .nio .file .Files ;
2730import java .nio .file .Path ;
2831import java .nio .file .Paths ;
32+ import java .util .HashMap ;
2933import java .util .Iterator ;
34+ import java .util .Map ;
3035
3136import org .apache .commons .io .filefilter .NameFileFilter ;
3237import org .junit .jupiter .api .Test ;
3742 */
3843public class PathUtilsTest extends TestArguments {
3944
45+ private static final String TEST_JAR_NAME = "test.jar" ;
46+
47+ private static final String TEST_JAR_PATH = "src/test/resources/org/apache/commons/io/test.jar" ;
48+
4049 private static final String PATH_FIXTURE = "NOTICE.txt" ;
4150
4251 /**
@@ -45,6 +54,110 @@ public class PathUtilsTest extends TestArguments {
4554 @ TempDir
4655 public Path tempDir ;
4756
57+ private FileSystem openArchive (final Path p , final boolean createNew ) throws IOException {
58+ final FileSystem archive ;
59+ if (createNew ) {
60+ final Map <String , String > env = new HashMap <>();
61+ env .put ("create" , "true" );
62+ final URI fileUri = p .toAbsolutePath ().toUri ();
63+ final URI uri = URI .create ("jar:" + fileUri .toASCIIString ());
64+ archive = FileSystems .newFileSystem (uri , env , null );
65+ } else {
66+ archive = FileSystems .newFileSystem (p , (ClassLoader ) null );
67+ }
68+ return archive ;
69+ }
70+
71+ @ Test
72+ public void testCopyDirectoryForDifferentFilesystemsWithAbsolutePath () throws IOException {
73+ final Path tempDir = Files .createTempDirectory (getClass ().getCanonicalName ()).toAbsolutePath ();
74+ try {
75+ final Path archivePath = Paths .get (TEST_JAR_PATH );
76+ try (final FileSystem archive = openArchive (archivePath , false )) {
77+ // relative jar -> absolute dir
78+ Path sourceDir = archive .getPath ("dir1" );
79+ PathUtils .copyDirectory (sourceDir , tempDir );
80+ assertTrue (Files .exists (tempDir .resolve ("f1" )));
81+
82+ // absolute jar -> absolute dir
83+ sourceDir = archive .getPath ("/next" );
84+ PathUtils .copyDirectory (sourceDir , tempDir );
85+ assertTrue (Files .exists (tempDir .resolve ("dir" )));
86+ }
87+ } finally {
88+ PathUtils .deleteDirectory (tempDir );
89+ }
90+ }
91+
92+ @ Test
93+ public void testCopyDirectoryForDifferentFilesystemsWithAbsolutePathReverse () throws IOException {
94+ final Path tempDir = Files .createTempDirectory (getClass ().getCanonicalName ());
95+ try {
96+ try (final FileSystem archive = openArchive (tempDir .resolve (TEST_JAR_NAME ), true )) {
97+ // absolute dir -> relative jar
98+ Path targetDir = archive .getPath ("target" );
99+ Files .createDirectory (targetDir );
100+ final Path sourceDir = Paths .get ("src/test/resources/org/apache/commons/io/dirs-2-file-size-2" )
101+ .toAbsolutePath ();
102+ PathUtils .copyDirectory (sourceDir , targetDir );
103+ assertTrue (Files .exists (targetDir .resolve ("dirs-a-file-size-1" )));
104+
105+ // absolute dir -> absolute jar
106+ targetDir = archive .getPath ("/" );
107+ PathUtils .copyDirectory (sourceDir , targetDir );
108+ assertTrue (Files .exists (targetDir .resolve ("dirs-a-file-size-1" )));
109+ }
110+ } finally {
111+ PathUtils .deleteDirectory (tempDir );
112+ }
113+ }
114+
115+ @ Test
116+ public void testCopyDirectoryForDifferentFilesystemsWithRelativePath () throws IOException {
117+ final Path tempDir = Files .createTempDirectory (getClass ().getCanonicalName ());
118+ try {
119+ final Path archivePath = Paths .get (TEST_JAR_PATH );
120+ try (final FileSystem archive = openArchive (archivePath , false );
121+ final FileSystem targetArchive = openArchive (tempDir .resolve (TEST_JAR_NAME ), true )) {
122+ final Path targetDir = targetArchive .getPath ("targetDir" );
123+ Files .createDirectory (targetDir );
124+ // relative jar -> relative dir
125+ Path sourceDir = archive .getPath ("next" );
126+ PathUtils .copyDirectory (sourceDir , targetDir );
127+ assertTrue (Files .exists (targetDir .resolve ("dir" )));
128+
129+ // absolute jar -> relative dir
130+ sourceDir = archive .getPath ("/dir1" );
131+ PathUtils .copyDirectory (sourceDir , targetDir );
132+ assertTrue (Files .exists (targetDir .resolve ("f1" )));
133+ }
134+ } finally {
135+ PathUtils .deleteDirectory (tempDir );
136+ }
137+ }
138+
139+ @ Test
140+ public void testCopyDirectoryForDifferentFilesystemsWithRelativePathReverse () throws IOException {
141+ final Path tempDir = Files .createTempDirectory (getClass ().getCanonicalName ());
142+ try {
143+ try (final FileSystem archive = openArchive (tempDir .resolve (TEST_JAR_NAME ), true )) {
144+ // relative dir -> relative jar
145+ Path targetDir = archive .getPath ("target" );
146+ Files .createDirectory (targetDir );
147+ final Path sourceDir = Paths .get ("src/test/resources/org/apache/commons/io/dirs-2-file-size-2" );
148+ PathUtils .copyDirectory (sourceDir , targetDir );
149+ assertTrue (Files .exists (targetDir .resolve ("dirs-a-file-size-1" )));
150+
151+ // relative dir -> absolute jar
152+ targetDir = archive .getPath ("/" );
153+ PathUtils .copyDirectory (sourceDir , targetDir );
154+ assertTrue (Files .exists (targetDir .resolve ("dirs-a-file-size-1" )));
155+ }
156+ } finally {
157+ PathUtils .deleteDirectory (tempDir );
158+ }
159+ }
160+
48161 @ Test
49162 public void testCopyFile () throws IOException {
50163 final Path tempDir = Files .createTempDirectory (getClass ().getCanonicalName ());
0 commit comments