Throw an IllegalArgumentException once a ClassCastException occurs.

Closes #11775
This commit is contained in:
Alexander Schwartz 2022-04-29 17:12:26 +02:00 committed by Hynek Mlnařík
parent ca2c60551d
commit bfab03b837
3 changed files with 60 additions and 7 deletions

View file

@ -145,7 +145,7 @@ public class MapRootAuthenticationSessionProvider implements AuthenticationSessi
DefaultModelCriteria<RootAuthenticationSessionModel> mcb = criteria();
mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId())
.compare(SearchableFields.EXPIRATION, Operator.LT, Time.currentTime());
.compare(SearchableFields.EXPIRATION, Operator.LT, (long) Time.currentTime());
long deletedCount = tx.delete(withCriteria(mcb));

View file

@ -16,6 +16,7 @@
*/
package org.keycloak.models.map.storage.chm;
import org.jboss.logging.Logger;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
import java.util.Arrays;
import java.util.Collection;
@ -24,11 +25,8 @@ import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -70,7 +68,7 @@ class CriteriaOperator {
/**
* Returns a predicate {@code P(x)} for comparing {@code value} and {@code x} as {@code x OP value}.
* <b>Implementation note:</b> Note that this may mean reverse logic to e.g. {@link Comparable#compareTo}.
* @param operator
* @param op
* @param value
* @return
*/
@ -261,8 +259,7 @@ class CriteriaOperator {
try {
return o != null && op.isComparisonTrue(cValue.compareTo(o));
} catch (ClassCastException ex) {
LOG.log(Level.WARNING, "Incomparable argument type for comparison operation: {0}", cValue.getClass().getSimpleName());
return false;
throw new IllegalArgumentException("Incomparable argument type for comparison operation: " + cValue.getClass().getSimpleName() + " vs. " + o.getClass().getSimpleName(), ex);
}
}

View file

@ -0,0 +1,56 @@
/*
* Copyright 2022. Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed 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.keycloak.models.map.storage.chm;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import java.util.function.Predicate;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
/**
* @author Alexander Schwartz
*/
public class CriteriaOperatorTest {
@Test
public void shouldCompareLongs() {
Predicate<Object> predicate = CriteriaOperator.ge(new Long[]{0L});
assertTrue(predicate.test(1L));
}
@Test
public void shouldCompareInts() {
Predicate<Object> predicate = CriteriaOperator.ge(new Integer[]{0});
assertTrue(predicate.test(1));
}
@Test
public void shouldThrowAnExceptionOnIncompatibleTypes() {
Predicate<Object> predicate = CriteriaOperator.ge(new Long[]{0L});
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> predicate.test(1));
MatcherAssert.assertThat(exception.getMessage(), CoreMatchers.startsWith("Incomparable argument type for comparison operation"));
}
}