GithubHelp home page GithubHelp logo

Comments (9)

stefanbirkner avatar stefanbirkner commented on July 26, 2024 1

I have a workaround for the lambda solution

private static final int String SFTP_SERVER_PORT = freePort();

@AfterAll
public static void afterClass() {
    System.clearProperty(DHUB_ENDPOINT_PROPERTY);
}

@BeforeAll
public static void beforeClass() {
    System.setProperty(DHUB_ENDPOINT_PROPERTY, "localhost:" + SFTP_SERVER_PORT);
}

@Test
void aTest() {
    withSftpServer(server -> {
        //your code
    });
}

private withSftpServer(
    FakeSftpServer.ExceptionThrowingConsumer testCode
) throws Exception {
    FakeSftpServer.withSftpServer(server -> {
        server.setPort(SFTP_SERVER_PORT);
        testCode.accept(server);
    });
}

private static freePort() {
    try (ServerSocket socket = new ServerSocket(0)) {
        return socket.getLocalPort();
    } catch (IOException e) {
        throw new RuntimeException(
            "Failed to find a free port.",
            e
        );
    }
}

Instead of the method freePort you may also use Spring's SocketUtils.findAvailableTcpPort().

from fake-sftp-server-rule.

davidkarlsen avatar davidkarlsen commented on July 26, 2024

Linking stefanbirkner/system-rules#55

from fake-sftp-server-rule.

stefanbirkner avatar stefanbirkner commented on July 26, 2024

I would like to avoid implementing ExternalResource because the migration support is only a hack that relies on a contract that is not really given (although it works for almost all rules that implement ExternalResource).

According to your comment in stefanbirkner/system-rules#55 the only problem why you cannot replace the rule with stefanbirkner/fake-sftp-server-lambda is that you need the port information before the Spring extension starts the application. Am I right?

from fake-sftp-server-rule.

davidkarlsen avatar davidkarlsen commented on July 26, 2024

Correct

from fake-sftp-server-rule.

stefanbirkner avatar stefanbirkner commented on July 26, 2024

How did you set the port for the SpringRunner with JUnit 4?

from fake-sftp-server-rule.

davidkarlsen avatar davidkarlsen commented on July 26, 2024
private static final String DHUB_ENDPOINT_PROPERTY = "dhub.sftp.endpoint";

    @ClassRule
    public static final FakeSftpServerRule sftpServer = new FakeSftpServerRule();

    @AfterAll
    public static void afterClass() {
        System.clearProperty(DHUB_ENDPOINT_PROPERTY);
    }

    @BeforeAll
    public static void beforeClass() {
        System.setProperty(DHUB_ENDPOINT_PROPERTY, "localhost:" + sftpServer.getPort());
    }

from fake-sftp-server-rule.

sbrannen avatar sbrannen commented on July 26, 2024

Glad you guys found a work-around! 🎉

from fake-sftp-server-rule.

sbrannen avatar sbrannen commented on July 26, 2024

And... I can also highly recommend Spring's SocketUtils (I promise: no personal bias).

from fake-sftp-server-rule.

davidkarlsen avatar davidkarlsen commented on July 26, 2024

It worked fine. Closing

from fake-sftp-server-rule.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.