You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2021/06/04 14:14:13 UTC
[jackrabbit-filevault] branch master updated: JCRVLT-526
PackageProperties should be able to deal with different timezone formats
produced by maven plugins (#143)
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git
The following commit(s) were added to refs/heads/master by this push:
new 2ae40f7 JCRVLT-526 PackageProperties should be able to deal with different timezone formats produced by maven plugins (#143)
2ae40f7 is described below
commit 2ae40f7bd6e3378d4e5a98db1d8dd447160dd205
Author: Dr. Hans-Peter Störr <hp...@ist-software.com>
AuthorDate: Fri Jun 4 16:14:03 2021 +0200
JCRVLT-526 PackageProperties should be able to deal with different timezone formats produced by maven plugins (#143)
---
.../packaging/impl/PackagePropertiesImpl.java | 27 +++++++++++---
.../packaging/impl/PackagePropertiesImplTest.java | 42 ++++++++++++++++++++--
2 files changed, 61 insertions(+), 8 deletions(-)
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/PackagePropertiesImpl.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/PackagePropertiesImpl.java
index 31c8ef2..8a56e98 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/PackagePropertiesImpl.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/PackagePropertiesImpl.java
@@ -25,6 +25,8 @@ import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.jackrabbit.util.ISO8601;
import org.apache.jackrabbit.vault.fs.io.AccessControlHandling;
@@ -46,6 +48,9 @@ public abstract class PackagePropertiesImpl implements PackageProperties {
private static final Logger log = LoggerFactory.getLogger(PackagePropertiesImpl.class);
+ /** Matches dates with timezone like +02 or +0200 so that these can be transformed to +02:00 in {@link #getDateProperty(String)}. */
+ private static final Pattern TIMEZONE_FIX_PATTERN = Pattern.compile("(?<dateUptoTzHours>.*[+-]\\d\\d)(?<tzMinutes>\\d\\d)?");
+
private PackageId id;
@Override
@@ -211,22 +216,34 @@ public abstract class PackagePropertiesImpl implements PackageProperties {
return dependenciesLocations;
}
}
-
+
/**
* {@inheritDoc}
*/
@Override
public Calendar getDateProperty(String name) {
+ Calendar result = null;
try {
// TODO: add timezone if not there?
String p = getProperty(name);
- return p == null
- ? null
- : ISO8601.parse(p);
+ if (p != null) {
+ result = ISO8601.parse(p);
+ if (result == null) {
+ // Perhaps this is due to unusual timezone format +02 or +0200 that aren't supported by ISO8601.parse
+ // but sometimes produced by maven plugins, compare https://issues.apache.org/jira/browse/JCRVLT-276
+ Matcher splittedDate = TIMEZONE_FIX_PATTERN.matcher(p);
+ if (splittedDate.matches()) {
+ String tzminutes = splittedDate.group("tzMinutes");
+ tzminutes = tzminutes != null ? tzminutes : "00";
+ String reformattedDate = splittedDate.group("dateUptoTzHours") + ":" + tzminutes;
+ result = ISO8601.parse(reformattedDate);
+ }
+ }
+ }
} catch (Exception e) {
log.error("Error while converting date property", e);
- return null;
}
+ return result;
}
/**
diff --git a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/impl/PackagePropertiesImplTest.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/impl/PackagePropertiesImplTest.java
index eb5c7ad..43a40eb 100644
--- a/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/impl/PackagePropertiesImplTest.java
+++ b/vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/impl/PackagePropertiesImplTest.java
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.vault.packaging.impl;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -38,7 +39,7 @@ public class PackagePropertiesImplTest {
this.properties = new Properties();
this.properties.putAll(properties);
}
-
+
@Override
protected Properties getPropertiesMap() {
return properties;
@@ -49,12 +50,12 @@ public class PackagePropertiesImplTest {
public void testGetDependenciesLocations() throws URISyntaxException {
PackageProperties packageProperties = new SimplePackageProperties(Collections.singletonMap(PackageProperties.NAME_DEPENDENCIES_LOCATIONS, "group1:name1:1.0=maven:com.example.mygroupid:myartifactId:1.0.0:zip"));
MatcherAssert.assertThat(packageProperties.getDependenciesLocations(), Matchers.equalTo(Collections.singletonMap(PackageId.fromString("group1:name1:1.0"), new URI("maven:com.example.mygroupid:myartifactId:1.0.0:zip"))));
-
+
packageProperties = new SimplePackageProperties(Collections.singletonMap(PackageProperties.NAME_DEPENDENCIES_LOCATIONS, "group1:name1:1.0=maven:com.example.mygroupid:myartifactId:1.0.0:zip,group2:name2:2.0=maven:com.example.mygroupid2:myartifactId2:2.0.0:zip,"));
Map<PackageId, URI> expectedDependenciesLocations = new HashMap<>();
expectedDependenciesLocations.put(PackageId.fromString("group1:name1:1.0"), new URI("maven:com.example.mygroupid:myartifactId:1.0.0:zip"));
expectedDependenciesLocations.put(PackageId.fromString("group2:name2:2.0"), new URI("maven:com.example.mygroupid2:myartifactId2:2.0.0:zip"));
-
+
MatcherAssert.assertThat(packageProperties.getDependenciesLocations(), Matchers.equalTo(expectedDependenciesLocations));
}
@@ -76,4 +77,39 @@ public class PackagePropertiesImplTest {
PackageProperties packageProperties = new SimplePackageProperties(Collections.emptyMap());
MatcherAssert.assertThat(packageProperties.getDependenciesLocations(), Matchers.equalTo(Collections.emptyMap()));
}
+
+ /**
+ * Tests the correct parsing of several a bit unusual date formats, see https://issues.apache.org/jira/browse/JCRVLT-526 .
+ * These date formats are e.g. used by the com.day.jcr.vault:content-package-maven-plugin, and thus should be supported.
+ */
+ @Test
+ public void testDateFormat() {
+ checkDateParsing("2021-05-26T15:12:21.673+02:00","2021-05-26T13:12:21.673Z");
+
+ // these are not parsed by org.apache.jackrabbit.util.ISO8601.parse but generated by the maven plugin(s)
+ checkDateParsing("2021-05-26T15:12:21.673+02","2021-05-26T13:12:21.673Z");
+ checkDateParsing("2021-05-26T15:12:21.673-02","2021-05-26T17:12:21.673Z");
+
+ checkDateParsing("2021-05-26T15:12:21.673+0200","2021-05-26T13:12:21.673Z");
+ checkDateParsing("2021-05-26T15:12:21.673-0230","2021-05-26T17:42:21.673Z");
+
+ // missing timezone is treated as UTC. TODO is that right?
+ checkDateParsing("2021-05-26T15:12:21.673","2021-05-26T15:12:21.673Z");
+
+ // check that some edge cases don't break
+ checkDateParsing("nonsense",null);
+ checkDateParsing("",null);
+ MatcherAssert.assertThat(new SimplePackageProperties(Collections.emptyMap()).getCreated(), Matchers.nullValue());
+ }
+
+ protected void checkDateParsing(String original, String expected){
+ PackageProperties packageProperties = new SimplePackageProperties(Collections.singletonMap(PackageProperties.NAME_CREATED, original));
+ Calendar created = packageProperties.getCreated();
+ if (expected != null) {
+ MatcherAssert.assertThat("Date was not be parsed: " + original, created, Matchers.notNullValue());
+ MatcherAssert.assertThat(created.toInstant().toString(), Matchers.equalTo(expected));
+ } else {
+ MatcherAssert.assertThat("Invalid date must return null when parsing: " + original, created, Matchers.nullValue());
+ }
+ }
}