-
Notifications
You must be signed in to change notification settings - Fork 819
/
PGPropertyTest.java
296 lines (262 loc) · 10.9 KB
/
PGPropertyTest.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (c) 2004, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.postgresql.Driver;
import org.postgresql.PGProperty;
import org.postgresql.ds.PGSimpleDataSource;
import org.postgresql.ds.common.BaseDataSource;
import org.postgresql.jdbc.AutoSave;
import org.postgresql.test.TestUtil;
import org.postgresql.util.URLCoder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.sql.DriverPropertyInfo;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
public class PGPropertyTest {
/**
* Some tests modify the "ssl" system property. To not disturb other test cases in the suite store
* the value of the property and restore it.
*/
private String bootSSLPropertyValue;
@Before
public void setUp() {
bootSSLPropertyValue = System.getProperty("ssl");
}
@After
public void tearDown() {
if (bootSSLPropertyValue == null) {
System.getProperties().remove("ssl");
} else {
System.setProperty("ssl", bootSSLPropertyValue);
}
}
/**
* Test that we can get and set all default values and all choices (if any).
*/
@Test
public void testGetSetAllProperties() {
Properties properties = new Properties();
for (PGProperty property : PGProperty.values()) {
String value = property.get(properties);
assertEquals(property.getDefaultValue(), value);
property.set(properties, value);
assertEquals(value, property.get(properties));
if (property.getChoices() != null && property.getChoices().length > 0) {
for (String choice : property.getChoices()) {
property.set(properties, choice);
assertEquals(choice, property.get(properties));
}
}
}
}
@Test
public void testSortOrder() {
String prevName = null;
for (PGProperty property : PGProperty.values()) {
String name = property.name();
if (prevName != null) {
assertTrue("PGProperty names should be sorted in ascending order: " + name + " < " + prevName, name.compareTo(prevName) > 0);
}
prevName = name;
}
}
/**
* Test that the enum constant is common with the underlying property name.
*/
@Test
public void testEnumConstantNaming() {
for (PGProperty property : PGProperty.values()) {
String enumName = property.name().replaceAll("_", "");
assertEquals("Naming of the enum constant [" + property.name()
+ "] should follow the naming of its underlying property [" + property.getName()
+ "] in PGProperty", property.getName().toLowerCase(), enumName.toLowerCase());
}
}
@Test
public void testDriverGetPropertyInfo() {
Driver driver = new Driver();
DriverPropertyInfo[] infos = driver.getPropertyInfo(
"jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true",
// this is the example we give in docs
new Properties());
for (DriverPropertyInfo info : infos) {
if ("user".equals(info.name)) {
assertEquals("fred", info.value);
} else if ("password".equals(info.name)) {
assertEquals("secret", info.value);
} else if ("ssl".equals(info.name)) {
assertEquals("true", info.value);
}
}
}
/**
* Test if the datasource has getter and setter for all properties.
*/
@Test
public void testDataSourceProperties() throws Exception {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
BeanInfo info = Introspector.getBeanInfo(dataSource.getClass());
// index PropertyDescriptors by name
Map<String, PropertyDescriptor> propertyDescriptors =
new TreeMap<String, PropertyDescriptor>(String.CASE_INSENSITIVE_ORDER);
for (PropertyDescriptor propertyDescriptor : info.getPropertyDescriptors()) {
propertyDescriptors.put(propertyDescriptor.getName(), propertyDescriptor);
}
// test for the existence of all read methods (getXXX/isXXX) and write methods (setXXX) for all
// known properties
for (PGProperty property : PGProperty.values()) {
if (!property.getName().startsWith("PG") && property != PGProperty.SERVICE) {
assertTrue("Missing getter/setter for property [" + property.getName() + "] in ["
+ BaseDataSource.class + "]", propertyDescriptors.containsKey(property.getName()));
assertNotNull("No getter for property [" + property.getName() + "] in ["
+ BaseDataSource.class + "]",
propertyDescriptors.get(property.getName()).getReadMethod());
assertNotNull("No setter for property [" + property.getName() + "] in ["
+ BaseDataSource.class + "]",
propertyDescriptors.get(property.getName()).getWriteMethod());
}
}
// test readability/writability of default value
for (PGProperty property : PGProperty.values()) {
if (!property.getName().startsWith("PG") && property != PGProperty.SERVICE) {
Object propertyValue =
propertyDescriptors.get(property.getName()).getReadMethod().invoke(dataSource);
propertyDescriptors.get(property.getName()).getWriteMethod().invoke(dataSource,
propertyValue);
}
}
}
/**
* Test to make sure that setURL doesn't overwrite autosave
* more should be put in but this scratches the current itch
*/
@Test
public void testOverWriteDSProperties() throws Exception {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setAutosave(AutoSave.CONSERVATIVE);
dataSource.setURL("jdbc:postgresql://localhost:5432/postgres");
assertSame(dataSource.getAutosave(),AutoSave.CONSERVATIVE);
}
/**
* Test that {@link PGProperty#isPresent(Properties)} returns a correct result in all cases.
*/
@Test
public void testIsPresentWithParseURLResult() throws Exception {
Properties givenProperties = new Properties();
givenProperties.setProperty("user", TestUtil.getUser());
givenProperties.setProperty("password", TestUtil.getPassword());
Properties sysProperties = System.getProperties();
sysProperties.remove("ssl");
System.setProperties(sysProperties);
Properties parsedProperties = Driver.parseURL(TestUtil.getURL(), givenProperties);
assertFalse("SSL property should not be present",
PGProperty.SSL.isPresent(parsedProperties));
System.setProperty("ssl", "true");
givenProperties.setProperty("ssl", "true");
parsedProperties = Driver.parseURL(TestUtil.getURL(), givenProperties);
assertTrue("SSL property should be present", PGProperty.SSL.isPresent(parsedProperties));
givenProperties.setProperty("ssl", "anotherValue");
parsedProperties = Driver.parseURL(TestUtil.getURL(), givenProperties);
assertTrue("SSL property should be present", PGProperty.SSL.isPresent(parsedProperties));
parsedProperties = Driver.parseURL(TestUtil.getURL() + "&ssl=true", null);
assertTrue("SSL property should be present", PGProperty.SSL.isPresent(parsedProperties));
}
/**
* Check whether the isPresent method really works.
*/
@Test
public void testPresenceCheck() {
Properties empty = new Properties();
Object value = PGProperty.READ_ONLY.get(empty);
assertNotNull(value);
assertFalse(PGProperty.READ_ONLY.isPresent(empty));
}
@Test
public void testEncodedUrlValues() {
String databaseName = "d&a%ta+base";
String userName = "&u%ser";
String password = "p%a&s^s#w!o@r*";
String url = "jdbc:postgresql://"
+ "localhost" + ":" + 5432 + "/"
+ URLCoder.encode(databaseName)
+ "?user=" + URLCoder.encode(userName)
+ "&password=" + URLCoder.encode(password);
Properties parsed = Driver.parseURL(url, new Properties());
assertEquals("database", databaseName, PGProperty.PG_DBNAME.get(parsed));
assertEquals("user", userName, PGProperty.USER.get(parsed));
assertEquals("password", password, PGProperty.PASSWORD.get(parsed));
}
@Test
public void testLowerCamelCase() {
// These are legacy properties excluded for backward compatibility.
ArrayList<String> excluded = new ArrayList<String>();
excluded.add("LOG_LEVEL"); // Remove with PR #722
excluded.add("PREPARED_STATEMENT_CACHE_SIZE_MIB"); // preparedStatementCacheSizeMi[B]
excluded.add("DATABASE_METADATA_CACHE_FIELDS_MIB"); // databaseMetadataCacheFieldsMi[B]
excluded.add("STRING_TYPE"); // string[t]ype
excluded.add("SSL_MODE"); // ssl[m]ode
excluded.add("SSL_FACTORY"); // ssl[f]actory
excluded.add("SSL_FACTORY_ARG"); // ssl[f]actory[a]rg
excluded.add("SSL_HOSTNAME_VERIFIER"); // ssl[h]ostname[v]erifier
excluded.add("SSL_CERT"); // ssl[c]ert
excluded.add("SSL_KEY"); // ssl[k]ey
excluded.add("SSL_ROOT_CERT"); // ssl[r]oot[c]ert
excluded.add("SSL_PASSWORD"); // ssl[p]assword
excluded.add("SSL_PASSWORD_CALLBACK"); // ssl[p]assword[c]allback
excluded.add("APPLICATION_NAME"); // [A]pplicationName
excluded.add("GSS_LIB"); // gss[l]ib
excluded.add("REWRITE_BATCHED_INSERTS"); // re[W]riteBatchedInserts
for (PGProperty property : PGProperty.values()) {
if (!property.name().startsWith("PG")) { // Ignore all properties that start with PG
String[] words = property.name().split("_");
if (words.length == 1) {
assertEquals(words[0].toLowerCase(), property.getName());
} else {
if (!excluded.contains(property.name())) {
String word = "";
for (int i = 0; i < words.length; i++) {
if (i == 0) {
word = words[i].toLowerCase();
} else {
word += words[i].substring(0, 1).toUpperCase() + words[i].substring(1).toLowerCase();
}
}
assertEquals(word, property.getName());
}
}
}
}
}
@Test
public void testEncodedUrlValuesFromDataSource() {
String databaseName = "d&a%ta+base";
String userName = "&u%ser";
String password = "p%a&s^s#w!o@r*";
String applicationName = "Laurel&Hardy=Best?Yes";
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setDatabaseName(databaseName);
dataSource.setUser(userName);
dataSource.setPassword(password);
dataSource.setApplicationName(applicationName);
Properties parsed = Driver.parseURL(dataSource.getURL(), new Properties());
assertEquals("database", databaseName, PGProperty.PG_DBNAME.get(parsed));
// datasources do not pass username and password as URL parameters
assertFalse("user", PGProperty.USER.isPresent(parsed));
assertFalse("password", PGProperty.PASSWORD.isPresent(parsed));
assertEquals("APPLICATION_NAME", applicationName, PGProperty.APPLICATION_NAME.get(parsed));
}
}