From 8c753a4e0c27124055c9033899df6b18d50f4286 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Tue, 2 Apr 2024 09:01:07 +0200 Subject: [PATCH] Folder close idempotent Signed-off-by: Jorge Bescos Gascon --- api/src/main/java/jakarta/mail/Folder.java | 11 +- .../test/java/jakarta/mail/FolderTest.java | 163 ++++++++++++++++++ 2 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 api/src/test/java/jakarta/mail/FolderTest.java diff --git a/api/src/main/java/jakarta/mail/Folder.java b/api/src/main/java/jakarta/mail/Folder.java index 38dcf8c0..d245c8fa 100644 --- a/api/src/main/java/jakarta/mail/Folder.java +++ b/api/src/main/java/jakarta/mail/Folder.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Vector; import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; /** * Folder is an abstract class that represents a folder for mail @@ -102,6 +103,7 @@ public abstract class Folder implements AutoCloseable { + private final AtomicBoolean closed = new AtomicBoolean(false); /** * The parent store. */ @@ -650,8 +652,13 @@ public abstract boolean delete(boolean recurse) */ @Override public void close() throws MessagingException { - close(true); - q.terminateQueue(); + if (!closed.getAndSet(true)) { + try { + close(true); + } finally { + q.terminateQueue(); + } + } } /** diff --git a/api/src/test/java/jakarta/mail/FolderTest.java b/api/src/test/java/jakarta/mail/FolderTest.java new file mode 100644 index 00000000..b7bbfe3b --- /dev/null +++ b/api/src/test/java/jakarta/mail/FolderTest.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package jakarta.mail; + +import static org.junit.Assert.assertEquals; + +import java.util.Properties; + +import org.junit.Test; + +public class FolderTest { + + @Test + public void closeIdempotent() throws MessagingException { + CustomFolder folder = new CustomFolder(new DummyStore(Session.getDefaultInstance(new Properties()), null)); + folder.close(); + folder.close(); + assertEquals(1, folder.closedTimes); + } + + private static class CustomFolder extends Folder { + + private int closedTimes = 0; + + protected CustomFolder(Store store) { + super(store); + } + + @Override + public String getName() { + return null; + } + + @Override + public String getFullName() { + return null; + } + + @Override + public Folder getParent() throws MessagingException { + return null; + } + + @Override + public boolean exists() throws MessagingException { + return false; + } + + @Override + public Folder[] list(String pattern) throws MessagingException { + return null; + } + + @Override + public char getSeparator() throws MessagingException { + return 0; + } + + @Override + public int getType() throws MessagingException { + return 0; + } + + @Override + public boolean create(int type) throws MessagingException { + return false; + } + + @Override + public boolean hasNewMessages() throws MessagingException { + return false; + } + + @Override + public Folder getFolder(String name) throws MessagingException { + return null; + } + + @Override + public boolean delete(boolean recurse) throws MessagingException { + return false; + } + + @Override + public boolean renameTo(Folder f) throws MessagingException { + return false; + } + + @Override + public void open(int mode) throws MessagingException { + } + + @Override + public void close(boolean expunge) throws MessagingException { + closedTimes++; + } + + @Override + public boolean isOpen() { + return false; + } + + @Override + public Flags getPermanentFlags() { + return null; + } + + @Override + public int getMessageCount() throws MessagingException { + return 0; + } + + @Override + public Message getMessage(int msgnum) throws MessagingException { + return null; + } + + @Override + public void appendMessages(Message[] msgs) throws MessagingException { + } + + @Override + public Message[] expunge() throws MessagingException { + return null; + } + } + + private static class DummyStore extends Store { + + protected DummyStore(Session session, URLName urlname) { + super(session, urlname); + } + + @Override + public Folder getDefaultFolder() throws MessagingException { + return null; + } + + @Override + public Folder getFolder(String name) throws MessagingException { + return null; + } + + @Override + public Folder getFolder(URLName url) throws MessagingException { + return null; + } + } +}