Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-16488: ensure resource setup/cleanup on the right time #8301

Merged
merged 1 commit into from Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,7 +31,7 @@
public abstract class FtpServerTestSupport extends BaseServerTestSupport {

@RegisterExtension
static FtpEmbeddedService service = new FtpEmbeddedService();
public FtpEmbeddedService service = new FtpEmbeddedService();

public void sendFile(String url, Object body, String fileName) {
template.sendBodyAndHeader(url, body, Exchange.FILE_NAME, simple(fileName));
Expand Down
Expand Up @@ -24,7 +24,7 @@

public class SftpServerTestSupport extends BaseServerTestSupport {
@RegisterExtension
protected static SftpEmbeddedService service = new SftpEmbeddedService();
protected SftpEmbeddedService service = new SftpEmbeddedService();

protected Path ftpFile(String file) {
return service.getFtpRootDir().resolve(file);
Expand Down
Expand Up @@ -19,19 +19,8 @@
import java.util.function.BiConsumer;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public abstract class AbstractTestService implements TestService, BeforeAllCallback, AfterAllCallback {

protected ExtensionContext context;

@Override
public void registerProperties() {
ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.GLOBAL);
registerProperties(store::put);
}
public abstract class AbstractTestService implements TestService {

@Override
public void initialize() {
Expand All @@ -52,25 +41,13 @@ public void shutdown() {
}
}

protected void registerProperties(BiConsumer<String, String> store) {
}
protected abstract void registerProperties(BiConsumer<String, String> store);

@Deprecated
protected void setUp() throws Exception {
}

@Deprecated
protected void tearDown() throws Exception {
}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
this.context = extensionContext;
initialize();
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
shutdown();
this.context = null;
}

}
Expand Up @@ -16,20 +16,6 @@
*/
package org.apache.camel.test.infra.ftp.services;

public class FtpRemoteService implements FtpService {
public interface FtpRemoteService extends FtpService {

@Override
public void registerProperties() {
// NO-OP
}

@Override
public void initialize() {
registerProperties();
}

@Override
public void shutdown() {
// NO-OP
}
}
Expand Up @@ -18,21 +18,13 @@

import org.apache.camel.test.infra.common.services.TestService;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.BeforeEachCallback;

/**
* Test infra service for Ftp
*/
public interface FtpService extends BeforeAllCallback, AfterAllCallback, TestService {
public interface FtpService extends TestService, BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {

@Override
default void beforeAll(ExtensionContext extensionContext) throws Exception {
initialize();
}

@Override
default void afterAll(ExtensionContext extensionContext) throws Exception {
shutdown();
}
}
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.test.infra.ftp.services;

import org.apache.camel.test.infra.common.services.SimpleTestServiceBuilder;
import org.apache.camel.test.infra.ftp.services.embedded.FtpEmbeddedService;

public final class FtpServiceFactory {

Expand All @@ -30,7 +31,7 @@ public static SimpleTestServiceBuilder<FtpService> builder() {

public static FtpService createService() {
return builder()
.addRemoteMapping(FtpRemoteService::new)
.addLocalMapping(FtpEmbeddedService::new)
.build();
}
}
Expand Up @@ -42,6 +42,7 @@
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -56,8 +57,8 @@ public class FtpEmbeddedService extends AbstractTestService implements FtpServic

protected Path rootDir;
private final EmbeddedConfigurationBuilder embeddedConfigurationTemplate;
private ExtensionContext context;

@Deprecated
public FtpEmbeddedService() {
this(EmbeddedConfigurationBuilder.defaultConfigurationTemplate());
}
Expand Down Expand Up @@ -126,6 +127,7 @@ protected FtpServerFactory createFtpServerFactory(EmbeddedConfiguration embedded
ListenerFactory factory = new ListenerFactory();
factory.setPort(port);
factory.setServerAddress(embeddedConfiguration.getServerAddress());

final Listener listener = factory.createListener();

serverFactory.addListener(DEFAULT_LISTENER, listener);
Expand Down Expand Up @@ -172,6 +174,12 @@ protected void registerProperties(BiConsumer<String, String> store) {
store.accept(FtpProperties.ROOT_DIR, rootDir.toString());
}

@Override
public void registerProperties() {
ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.GLOBAL);
registerProperties(store::put);
}

public Path getFtpRootDir() {
return rootDir;
}
Expand Down Expand Up @@ -201,4 +209,26 @@ public int countConnections() {

return count;
}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
this.context = extensionContext;
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
this.context = null;
}

@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
shutdown();
this.context = null;
}

@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
this.context = extensionContext;
initialize();
}
}
Expand Up @@ -40,6 +40,7 @@
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
import org.apache.sshd.sftp.server.SftpSubsystemFactory;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -53,6 +54,7 @@ public class SftpEmbeddedService extends AbstractTestService implements FtpServi
private Path rootDir;
private Path knownHosts;
private final EmbeddedConfiguration embeddedConfiguration;
private ExtensionContext context;

public SftpEmbeddedService() {
this(false);
Expand Down Expand Up @@ -164,7 +166,35 @@ protected void registerProperties(BiConsumer<String, String> store) {
store.accept(FtpProperties.ROOT_DIR, rootDir.toString());
}

@Override
public void registerProperties() {
ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.GLOBAL);
registerProperties(store::put);
}

public int getPort() {
return port;
}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
this.context = extensionContext;
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
this.context = null;
}

@Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
shutdown();
this.context = null;
}

@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
this.context = extensionContext;
initialize();
}
}