gnodet-bot commented on code in PR #2318:
URL: https://github.com/apache/maven-resolver/pull/2318#discussion_r4579542028


##########
maven-resolver-util/src/test/java/org/eclipse/aether/util/FileUtilsTest.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eclipse.aether.util;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class FileUtilsTest {
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    /**
+     * A time in the past, in whole seconds, as file systems differ in the 
precision they store.
+     */
+    private static final long LAST_MODIFIED = (System.currentTimeMillis() / 
1000 - TimeUnit.DAYS.toSeconds(12)) * 1000;
+
+    @Test
+    public void collocatedTempFileMoveKeepsLastModifiedOfNewFile() throws 
IOException {
+        Path target = temporaryFolder.getRoot().toPath().resolve("target.txt");
+
+        moveWithLastModified(target, "content");
+
+        assertEquals("content", read(target));
+        assertEquals(LAST_MODIFIED, target.toFile().lastModified());
+    }
+
+    @Test
+    public void collocatedTempFileMoveKeepsLastModifiedOfReplacedFile() throws 
IOException {
+        Path target = temporaryFolder.newFile("target.txt").toPath();
+        Files.write(target, "old content".getBytes(StandardCharsets.UTF_8));
+
+        moveWithLastModified(target, "new content");
+
+        assertEquals("new content", read(target));
+        assertEquals(LAST_MODIFIED, target.toFile().lastModified());
+    }
+
+    /**
+     * Writes the content the way the connector and the transports do: into a 
collocated temp file, that gets the
+     * remote last modified time, and is then moved onto the target.
+     */
+    private static void moveWithLastModified(Path target, String content) 
throws IOException {
+        Path tempPath;
+        try (FileUtils.CollocatedTempFile tempFile = 
FileUtils.newTempFile(target)) {
+            tempPath = tempFile.getPath();
+            Files.write(tempPath, content.getBytes(StandardCharsets.UTF_8));
+            assertTrue(tempPath.toFile().setLastModified(LAST_MODIFIED));

Review Comment:
   ⚠️ **Pre-fix test validation:** The test verifies the correct behaviour 
cross-platform, but the new tests do not exercise the fixed code path 
(`copy()`) on Linux CI — `CollocatedTempFile.close()` only calls `copy()` when 
`IS_WINDOWS` is true; on Linux it takes `Files.move`, which preserves the 
last-modified time regardless of this fix. The companion PR on master (#2319) 
addresses this by overriding `fileSystemMove` to throw `AccessDeniedException`, 
which forces the `classicCopy` fallback and makes the test red before the fix 
on any OS.
   
   If `FileUtils` were refactored to allow overriding `IS_WINDOWS` (or 
extracting `copy()` as a protected method), the same approach would apply here. 
Given that `FileUtils` is a simple `final` utility class on 1.9.x, that 
refactor would be out of scope — but it means CI on Linux will not catch a 
future regression that removes this fix.
   
   Also, minor: the test helper sets the temp file's time with the legacy 
`File.setLastModified(long)` API, inconsistent with the NIO2 fix and with the 
equivalent test in #2319.
   
   ```suggestion
               Files.setLastModifiedTime(tempPath, 
java.nio.file.attribute.FileTime.fromMillis(LAST_MODIFIED));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to