Advertisement
Guest User

Untitled

a guest
Jan 6th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. package app;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5.  
  6. import ru.yandex.qatools.embed.postgresql.PostgresExecutable;
  7. import ru.yandex.qatools.embed.postgresql.PostgresProcess;
  8. import ru.yandex.qatools.embed.postgresql.PostgresStarter;
  9. import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.Credentials;
  10. import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.Net;
  11. import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.Storage;
  12. import ru.yandex.qatools.embed.postgresql.config.AbstractPostgresConfig.Timeout;
  13. import ru.yandex.qatools.embed.postgresql.config.PostgresConfig;
  14. import ru.yandex.qatools.embed.postgresql.distribution.Version;
  15.  
  16. public class EmbedPgMain {
  17. public static void main(String[] args) throws Exception {
  18. int port = 58697;
  19. if (args.length > 0) {
  20. port = Integer.parseInt(args[0]);
  21. }
  22. if (args.length > 1) {
  23. System.setProperty("de.flapdoodle.embed.io.tmpdir", args[1]);
  24. }
  25.  
  26. PostgresStarter<PostgresExecutable, PostgresProcess> runtime = PostgresStarter.getDefaultInstance();
  27. final PostgresConfig config = new PostgresConfig(Version.V9_3_6,
  28. new Net("127.0.0.1", port),
  29. new Storage("test"),
  30. new Timeout(),
  31. new Credentials("test", "test123"));
  32. PostgresExecutable exec = runtime.prepare(config);
  33. final PostgresProcess process = exec.start();
  34. System.out.println(config.storage().dbDir());
  35. System.out.println(process.getProcessId());
  36. Runtime.getRuntime().addShutdownHook(new Thread() {
  37. @Override
  38. public void run() {
  39. stopPostgresProcess(process);
  40. }
  41. });
  42.  
  43. // Create two login roles: arinweb_app and corerep_app
  44. String url = String.format("jdbc:postgresql://%s:%s/%s?user=%s&password=%s",
  45. config.net().host(),
  46. config.net().port(),
  47. config.storage().dbName(),
  48. config.credentials().username(),
  49. config.credentials().password()
  50. );
  51. Connection conn = DriverManager.getConnection(url);
  52. conn.createStatement().execute("CREATE ROLE test_app1 LOGIN PASSWORD 'test_app1'");
  53. conn.createStatement().execute("CREATE ROLE test_app2 LOGIN PASSWORD 'test_app2'");
  54. conn.close();
  55.  
  56. System.out.println("EMBED-PG-STARTED");
  57. System.out.println("Press any key to exit");
  58.  
  59. System.in.read();
  60. stopPostgresProcess(process);
  61. }
  62.  
  63. private static void stopPostgresProcess(PostgresProcess process) {
  64. if (process.isProcessRunning()) {
  65. process.stop();
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement