This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit f974e558ad1727281492abd1bed20590d21cf951 Author: Quan Tran <[email protected]> AuthorDate: Thu Jun 4 14:10:01 2026 +0700 [REFACTORING] Split IMAPServerConnectionCheckTest --- .../netty/IMAPServerConnectionCheckTest.java | 93 ++++++++++++++++++++++ .../james/imapserver/netty/IMAPServerTest.java | 59 -------------- 2 files changed, 93 insertions(+), 59 deletions(-) diff --git a/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPServerConnectionCheckTest.java b/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPServerConnectionCheckTest.java new file mode 100644 index 0000000000..b1ee47846f --- /dev/null +++ b/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPServerConnectionCheckTest.java @@ -0,0 +1,93 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you 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.apache.james.imapserver.netty; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import java.util.Set; + +import org.apache.commons.configuration2.HierarchicalConfiguration; +import org.apache.commons.configuration2.tree.ImmutableNode; +import org.apache.james.protocols.lib.mock.ConfigLoader; +import org.apache.james.util.ClassLoaderUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +@SuppressWarnings("checkstyle:membername") +class IMAPServerConnectionCheckTest extends AbstractIMAPServerTest { + + IMAPServer imapServer; + private int port; + + @BeforeEach + void beforeEach() throws Exception { + HierarchicalConfiguration<ImmutableNode> config = ConfigLoader.getConfig(ClassLoaderUtils.getSystemResourceAsSharedStream("imapServerImapConnectCheck.xml")); + imapServer = createImapServer(config); + port = imapServer.getListenAddresses().get(0).getPort(); + } + + @AfterEach + void tearDown() { + imapServer.destroy(); + } + + @Test + void banIpWhenBannedIpConnect() { + imapServer.getConnectionChecks().stream() + .filter(check -> check instanceof IpConnectionCheck) + .map(check -> (IpConnectionCheck) check) + .forEach(ipCheck -> ipCheck.setBannedIps(Set.of("127.0.0.1"))); + + assertThatThrownBy(() -> testIMAPClient.connect("127.0.0.1", port) + .login(USER.asString(), USER_PASS) + .append("INBOX", SMALL_MESSAGE)); + } + + @Test + void logoutShouldDisconnectUser() throws Exception { + testIMAPClient.connect("127.0.0.1", port) + .login(USER.asString(), USER_PASS); + + imapServer.disconnect(USER::equals); + + assertThatThrownBy(() -> testIMAPClient + .append("INBOX", SMALL_MESSAGE)); + } + + @Test + void allowConnectWithUnbannedIp() throws IOException { + imapServer.getConnectionChecks().stream() + .filter(check -> check instanceof IpConnectionCheck) + .map(check -> (IpConnectionCheck) check) + .forEach(ipCheck -> ipCheck.setBannedIps(Set.of("127.0.0.2"))); + + testIMAPClient.connect("127.0.0.1", port) + .login(USER.asString(), USER_PASS) + .append("INBOX", SMALL_MESSAGE); + + assertThat(testIMAPClient + .select("INBOX") + .readFirstMessage()) + .contains("* 1 FETCH (FLAGS (\\Recent \\Seen) BODY[] {21}\r\nheader: value\r\n\r\nBODY)\r\n"); + } +} diff --git a/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPServerTest.java b/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPServerTest.java index 6d8bbe3747..5f47296a70 100644 --- a/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPServerTest.java +++ b/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPServerTest.java @@ -233,65 +233,6 @@ class IMAPServerTest { return ImmutableSet.of(new IpConnectionCheck()); } - @Nested - class ConnectionCheckTest { - - IMAPServer imapServer; - private int port; - - @BeforeEach - void beforeEach() throws Exception { - HierarchicalConfiguration<ImmutableNode> config = ConfigLoader.getConfig(ClassLoaderUtils.getSystemResourceAsSharedStream("imapServerImapConnectCheck.xml")); - imapServer = createImapServer(config); - port = imapServer.getListenAddresses().get(0).getPort(); - } - - @AfterEach - void tearDown() { - imapServer.destroy(); - } - - @Test - void banIpWhenBannedIpConnect() { - imapServer.getConnectionChecks().stream() - .filter(check -> check instanceof IpConnectionCheck) - .map(check -> (IpConnectionCheck) check) - .forEach(ipCheck -> ipCheck.setBannedIps(Set.of("127.0.0.1"))); - - assertThatThrownBy(() -> testIMAPClient.connect("127.0.0.1", port) - .login(USER.asString(), USER_PASS) - .append("INBOX", SMALL_MESSAGE)); - } - - @Test - void logoutShouldDisconnectUser() throws Exception { - testIMAPClient.connect("127.0.0.1", port) - .login(USER.asString(), USER_PASS); - - imapServer.disconnect(USER::equals); - - assertThatThrownBy(() -> testIMAPClient - .append("INBOX", SMALL_MESSAGE)); - } - - @Test - void allowConnectWithUnbannedIp() throws IOException { - imapServer.getConnectionChecks().stream() - .filter(check -> check instanceof IpConnectionCheck) - .map(check -> (IpConnectionCheck) check) - .forEach(ipCheck -> ipCheck.setBannedIps(Set.of("127.0.0.2"))); - - testIMAPClient.connect("127.0.0.1", port) - .login(USER.asString(), USER_PASS) - .append("INBOX", SMALL_MESSAGE); - - assertThat(testIMAPClient - .select("INBOX") - .readFirstMessage()) - .contains("* 1 FETCH (FLAGS (\\Recent \\Seen) BODY[] {21}\r\nheader: value\r\n\r\nBODY)\r\n"); - } - } - @Nested class PartialFetch { IMAPServer imapServer; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
