16
16
17
17
package org .springframework .boot .autoconfigure .hazelcast ;
18
18
19
+ import java .io .File ;
20
+ import java .io .FileReader ;
21
+ import java .io .FileWriter ;
22
+ import java .io .IOException ;
23
+ import java .net .InetSocketAddress ;
24
+ import java .net .MalformedURLException ;
25
+ import java .nio .file .Files ;
26
+
19
27
import com .hazelcast .client .HazelcastClient ;
20
28
import com .hazelcast .client .config .ClientConfig ;
21
- import com .hazelcast .client .config .ClientConnectionStrategyConfig ;
22
- import com .hazelcast .client .config .ConnectionRetryConfig ;
23
29
import com .hazelcast .client .impl .clientside .HazelcastClientProxy ;
24
30
import com .hazelcast .config .Config ;
25
31
import com .hazelcast .core .Hazelcast ;
36
42
import org .springframework .boot .test .context .runner .ContextConsumer ;
37
43
import org .springframework .context .annotation .Bean ;
38
44
import org .springframework .context .annotation .Configuration ;
45
+ import org .springframework .util .FileCopyUtils ;
39
46
40
47
import static org .assertj .core .api .Assertions .assertThat ;
41
48
@@ -52,9 +59,15 @@ class HazelcastAutoConfigurationClientTests {
52
59
*/
53
60
private static HazelcastInstance hazelcastServer ;
54
61
62
+ private static String endpointAddress ;
63
+
55
64
@ BeforeAll
56
65
static void init () {
57
- hazelcastServer = Hazelcast .newHazelcastInstance ();
66
+ Config config = Config .load ();
67
+ config .getNetworkConfig ().setPort (0 );
68
+ hazelcastServer = Hazelcast .newHazelcastInstance (config );
69
+ InetSocketAddress inetSocketAddress = (InetSocketAddress ) hazelcastServer .getLocalEndpoint ().getSocketAddress ();
70
+ endpointAddress = inetSocketAddress .getHostString () + ":" + inetSocketAddress .getPort ();
58
71
}
59
72
60
73
@ AfterAll
@@ -69,73 +82,52 @@ static void close() {
69
82
70
83
@ Test
71
84
void systemPropertyWithXml () {
85
+ File config = prepareConfiguration ("src/test/resources/org/springframework/"
86
+ + "boot/autoconfigure/hazelcast/hazelcast-client-specific.xml" );
72
87
this .contextRunner
73
- .withSystemProperties (HazelcastClientConfiguration .CONFIG_SYSTEM_PROPERTY
74
- + "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.xml" )
88
+ .withSystemProperties (HazelcastClientConfiguration .CONFIG_SYSTEM_PROPERTY + "=" + config .getAbsolutePath ())
75
89
.run (assertSpecificHazelcastClient ("explicit-xml" ));
76
90
}
77
91
78
92
@ Test
79
93
void systemPropertyWithYaml () {
94
+ File config = prepareConfiguration ("src/test/resources/org/springframework/"
95
+ + "boot/autoconfigure/hazelcast/hazelcast-client-specific.yaml" );
80
96
this .contextRunner
81
- .withSystemProperties (HazelcastClientConfiguration .CONFIG_SYSTEM_PROPERTY
82
- + "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.yaml" )
97
+ .withSystemProperties (HazelcastClientConfiguration .CONFIG_SYSTEM_PROPERTY + "=" + config .getAbsolutePath ())
83
98
.run (assertSpecificHazelcastClient ("explicit-yaml" ));
84
99
}
85
100
86
101
@ Test
87
102
void systemPropertyWithYml () {
103
+ File config = prepareConfiguration ("src/test/resources/org/springframework/"
104
+ + "boot/autoconfigure/hazelcast/hazelcast-client-specific.yml" );
88
105
this .contextRunner
89
- .withSystemProperties (HazelcastClientConfiguration .CONFIG_SYSTEM_PROPERTY
90
- + "=classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-client-specific.yml" )
91
- .run (assertSpecificHazelcastClient ("explicit-yml" ));
92
- }
93
-
94
- @ Test
95
- void explicitConfigFileWithXml () {
96
- this .contextRunner
97
- .withPropertyValues ("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
98
- + "hazelcast/hazelcast-client-specific.xml" )
99
- .run (assertSpecificHazelcastClient ("explicit-xml" ));
100
- }
101
-
102
- @ Test
103
- void explicitConfigFileWithYaml () {
104
- this .contextRunner
105
- .withPropertyValues ("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
106
- + "hazelcast/hazelcast-client-specific.yaml" )
107
- .run (assertSpecificHazelcastClient ("explicit-yaml" ));
108
- }
109
-
110
- @ Test
111
- void explicitConfigFileWithYml () {
112
- this .contextRunner
113
- .withPropertyValues ("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
114
- + "hazelcast/hazelcast-client-specific.yml" )
106
+ .withSystemProperties (HazelcastClientConfiguration .CONFIG_SYSTEM_PROPERTY + "=" + config .getAbsolutePath ())
115
107
.run (assertSpecificHazelcastClient ("explicit-yml" ));
116
108
}
117
109
118
110
@ Test
119
- void explicitConfigUrlWithXml () {
120
- this . contextRunner
121
- . withPropertyValues ( "spring.hazelcast.config=classpath:org/springframework/"
122
- + "boot/autoconfigure/ hazelcast/hazelcast-client-specific.xml" )
111
+ void explicitConfigUrlWithXml () throws MalformedURLException {
112
+ File config = prepareConfiguration ( "src/test/resources/org/springframework/"
113
+ + "boot/autoconfigure/hazelcast/hazelcast-client-specific.xml" );
114
+ this . contextRunner . withPropertyValues ( "spring. hazelcast.config=" + config . toURI (). toURL () )
123
115
.run (assertSpecificHazelcastClient ("explicit-xml" ));
124
116
}
125
117
126
118
@ Test
127
- void explicitConfigUrlWithYaml () {
128
- this . contextRunner
129
- . withPropertyValues ( "spring.hazelcast.config=classpath:org/springframework/"
130
- + "boot/autoconfigure/ hazelcast/hazelcast-client-specific.yaml" )
119
+ void explicitConfigUrlWithYaml () throws MalformedURLException {
120
+ File config = prepareConfiguration ( "src/test/resources/org/springframework/"
121
+ + "boot/autoconfigure/hazelcast/hazelcast-client-specific.yaml" );
122
+ this . contextRunner . withPropertyValues ( "spring. hazelcast.config=" + config . toURI (). toURL () )
131
123
.run (assertSpecificHazelcastClient ("explicit-yaml" ));
132
124
}
133
125
134
126
@ Test
135
- void explicitConfigUrlWithYml () {
136
- this . contextRunner
137
- . withPropertyValues ( "spring.hazelcast.config=classpath:org/springframework/"
138
- + "boot/autoconfigure/ hazelcast/hazelcast-client-specific.yml" )
127
+ void explicitConfigUrlWithYml () throws MalformedURLException {
128
+ File config = prepareConfiguration ( "src/test/resources/org/springframework/"
129
+ + "boot/autoconfigure/hazelcast/hazelcast-client-specific.yml" );
130
+ this . contextRunner . withPropertyValues ( "spring. hazelcast.config=" + config . toURI (). toURL () )
139
131
.run (assertSpecificHazelcastClient ("explicit-yml" ));
140
132
}
141
133
@@ -156,28 +148,26 @@ void clientConfigTakesPrecedence() {
156
148
}
157
149
158
150
@ Test
159
- void clientConfigWithInstanceNameCreatesClientIfNecessary () {
151
+ void clientConfigWithInstanceNameCreatesClientIfNecessary () throws MalformedURLException {
160
152
assertThat (HazelcastClient .getHazelcastClientByName ("spring-boot" )).isNull ();
161
- this . contextRunner
162
- . withPropertyValues ( "spring.hazelcast.config=classpath:org/springframework/"
163
- + "boot/autoconfigure/ hazelcast/hazelcast-client-instance.xml" )
153
+ File config = prepareConfiguration ( "src/test/resources/org/springframework/"
154
+ + "boot/autoconfigure/hazelcast/hazelcast-client-instance.xml" );
155
+ this . contextRunner . withPropertyValues ( "spring. hazelcast.config=" + config . toURI (). toURL () )
164
156
.run ((context ) -> assertThat (context ).getBean (HazelcastInstance .class )
165
157
.extracting (HazelcastInstance ::getName )
166
158
.isEqualTo ("spring-boot" ));
167
159
}
168
160
169
161
@ Test
170
- void autoConfiguredClientConfigUsesApplicationClassLoader () {
171
- this .contextRunner
172
- .withPropertyValues ("spring.hazelcast.config=org/springframework/boot/autoconfigure/"
173
- + "hazelcast/hazelcast-client-specific.xml" )
174
- .run ((context ) -> {
175
- HazelcastInstance hazelcast = context .getBean (HazelcastInstance .class );
176
- assertThat (hazelcast ).isInstanceOf (HazelcastClientProxy .class );
177
- ClientConfig clientConfig = ((HazelcastClientProxy ) hazelcast ).getClientConfig ();
178
- assertThat (clientConfig .getClassLoader ())
179
- .isSameAs (context .getSourceApplicationContext ().getClassLoader ());
180
- });
162
+ void autoConfiguredClientConfigUsesApplicationClassLoader () throws MalformedURLException {
163
+ File config = prepareConfiguration ("src/test/resources/org/springframework/"
164
+ + "boot/autoconfigure/hazelcast/hazelcast-client-specific.xml" );
165
+ this .contextRunner .withPropertyValues ("spring.hazelcast.config=" + config .toURI ().toURL ()).run ((context ) -> {
166
+ HazelcastInstance hazelcast = context .getBean (HazelcastInstance .class );
167
+ assertThat (hazelcast ).isInstanceOf (HazelcastClientProxy .class );
168
+ ClientConfig clientConfig = ((HazelcastClientProxy ) hazelcast ).getClientConfig ();
169
+ assertThat (clientConfig .getClassLoader ()).isSameAs (context .getSourceApplicationContext ().getClassLoader ());
170
+ });
181
171
}
182
172
183
173
private ContextConsumer <AssertableApplicationContext > assertSpecificHazelcastClient (String label ) {
@@ -193,6 +183,22 @@ private static Condition<HazelcastInstance> labelEqualTo(String label) {
193
183
.anyMatch ((e ) -> e .equals (label )), "Label equals to " + label );
194
184
}
195
185
186
+ private File prepareConfiguration (String input ) {
187
+ File configFile = new File (input );
188
+ try {
189
+ String config = FileCopyUtils .copyToString (new FileReader (configFile ));
190
+ config = config .replace ("${address}" , endpointAddress );
191
+ System .out .println (config );
192
+ File outputFile = new File (Files .createTempDirectory (getClass ().getSimpleName ()).toFile (),
193
+ configFile .getName ());
194
+ FileCopyUtils .copy (config , new FileWriter (outputFile ));
195
+ return outputFile ;
196
+ }
197
+ catch (IOException ex ) {
198
+ throw new RuntimeException (ex );
199
+ }
200
+ }
201
+
196
202
@ Configuration (proxyBeanMethods = false )
197
203
static class HazelcastServerAndClientConfig {
198
204
@@ -203,8 +209,10 @@ Config config() {
203
209
204
210
@ Bean
205
211
ClientConfig clientConfig () {
206
- return new ClientConfig ().setConnectionStrategyConfig (new ClientConnectionStrategyConfig ()
207
- .setConnectionRetryConfig (new ConnectionRetryConfig ().setClusterConnectTimeoutMillis (60000 )));
212
+ ClientConfig config = new ClientConfig ();
213
+ config .getConnectionStrategyConfig ().getConnectionRetryConfig ().setClusterConnectTimeoutMillis (60000 );
214
+ config .getNetworkConfig ().getAddresses ().add (endpointAddress );
215
+ return config ;
208
216
}
209
217
210
218
}
0 commit comments