Skip to content

Commit 1c668e0

Browse files
author
Gary Gregory
committed
PathUtilsTest does not need two temp dirs.
1 parent 9a919d0 commit 1c668e0

1 file changed

Lines changed: 52 additions & 80 deletions

File tree

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

Lines changed: 52 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -69,106 +69,78 @@ private FileSystem openArchive(final Path p, final boolean createNew) throws IOE
6969

7070
@Test
7171
public void testCopyDirectoryForDifferentFilesystemsWithAbsolutePath() throws IOException {
72-
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName()).toAbsolutePath();
73-
try {
74-
final Path archivePath = Paths.get(TEST_JAR_PATH);
75-
try (final FileSystem archive = openArchive(archivePath, false)) {
76-
// relative jar -> absolute dir
77-
Path sourceDir = archive.getPath("dir1");
78-
PathUtils.copyDirectory(sourceDir, tempDir);
79-
assertTrue(Files.exists(tempDir.resolve("f1")));
80-
81-
// absolute jar -> absolute dir
82-
sourceDir = archive.getPath("/next");
83-
PathUtils.copyDirectory(sourceDir, tempDir);
84-
assertTrue(Files.exists(tempDir.resolve("dir")));
85-
}
86-
} finally {
87-
PathUtils.deleteDirectory(tempDir);
72+
final Path archivePath = Paths.get(TEST_JAR_PATH);
73+
try (final FileSystem archive = openArchive(archivePath, false)) {
74+
// relative jar -> absolute dir
75+
Path sourceDir = archive.getPath("dir1");
76+
PathUtils.copyDirectory(sourceDir, tempDir);
77+
assertTrue(Files.exists(tempDir.resolve("f1")));
78+
79+
// absolute jar -> absolute dir
80+
sourceDir = archive.getPath("/next");
81+
PathUtils.copyDirectory(sourceDir, tempDir);
82+
assertTrue(Files.exists(tempDir.resolve("dir")));
8883
}
8984
}
9085

9186
@Test
9287
public void testCopyDirectoryForDifferentFilesystemsWithAbsolutePathReverse() throws IOException {
93-
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
94-
try {
95-
try (final FileSystem archive = openArchive(tempDir.resolve(TEST_JAR_NAME), true)) {
96-
// absolute dir -> relative jar
97-
Path targetDir = archive.getPath("target");
98-
Files.createDirectory(targetDir);
99-
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2")
100-
.toAbsolutePath();
101-
PathUtils.copyDirectory(sourceDir, targetDir);
102-
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
103-
104-
// absolute dir -> absolute jar
105-
targetDir = archive.getPath("/");
106-
PathUtils.copyDirectory(sourceDir, targetDir);
107-
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
108-
}
109-
} finally {
110-
PathUtils.deleteDirectory(tempDir);
88+
try (final FileSystem archive = openArchive(tempDir.resolve(TEST_JAR_NAME), true)) {
89+
// absolute dir -> relative jar
90+
Path targetDir = archive.getPath("target");
91+
Files.createDirectory(targetDir);
92+
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toAbsolutePath();
93+
PathUtils.copyDirectory(sourceDir, targetDir);
94+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
95+
96+
// absolute dir -> absolute jar
97+
targetDir = archive.getPath("/");
98+
PathUtils.copyDirectory(sourceDir, targetDir);
99+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
111100
}
112101
}
113102

114103
@Test
115104
public void testCopyDirectoryForDifferentFilesystemsWithRelativePath() throws IOException {
116-
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
117-
try {
118-
final Path archivePath = Paths.get(TEST_JAR_PATH);
119-
try (final FileSystem archive = openArchive(archivePath, false);
120-
final FileSystem targetArchive = openArchive(tempDir.resolve(TEST_JAR_NAME), true)) {
121-
final Path targetDir = targetArchive.getPath("targetDir");
122-
Files.createDirectory(targetDir);
123-
// relative jar -> relative dir
124-
Path sourceDir = archive.getPath("next");
125-
PathUtils.copyDirectory(sourceDir, targetDir);
126-
assertTrue(Files.exists(targetDir.resolve("dir")));
127-
128-
// absolute jar -> relative dir
129-
sourceDir = archive.getPath("/dir1");
130-
PathUtils.copyDirectory(sourceDir, targetDir);
131-
assertTrue(Files.exists(targetDir.resolve("f1")));
132-
}
133-
} finally {
134-
PathUtils.deleteDirectory(tempDir);
105+
final Path archivePath = Paths.get(TEST_JAR_PATH);
106+
try (final FileSystem archive = openArchive(archivePath, false); final FileSystem targetArchive = openArchive(tempDir.resolve(TEST_JAR_NAME), true)) {
107+
final Path targetDir = targetArchive.getPath("targetDir");
108+
Files.createDirectory(targetDir);
109+
// relative jar -> relative dir
110+
Path sourceDir = archive.getPath("next");
111+
PathUtils.copyDirectory(sourceDir, targetDir);
112+
assertTrue(Files.exists(targetDir.resolve("dir")));
113+
114+
// absolute jar -> relative dir
115+
sourceDir = archive.getPath("/dir1");
116+
PathUtils.copyDirectory(sourceDir, targetDir);
117+
assertTrue(Files.exists(targetDir.resolve("f1")));
135118
}
136119
}
137120

138121
@Test
139122
public void testCopyDirectoryForDifferentFilesystemsWithRelativePathReverse() throws IOException {
140-
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
141-
try {
142-
try (final FileSystem archive = openArchive(tempDir.resolve(TEST_JAR_NAME), true)) {
143-
// relative dir -> relative jar
144-
Path targetDir = archive.getPath("target");
145-
Files.createDirectory(targetDir);
146-
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2");
147-
PathUtils.copyDirectory(sourceDir, targetDir);
148-
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
149-
150-
// relative dir -> absolute jar
151-
targetDir = archive.getPath("/");
152-
PathUtils.copyDirectory(sourceDir, targetDir);
153-
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
154-
}
155-
} finally {
156-
PathUtils.deleteDirectory(tempDir);
123+
try (final FileSystem archive = openArchive(tempDir.resolve(TEST_JAR_NAME), true)) {
124+
// relative dir -> relative jar
125+
Path targetDir = archive.getPath("target");
126+
Files.createDirectory(targetDir);
127+
final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2");
128+
PathUtils.copyDirectory(sourceDir, targetDir);
129+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
130+
131+
// relative dir -> absolute jar
132+
targetDir = archive.getPath("/");
133+
PathUtils.copyDirectory(sourceDir, targetDir);
134+
assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1")));
157135
}
158136
}
159137

160138
@Test
161139
public void testCopyFile() throws IOException {
162-
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
163-
try {
164-
final Path sourceFile = Paths
165-
.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
166-
final Path targetFile = PathUtils.copyFileToDirectory(sourceFile, tempDir);
167-
assertTrue(Files.exists(targetFile));
168-
assertEquals(Files.size(sourceFile), Files.size(targetFile));
169-
} finally {
170-
PathUtils.deleteDirectory(tempDir);
171-
}
140+
final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
141+
final Path targetFile = PathUtils.copyFileToDirectory(sourceFile, tempDir);
142+
assertTrue(Files.exists(targetFile));
143+
assertEquals(Files.size(sourceFile), Files.size(targetFile));
172144
}
173145

174146
@Test

0 commit comments

Comments
 (0)