Skip to content

Commit

Permalink
CONJ-858 Properties.put with object that differ from String supported…
Browse files Browse the repository at this point in the history
… even if use is not recommended

```
Properties connProperties = new Properties();
connProperties.setProperty("useSSL", "true");
```
is ok,
but using the same as object will only be taken in account when value has a string value
```
Properties connProperties = new Properties();
connProperties.put("useSSL", true);
```
useSSL will not be enable, without any error.
  • Loading branch information
rusher committed May 12, 2021
1 parent 6644194 commit 90d6d19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/main/java/org/mariadb/jdbc/util/DefaultOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -870,15 +870,15 @@ private static Options parse(
// loop on properties,
// - check DefaultOption to check that property value correspond to type (and range)
// - set values
for (final String key : properties.stringPropertyNames()) {
final String propertyValue = properties.getProperty(key);
final DefaultOptions o = OptionUtils.OPTIONS_MAP.get(key);
for (final Object keyObj : properties.keySet()) {
final Object propertyValue = properties.get(keyObj);
final DefaultOptions o = OptionUtils.OPTIONS_MAP.get(keyObj);
if (o != null && propertyValue != null) {
final Field field = Options.class.getField(o.optionName);
if (o.objType.equals(String.class)) {
field.set(options, propertyValue);
} else if (o.objType.equals(Boolean.class)) {
switch (propertyValue.toLowerCase()) {
switch (propertyValue.toString().toLowerCase()) {
case "":
case "1":
case "true":
Expand All @@ -900,7 +900,7 @@ private static Options parse(
}
} else if (o.objType.equals(Integer.class)) {
try {
final Integer value = Integer.parseInt(propertyValue);
final Integer value = Integer.parseInt(propertyValue.toString());
assert o.minValue != null;
assert o.maxValue != null;
if (value.compareTo((Integer) o.minValue) < 0
Expand Down Expand Up @@ -928,7 +928,7 @@ private static Options parse(
}
} else if (o.objType.equals(Long.class)) {
try {
final Long value = Long.parseLong(propertyValue);
final Long value = Long.parseLong(propertyValue.toString());
assert o.minValue != null;
assert o.maxValue != null;
if (value.compareTo((Long) o.minValue) < 0
Expand Down Expand Up @@ -958,7 +958,7 @@ private static Options parse(
} else {
// keep unknown option:
// those might be used in authentication or identity plugin
options.nonMappedOptions.setProperty(key, properties.getProperty(key));
options.nonMappedOptions.put(keyObj, propertyValue);
}
}

Expand Down Expand Up @@ -1036,11 +1036,10 @@ public static void propertyString(
sb.append('&');
}
sb.append(o.optionName).append('=');
if (o.objType.equals(String.class)) {
sb.append((String) value);
} else if (o.objType.equals(Boolean.class)) {
sb.append(((Boolean) value).toString());
} else if (o.objType.equals(Integer.class) || o.objType.equals(Long.class)) {
if (o.objType.equals(String.class)
|| o.objType.equals(Boolean.class)
|| o.objType.equals(Integer.class)
|| o.objType.equals(Long.class)) {
sb.append(value);
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/mariadb/jdbc/JdbcParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ public void testMariaAlias() throws Throwable {
assertEquals(jdbc.getHaMode(), jdbc2.getHaMode());
}

@Test
public void testPropertiesObjectParsing() throws SQLException {
String url =
"jdbc:mariadb://master:3306,replica1:3307,replica2:3308/database?autoReconnect=true";

UrlParser urlParser = UrlParser.parse(url, null);
assertEquals("database", urlParser.getDatabase());
assertNull(urlParser.getUsername());
assertNull(urlParser.getPassword());
assertNull(urlParser.getOptions().useSsl);
assertNull(urlParser.getOptions().serverSslCert);

Properties prop = new Properties();
prop.setProperty("user", "greg");
prop.setProperty("password", "pass");
prop.put("useSSL", true);
prop.put("serverSslCert", "/path/to/cert.pem");

urlParser = UrlParser.parse(url, prop);

assertEquals("database", urlParser.getDatabase());
assertEquals("greg", urlParser.getUsername());
assertEquals("pass", urlParser.getPassword());
assertTrue(urlParser.getOptions().useSsl);
assertEquals("/path/to/cert.pem", urlParser.getOptions().serverSslCert);
}

@Test
public void testAuroraUseBatchMultiSend() throws Throwable {
assertTrue(
Expand Down

0 comments on commit 90d6d19

Please sign in to comment.