1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package de.softwareforge.testing.postgres.embedded;
16
17 import static com.google.common.base.Preconditions.checkState;
18 import static java.lang.String.format;
19
20 import jakarta.annotation.Nonnull;
21 import java.sql.SQLException;
22 import java.util.Optional;
23 import java.util.stream.Collectors;
24 import javax.sql.DataSource;
25
26 import com.google.auto.value.AutoValue;
27 import com.google.common.collect.ImmutableMap;
28
29
30
31
32 @AutoValue
33 public abstract class DatabaseInfo {
34
35 private static final String JDBC_FORMAT = "jdbc:postgresql://localhost:%d/%s?user=%s";
36
37 DatabaseInfo() {
38 }
39
40
41
42
43 static final String PG_DEFAULT_USER = "postgres";
44
45
46
47
48 static final String PG_DEFAULT_DB = "postgres";
49
50
51
52
53
54
55 @Nonnull
56 public abstract String dbName();
57
58
59
60
61
62
63 public abstract int port();
64
65
66
67
68
69
70 @Nonnull
71 public abstract String user();
72
73
74
75
76
77
78
79
80
81 @Nonnull
82 public abstract ImmutableMap<String, String> connectionProperties();
83
84 @Nonnull
85 abstract Optional<SQLException> exception();
86
87 @Nonnull
88 static Builder builder() {
89 return new AutoValue_DatabaseInfo.Builder()
90 .dbName(PG_DEFAULT_DB)
91 .user(PG_DEFAULT_USER);
92 }
93
94 @Nonnull
95 static DatabaseInfo forException(SQLException e) {
96 return builder().exception(e).port(-1).build();
97 }
98
99
100
101
102
103
104 @Nonnull
105 public String asJdbcUrl() {
106 checkState(exception().isEmpty(), "DatabaseInfo contains SQLException: %s", exception());
107
108 String additionalParameters = connectionProperties().entrySet().stream()
109 .map(e -> format("&%s=%s", e.getKey(), e.getValue()))
110 .collect(Collectors.joining());
111 return format(JDBC_FORMAT, port(), dbName(), user()) + additionalParameters;
112 }
113
114
115
116
117
118
119
120 @Nonnull
121 public DataSource asDataSource() throws SQLException {
122 if (exception().isPresent()) {
123 throw exception().get();
124 }
125
126 return EmbeddedPostgres.createDataSource(user(), dbName(), port(), connectionProperties());
127 }
128
129 @AutoValue.Builder
130 abstract static class Builder {
131
132 abstract Builder dbName(String dbName);
133
134 abstract Builder port(int port);
135
136 abstract Builder user(String user);
137
138 abstract Builder exception(SQLException exception);
139
140 abstract ImmutableMap.Builder<String, String> connectionPropertiesBuilder();
141
142 final Builder addConnectionProperty(String key, String value) {
143 connectionPropertiesBuilder().put(key, value);
144 return this;
145 }
146
147 abstract Builder connectionProperties(ImmutableMap<String, String> connectionProperties);
148
149 abstract DatabaseInfo build();
150
151 }
152 }