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

Jar entries should not depend on user TZ #185

Merged
merged 1 commit into from
Sep 9, 2024
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
2 changes: 1 addition & 1 deletion takari-lifecycle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
<dependency>
<groupId>io.takari</groupId>
<artifactId>takari-archiver</artifactId>
<version>1.0.0</version>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public AggregateSource(List<Iterable<ExtendedArchiveEntry>> sources) {

@Override
public Iterable<ExtendedArchiveEntry> entries() {
final Predicate<ExtendedArchiveEntry> uniquePathFilter = new Predicate<ExtendedArchiveEntry>() {
final Predicate<ExtendedArchiveEntry> uniquePathFilter = new Predicate<>() {
private final Set<String> entryNames = new HashSet<>();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private Iterable<ExtendedArchiveEntry> jarManifestSource(MavenProject project) t
ByteArrayOutputStream buf = new ByteArrayOutputStream();
manifest.write(buf);

return singleton((ExtendedArchiveEntry) new BytesEntry(MANIFEST_PATH, buf.toByteArray()));
return singleton(new BytesEntry(MANIFEST_PATH, buf.toByteArray()));
}

protected ExtendedArchiveEntry pomPropertiesSource(MavenProject project) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -204,7 +207,7 @@ private static void assertZipEntries(File zipFile, String... expected) throws IO
actualSb.append(entry.isDirectory() ? "D " : "F ")
.append(entry.getName())
.append(' ')
.append(entry.getTime())
.append(dosToJavaTime(entry.getTime()))
.append('\n');
}

Expand All @@ -217,6 +220,18 @@ private static void assertZipEntries(File zipFile, String... expected) throws IO
}
}

/**
* The method {@link ZipEntry#getTime()} "adjusts" the DOS timestamp ("local time") to Java time ("UTC") by using
* "default timezone" of the user. This means, that the method returns different timestamps in different Timezones.
* while the value to assert against is Java UTC time. Meaning, test would pass in some TZ but fail in others.
* Same applies to DST as well. This method merely "undoes" the adjustment, making it into UTC.
*/
private static long dosToJavaTime(long time) {
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.ROOT);
cal.setTimeInMillis(time);
return time + ((cal.get(Calendar.ZONE_OFFSET) + (cal.get(Calendar.DST_OFFSET))));
}

private static <T> List<T> toList(Enumeration<T> e) {
ArrayList<T> l = new ArrayList<T>();
while (e.hasMoreElements()) {
Expand Down