From 1889a4c64e3c0ccdd9b90de0795af26f6c849e97 Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Wed, 5 Oct 2022 22:15:57 +0200 Subject: [PATCH] Use standard Spring utils in MongoDB module This commit replaces usages of custom assert utility class with standard one from Spring Framework, and removes the custom utility. See gh-2170 --- .../integration/AbstractClassLoaderTest.java | 8 ++-- .../mongo/AbstractMongoSessionConverter.java | 6 ++- .../session/data/mongo/Assert.java | 40 ------------------- .../mongo/MongoIndexedSessionRepository.java | 8 ++-- 4 files changed, 12 insertions(+), 50 deletions(-) delete mode 100644 spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/Assert.java diff --git a/spring-session-data-mongodb/src/integration-test/java/org/springframework/session/data/mongo/integration/AbstractClassLoaderTest.java b/spring-session-data-mongodb/src/integration-test/java/org/springframework/session/data/mongo/integration/AbstractClassLoaderTest.java index 2a02654c..81ba6530 100644 --- a/spring-session-data-mongodb/src/integration-test/java/org/springframework/session/data/mongo/integration/AbstractClassLoaderTest.java +++ b/spring-session-data-mongodb/src/integration-test/java/org/springframework/session/data/mongo/integration/AbstractClassLoaderTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.core.serializer.DefaultDeserializer; import org.springframework.core.serializer.support.DeserializingConverter; import org.springframework.session.data.mongo.AbstractMongoSessionConverter; -import org.springframework.session.data.mongo.Assert; import org.springframework.session.data.mongo.JdkMongoSessionConverter; import org.springframework.util.ReflectionUtils; @@ -49,8 +48,7 @@ public abstract class AbstractClassLoaderTest extends AbstractITest { Field mongoSessionConverterField = ReflectionUtils.findField(this.sessionRepository.getClass(), "mongoSessionConverter"); - ReflectionUtils.makeAccessible( - Assert.requireNonNull(mongoSessionConverterField, "mongoSessionConverter must not be null!")); + ReflectionUtils.makeAccessible(mongoSessionConverterField); AbstractMongoSessionConverter sessionConverter = (AbstractMongoSessionConverter) ReflectionUtils .getField(mongoSessionConverterField, this.sessionRepository); @@ -70,7 +68,7 @@ public abstract class AbstractClassLoaderTest extends AbstractITest { private static Object extractField(Class clazz, String fieldName, Object obj) { Field field = ReflectionUtils.findField(clazz, fieldName); - ReflectionUtils.makeAccessible(Assert.requireNonNull(field, fieldName + " must not be null!")); + ReflectionUtils.makeAccessible(field); return ReflectionUtils.getField(field, obj); } diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java index 2245b7a5..da1047ed 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/AbstractMongoSessionConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ import org.springframework.session.DelegatingIndexResolver; import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.IndexResolver; import org.springframework.session.PrincipalNameIndexResolver; +import org.springframework.util.Assert; /** * Base class for serializing and deserializing session objects. To create custom @@ -123,7 +124,8 @@ public abstract class AbstractMongoSessionConverter implements GenericConverter protected abstract MongoSession convert(Document sessionWrapper); public void setIndexResolver(IndexResolver indexResolver) { - this.indexResolver = Assert.requireNonNull(indexResolver, "indexResolver must not be null!"); + Assert.notNull(indexResolver, "indexResolver must not be null"); + this.indexResolver = indexResolver; } } diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/Assert.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/Assert.java deleted file mode 100644 index bb599b96..00000000 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/Assert.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2019 the original author or authors. - * - * 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 - * - * https://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.springframework.session.data.mongo; - -import org.springframework.lang.Nullable; - -/** - * Utility to verify non null fields. - * - * @author Greg Turnquist - */ -public final class Assert { - - private Assert() { - } - - public static T requireNonNull(@Nullable T item, String message) { - - if (item == null) { - throw new IllegalArgumentException(message); - } - - return item; - } - -} diff --git a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java index 301eacaf..3c5f5aff 100644 --- a/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java +++ b/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoIndexedSessionRepository.java @@ -22,6 +22,7 @@ import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; +import com.mongodb.DBObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.bson.Document; @@ -38,6 +39,7 @@ import org.springframework.session.MapSession; import org.springframework.session.events.SessionCreatedEvent; import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionExpiredEvent; +import org.springframework.util.Assert; /** * Session repository implementation which stores sessions in Mongo. Uses @@ -97,9 +99,9 @@ public class MongoIndexedSessionRepository @Override public void save(MongoSession session) { - this.mongoOperations - .save(Assert.requireNonNull(MongoSessionUtils.convertToDBObject(this.mongoSessionConverter, session), - "convertToDBObject must not null!"), this.collectionName); + DBObject dbObject = MongoSessionUtils.convertToDBObject(this.mongoSessionConverter, session); + Assert.notNull(dbObject, "dbObject must not be null"); + this.mongoOperations.save(dbObject, this.collectionName); } @Override