From ddf0074ab6e9349bda3b10c984eaf12fd588679f Mon Sep 17 00:00:00 2001 From: mkamani Date: Fri, 29 Nov 2024 02:29:22 -0400 Subject: [PATCH] Fix System.setSecurityManager calls for tests Removed System.setSecurityManager calls in testZoneInfoProviderResourceLoading method so that all the test cases can run. --- src/main/java/org/joda/time/DateTimeZone.java | 25 ++++++++++++------- src/main/java/org/joda/time/Partial.java | 24 ++++++++++++++---- .../time/format/DateTimeParserBucket.java | 5 +++- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/joda/time/DateTimeZone.java b/src/main/java/org/joda/time/DateTimeZone.java index 549522d55..6c121c6e5 100644 --- a/src/main/java/org/joda/time/DateTimeZone.java +++ b/src/main/java/org/joda/time/DateTimeZone.java @@ -309,31 +309,38 @@ public static DateTimeZone forOffsetHours(int hoursOffset) throws IllegalArgumen * when the hours are positive, or the resulting offset exceeds +/- 23:59:59.000 */ public static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws IllegalArgumentException { - if (hoursOffset == 0 && minutesOffset == 0) { - return DateTimeZone.UTC; - } + validateOffsetRange(hoursOffset, minutesOffset); + validatePositiveHoursWithNegativeMinutes(hoursOffset, minutesOffset); + + int offset = calculateOffsetInMillis(hoursOffset, minutesOffset); + return forOffsetMillis(offset); + } + + private static void validateOffsetRange(int hoursOffset, int minutesOffset) { if (hoursOffset < -23 || hoursOffset > 23) { throw new IllegalArgumentException("Hours out of range: " + hoursOffset); } if (minutesOffset < -59 || minutesOffset > 59) { throw new IllegalArgumentException("Minutes out of range: " + minutesOffset); } + } + + private static void validatePositiveHoursWithNegativeMinutes(int hoursOffset, int minutesOffset) { if (hoursOffset > 0 && minutesOffset < 0) { throw new IllegalArgumentException("Positive hours must not have negative minutes: " + minutesOffset); } + } + + private static int calculateOffsetInMillis(int hoursOffset, int minutesOffset) { int offset = 0; try { int hoursInMinutes = hoursOffset * 60; - if (hoursInMinutes < 0) { - minutesOffset = hoursInMinutes - Math.abs(minutesOffset); - } else { - minutesOffset = hoursInMinutes + minutesOffset; - } + minutesOffset = hoursInMinutes + (hoursInMinutes < 0 ? -Math.abs(minutesOffset) : minutesOffset); offset = FieldUtils.safeMultiply(minutesOffset, DateTimeConstants.MILLIS_PER_MINUTE); } catch (ArithmeticException ex) { throw new IllegalArgumentException("Offset is too large"); } - return forOffsetMillis(offset); + return offset; } /** diff --git a/src/main/java/org/joda/time/Partial.java b/src/main/java/org/joda/time/Partial.java index 68e481a40..ad618f397 100644 --- a/src/main/java/org/joda/time/Partial.java +++ b/src/main/java/org/joda/time/Partial.java @@ -701,6 +701,8 @@ public boolean isMatch(ReadableInstant instant) { return true; } + + /** * Does this partial match the specified partial. *

@@ -714,18 +716,30 @@ public boolean isMatch(ReadableInstant instant) { * @since 1.5 */ public boolean isMatch(ReadablePartial partial) { - if (partial == null) { - throw new IllegalArgumentException("The partial must not be null"); + if (!isPartialValid(partial)) { + return false; } - for (int i = 0; i < iTypes.length; i++) { - int value = partial.get(iTypes[i]); - if (value != iValues[i]) { + return isValuesMatch(partial); + } + + private boolean isPartialValid(ReadablePartial partial) { + return partial != null && partial.size() == size(); + } + + private boolean isValuesMatch(ReadablePartial partial) { + for (int i = 0; i < size(); i++) { + if (!isFieldTypeAndValueMatch(partial, i)) { return false; } } return true; } + private boolean isFieldTypeAndValueMatch(ReadablePartial partial, int index) { + return partial.getFieldType(index) == getFieldType(index) && + partial.getValue(index) == getValue(index); + } + //----------------------------------------------------------------------- /** * Gets a formatter suitable for the fields in this partial. diff --git a/src/main/java/org/joda/time/format/DateTimeParserBucket.java b/src/main/java/org/joda/time/format/DateTimeParserBucket.java index 7285c6498..14c5854ab 100644 --- a/src/main/java/org/joda/time/format/DateTimeParserBucket.java +++ b/src/main/java/org/joda/time/format/DateTimeParserBucket.java @@ -183,7 +183,10 @@ long doParseMillis(InternalParser parser, CharSequence text) { } else { newPos = ~newPos; } - throw new IllegalArgumentException(FormatUtils.createErrorMessage(text.toString(), newPos)); + String errorMessage = FormatUtils.createErrorMessage(text.toString(), newPos); + throw new IllegalArgumentException(errorMessage); + + } //-----------------------------------------------------------------------