views:

451

answers:

2

I'm creating a test which will perform some stress testing of the app which talks to a Postgres database. In order to make it replicable I'd like to recreate the database on every run. Startup method decorated with org.junit.Before annotation will drop the database (if it exists) and restore it from a dump file. Doing this form shell/command is trivial:

psql -f db.out newdb

or something along these lines. Since the test may be performed on a JVM that's not running on the same machine which hosts the database I'm wondering if it's possible to invoke shell/batch script on remote machine from Java programmaticaly.

Thanks

+3  A: 

If you have knowledge of the machine details (hostname, login, password) the only thing I can think of at the moment is using a Java SSH library to login to the box and run that command.

Or you could write a script on your local machine which ran the script on the remote machine given the correct parameters.

That's the simplest way I can think of anyway!

Phill Sacre
+1  A: 

If setting up SSH is too complex, you can write a small Java program which listens for connections on a socket and then runs the script. Install that on the Postgres server and connect to it in the test case.

That said, I suggest to install a copy of Postgres on every developer machine. That would make your tests run much faster and you wouldn't get spurious errors when two developers run the tests at the same time. Also, you won't have errors because of network problems, because someone does maintenance on the server, because developer X has changed the DB schema, etc. etc.

Aaron Digulla
Thanks for giving me an idea. Every test run can recreate a db schema with unique name based on the ip of the JVM host machine and current time in milliseconds.
Boris Pavlović
Why not use the name of the developer instead? That would make it much more simple for them to debug the DB in case of an error.
Aaron Digulla