From 48398e501e7547312479975e516ff38ea5b668a5 Mon Sep 17 00:00:00 2001 From: tseyzer Date: Mon, 24 Aug 2015 16:40:06 +0200 Subject: [PATCH 1/2] XSDuration in EWSUtilities fix --- .../webservices/data/core/EwsUtilities.java | 167 ++---------------- .../TimeSpanPropertyDefinition.java | 2 +- .../webservices/data/core/XSDurationTest.java | 81 +++++++++ 3 files changed, 93 insertions(+), 157 deletions(-) create mode 100644 src/test/java/microsoft/exchange/webservices/data/core/XSDurationTest.java diff --git a/src/main/java/microsoft/exchange/webservices/data/core/EwsUtilities.java b/src/main/java/microsoft/exchange/webservices/data/core/EwsUtilities.java index 374b96caf..c822d9bf1 100644 --- a/src/main/java/microsoft/exchange/webservices/data/core/EwsUtilities.java +++ b/src/main/java/microsoft/exchange/webservices/data/core/EwsUtilities.java @@ -50,8 +50,11 @@ import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException; import microsoft.exchange.webservices.data.misc.TimeSpan; import microsoft.exchange.webservices.data.property.complex.ItemAttachment; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.joda.time.Period; +import org.joda.time.format.ISOPeriodFormat; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; @@ -875,170 +878,22 @@ public static TimeSpan getXSDurationToTimeSpan(String xsDuration) { negative = true; } - // Year - m = PATTERN_YEAR.matcher(xsDuration); - int year = 0; - if (m.find()) { - year = Integer.parseInt(m.group().substring(0, - m.group().indexOf("Y"))); - } - - // Month - m = PATTERN_MONTH.matcher(xsDuration); - int month = 0; - if (m.find()) { - month = Integer.parseInt(m.group().substring(0, - m.group().indexOf("M"))); - } - - // Day - m = PATTERN_DAY.matcher(xsDuration); - int day = 0; - if (m.find()) { - day = Integer.parseInt(m.group().substring(0, - m.group().indexOf("D"))); - } - - // Hour - m = PATTERN_HOUR.matcher(xsDuration); - int hour = 0; - if (m.find()) { - hour = Integer.parseInt(m.group().substring(0, - m.group().indexOf("H"))); - } - - // Minute - m = PATTERN_MINUTES.matcher(xsDuration); - int minute = 0; - if (m.find()) { - minute = Integer.parseInt(m.group().substring(0, - m.group().indexOf("M"))); - } - - // Seconds - m = PATTERN_SECONDS.matcher(xsDuration); - int seconds = 0; - if (m.find()) { - seconds = Integer.parseInt(m.group().substring(0, - m.group().indexOf("."))); - } - - int milliseconds = 0; - m = PATTERN_MILLISECONDS.matcher(xsDuration); - if (m.find()) { - // Only allowed 4 digits of precision - if (m.group().length() > 5) { - milliseconds = Integer.parseInt(m.group().substring(0, 4)); - } else { - seconds = Integer.parseInt(m.group().substring(0, - m.group().indexOf("S"))); - } - } - - // Apply conversions of year and months to days. - // Year = 365 days - // Month = 30 days - day = day + (year * 365) + (month * 30); - // TimeSpan retval = new TimeSpan(day, hour, minute, seconds, - // milliseconds); - long retval = (((((((day * 24) + hour) * 60) + minute) * 60) + - seconds) * 1000) + milliseconds; + // Removing leading '-' if (negative) { - retval = -retval; - } - return new TimeSpan(retval); - - } - - /** - * Takes an xs:duration string as defined by the W3 Consortiums - * Recommendation "XML Schema Part 2: Datatypes Second Edition", - * http://www.w3.org/TR/xmlschema-2/#duration, and converts it into a - * System.TimeSpan structure This method uses the following approximations: - * 1 year = 365 days 1 month = 30 days Additionally, it only allows for four - * decimal points of seconds precision. - * - * @param xsDuration xs:duration string to convert - * @return System.TimeSpan structure - */ - public static TimeSpan getXSDurationToTimeSpanValue(String xsDuration) { - // TODO: Need to check whether this should be the equivalent or not - Matcher m = PATTERN_TIME_SPAN.matcher(xsDuration); - boolean negative = false; - if (m.find()) { - negative = true; - } - - // Year - // m = Pattern.compile("(\\d+)Y").matcher(xsDuration); - // int year = 0; - // if (m.find()) { - // year = Integer.parseInt(m.group().substring(0, - // m.group().indexOf("Y"))); - // } - - // Month - // m = Pattern.compile("(\\d+)M").matcher(xsDuration); - // int month = 0; - // if (m.find()) { - // month = Integer.parseInt(m.group().substring(0, - // m.group().indexOf("M"))); - // } - - // Day - m = PATTERN_DAY.matcher(xsDuration); - long day = 0; - if (m.find()) { - day = Integer.parseInt(m.group().substring(0, - m.group().indexOf("D"))); - } - - // Hour - m = PATTERN_HOUR.matcher(xsDuration); - int hour = 0; - if (m.find()) { - hour = Integer.parseInt(m.group().substring(0, - m.group().indexOf("H"))); - } - - // Minute - m = PATTERN_MINUTES.matcher(xsDuration); - int minute = 0; - if (m.find()) { - minute = Integer.parseInt(m.group().substring(0, - m.group().indexOf("M"))); - } - - // Seconds - m = PATTERN_SECONDS.matcher(xsDuration); - int seconds = 0; - int milliseconds = 0; - m = PATTERN_MILLISECONDS.matcher(xsDuration); - if (m.find()) { - // Only allowed 4 digits of precision - if (m.group().length() > 5) { - milliseconds = Integer.parseInt(m.group().substring(0, 4)); - } else { - seconds = Integer.parseInt(m.group().substring(0, m.group().indexOf("S"))); - } + xsDuration = xsDuration.replace("-P", "P"); } - // Apply conversions of year and months to days. - // Year = 365 days - // Month = 30 days - // day = day + (year * 365) + (month * 30); - //TimeSpan retval = new TimeSpan(day, hour, minute, seconds, - // milliseconds); - - long retval = - day * TimeSpan.DAYS + hour * TimeSpan.HOURS + minute * TimeSpan.MINUTES + seconds * TimeSpan.SECONDS - + milliseconds * TimeSpan.MILLISECONDS; + Period period = Period.parse(xsDuration, ISOPeriodFormat.standard()); + + long retval = period.toStandardDuration().getMillis(); + if (negative) { retval = -retval; } + return new TimeSpan(retval); - } + } /** * Time span to xs time. diff --git a/src/main/java/microsoft/exchange/webservices/data/property/definition/TimeSpanPropertyDefinition.java b/src/main/java/microsoft/exchange/webservices/data/property/definition/TimeSpanPropertyDefinition.java index 1727be3ed..8a979eb4b 100644 --- a/src/main/java/microsoft/exchange/webservices/data/property/definition/TimeSpanPropertyDefinition.java +++ b/src/main/java/microsoft/exchange/webservices/data/property/definition/TimeSpanPropertyDefinition.java @@ -57,7 +57,7 @@ public TimeSpanPropertyDefinition(String xmlElementName, String uri, EnumSet Date: Mon, 24 Aug 2015 17:45:07 +0200 Subject: [PATCH 2/2] XSDurationTest replacing constants with inline strings --- .../webservices/data/core/XSDurationTest.java | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/test/java/microsoft/exchange/webservices/data/core/XSDurationTest.java b/src/test/java/microsoft/exchange/webservices/data/core/XSDurationTest.java index be2a3edfe..f18445d6f 100644 --- a/src/test/java/microsoft/exchange/webservices/data/core/XSDurationTest.java +++ b/src/test/java/microsoft/exchange/webservices/data/core/XSDurationTest.java @@ -32,50 +32,40 @@ public class XSDurationTest { // Tests for EwsUtilities.getXSDurationToTimeSpan() - private static final String PERIOD_HOURS = "-PT13H"; - private static final String PERIOD_HOURS_MINUTES = "-PT5H30M"; - private static final String PERIOD_FULL = "PT2H30M59.0S"; - private static final String PERIOD_FULL_NEGATIVE = "-PT2H30M59.0S"; - private static final String PERIOD_OVERFLOW = "PT2H100M59.0S"; - private static final String PERIOD_FAIL = "P2H30M59.0S"; - - @Test public void testPeriodHours() { - TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan(PERIOD_HOURS); + TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan("-PT13H"); Assert.assertEquals("-P0DT13H0M0.0S", EwsUtilities.getTimeSpanToXSDuration(timeSpan)); } @Test public void testPeriodHoursMinutes() { - TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan(PERIOD_HOURS_MINUTES); + TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan("-PT5H30M"); Assert.assertEquals("-P0DT5H30M0.0S", EwsUtilities.getTimeSpanToXSDuration(timeSpan)); } @Test public void testPeriodFull() { - TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan(PERIOD_FULL); + TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan("PT2H30M59.0S"); Assert.assertEquals("P0DT2H30M59.0S", EwsUtilities.getTimeSpanToXSDuration(timeSpan)); } @Test public void testPeriodFullNegative() { - TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan(PERIOD_FULL_NEGATIVE); + TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan("-PT2H30M59.0S"); Assert.assertEquals("-P0DT2H30M59.0S", EwsUtilities.getTimeSpanToXSDuration(timeSpan)); } - + @Test public void testPeriodFail2() { - TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan(PERIOD_OVERFLOW); + TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan("PT2H100M59.0S"); Assert.assertEquals("P0DT3H40M59.0S", EwsUtilities.getTimeSpanToXSDuration(timeSpan)); } - + @Test(expected = IllegalArgumentException.class) public void testPeriodFail() { - TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan(PERIOD_FAIL); + TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan("P2H30M59.0S"); Assert.assertEquals("-P0DT2H30M59.0S", EwsUtilities.getTimeSpanToXSDuration(timeSpan)); } - - }