/
ClientPropertiesConfigurationSelfTest.java
232 lines (175 loc) · 8.13 KB
/
ClientPropertiesConfigurationSelfTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.client.impl;
import org.apache.commons.io.*;
import org.apache.ignite.client.*;
import org.apache.ignite.client.balancer.*;
import org.apache.ignite.internal.util.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.testframework.junits.common.*;
import org.springframework.context.support.*;
import java.io.*;
import java.net.*;
import java.util.*;
import static org.apache.ignite.client.GridClientConfiguration.*;
/**
* Properties-based configuration self test.
*/
public class ClientPropertiesConfigurationSelfTest extends GridCommonAbstractTest {
/**
* Grid client spring configuration.
*/
private static final URL GRID_CLIENT_SPRING_CONFIG;
/**
* Grid client properties-based configuration.
*/
private static final URL GRID_CLIENT_CONFIG;
/**
*
*/
static {
GRID_CLIENT_SPRING_CONFIG =
U.resolveIgniteUrl("/modules/clients/config/grid-client-spring-config.xml");
GRID_CLIENT_CONFIG = U.resolveIgniteUrl("/modules/clients/config/grid-client-config.properties");
}
/**
* Test client configuration loaded from the properties.
*
* @throws Exception In case of exception.
*/
public void testCreation() throws Exception {
// Validate default configuration.
GridClientConfiguration cfg = new GridClientConfiguration();
cfg.setServers(Arrays.asList("localhost:11211"));
validateConfig(0, cfg);
// Validate default properties-based configuration.
cfg = new GridClientConfiguration();
cfg.setServers(Arrays.asList("localhost:11211"));
validateConfig(0, cfg);
// Validate loaded configuration.
Properties props = loadProperties(1, GRID_CLIENT_CONFIG);
validateConfig(0, new GridClientConfiguration(props));
// Validate loaded configuration with changed key prefixes.
Properties props2 = new Properties();
for (Map.Entry<Object, Object> e : props.entrySet())
props2.put("new." + e.getKey(), e.getValue());
validateConfig(0, new GridClientConfiguration("new.gg.client", props2));
validateConfig(0, new GridClientConfiguration("new.gg.client.", props2));
// Validate loaded test configuration.
File tmp = uncommentProperties(GRID_CLIENT_CONFIG);
props = loadProperties(25, tmp.toURI().toURL());
validateConfig(2, new GridClientConfiguration(props));
// Validate loaded test configuration with changed key prefixes.
props2 = new Properties();
for (Map.Entry<Object, Object> e : props.entrySet())
props2.put("new." + e.getKey(), e.getValue());
validateConfig(2, new GridClientConfiguration("new.gg.client", props2));
validateConfig(2, new GridClientConfiguration("new.gg.client.", props2));
// Validate loaded test configuration with empty key prefixes.
props2 = new Properties();
for (Map.Entry<Object, Object> e : props.entrySet())
props2.put(e.getKey().toString().replace("gg.client.", ""), e.getValue());
validateConfig(2, new GridClientConfiguration("", props2));
validateConfig(2, new GridClientConfiguration(".", props2));
}
/**
* Validate spring client configuration.
*
* @throws Exception In case of any exception.
*/
public void testSpringConfig() throws Exception {
GridClientConfiguration cfg = new FileSystemXmlApplicationContext(
GRID_CLIENT_SPRING_CONFIG.toString()).getBean(GridClientConfiguration.class);
assertEquals(Arrays.asList("127.0.0.1:11211"), new ArrayList<>(cfg.getServers()));
assertNull(cfg.getSecurityCredentialsProvider());
Collection<GridClientDataConfiguration> dataCfgs = cfg.getDataConfigurations();
assertEquals(1, dataCfgs.size());
GridClientDataConfiguration dataCfg = dataCfgs.iterator().next();
assertEquals("partitioned", dataCfg.getName());
assertNotNull(dataCfg.getPinnedBalancer());
assertEquals(GridClientRandomBalancer.class, dataCfg.getPinnedBalancer().getClass());
assertNotNull(dataCfg.getAffinity());
assertEquals(GridClientPartitionAffinity.class, dataCfg.getAffinity().getClass());
}
/**
* Uncomment properties.
*
* @param url Source to uncomment client properties for.
* @return Temporary file with uncommented client properties.
* @throws IOException In case of IO exception.
*/
private File uncommentProperties(URL url) throws IOException {
InputStream in = url.openStream();
assertNotNull(in);
LineIterator it = IOUtils.lineIterator(in, "UTF-8");
Collection<String> lines = new ArrayList<>();
while (it.hasNext())
lines.add(it.nextLine().replace("#gg.client.", "gg.client."));
IgniteUtils.closeQuiet(in);
File tmp = File.createTempFile(UUID.randomUUID().toString(), "properties");
tmp.deleteOnExit();
FileUtils.writeLines(tmp, lines);
return tmp;
}
/**
* Load properties from the url.
*
* @param expLoaded Expected number of loaded properties.
* @param url URL to load properties from.
* @return Loaded properties.
* @throws IOException In case of IO exception.
*/
private Properties loadProperties(int expLoaded, URL url) throws IOException {
InputStream in = url.openStream();
Properties props = new Properties();
assertEquals(0, props.size());
props.load(in);
assertEquals(expLoaded, props.size());
IgniteUtils.closeQuiet(in);
return props;
}
/**
* Validate loaded configuration.
*
* @param expDataCfgs Expected data configurations count.
* @param cfg Client configuration to validate.
*/
private void validateConfig(int expDataCfgs, GridClientConfiguration cfg) {
assertEquals(GridClientRandomBalancer.class, cfg.getBalancer().getClass());
assertEquals(10000, cfg.getConnectTimeout());
assertEquals(null, cfg.getSecurityCredentialsProvider());
assertEquals(expDataCfgs, cfg.getDataConfigurations().size());
if (expDataCfgs == 2) {
GridClientDataConfiguration nullCfg = cfg.getDataConfiguration(null);
assertEquals(null, nullCfg.getName());
assertEquals(null, nullCfg.getAffinity());
assertEquals(GridClientRandomBalancer.class, nullCfg.getPinnedBalancer().getClass());
GridClientDataConfiguration partCfg = cfg.getDataConfiguration("partitioned");
assertEquals("partitioned", partCfg.getName());
assertEquals(GridClientPartitionAffinity.class, partCfg.getAffinity().getClass());
assertEquals(GridClientRoundRobinBalancer.class, partCfg.getPinnedBalancer().getClass());
}
assertEquals(DFLT_MAX_CONN_IDLE_TIME, cfg.getMaxConnectionIdleTime());
assertEquals(GridClientProtocol.TCP, cfg.getProtocol());
assertEquals(Arrays.asList("localhost:11211"), new ArrayList<>(cfg.getServers()));
assertEquals(true, cfg.isEnableAttributesCache());
assertEquals(true, cfg.isEnableMetricsCache());
assertEquals(true, cfg.isTcpNoDelay());
assertEquals(null, cfg.getSslContextFactory(), null);
assertEquals(DFLT_TOP_REFRESH_FREQ, cfg.getTopologyRefreshFrequency());
}
}