Skip to content

Commit 61216e5

Browse files
committed
Split out copy tests out of PathUtilsTest into a new class
PathUtilsCopyTest.
1 parent d199660 commit 61216e5

2 files changed

Lines changed: 153 additions & 117 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.file;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import java.io.IOException;
24+
import java.net.URI;
25+
import java.net.URL;
26+
import java.nio.file.FileSystem;
27+
import java.nio.file.FileSystems;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
31+
import java.util.HashMap;
32+
import java.util.Map;
33+
34+
import org.apache.commons.io.FilenameUtils;
35+
import org.junit.jupiter.api.Test;
36+
37+
/**
38+
* Tests {@link PathUtils}.
39+
*/
40+
class PathUtilsCopyTest extends AbstractTempDirTest {
41+
42+
private static final String TEST_JAR_NAME = "test.jar";
43+
44+
private static final String TEST_JAR_PATH = "src/test/resources/org/apache/commons/io/test.jar";
45+
46+
private FileSystem openArchive(final Path p, final boolean createNew) throws IOException {
47+
if (createNew) {
48+
final Map<String, String> env = new HashMap<>();
49+
env.put("create", "true");
50+
final URI fileUri = p.toAbsolutePath().toUri();
51+
final URI uri = URI.create("jar:" + fileUri.toASCIIString());
52+
return FileSystems.newFileSystem(uri, env, null);
53+
}
54+
return FileSystems.newFileSystem(p, (ClassLoader) null);
55+
}
56+
57+
@Test
58+
void testCopyDirectoryForDifferentFilesystemsWithAbsolutePath() throws IOException {
59+
final Path archivePath = Paths.get(TEST_JAR_PATH);
60+
try (FileSystem archive = openArchive(archivePath, false)) {
61+
// relative jar -> absolute dir
62+
Path sourceDir = archive.getPath("dir1");
63+
PathUtils.copyDirectory(sourceDir, tempDirPath);
64+
assertTrue(Files.exists(tempDirPath.resolve("f1")));
65+
66+
// absolute jar -> absolute dir
67+
sourceDir = archive.getPath("/next");
68+
PathUtils.copyDirectory(sourceDir, tempDirPath);
69+
assertTrue(Files.exists(tempDirPath.resolve("dir")));
70+
}
71+
}
72+
73+
@Test
74+
void testCopyDirectoryForDifferentFilesystemsWithAbsolutePathReverse() throws IOException {
75+
try (FileSystem archive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
76+
// absolute dir -> relative jar
77+
Path targetDir = archive.getPath("target");
78+
Files.createDirectory(targetDir);
79+
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toAbsolutePath();
80+
PathUtils.copyDirectory(sourceDir, targetDir);
81+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
82+
83+
// absolute dir -> absolute jar
84+
targetDir = archive.getPath("/");
85+
PathUtils.copyDirectory(sourceDir, targetDir);
86+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
87+
}
88+
}
89+
90+
@Test
91+
void testCopyDirectoryForDifferentFilesystemsWithRelativePath() throws IOException {
92+
final Path archivePath = Paths.get(TEST_JAR_PATH);
93+
try (FileSystem archive = openArchive(archivePath, false);
94+
FileSystem targetArchive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
95+
final Path targetDir = targetArchive.getPath("targetDir");
96+
Files.createDirectory(targetDir);
97+
// relative jar -> relative dir
98+
Path sourceDir = archive.getPath("next");
99+
PathUtils.copyDirectory(sourceDir, targetDir);
100+
assertTrue(Files.exists(targetDir.resolve("dir")));
101+
102+
// absolute jar -> relative dir
103+
sourceDir = archive.getPath("/dir1");
104+
PathUtils.copyDirectory(sourceDir, targetDir);
105+
assertTrue(Files.exists(targetDir.resolve("f1")));
106+
}
107+
}
108+
109+
@Test
110+
void testCopyDirectoryForDifferentFilesystemsWithRelativePathReverse() throws IOException {
111+
try (FileSystem archive = openArchive(tempDirPath.resolve(TEST_JAR_NAME), true)) {
112+
// relative dir -> relative jar
113+
Path targetDir = archive.getPath("target");
114+
Files.createDirectory(targetDir);
115+
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2");
116+
PathUtils.copyDirectory(sourceDir, targetDir);
117+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
118+
119+
// relative dir -> absolute jar
120+
targetDir = archive.getPath("/");
121+
PathUtils.copyDirectory(sourceDir, targetDir);
122+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
123+
}
124+
}
125+
126+
@Test
127+
void testCopyFile() throws IOException {
128+
final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
129+
final Path targetFile = PathUtils.copyFileToDirectory(sourceFile, tempDirPath);
130+
assertTrue(Files.exists(targetFile));
131+
assertEquals(Files.size(sourceFile), Files.size(targetFile));
132+
}
133+
134+
@Test
135+
void testCopyFileTwoFileSystem() throws IOException {
136+
try (FileSystem archive = openArchive(Paths.get(TEST_JAR_PATH), false)) {
137+
final Path sourceFile = archive.getPath("next/dir/test.log");
138+
final Path targetFile = PathUtils.copyFileToDirectory(sourceFile, tempDirPath);
139+
assertTrue(Files.exists(targetFile));
140+
assertEquals(Files.size(sourceFile), Files.size(targetFile));
141+
}
142+
}
143+
144+
@Test
145+
void testCopyURL() throws IOException {
146+
final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
147+
final URL url = new URL("file:///" + FilenameUtils.getPath(sourceFile.toAbsolutePath().toString()) + sourceFile.getFileName());
148+
final Path targetFile = PathUtils.copyFileToDirectory(url, tempDirPath);
149+
assertTrue(Files.exists(targetFile));
150+
assertEquals(Files.size(sourceFile), Files.size(targetFile));
151+
}
152+
153+
}

src/test/java/org/apache/commons/io/file/PathUtilsTest.java

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@
3030
import java.io.File;
3131
import java.io.IOException;
3232
import java.io.OutputStream;
33-
import java.net.URI;
3433
import java.net.URISyntaxException;
35-
import java.net.URL;
3634
import java.nio.charset.StandardCharsets;
3735
import java.nio.file.DirectoryStream;
38-
import java.nio.file.FileSystem;
3936
import java.nio.file.FileSystems;
4037
import java.nio.file.Files;
4138
import java.nio.file.LinkOption;
@@ -45,12 +42,9 @@
4542
import java.nio.file.attribute.FileTime;
4643
import java.nio.file.attribute.PosixFileAttributes;
4744
import java.util.GregorianCalendar;
48-
import java.util.HashMap;
4945
import java.util.Iterator;
50-
import java.util.Map;
5146

5247
import org.apache.commons.io.FileUtils;
53-
import org.apache.commons.io.FilenameUtils;
5448
import org.apache.commons.io.filefilter.NameFileFilter;
5549
import org.apache.commons.io.test.TestUtils;
5650
import 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

Comments
 (0)