You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by bd...@apache.org on 2022/07/26 22:30:47 UTC

[directory-scimple] branch remove-extra-mapper created (now 26ff0a6)

This is an automated email from the ASF dual-hosted git repository.

bdemers pushed a change to branch remove-extra-mapper
in repository https://gitbox.apache.org/repos/asf/directory-scimple.git


      at 26ff0a6  Remove duplicate Mapper class

This branch includes the following new commits:

     new 26ff0a6  Remove duplicate Mapper class

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[directory-scimple] 01/01: Remove duplicate Mapper class

Posted by bd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bdemers pushed a commit to branch remove-extra-mapper
in repository https://gitbox.apache.org/repos/asf/directory-scimple.git

commit 26ff0a6bd27f01a076767e6e8dc851e8cb0eb485
Author: Brian Demers <bd...@apache.org>
AuthorDate: Tue Jul 26 18:30:42 2022 -0400

    Remove duplicate Mapper class
    
    This resolves a split package issue with this class
---
 .../apache/directory/scim/spec/schema/Mapper.java  |  92 ++----------
 .../directory/scim/spec/schema/MapperTest.java     |  77 ++++++++++
 .../apache/directory/scim/spec/schema/Mapper.java  | 137 -----------------
 .../directory/scim/spec/schema/MapperTest.java     | 162 ---------------------
 4 files changed, 89 insertions(+), 379 deletions(-)

diff --git a/scim-spec/scim-spec-schema/src/main/java/org/apache/directory/scim/spec/schema/Mapper.java b/scim-spec/scim-spec-schema/src/main/java/org/apache/directory/scim/spec/schema/Mapper.java
index 7ab2afb..90f0f86 100644
--- a/scim-spec/scim-spec-schema/src/main/java/org/apache/directory/scim/spec/schema/Mapper.java
+++ b/scim-spec/scim-spec-schema/src/main/java/org/apache/directory/scim/spec/schema/Mapper.java
@@ -22,12 +22,9 @@
  */
 package org.apache.directory.scim.spec.schema;
 
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.SimpleTimeZone;
-import java.util.TimeZone;
-import java.util.regex.Pattern;
+import java.time.Instant;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAccessor;
 
 /**
  * The Mapper provides methods to bi-directionally transform SCIM attribute
@@ -50,88 +47,23 @@ import java.util.regex.Pattern;
  */
 public class Mapper {
 
-  // Pattern for ISO 8601 DateTime - see: https://en.wikipedia.org/wiki/ISO_8601
-  static final String ISO8601_PATTERN = "^([0-9]{4})-([0-9]{2})-([0-9]{2})[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})([Zz]|([+-]{1}[0-9]{2}:[0-9]{2}))";
-
-  // Date time component indexes
-  static final int DATE_COMPONENT_INDEX_YEAR = 1;
-  static final int DATE_COMPONENT_INDEX_MONTH = 2;
-  static final int DATE_COMPONENT_INDEX_DAY = 3;
-  static final int TIME_COMPONENT_INDEX_HOUR = 4;
-  static final int TIME_COMPONENT_INDEX_MINUTE = 5;
-  static final int TIME_COMPONENT_INDEX_SECOND = 6;
-  static final int TIMEZONE_COMPONENT_INDEX = 7;
-
-  // Format string to create an ISO 8601 date string from a Java date object
-  static final String ISO8601_DATETIME_FORMATTER = "yyyy-MM-dd'T'HH:mm:ssXXX";
-
-  SimpleDateFormat iso8601DateFormat;
-  Pattern iso8601Pattern;
+  DateTimeFormatter iso8601DateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
 
   public Mapper() {
-    iso8601DateFormat = new SimpleDateFormat(ISO8601_DATETIME_FORMATTER);
-    iso8601Pattern = Pattern.compile(ISO8601_PATTERN);
   }
 
-  public String convertDateTime(Date date) {
-    return iso8601DateFormat.format(date);
+  public String convertDateTime(Instant instant) {
+    return iso8601DateTimeFormatter.format(instant);
   }
 
   /**
    * Converts an ISO 8601 DateTime string into the equivalent Java Date object.
-   * 
-   * @param date the ISO 8601 DateTime to be converted.
-   * @return the equivalent Java Date object.
-   * @throws ParseException 
+   *
+   * @param isodate the ISO 8601 DateTime to be converted.
+   * @return the equivalent Java Instant object.
    */
-  public Date convertDateTime(String isodate) throws ParseException {
-//    Calendar calendar = null;
-//    Matcher matcher = iso8601Pattern.matcher(date);
-//    if (matcher.matches()) {
-//      TimeZone timeZone = convertTimeZone(matcher.group(TIMEZONE_COMPONENT_INDEX));
-//      calendar = new GregorianCalendar(timeZone);
-//      calendar.set(
-//          Integer.parseInt(matcher.group(DATE_COMPONENT_INDEX_YEAR)),
-//          Integer.parseInt(matcher.group(DATE_COMPONENT_INDEX_MONTH)),
-//          Integer.parseInt(matcher.group(DATE_COMPONENT_INDEX_DAY)),
-//          Integer.parseInt(matcher.group(TIME_COMPONENT_INDEX_HOUR)),
-//          Integer.parseInt(matcher.group(TIME_COMPONENT_INDEX_MINUTE)),
-//          Integer.parseInt(matcher.group(TIME_COMPONENT_INDEX_SECOND))
-//          );
-//    } else {
-//      // TODO - This is an error
-//    }
-//    return calendar.getTime();
-    Date date = iso8601DateFormat.parse(isodate);
-    return date;
+  public Instant convertDateTime(String isodate) {
+    TemporalAccessor temporal = iso8601DateTimeFormatter.parse(isodate);
+    return Instant.from(temporal);
   }
-
-  /**
-   * Converts the timeZone portion of an ISO 8601 date into a Java TimeZone
-   * object.
-   * 
-   * @param timeZone the ISO 8601 representation of the time zone.
-   * @return the equivalent Java TimeZone object.
-   */
-  TimeZone convertTimeZone(String timeZone) {
-    String[] timeZoneTokens = timeZone.split(":");
-    int hours = 0;
-    int minutes = 0;
-
-    if (!timeZone.startsWith("Z") && !timeZone.startsWith("z")) {
-      if (timeZoneTokens.length > 0) {
-        hours = Integer.parseInt(timeZoneTokens[0]);
-      }
-      if (timeZoneTokens.length > 1) {
-        minutes = Integer.parseInt(timeZoneTokens[1]);
-        if (timeZone.startsWith("-")) {
-          minutes = minutes * -1;
-        }
-      }
-    }
-    
-    int timeZoneOffsetMilliSeconds = ((hours * 60) + minutes) * 1000;
-    return new SimpleTimeZone(timeZoneOffsetMilliSeconds, "");
-  }
-
 }
diff --git a/scim-spec/scim-spec-schema/src/test/java/org/apache/directory/scim/spec/schema/MapperTest.java b/scim-spec/scim-spec-schema/src/test/java/org/apache/directory/scim/spec/schema/MapperTest.java
new file mode 100644
index 0000000..d307743
--- /dev/null
+++ b/scim-spec/scim-spec-schema/src/test/java/org/apache/directory/scim/spec/schema/MapperTest.java
@@ -0,0 +1,77 @@
+/*
+* 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.directory.scim.spec.schema;
+
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.text.ParseException;
+import java.time.Instant;
+import java.util.*;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+
+public class MapperTest {
+
+  static final String[] ISO_DATETIME_EXAMPLES = {
+      "2015-04-26T01:37:17+00:00",
+      "2015-04-26T01:37:17Z"
+  };
+
+  static String[] getIsoDateTimeExamples() {
+    return ISO_DATETIME_EXAMPLES;
+  }
+
+  @ParameterizedTest
+  @MethodSource("getIsoDateTimeExamples")
+  public void testConvertDateTimeFromString(String isoDateTime) throws ParseException {
+    Mapper mapper = new Mapper();
+    Instant instant = mapper.convertDateTime(isoDateTime);
+    TimeZone timeZone = new SimpleTimeZone(0, "GMT");
+    GregorianCalendar calendar = new GregorianCalendar(timeZone);
+    calendar.setTime(Date.from(instant));
+    assertEquals(2015, calendar.get(Calendar.YEAR));
+    assertEquals(3, calendar.get(Calendar.MONTH));
+    assertEquals(26, calendar.get(Calendar.DATE));
+    assertEquals(1, calendar.get(Calendar.HOUR_OF_DAY));
+    assertEquals(37, calendar.get(Calendar.MINUTE));
+    assertEquals(17, calendar.get(Calendar.SECOND));
+  }
+  
+  @Test
+  @Disabled
+  public void convertDateTimeFromDate() {
+    Mapper mapper = new Mapper();
+    TimeZone timeZone = new SimpleTimeZone(0, "GMT");
+    GregorianCalendar calendar = new GregorianCalendar(timeZone);
+    calendar.set(Calendar.YEAR, 2015);
+    calendar.set(Calendar.MONTH, 03);
+    calendar.set(Calendar.DATE, 26);
+    calendar.set(Calendar.HOUR_OF_DAY, 1);
+    calendar.set(Calendar.MINUTE, 37);
+    calendar.set(Calendar.SECOND, 17);
+    Instant instant = calendar.toInstant();
+    String actualDateTime = mapper.convertDateTime(instant);
+    assertEquals(ISO_DATETIME_EXAMPLES[0], actualDateTime);
+  }
+}
diff --git a/scim-tools/scim-tools-common/src/main/java/org/apache/directory/scim/spec/schema/Mapper.java b/scim-tools/scim-tools-common/src/main/java/org/apache/directory/scim/spec/schema/Mapper.java
deleted file mode 100644
index 7ab2afb..0000000
--- a/scim-tools/scim-tools-common/src/main/java/org/apache/directory/scim/spec/schema/Mapper.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
-* 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.directory.scim.spec.schema;
-
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.SimpleTimeZone;
-import java.util.TimeZone;
-import java.util.regex.Pattern;
-
-/**
- * The Mapper provides methods to bi-directionally transform SCIM attribute
- * values into Java types. The eight types supported by SCIM are defined in
- * section 2.2 of the SCIM schema specification. The mapping to Java objects are
- * as follows:
- * 
- * <pre>
- *   String    -> string    -> String
- *   Boolean   -> boolean   -> Boolean
- *   Decimal   -> decimal   -> Double
- *   Integer -  > integer   -> Long
- *   DateTime  -> dateTime  -> Date
- *   Binary    -> binary    -> Byte[]
- *   Reference -> reference -> URN?
- *   Complex   -> complex   -> (Java Object as defined)
- * </pre>
- * 
- * @author Steve Moyer
- */
-public class Mapper {
-
-  // Pattern for ISO 8601 DateTime - see: https://en.wikipedia.org/wiki/ISO_8601
-  static final String ISO8601_PATTERN = "^([0-9]{4})-([0-9]{2})-([0-9]{2})[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})([Zz]|([+-]{1}[0-9]{2}:[0-9]{2}))";
-
-  // Date time component indexes
-  static final int DATE_COMPONENT_INDEX_YEAR = 1;
-  static final int DATE_COMPONENT_INDEX_MONTH = 2;
-  static final int DATE_COMPONENT_INDEX_DAY = 3;
-  static final int TIME_COMPONENT_INDEX_HOUR = 4;
-  static final int TIME_COMPONENT_INDEX_MINUTE = 5;
-  static final int TIME_COMPONENT_INDEX_SECOND = 6;
-  static final int TIMEZONE_COMPONENT_INDEX = 7;
-
-  // Format string to create an ISO 8601 date string from a Java date object
-  static final String ISO8601_DATETIME_FORMATTER = "yyyy-MM-dd'T'HH:mm:ssXXX";
-
-  SimpleDateFormat iso8601DateFormat;
-  Pattern iso8601Pattern;
-
-  public Mapper() {
-    iso8601DateFormat = new SimpleDateFormat(ISO8601_DATETIME_FORMATTER);
-    iso8601Pattern = Pattern.compile(ISO8601_PATTERN);
-  }
-
-  public String convertDateTime(Date date) {
-    return iso8601DateFormat.format(date);
-  }
-
-  /**
-   * Converts an ISO 8601 DateTime string into the equivalent Java Date object.
-   * 
-   * @param date the ISO 8601 DateTime to be converted.
-   * @return the equivalent Java Date object.
-   * @throws ParseException 
-   */
-  public Date convertDateTime(String isodate) throws ParseException {
-//    Calendar calendar = null;
-//    Matcher matcher = iso8601Pattern.matcher(date);
-//    if (matcher.matches()) {
-//      TimeZone timeZone = convertTimeZone(matcher.group(TIMEZONE_COMPONENT_INDEX));
-//      calendar = new GregorianCalendar(timeZone);
-//      calendar.set(
-//          Integer.parseInt(matcher.group(DATE_COMPONENT_INDEX_YEAR)),
-//          Integer.parseInt(matcher.group(DATE_COMPONENT_INDEX_MONTH)),
-//          Integer.parseInt(matcher.group(DATE_COMPONENT_INDEX_DAY)),
-//          Integer.parseInt(matcher.group(TIME_COMPONENT_INDEX_HOUR)),
-//          Integer.parseInt(matcher.group(TIME_COMPONENT_INDEX_MINUTE)),
-//          Integer.parseInt(matcher.group(TIME_COMPONENT_INDEX_SECOND))
-//          );
-//    } else {
-//      // TODO - This is an error
-//    }
-//    return calendar.getTime();
-    Date date = iso8601DateFormat.parse(isodate);
-    return date;
-  }
-
-  /**
-   * Converts the timeZone portion of an ISO 8601 date into a Java TimeZone
-   * object.
-   * 
-   * @param timeZone the ISO 8601 representation of the time zone.
-   * @return the equivalent Java TimeZone object.
-   */
-  TimeZone convertTimeZone(String timeZone) {
-    String[] timeZoneTokens = timeZone.split(":");
-    int hours = 0;
-    int minutes = 0;
-
-    if (!timeZone.startsWith("Z") && !timeZone.startsWith("z")) {
-      if (timeZoneTokens.length > 0) {
-        hours = Integer.parseInt(timeZoneTokens[0]);
-      }
-      if (timeZoneTokens.length > 1) {
-        minutes = Integer.parseInt(timeZoneTokens[1]);
-        if (timeZone.startsWith("-")) {
-          minutes = minutes * -1;
-        }
-      }
-    }
-    
-    int timeZoneOffsetMilliSeconds = ((hours * 60) + minutes) * 1000;
-    return new SimpleTimeZone(timeZoneOffsetMilliSeconds, "");
-  }
-
-}
diff --git a/scim-tools/scim-tools-common/src/test/java/org/apache/directory/scim/spec/schema/MapperTest.java b/scim-tools/scim-tools-common/src/test/java/org/apache/directory/scim/spec/schema/MapperTest.java
deleted file mode 100644
index 136d325..0000000
--- a/scim-tools/scim-tools-common/src/test/java/org/apache/directory/scim/spec/schema/MapperTest.java
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
-* 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.directory.scim.spec.schema;
-
-import java.text.ParseException;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.GregorianCalendar;
-import java.util.SimpleTimeZone;
-import java.util.TimeZone;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.junit.jupiter.api.Disabled;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.CsvSource;
-import org.junit.jupiter.params.provider.MethodSource;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assumptions.assumeTrue;
-
-
-public class MapperTest {
-
-  static final String[] ISO_DATETIME_EXAMPLES = {
-      "2015-04-26T01:37:17+00:00",
-      "2015-04-26T01:37:17Z"
-  };
-
-  static String[] getIsoDateTimeExamples() {
-    return ISO_DATETIME_EXAMPLES;
-  }
-
-  /**
-   * Tests that the regular expression provided used to decompose an ISO 8601
-   * DateTime string works with the examples from wikipedia.
-   * 
-   * @param isoDateTime a String[] of examples to test.
-   */
-  @ParameterizedTest
-  @MethodSource("getIsoDateTimeExamples")
-  public void testDateTimePatternWorksForIso8601Strings(String isoDateTime) {
-    Pattern pattern = Pattern.compile(Mapper.ISO8601_PATTERN);
-    assertNotNull(pattern);
-    Matcher matcher = pattern.matcher(isoDateTime);
-    assertTrue(matcher.matches());
-  }
-
-  /**
-   * Tests that the static final indexes in the Mapper class correspond to the
-   * correct matching groups in the ISO 8601 regular expression.
-   * 
-   * @param isoDateTime a String[] of examples to test.
-   */
-  @ParameterizedTest
-  @MethodSource("getIsoDateTimeExamples")
-  public void testDateTimeGroupIndexesProvideCorrectSubstrings(String isoDateTime) {
-    Pattern pattern = Pattern.compile(Mapper.ISO8601_PATTERN);
-    Matcher matcher = pattern.matcher(isoDateTime);
-    assumeTrue(matcher.matches());
-    assertEquals("2015", matcher.group(Mapper.DATE_COMPONENT_INDEX_YEAR));
-    assertEquals("04", matcher.group(Mapper.DATE_COMPONENT_INDEX_MONTH));
-    assertEquals("26", matcher.group(Mapper.DATE_COMPONENT_INDEX_DAY));
-    assertEquals("01", matcher.group(Mapper.TIME_COMPONENT_INDEX_HOUR));
-    assertEquals("37", matcher.group(Mapper.TIME_COMPONENT_INDEX_MINUTE));
-    assertEquals("17", matcher.group(Mapper.TIME_COMPONENT_INDEX_SECOND));
-    assertTrue("+00:00".equals(matcher.group(Mapper.TIMEZONE_COMPONENT_INDEX)) ||
-        "Z".equals(matcher.group(Mapper.TIMEZONE_COMPONENT_INDEX)));
-  }
-  
-  @ParameterizedTest
-  @MethodSource("getIsoDateTimeExamples")
-  public void testConvertDateTimeFromString(String isoDateTime) throws ParseException {
-    Mapper mapper = new Mapper();
-    Date date = mapper.convertDateTime(isoDateTime);
-    TimeZone timeZone = new SimpleTimeZone(0, "GMT");
-    GregorianCalendar calendar = new GregorianCalendar(timeZone);
-    calendar.setTime(date);
-    assertEquals(2015, calendar.get(Calendar.YEAR));
-    assertEquals(3, calendar.get(Calendar.MONTH));
-    assertEquals(26, calendar.get(Calendar.DATE));
-    assertEquals(1, calendar.get(Calendar.HOUR_OF_DAY));
-    assertEquals(37, calendar.get(Calendar.MINUTE));
-    assertEquals(17, calendar.get(Calendar.SECOND));
-  }
-  
-  @Test
-  @Disabled
-  public void convertDateTimeFromDate() {
-    Mapper mapper = new Mapper();
-    TimeZone timeZone = new SimpleTimeZone(0, "GMT");
-    GregorianCalendar calendar = new GregorianCalendar(timeZone);
-    calendar.set(Calendar.YEAR, 2015);
-    calendar.set(Calendar.MONTH, 03);
-    calendar.set(Calendar.DATE, 26);
-    calendar.set(Calendar.HOUR_OF_DAY, 1);
-    calendar.set(Calendar.MINUTE, 37);
-    calendar.set(Calendar.SECOND, 17);
-    Date date = calendar.getTime();
-    String actualDateTime = mapper.convertDateTime(date);
-    assertEquals(ISO_DATETIME_EXAMPLES[0], actualDateTime);
-  }
-
-  /**
-   * Tests that the convertTimeZone() method properly sets the raw offset of
-   * the Java TimeZone object (ignoring locality so there are no IDs provided).
-   * 
-   * @param isoTimeZone an ISO 8601 formatted timezone string.
-   * @param expectedHours the expected hours.
-   * @param expectedMinutes the expected minutes.
-   */
-  @ParameterizedTest
-  @CsvSource({
-      " 00:00,   0,    0",
-      "+00:00,   0,    0",
-      "-00:00,  -0,    0",
-      " 00:30,   0,   30",
-      "+00:30,   0,   30",
-      "-00:30,   0,  -30",
-      " 05:00,   5,    0",
-      "+05:00,   5,    0",
-      "-05:00,  -5,    0",
-      " 05:30,   5,   30",
-      "+05:30,   5,   30",
-      "-05:30,  -5,  -30",
-      " 00,      0,    0",
-      "+00,      0,    0",
-      "-00,      0,    0",
-      "+05,      5,    0",
-      "-05,     -5,    0",
-  })
-  public void testConvertTimeZone(String isoTimeZone, int expectedHours, int expectedMinutes) {
-    Mapper mapper = new Mapper();
-    TimeZone timeZone = mapper.convertTimeZone(isoTimeZone);
-    int actualOffsetMinutes = timeZone.getRawOffset() / 1000;
-    int actualHours = actualOffsetMinutes / 60;
-    int actualMinutes = actualOffsetMinutes % 60;
-    assertEquals(expectedHours, actualHours);
-    assertEquals(expectedMinutes, actualMinutes);
-  }
-
-}