Skip to content
Merged

Cleanup #1488

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix copyDirectory and add test inspired #1075
  • Loading branch information
aleksandy committed Nov 13, 2025
commit 176a9e6a5fabba0b7425808c2db85c704c390fd2
1 change: 1 addition & 0 deletions framework/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@
<include name="**/*.gif"/>
<include name="**/*.png"/>
<include name="**/*.jpg"/>
<include name="**/IOTest/**"/>
</fileset>
</copy>
</target>
Expand Down
27 changes: 16 additions & 11 deletions framework/src/play/libs/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.apache.commons.io.Charsets.toCharset;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.*;
import java.nio.channels.Channels;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -293,14 +287,25 @@ public static void write(InputStream is, File f) {

// If targetLocation does not exist, it will be created.
public static void copyDirectory(File source, File target) {
try (Stream<Path> dirs = Files.walk(source.toPath())) {
dirs.forEach(src -> {
var sourcePath = source.toPath();
var targetPath = target.toPath();

try (var stream = Files.walk(sourcePath)) {
stream.forEach(src -> {
try {
Files.copy(source.toPath(), target.toPath(), REPLACE_EXISTING);
Path dst = targetPath.resolve(sourcePath.relativize(src));

if (Files.isDirectory(src)) {
Files.createDirectories(dst);
} else {
Files.copy(src, dst, REPLACE_EXISTING);
}
} catch (IOException e) {
throw new UnexpectedException(e);
throw new UncheckedIOException(e);
}
});
} catch (UncheckedIOException e) {
throw new UnexpectedException(e.getCause());
} catch (IOException e) {
throw new UnexpectedException(e);
}
Expand Down
38 changes: 38 additions & 0 deletions framework/test-src/play/libs/IOTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package play.libs;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.nio.file.Files;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.io.CleanupMode.ALWAYS;

class IOTest {

@Test void copyDirectoryShouldCopyDirectoryContentRecursively(@TempDir(cleanup = ALWAYS) File target) throws Exception {
File source = new File(getClass().getResource(getClass().getSimpleName() + "/copyDirectory").toURI());
if (target.exists()) {
FileUtils.forceDelete(target);
}

assertThat(source).exists();
assertThat(target).doesNotExist();

IO.copyDirectory(source, target);

assertThat(target).exists();
try (var files = Files.walk(source.toPath())) {
files.forEach(file -> {
var original = source.toPath().relativize(file);
var copy = target.toPath().resolve(original);
assertThat(copy).exists();
if (Files.isRegularFile(copy)) {
assertThat(copy).hasSameBinaryContentAs(file);
}
});
}
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
雲河海原森林火山島川田都市街路家屋門窗燈火影光音色香味觸感喜怒哀樂愛憎勇怯智愚美醜善惡真假虛實陰陽天地人神佛鬼仙妖怪靈獸鳥魚蟲木草花石鐵金銀銅玉寶珠鏡劍弓矢盾鎧甲衣冠履帯飾品書畫筆墨紙硯琴棋酒茶食米魚肉菜果酒煙藥毒醫病生死老幼男女父子兄弟友君臣民官兵農工商旅商売買貸借贈受取捨選決議論説話聞讀書寫計算測量建設製造破壞開始終了停止進行回轉飛行游泳奔跑跳躍舞遊歌笑泣叫怒靜動速遲輕重高低長短廣狹深淺濃淡硬軟強弱多少遠近前後左右上下中內外早晚晝夜朝夕年月日時分秒春夏秋冬寒暖涼熱乾濕晴雨風雪雷電雲霧霞虹天地山海川原野森林草木花實葉根枝幹皮血骨肉膚毛爪牙角羽翼卵巢穴巢家屋室戶窗床椅机燈燭鏡箱袋瓶杯