diff --git a/protocols/imap/src/main/java/org/apache/james/imap/main/DefaultImapDecoderFactory.java b/protocols/imap/src/main/java/org/apache/james/imap/main/DefaultImapDecoderFactory.java index 87730468fbd..17ecee50a59 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/main/DefaultImapDecoderFactory.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/main/DefaultImapDecoderFactory.java @@ -19,6 +19,7 @@ package org.apache.james.imap.main; +import org.apache.james.imap.api.message.response.StatusResponseFactory; import org.apache.james.imap.decode.ImapCommandParserFactory; import org.apache.james.imap.decode.ImapDecoder; import org.apache.james.imap.decode.ImapDecoderFactory; @@ -27,20 +28,36 @@ import org.apache.james.imap.message.response.UnpooledStatusResponseFactory; /** - * TODO: this is temporary: should let the container do the coupling. TODO: - * convert to POJO + * Factory class for creating `ImapDecoder` instances. + *

+ * This class is a POJO that manually manages its dependencies. + * Dependencies are injected through the constructor, which allows for + * better decoupling and easier testing. + *

+ * The creation of `ImapCommandParserFactory` is handled internally by + * this factory, based on the provided `UnpooledStatusResponseFactory`. */ public class DefaultImapDecoderFactory implements ImapDecoderFactory { - public static ImapDecoder createDecoder() { - final UnpooledStatusResponseFactory unpooledStatusResponseFactory = new UnpooledStatusResponseFactory(); - final ImapCommandParserFactory imapCommands = new ImapParserFactory(unpooledStatusResponseFactory); - return new DefaultImapDecoder(unpooledStatusResponseFactory, imapCommands); + private final StatusResponseFactory statusResponseFactory; + private final ImapCommandParserFactory imapCommandParserFactory; + + public DefaultImapDecoderFactory() { + this(new UnpooledStatusResponseFactory()); + } + + public DefaultImapDecoderFactory(StatusResponseFactory statusResponseFactory) { + this.statusResponseFactory = statusResponseFactory; + this.imapCommandParserFactory = new ImapParserFactory(statusResponseFactory); + } + + public DefaultImapDecoderFactory(ImapCommandParserFactory imapCommandParserFactory, StatusResponseFactory statusResponseFactory) { + this.statusResponseFactory = statusResponseFactory; + this.imapCommandParserFactory = imapCommandParserFactory; } @Override public ImapDecoder buildImapDecoder() { - return createDecoder(); + return new DefaultImapDecoder(statusResponseFactory, imapCommandParserFactory); } - } diff --git a/protocols/imap/src/test/java/org/apache/james/imap/main/DefaultImapDecoderFactoryTest.java b/protocols/imap/src/test/java/org/apache/james/imap/main/DefaultImapDecoderFactoryTest.java new file mode 100644 index 00000000000..4f85b671cb3 --- /dev/null +++ b/protocols/imap/src/test/java/org/apache/james/imap/main/DefaultImapDecoderFactoryTest.java @@ -0,0 +1,59 @@ +/**************************************************************** + * 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.imap.main; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.imap.api.message.response.StatusResponseFactory; +import org.apache.james.imap.decode.ImapDecoder; +import org.apache.james.imap.message.response.UnpooledStatusResponseFactory; +import org.junit.jupiter.api.Test; + +class DefaultImapDecoderFactoryTest { + + @Test + void createDefaultImapDecoderFactoryWithDefaultConstructor() { + // Create an instance using the default constructor + DefaultImapDecoderFactory factory = new DefaultImapDecoderFactory(); + + // Assert that the factory is not null with a custom message + assertThat(factory).as("DefaultImapDecoderFactory should not be null when created with default constructor").isNotNull(); + + // Build an ImapDecoder using the factory and assert it's not null with a custom message + ImapDecoder decoder = factory.buildImapDecoder(); + assertThat(decoder).as("ImapDecoder should not be null when built from DefaultImapDecoderFactory").isNotNull(); + } + + @Test + void createDefaultImapDecoderFactoryWithCustomStatusResponseFactory() { + // Create a custom StatusResponseFactory + StatusResponseFactory customStatusResponseFactory = new UnpooledStatusResponseFactory(); + + // Create an instance using the custom StatusResponseFactory + DefaultImapDecoderFactory factory = new DefaultImapDecoderFactory(customStatusResponseFactory); + + // Assert that the factory is not null with a custom message + assertThat(factory).as("DefaultImapDecoderFactory should not be null when created with a custom StatusResponseFactory").isNotNull(); + + // Build an ImapDecoder using the factory and assert it's not null with a custom message + ImapDecoder decoder = factory.buildImapDecoder(); + assertThat(decoder).as("ImapDecoder should not be null when built from DefaultImapDecoderFactory with a custom StatusResponseFactory").isNotNull(); + } +} diff --git a/server/container/guice/protocols/imap/src/main/java/org/apache/james/modules/protocols/IMAPServerModule.java b/server/container/guice/protocols/imap/src/main/java/org/apache/james/modules/protocols/IMAPServerModule.java index a3e7a1cb1be..afa81f402c8 100644 --- a/server/container/guice/protocols/imap/src/main/java/org/apache/james/modules/protocols/IMAPServerModule.java +++ b/server/container/guice/protocols/imap/src/main/java/org/apache/james/modules/protocols/IMAPServerModule.java @@ -49,6 +49,7 @@ import org.apache.james.imap.encode.base.EndImapEncoder; import org.apache.james.imap.encode.main.DefaultImapEncoderFactory; import org.apache.james.imap.encode.main.DefaultLocalizer; +import org.apache.james.imap.main.DefaultImapDecoderFactory; import org.apache.james.imap.message.response.UnpooledStatusResponseFactory; import org.apache.james.imap.processor.AuthenticateProcessor; import org.apache.james.imap.processor.CapabilityImplementingProcessor; @@ -187,7 +188,7 @@ private ThrowingFunction, ImapSuite> im } ImapDecoder provideImapDecoder(ImapCommandParserFactory imapCommandParserFactory, StatusResponseFactory statusResponseFactory) { - return new DefaultImapDecoder(statusResponseFactory, imapCommandParserFactory); + return new DefaultImapDecoderFactory(imapCommandParserFactory, statusResponseFactory).buildImapDecoder(); } ImapEncoder provideImapEncoder(ImapPackage imapPackage, GuiceGenericLoader loader) { diff --git a/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPHealthCheckTest.java b/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPHealthCheckTest.java index 8e4937b1fdc..25b5fa509dd 100644 --- a/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPHealthCheckTest.java +++ b/server/protocols/protocols-imap4/src/test/java/org/apache/james/imapserver/netty/IMAPHealthCheckTest.java @@ -76,7 +76,7 @@ void setUp() throws Exception { imapServerFactory = new IMAPServerFactory( FileSystemImpl.forTestingWithConfigurationFromClasspath(), - DefaultImapDecoderFactory.createDecoder(), + new DefaultImapDecoderFactory().buildImapDecoder(), new DefaultImapEncoderFactory().buildImapEncoder(), DefaultImapProcessorFactory.createXListSupportingProcessor( mailboxManager, 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 50d075e30fe..c39d54c0322 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 @@ -166,7 +166,7 @@ private IMAPServer createImapServer(HierarchicalConfiguration con Set connectionChecks = defaultConnectionChecks(); mailboxManager = spy(memoryIntegrationResources.getMailboxManager()); IMAPServer imapServer = new IMAPServer( - DefaultImapDecoderFactory.createDecoder(), + new DefaultImapDecoderFactory().buildImapDecoder(), new DefaultImapEncoderFactory().buildImapEncoder(), DefaultImapProcessorFactory.createXListSupportingProcessor( mailboxManager,