AlexanderBartash commented on code in PR #2318:
URL: https://github.com/apache/maven-resolver/pull/2318#discussion_r4579575059


##########
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:
   Thank you, fixed.



-- 
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