From ac20fce67340d966fabd755baa2502e678514ec9 Mon Sep 17 00:00:00 2001 From: Jiandong Date: Thu, 12 Jun 2025 22:44:54 +0800 Subject: [PATCH] GH-10100: Fix NPE in `AbstractMailReceiver.obtainFolderInstance()` Fixes: #10083 Issue link: https://github.com/spring-projects/spring-integration/issues/10100 if `url` is not provided, use `store.getDefaultFolder()` instead. Signed-off-by: Jiandong Ma (cherry picked from commit cbb0c6fa042a03840c6cae0008c5b3650301b9d5) --- .../integration/mail/AbstractMailReceiver.java | 6 +++++- .../springframework/integration/mail/MailReceiverTests.java | 5 ++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java b/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java index 2086d930d2..de1c9085ea 100755 --- a/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java +++ b/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -69,6 +69,7 @@ import org.springframework.util.ObjectUtils; * @author Yuxin Wang * @author Ngoc Nhan * @author Filip Hrisafov + * @author Jiandong Ma */ public abstract class AbstractMailReceiver extends IntegrationObjectSupport implements MailReceiver, DisposableBean { @@ -370,6 +371,9 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl } private Folder obtainFolderInstance() throws MessagingException { + if (this.url == null) { + return this.store.getDefaultFolder(); + } return this.store.getFolder(this.url); } diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/MailReceiverTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/MailReceiverTests.java index 4de6537ee1..752a1b8319 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/MailReceiverTests.java +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/MailReceiverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 the original author or authors. + * Copyright 2014-2025 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. @@ -22,7 +22,6 @@ import jakarta.mail.Folder; import jakarta.mail.Message; import jakarta.mail.Session; import jakarta.mail.Store; -import jakarta.mail.URLName; import org.junit.jupiter.api.Test; import org.springframework.beans.DirectFieldAccessor; @@ -68,7 +67,7 @@ public class MailReceiverTests { Folder folder = mock(Folder.class); when(folder.exists()).thenReturn(true); when(folder.isOpen()).thenReturn(false, true); - doReturn(folder).when(store).getFolder((URLName) null); + doReturn(folder).when(store).getDefaultFolder(); doNothing().when(store).connect(); receiver.openFolder(); receiver.openFolder();