1
1
package com .github .pgasync .impl ;
2
2
3
+ import static java .lang .System .getenv ;
4
+ import static ru .yandex .qatools .embed .postgresql .distribution .Version .V9_5_0 ;
5
+
3
6
import com .github .pgasync .ConnectionPool ;
4
7
import com .github .pgasync .ConnectionPoolBuilder ;
5
8
import com .github .pgasync .Db ;
6
9
import com .github .pgasync .ResultSet ;
7
10
import org .junit .rules .ExternalResource ;
8
11
import rx .Observable ;
9
12
13
+ import java .io .IOException ;
10
14
import java .util .AbstractMap .SimpleImmutableEntry ;
11
15
import java .util .List ;
12
16
import java .util .Map .Entry ;
13
17
import java .util .concurrent .ArrayBlockingQueue ;
14
18
import java .util .concurrent .BlockingQueue ;
15
19
import java .util .concurrent .TimeUnit ;
16
20
21
+ import ru .yandex .qatools .embed .postgresql .PostgresExecutable ;
22
+ import ru .yandex .qatools .embed .postgresql .PostgresProcess ;
23
+ import ru .yandex .qatools .embed .postgresql .PostgresStarter ;
24
+ import ru .yandex .qatools .embed .postgresql .config .AbstractPostgresConfig ;
25
+ import ru .yandex .qatools .embed .postgresql .config .PostgresConfig ;
26
+
17
27
/**
18
28
* @author Antti Laisi
19
29
*/
20
30
class DatabaseRule extends ExternalResource {
21
31
22
32
final ConnectionPoolBuilder builder ;
33
+ static PostgresProcess process ;
23
34
ConnectionPool pool ;
24
35
25
36
DatabaseRule () {
@@ -28,6 +39,28 @@ class DatabaseRule extends ExternalResource {
28
39
29
40
DatabaseRule (ConnectionPoolBuilder builder ) {
30
41
this .builder = builder ;
42
+ if (builder instanceof EmbeddedConnectionPoolBuilder )
43
+ {
44
+ if (process == null )
45
+ {
46
+ try
47
+ {
48
+ PostgresStarter <PostgresExecutable , PostgresProcess > runtime = PostgresStarter .getDefaultInstance ();
49
+ PostgresConfig config = new PostgresConfig (V9_5_0 , new AbstractPostgresConfig .Net (),
50
+ new AbstractPostgresConfig .Storage ("async-pg" ), new AbstractPostgresConfig .Timeout (),
51
+ new AbstractPostgresConfig .Credentials ("async-pg" , "async-pg" ));
52
+ PostgresExecutable exec = runtime .prepare (config );
53
+ process = exec .start ();
54
+ }
55
+ catch (IOException e )
56
+ {
57
+ throw new RuntimeException (e );
58
+ }
59
+ }
60
+
61
+ builder .hostname (process .getConfig ().net ().host ());
62
+ builder .port (process .getConfig ().net ().port ());
63
+ }
31
64
}
32
65
33
66
@ Override
@@ -85,11 +118,27 @@ Db db() {
85
118
return pool ;
86
119
}
87
120
121
+ static class EmbeddedConnectionPoolBuilder extends ConnectionPoolBuilder {
122
+ EmbeddedConnectionPoolBuilder () {
123
+ database ("async-pg" );
124
+ username ("async-pg" );
125
+ password ("async-pg" );
126
+ port (2345 );
127
+ }
128
+ }
129
+
88
130
static ConnectionPool createPool (int size ) {
89
131
return createPoolBuilder (size ).build ();
90
132
}
91
133
static ConnectionPoolBuilder createPoolBuilder (int size ) {
92
- return new ConnectionPoolBuilder ()
134
+ String db = getenv ("PG_DATABASE" );
135
+ String user = getenv ("PG_USERNAME" );
136
+ String pass = getenv ("PG_PASSWORD" );
137
+
138
+ if (db == null && user == null && pass == null ) {
139
+ return new EmbeddedConnectionPoolBuilder ().poolSize (size );
140
+ }
141
+ return new EmbeddedConnectionPoolBuilder ()
93
142
.database (envOrDefault ("PG_DATABASE" , "postgres" ))
94
143
.username (envOrDefault ("PG_USERNAME" , "postgres" ))
95
144
.password (envOrDefault ("PG_PASSWORD" , "postgres" ))
@@ -99,7 +148,7 @@ static ConnectionPoolBuilder createPoolBuilder(int size) {
99
148
100
149
101
150
static String envOrDefault (String var , String def ) {
102
- String value = System . getenv (var );
151
+ String value = getenv (var );
103
152
return value != null ? value : def ;
104
153
}
105
154
}
0 commit comments