Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import ru.yandex.qatools.embed.postgresql.PostgresExecutable;
- import ru.yandex.qatools.embed.postgresql.PostgresProcess;
- import ru.yandex.qatools.embed.postgresql.PostgresStarter;
- import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.Credentials;
- import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.Net;
- import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.Storage;
- import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.Timeout;
- import ru.yandex.qatools.embed.postgresql.config.PostgresConfig;
- import ru.yandex.qatools.embed.postgresql.distribution.Version;
- public class EmbedPgMain {
- public static void main(String[] args) throws Exception {
- int port = 58697;
- if (args.length > 0) {
- port = Integer.parseInt(args[0]);
- }
- if (args.length > 1) {
- System.setProperty("de.flapdoodle.embed.io.tmpdir", args[1]);
- }
- PostgresStarter<PostgresExecutable, PostgresProcess> runtime = PostgresStarter.getDefaultInstance();
- final PostgresConfig config = new PostgresConfig(Version.V9_3_6,
- new Net("127.0.0.1", port),
- new Storage("test"),
- new Timeout(),
- new Credentials("test", "test123"));
- PostgresExecutable exec = runtime.prepare(config);
- final PostgresProcess process = exec.start();
- System.out.println(config.storage().dbDir());
- System.out.println(process.getProcessId());
- Runtime.getRuntime().addShutdownHook(new Thread() {
- @Override
- public void run() {
- stopPostgresProcess(process);
- }
- });
- // Create two login roles: arinweb_app and corerep_app
- String url = String.format("jdbc:postgresql://%s:%s/%s?user=%s&password=%s",
- config.net().host(),
- config.net().port(),
- config.storage().dbName(),
- config.credentials().username(),
- config.credentials().password()
- );
- Connection conn = DriverManager.getConnection(url);
- conn.createStatement().execute("CREATE ROLE test_app1 LOGIN PASSWORD 'test_app1'");
- conn.createStatement().execute("CREATE ROLE test_app2 LOGIN PASSWORD 'test_app2'");
- conn.close();
- System.out.println("EMBED-PG-STARTED");
- System.out.println("Press any key to exit");
- System.in.read();
- stopPostgresProcess(process);
- }
- private static void stopPostgresProcess(PostgresProcess process) {
- if (process.isProcessRunning()) {
- process.stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement