Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (jkube-kit) : lastModified timestamps should be preserved while preparing assembly #2405

Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

/**
Expand Down Expand Up @@ -201,7 +202,7 @@ public static void copy(File sourceFile, File targetFile) throws IOException {
}

public static void copy(Path sourcePath, Path targetPath) throws IOException {
Files.copy(sourcePath, targetPath, REPLACE_EXISTING);
Files.copy(sourcePath, targetPath, REPLACE_EXISTING, COPY_ATTRIBUTES);
}

public static void copyDirectoryIfNotExists(File sourceDir, File targetDir) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -151,6 +154,23 @@ void testGetRelativePath() throws IOException {
assertThat(relativeFile.getPath()).isEqualTo("foo" + File.separator + "fileInFoo1");
}

@Test
void copy_whenFileCopied_shouldPreserveLastModifiedTimestamp() throws IOException {
// Given
File sourceFile = new File(folder, "source");
Files.write(sourceFile.toPath(), "testdata".getBytes(StandardCharsets.UTF_8));
long originalTimestamp = new Date().getTime() - 10;
assertThat(sourceFile.setLastModified(originalTimestamp)).isTrue();
Path targetFilePath = folder.toPath().resolve("target");

// When
FileUtil.copy(sourceFile.toPath(), targetFilePath);

// Then
assertThat(targetFilePath.toFile()).hasSameTextualContentAs(sourceFile);
assertThat(targetFilePath.toFile().lastModified()).isEqualTo(originalTimestamp);
}

private void prepareDirectory() throws IOException {
final File dir1 = Files.createDirectories(folder.toPath().resolve("foo")).toFile();
assertThat(new File(dir1, "fileInFoo1").createNewFile()).isTrue();
Expand Down