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

Add CloseableFakeSftpServer extends FakeSftpServer #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kriegaex
Copy link

@kriegaex kriegaex commented Feb 13, 2022

The class can be instantiated, configured and closed manually, if doing everything within a single lambda expression is undesirable for a given use case. In order to achieve that, some methods and fields had to be made protected (from final).

For example, in BDD (behaviour-driven development) users want to test in a given-when-then fashion, i.e. separate the stimulus to the system under test (when) from verifying results later (then). Some test frameworks like Spock even require separate blocks, making the use of a single lambda feel somewhat unnatural and clunky there. See also this Stack Overflow answer.

Because the class implements AutoCloseable, it can be optionally also be instantiated in a try with resources structure.

A parametrised Spock (Groovy) example test looks like this:

class MySFTPServerTest extends Specification {
  @AutoCleanup
  def server = new CloseableFakeSftpServer()

  @Unroll("user #user downloads file #file")
  def "users can download text files"() {
    given: "a preconfigured fake SFTP server"
    server.addUser(user, password)
    server.putFile(file, content, UTF_8)

    and: "an SFTP client under test"
    def client = new SFTPFileDownloader("localhost", server.port, user, password)

    expect:
    client.getFileContent(file) == content

    where:
    user    | password | file                   | content
    "me"    | "xoxox"  | "/a/b/c/one.txt"       | "First line\nSecondLine\n"
    "you"   | "mypass" | "/etc/two.xml"         | "<root><foo>abc</foo></root>"
    "admin" | "secret" | "/home/admin/three.sh" | "#!/usr/bin/bash\n\nls -al\n"
  }
}

With the original FakeSftpServer, everything that is now cleanly separated by concern in the given:, and:, expect: blocks would be done within a single lambda expression, only for the sake of resource management, which is now handled via @AutoCleanup for the server field definition. As an alternative, in other test frameworks, server.close() could be called in a tear-down method running after the test or by using try with resources within the test.

Otherwise, builds with 'install' overwrite an existing release in the
local Maven repository.
The class can be instantiated, configured and closed manually, if doing
everything within a single lambda expression is undesirable for a
given use case. In order to achieve that, some methods and fields had to
be made protected (from final).

For example, in BDD (behaviour-driven development) users want to test in
a given-when-then fashion, i.e. separate the stimulus to the system
under test (when) from verifying results later (then). Some test
frameworks like Spock even require separate blocks, making the use of a
single lambda feel somewhat unnatural and clunky there.

Because the class implements AutoCloseable, it can be instantiated in a
try with resources structure.
@kriegaex
Copy link
Author

With regard to the Maven build, I want to mention that even before my changes, several tests were failing. The exact same tests are failing now, i.e. I did not break anything in the base class.

Now Findbugs also complains about the FakeSftpServer.server being used in 3 places without null checks. This is not really relevant, so I did not address it but simply built with mvn -DskipTests -Dfindbugs.failOnError=false install and tested from my Spock project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant