postgresql-jdbc.git / summary / log / commit / refs

commit    2277ffb7b65d3cba9ef05be36408e2fdbef00ee7
Author:   mjanczykowski <mjanczykowski@users.noreply.github.com>
Commit:   Dave Cramer <davecramer@gmail.com>
Date:     Tue Nov 07 12:24:39 2017 +0000

    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.


.../org/postgresql/ds/common/BaseDataSource.java | 19 +++++++ .../test/jdbc2/optional/OptionalTestSuite.java | 1 + .../optional/SimpleDataSourceWithSetURLTest.java | 62 ++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java b/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java index 41b01eca..2716aac5 100644 --- a/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java +++ b/pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java @@ -1075,6 +1075,15 @@ public abstract class BaseDataSource implements CommonDataSource, Referenceable 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 abstract class BaseDataSource implements CommonDataSource, Referenceable } } + /** + * 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) { diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/optional/OptionalTestSuite.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/optional/OptionalTestSuite.java index fe59de08..00c5d1a0 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/optional/OptionalTestSuite.java +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/optional/OptionalTestSuite.java @@ -17,6 +17,7 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({SimpleDataSourceTest.class, SimpleDataSourceWithUrlTest.class, + SimpleDataSourceWithSetURLTest.class, ConnectionPoolTest.class, PoolingDataSourceTest.class, CaseOptimiserDataSourceTest.class}) diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/optional/SimpleDataSourceWithSetURLTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/optional/SimpleDataSourceWithSetURLTest.java new file mode 100644 index 00000000..f9bbd5a9 --- /dev/null +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/optional/SimpleDataSourceWithSetURLTest.java @@ -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()); + } +} [parent: 2f6633bd9e1e]