Skip to content

Commit

Permalink
Fix System.setSecurityManager calls for tests
Browse files Browse the repository at this point in the history
Removed System.setSecurityManager calls in testZoneInfoProviderResourceLoading method so that all the test cases can run.
  • Loading branch information
mkamani committed Nov 29, 2024
1 parent c20f2dc commit ddf0074
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
25 changes: 16 additions & 9 deletions src/main/java/org/joda/time/DateTimeZone.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/org/joda/time/Partial.java
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,8 @@ public boolean isMatch(ReadableInstant instant) {
return true;
}



/**
* Does this partial match the specified partial.
* <p>
Expand All @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/joda/time/format/DateTimeParserBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);


}

//-----------------------------------------------------------------------
Expand Down

0 comments on commit ddf0074

Please sign in to comment.