5
5
*/
6
6
package org .hibernate .reactive .configuration ;
7
7
8
+ import java .net .URI ;
9
+ import java .util .HashMap ;
10
+ import java .util .Map ;
11
+
8
12
import org .hibernate .HibernateError ;
9
13
import org .hibernate .reactive .pool .impl .DefaultSqlClientPool ;
10
14
import org .hibernate .reactive .pool .impl .DefaultSqlClientPoolConfiguration ;
11
15
12
16
import org .junit .Test ;
13
17
14
- import java .net .URI ;
15
-
16
18
import io .vertx .sqlclient .SqlConnectOptions ;
17
19
18
20
import static org .assertj .core .api .Assertions .assertThat ;
21
+ import static org .hibernate .reactive .containers .DatabaseConfiguration .DBType .SQLSERVER ;
22
+ import static org .hibernate .reactive .containers .DatabaseConfiguration .createJdbcUrl ;
23
+ import static org .hibernate .reactive .containers .DatabaseConfiguration .dbType ;
19
24
import static org .junit .Assert .assertThrows ;
20
25
26
+ /**
27
+ * Test the correct creation of the {@link SqlConnectOptions}
28
+ * given a JDBC connection URL.
29
+ * <p>
30
+ * This test doesn't require docker.
31
+ */
21
32
public class JdbcUrlParserTest {
22
33
23
34
@ Test
@@ -29,43 +40,93 @@ public void returnsNullForNull() {
29
40
}
30
41
31
42
@ Test
32
- public void invalidParameter () {
43
+ public void missingUser () {
33
44
assertThrows ( HibernateError .class , () -> {
34
- URI uri = DefaultSqlClientPool .parse ( "jdbc:postgresql://localhost:5432/hreact" );
35
- DefaultSqlClientPoolConfiguration cfg = new DefaultSqlClientPoolConfiguration ();
36
- final SqlConnectOptions connectOptions = cfg .connectOptions ( uri );
45
+ URI uri = DefaultSqlClientPool .parse ( defaultUrl () );
46
+ new DefaultSqlClientPoolConfiguration ().connectOptions ( uri );
37
47
} );
38
48
}
39
49
40
50
@ Test
41
- public void parameters () {
42
- URI uri = DefaultSqlClientPool .parse ( "jdbc:postgresql://localhost:5432/hreact?user=hello" );
43
- DefaultSqlClientPoolConfiguration cfg = new DefaultSqlClientPoolConfiguration ();
44
- final SqlConnectOptions connectOptions = cfg .connectOptions ( uri );
45
- assertThat (connectOptions ).isNotNull ();
51
+ public void testOptionsWithExtraProperties () {
52
+ Map <String , String > params = new HashMap <>();
53
+ params .put ( "user" , "hello" );
54
+ params .put ( "param1" , "value1" );
55
+ params .put ( "param2" , "value2" );
56
+ params .put ( "param3" , "value3" );
57
+
58
+ assertOptions ( createUrl ( params ), defaultDb (), params );
46
59
}
47
60
48
61
@ Test
49
- public void uriCreation () {
50
- URI uri = DefaultSqlClientPool .parse ( "jdbc:postgresql://localhost:5432/hreact" );
51
- assertThat (uri ).isNotNull ();
62
+ public void testOptionsWithoutExtraProperties () {
63
+ // Without a user we would have an exception
64
+ Map <String , String > params = new HashMap <>();
65
+ params .put ( "user" , "PerryThePlatypus" );
66
+
67
+ assertOptions ( createUrl ( params ), defaultDb (), params );
52
68
}
53
69
54
70
@ Test
55
- public void parsePort () {
56
- URI uri = DefaultSqlClientPool .parse ( "jdbc:postgresql://localhost:5432/hreact" );
57
- assertThat (uri ).hasPort (5432 );
71
+ public void testOptionsWithPasswordAndProperties () {
72
+ Map <String , String > params = new HashMap <>();
73
+ params .put ( "password" , "helloPwd" );
74
+ params .put ( "user" , "username" );
75
+ params .put ( "param2" , "Value2" );
76
+
77
+ assertOptions ( createUrl ( params ), defaultDb (), params );
58
78
}
59
79
60
80
@ Test
61
- public void parseHost () {
62
- URI uri = DefaultSqlClientPool .parse ( "jdbc:postgresql://localhost:5432/hreact" );
63
- assertThat (uri ).hasHost ("localhost" );
81
+ public void testDatabaseAsProperty () {
82
+ Map <String , String > params = new HashMap <>();
83
+ params .put ( "database" , "helloDatabase" );
84
+ params .put ( "user" , "PerryThePlatypus" );
85
+ params .put ( "password" , "XxXxX" );
86
+ params .put ( "param2" , "Value2" );
87
+
88
+ assertOptions ( createUrl ( params ), "helloDatabase" , params );
64
89
}
65
90
66
- @ Test
67
- public void parseScheme () {
68
- URI uri = DefaultSqlClientPool .parse ( "jdbc:postgresql://localhost:5432/hreact" );
69
- assertThat (uri ).hasScheme ("postgresql" );
91
+ /**
92
+ * Create the default {@link SqlConnectOptions} with the given extra properties
93
+ * and assert that's correct.
94
+ *
95
+ * @return the created options in case a test needs custom extra assertions
96
+ */
97
+ private SqlConnectOptions assertOptions (String url , String expectedDbName , Map <String , String > parameters ) {
98
+ URI uri = DefaultSqlClientPool .parse ( url );
99
+ SqlConnectOptions options = new DefaultSqlClientPoolConfiguration ().connectOptions ( uri );
100
+
101
+ // These keys won't be mapped as properties
102
+ String username = parameters .remove ( "user" );
103
+ String password = parameters .remove ( "password" );
104
+ parameters .remove ( "database" );
105
+
106
+ assertThat ( options ).as ( "URL: " + url ).isNotNull ();
107
+ assertThat ( options .getUser () ).as ( "URL: " + url ).isEqualTo ( username );
108
+ assertThat ( options .getPassword () ).as ( "URL: " + url ).isEqualTo ( password );
109
+ assertThat ( options .getDatabase () ).as ( "URL: " + url ).isEqualTo ( expectedDbName );
110
+ assertThat ( options .getHost () ).as ( "URL: " + url ).isEqualTo ( "localhost" );
111
+ assertThat ( options .getPort () ).as ( "URL: " + url ).isEqualTo ( dbType ().getDefaultPort () );
112
+
113
+ // Check extra properties
114
+ assertThat ( options .getProperties () ).as ( "URL: " + url ).containsExactlyInAnyOrderEntriesOf ( parameters );
115
+ return options ;
116
+ }
117
+
118
+ private String defaultUrl () {
119
+ return createUrl ( new HashMap <>() );
120
+ }
121
+
122
+ /**
123
+ * Create the JDBC Url with the additional extra properties (if any)
124
+ */
125
+ private String createUrl (Map <String , String > properties ) {
126
+ return createJdbcUrl ( "localhost" , dbType ().getDefaultPort (), defaultDb (), properties );
127
+ }
128
+
129
+ private String defaultDb () {
130
+ return dbType () == SQLSERVER ? "" : "hreactDB" ;
70
131
}
71
132
}
0 commit comments