Permalink
Browse files

feat: add setURL method to BaseDataSource (#999)

* feat: add setURL method to BaseDataSource

Add setURL method to BaseDateSource in order to follow convention used in other DBMS drivers.
Improves JBoss support, allowing to configure XA data source to switch between various DBMS
drivers. Most of vendors use uppercase 'URL' parameter, contrary to PostgreSQL.
  • Loading branch information...
mjanczykowski authored and davecramer committed Nov 7, 2017
1 parent 2f6633b commit 2277ffb7b65d3cba9ef05be36408e2fdbef00ee7
@@ -1075,6 +1075,15 @@ public String getUrl() {
return url.toString();
}

/**
* Generates a {@link DriverManager} URL from the other properties supplied.
*
* @return {@link DriverManager} URL from the other properties supplied
*/
public String getURL() {
return getUrl();
}

/**
* Sets properties from a {@link DriverManager} URL.
*
@@ -1089,6 +1098,16 @@ public void setUrl(String url) {
}
}

/**
* Sets properties from a {@link DriverManager} URL.
* Added to follow convention used in other DBMS.
*
* @param url properties to set
*/
public void setURL(String url) {
setUrl(url);
}

public String getProperty(String name) throws SQLException {
PGProperty pgProperty = PGProperty.forName(name);
if (pgProperty != null) {
@@ -17,6 +17,7 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({SimpleDataSourceTest.class,
SimpleDataSourceWithUrlTest.class,
SimpleDataSourceWithSetURLTest.class,
ConnectionPoolTest.class,
PoolingDataSourceTest.class,
CaseOptimiserDataSourceTest.class})
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2017, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/

package org.postgresql.test.jdbc2.optional;

import static org.junit.Assert.assertEquals;
import static org.postgresql.Driver.parseURL;

import org.postgresql.PGProperty;
import org.postgresql.jdbc2.optional.SimpleDataSource;
import org.postgresql.test.TestUtil;

import org.junit.Test;

import java.util.Properties;

/**
* Performs the basic tests defined in the superclass. Just adds the configuration logic.
*/
public class SimpleDataSourceWithSetURLTest extends BaseDataSourceTest {
/**
* Creates and configures a new SimpleDataSource using setURL method.
*/
@Override
protected void initializeDataSource() {
if (bds == null) {
bds = new SimpleDataSource();
bds.setURL(String.format("jdbc:postgresql://%s:%d/%s?prepareThreshold=%d&loggerLevel=%s", TestUtil.getServer(), TestUtil.getPort(), TestUtil.getDatabase(), TestUtil.getPrepareThreshold(),
TestUtil.getLogLevel()));
bds.setUser(TestUtil.getUser());
bds.setPassword(TestUtil.getPassword());
bds.setProtocolVersion(TestUtil.getProtocolVersion());
}
}

@Test
public void testGetURL() throws Exception {
con = getDataSourceConnection();

String url = bds.getURL();
Properties properties = parseURL(url, null);

assertEquals(TestUtil.getServer(), properties.getProperty(PGProperty.PG_HOST.getName()));
assertEquals(Integer.toString(TestUtil.getPort()), properties.getProperty(PGProperty.PG_PORT.getName()));
assertEquals(TestUtil.getDatabase(), properties.getProperty(PGProperty.PG_DBNAME.getName()));
assertEquals(Integer.toString(TestUtil.getPrepareThreshold()), properties.getProperty(PGProperty.PREPARE_THRESHOLD.getName()));
assertEquals(TestUtil.getLogLevel(), properties.getProperty(PGProperty.LOGGER_LEVEL.getName()));
}

@Test
public void testSetURL() throws Exception {
initializeDataSource();

assertEquals(TestUtil.getServer(), bds.getServerName());
assertEquals(TestUtil.getPort(), bds.getPortNumber());
assertEquals(TestUtil.getDatabase(), bds.getDatabaseName());
assertEquals(TestUtil.getPrepareThreshold(), bds.getPrepareThreshold());
assertEquals(TestUtil.getLogLevel(), bds.getLoggerLevel());
}
}

0 comments on commit 2277ffb

Please sign in to comment.