sane handling of file times
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/8a9bc87f Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/8a9bc87f Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/8a9bc87f Branch: refs/heads/compress-2.0 Commit: 8a9bc87f8efdfcb5c622e638b9127f962bb040a0 Parents: 7df45e0 Author: Stefan Bodewig <bode...@apache.org> Authored: Sun Mar 27 16:25:45 2016 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Sun Mar 27 16:25:45 2016 +0200 ---------------------------------------------------------------------- .../archivers/spi/SimpleArchiveEntry.java | 22 ++++-- .../archivers/spi/SimpleArchiveEntryTest.java | 82 ++++++++++++++++++++ 2 files changed, 98 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/8a9bc87f/src/main/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntry.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntry.java b/src/main/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntry.java index ac1294d..b90e798 100644 --- a/src/main/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntry.java +++ b/src/main/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntry.java @@ -51,9 +51,10 @@ public class SimpleArchiveEntry implements ArchiveEntry { this.size = params.size(); this.type = params.getType(); this.fileKey = params.fileKey(); - this.lastModified = params.lastModifiedTime(); - this.lastAccess = params.lastAccessTime(); - this.created = params.creationTime(); + this.lastModified = params.lastModifiedTime() != null ? params.lastModifiedTime() + : FileTime.fromMillis(0l);; + this.lastAccess = params.lastAccessTime() != null ? params.lastAccessTime() : this.lastModified; + this.created = params.creationTime() != null ? params.creationTime() : this.lastModified; this.owner = params.getOwnerInformation(); this.permissions = params.getPermissions().map(Collections::unmodifiableSet); this.mode = params.getMode(); @@ -89,16 +90,25 @@ public class SimpleArchiveEntry implements ArchiveEntry { return type == FileType.OTHER; } + /** + * @return a {@link FileTime} representing the epoch (1970-01-01T00:00:00Z) if the modified time is unknown. + */ @Override public FileTime lastModifiedTime() { return lastModified; } + /** + * @return {@link #lastModifiedTime} if the access time is unknown. + */ @Override public FileTime lastAccessTime() { return lastAccess; } + /** + * @return {@link #lastModifiedTime} if the creation time is unknown. + */ @Override public FileTime creationTime() { return created; @@ -145,9 +155,9 @@ public class SimpleArchiveEntry implements ArchiveEntry { && size == other.size && type == other.type && Objects.equals(fileKey, other.fileKey) - && Objects.equals(lastModified, other.lastModified) - && Objects.equals(lastAccess, other.lastAccess) - && Objects.equals(created, other.created) + && lastModified.equals(other.lastModified) + && lastAccess.equals(other.lastAccess) + && created.equals(other.created) && Objects.equals(mode, other.mode) && Objects.equals(permissions, other.permissions) && Objects.equals(owner, other.owner); http://git-wip-us.apache.org/repos/asf/commons-compress/blob/8a9bc87f/src/test/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntryTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntryTest.java b/src/test/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntryTest.java new file mode 100644 index 0000000..90ca547 --- /dev/null +++ b/src/test/java/org/apache/commons/compress2/archivers/spi/SimpleArchiveEntryTest.java @@ -0,0 +1,82 @@ +/* + * 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.apache.commons.compress2.archivers.spi; + +import static org.junit.Assert.assertEquals; + +import java.nio.file.attribute.FileTime; +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +import org.apache.commons.compress2.archivers.ArchiveEntryParameters; +import org.junit.Test; + +public class SimpleArchiveEntryTest { + + @Test + public void nullLastModifiedIsTranslatedToEpoch() { + SimpleArchiveEntry e = new SimpleArchiveEntry(new ArchiveEntryParameters()); + assertEquals(0l, e.lastModifiedTime().toMillis()); + } + + @Test + public void lastModifiedIsUsedWhenPresent() { + FileTime d = FileTime.from(Instant.now()); + SimpleArchiveEntry e = new SimpleArchiveEntry(new ArchiveEntryParameters() + .withLastModifiedTime(d)); + assertEquals(d, e.lastModifiedTime()); + } + + @Test + public void nullLastAccessUsesLastModified() { + FileTime d = FileTime.from(Instant.now()); + SimpleArchiveEntry e = new SimpleArchiveEntry(new ArchiveEntryParameters() + .withLastModifiedTime(d)); + assertEquals(d, e.lastAccessTime()); + } + + @Test + public void lastAccessIsUsedWhenPresent() { + FileTime d1 = FileTime.from(Instant.now()); + FileTime d2 = FileTime.from(Instant.now().minus(1, ChronoUnit.MINUTES)); + SimpleArchiveEntry e = new SimpleArchiveEntry(new ArchiveEntryParameters() + .withLastModifiedTime(d1) + .withLastAccessTime(d2)); + assertEquals(d2, e.lastAccessTime()); + } + + @Test + public void nullCreationUsesLastModified() { + FileTime d = FileTime.from(Instant.now()); + SimpleArchiveEntry e = new SimpleArchiveEntry(new ArchiveEntryParameters() + .withLastModifiedTime(d)); + assertEquals(d, e.creationTime()); + } + + @Test + public void creationIsUsedWhenPresent() { + FileTime d1 = FileTime.from(Instant.now()); + FileTime d2 = FileTime.from(Instant.now().minus(1, ChronoUnit.MINUTES)); + SimpleArchiveEntry e = new SimpleArchiveEntry(new ArchiveEntryParameters() + .withLastModifiedTime(d1) + .withCreationTime(d2)); + assertEquals(d2, e.creationTime()); + } + +}