Search in sources :

Example 11 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class ConnectionUrlTest method testLoadingPropertiesFromConfigFiles.

/**
 * Tests loading properties from config files.
 */
@Test
public void testLoadingPropertiesFromConfigFiles() {
    Properties propsFromFile = ConnectionUrl.getPropertiesFromConfigFiles("fullDebug");
    List<String> connStr = new ArrayList<>();
    connStr.add("jdbc:mysql://johndoe:secret@mysql:1234/sakila");
    connStr.add("jdbc:mysql://johndoe:secret@mysql:1234/sakila?useConfigs=fullDebug");
    connStr.add("jdbc:mysql://johndoe:secret@address=(host=mysql)(port=1234)(useConfigs=fullDebug)/sakila");
    for (String cs : connStr) {
        Properties props = new Properties();
        if (cs.indexOf(PropertyKey.useConfigs.getKeyName()) == -1) {
            // Send "useConfigs" through external properties.
            props.setProperty(PropertyKey.useConfigs.getKeyName(), "fullDebug");
        }
        ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(cs, props);
        if (cs.indexOf("address=") == -1) {
            // Properties from file must be found simultaneously in per connection properties and per host.
            Properties asProps = connUrl.getConnectionArgumentsAsProperties();
            for (String key : propsFromFile.stringPropertyNames()) {
                assertEquals(propsFromFile.getProperty(key), asProps.getProperty(key), cs + "#" + key);
            }
            Map<String, String> asMap = connUrl.getOriginalProperties();
            for (String key : propsFromFile.stringPropertyNames()) {
                assertEquals(propsFromFile.getProperty(key), asMap.get(key), cs + "#" + key);
            }
        }
        Properties hostProps = connUrl.getMainHost().exposeAsProperties();
        for (String key : propsFromFile.stringPropertyNames()) {
            assertEquals(propsFromFile.getProperty(key), hostProps.getProperty(key), cs + "#" + key);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ReplicationConnectionUrl(com.mysql.cj.conf.url.ReplicationConnectionUrl) XDevApiConnectionUrl(com.mysql.cj.conf.url.XDevApiConnectionUrl) SingleConnectionUrl(com.mysql.cj.conf.url.SingleConnectionUrl) LoadBalanceConnectionUrl(com.mysql.cj.conf.url.LoadBalanceConnectionUrl) XDevApiDnsSrvConnectionUrl(com.mysql.cj.conf.url.XDevApiDnsSrvConnectionUrl) LoadBalanceDnsSrvConnectionUrl(com.mysql.cj.conf.url.LoadBalanceDnsSrvConnectionUrl) FailoverDnsSrvConnectionUrl(com.mysql.cj.conf.url.FailoverDnsSrvConnectionUrl) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) ReplicationDnsSrvConnectionUrl(com.mysql.cj.conf.url.ReplicationDnsSrvConnectionUrl) FailoverConnectionUrl(com.mysql.cj.conf.url.FailoverConnectionUrl) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 12 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class ConnectionUrlTest method testConnectionStringCache.

/**
 * Tests the connection strings internal cache.
 */
@Test
public void testConnectionStringCache() {
    Properties props1 = new Properties();
    props1.setProperty("propKey", "propValue");
    Properties props2 = new Properties(props1);
    ConnectionUrl cu1 = ConnectionUrl.getConnectionUrlInstance("jdbc:mysql://localhost:3306/?param=value", null);
    ConnectionUrl cu2 = ConnectionUrl.getConnectionUrlInstance("jdbc:mysql://localhost:3306/?param=value", props1);
    ConnectionUrl cu3 = ConnectionUrl.getConnectionUrlInstance("jdbc:mysql://localhost:3306/?param=value", props1);
    ConnectionUrl cu4 = ConnectionUrl.getConnectionUrlInstance("jdbc:mysql://localhost:3306/?param=value", props2);
    ConnectionUrl cu5 = ConnectionUrl.getConnectionUrlInstance("jdbc:mysql://localhost:3306/?param=value&flag", props1);
    assertNotSame(cu1, cu2);
    assertSame(cu2, cu3);
    assertSame(cu3, cu4);
    assertNotSame(cu4, cu5);
    assertNotSame(cu5, cu1);
}
Also used : ReplicationConnectionUrl(com.mysql.cj.conf.url.ReplicationConnectionUrl) XDevApiConnectionUrl(com.mysql.cj.conf.url.XDevApiConnectionUrl) SingleConnectionUrl(com.mysql.cj.conf.url.SingleConnectionUrl) LoadBalanceConnectionUrl(com.mysql.cj.conf.url.LoadBalanceConnectionUrl) XDevApiDnsSrvConnectionUrl(com.mysql.cj.conf.url.XDevApiDnsSrvConnectionUrl) LoadBalanceDnsSrvConnectionUrl(com.mysql.cj.conf.url.LoadBalanceDnsSrvConnectionUrl) FailoverDnsSrvConnectionUrl(com.mysql.cj.conf.url.FailoverDnsSrvConnectionUrl) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) ReplicationDnsSrvConnectionUrl(com.mysql.cj.conf.url.ReplicationDnsSrvConnectionUrl) FailoverConnectionUrl(com.mysql.cj.conf.url.FailoverConnectionUrl) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 13 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class ConnectionUrlTest method testBug28150662.

/**
 * Tests fix for BUG#28150662, CONNECTOR/J 8 MALFORMED DATABASE URL EXCEPTION WHIT CORRECT URL STRING.
 */
@Test
public void testBug28150662() {
    List<String> connStr = new ArrayList<>();
    connStr.add("jdbc:mysql://localhost:3306/db1?connectionCollation=utf8mb4_unicode_ci&user=user1&sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0");
    connStr.add("jdbc:mysql://localhost:3306/db1?connectionCollation=utf8mb4_unicode_ci&sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0&user=user1");
    connStr.add("jdbc:mysql://address=(host=localhost)(port=3306)(connectionCollation=utf8mb4_unicode_ci)(sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0)(user=user1)/db1");
    connStr.add("jdbc:mysql://(host=localhost,port=3306,connectionCollation=utf8mb4_unicode_ci,sessionVariables=sql_mode='IGNORE_SPACE%2CANSI'%2CFOREIGN_KEY_CHECKS=0,user=user1)/db1");
    connStr.add("jdbc:mysql://verylonghostname01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:3306/db1?connectionCollation=utf8mb4_unicode_ci&user=user1&sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0");
    connStr.add("jdbc:mysql://verylonghostname01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:3306/db1?connectionCollation=utf8mb4_unicode_ci&sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0&user=user1");
    connStr.add("jdbc:mysql://address=(host=verylonghostname01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789)(port=3306)(connectionCollation=utf8mb4_unicode_ci)(sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0)(user=user1)/db1");
    connStr.add("jdbc:mysql://(host=verylonghostname01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789,port=3306,connectionCollation=utf8mb4_unicode_ci,sessionVariables=sql_mode='IGNORE_SPACE%2CANSI'%2CFOREIGN_KEY_CHECKS=0,user=user1)/db1");
    for (String cs : connStr) {
        ConnectionUrl url = ConnectionUrl.getConnectionUrlInstance(cs, null);
        HostInfo hi = url.getMainHost();
        assertEquals("utf8mb4_unicode_ci", hi.getHostProperties().get("connectionCollation"));
        assertEquals("user1", hi.getUser());
        assertEquals("sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0", hi.getHostProperties().get("sessionVariables"));
    }
}
Also used : ArrayList(java.util.ArrayList) ReplicationConnectionUrl(com.mysql.cj.conf.url.ReplicationConnectionUrl) XDevApiConnectionUrl(com.mysql.cj.conf.url.XDevApiConnectionUrl) SingleConnectionUrl(com.mysql.cj.conf.url.SingleConnectionUrl) LoadBalanceConnectionUrl(com.mysql.cj.conf.url.LoadBalanceConnectionUrl) XDevApiDnsSrvConnectionUrl(com.mysql.cj.conf.url.XDevApiDnsSrvConnectionUrl) LoadBalanceDnsSrvConnectionUrl(com.mysql.cj.conf.url.LoadBalanceDnsSrvConnectionUrl) FailoverDnsSrvConnectionUrl(com.mysql.cj.conf.url.FailoverDnsSrvConnectionUrl) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) ReplicationDnsSrvConnectionUrl(com.mysql.cj.conf.url.ReplicationDnsSrvConnectionUrl) FailoverConnectionUrl(com.mysql.cj.conf.url.FailoverConnectionUrl) HostInfo(com.mysql.cj.conf.HostInfo) Test(org.junit.jupiter.api.Test)

Example 14 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class ConnectionUrlTest method testPropertiesTransformer.

/**
 * Tests the usage of a properties transformer.
 */
@Test
public void testPropertiesTransformer() {
    String propsTransClassName = ConnectionPropertiesTest.class.getName();
    List<String> connStr = new ArrayList<>();
    connStr.add("jdbc:mysql://johndoe:secret@mysql:1234/sakila");
    connStr.add("jdbc:mysql://johndoe:secret@mysql:1234/sakila?stars=*");
    connStr.add("jdbc:mysql://johndoe:secret@address=(host=mysql)(port=1234)/sakila");
    connStr.add("jdbc:mysql://johndoe:secret@address=(host=mysql)(port=1234)(stars=*)/sakila");
    connStr.add("jdbc:mysql://johndoe:secret@address=(host=mysql)(port=1234)/sakila?stars=*");
    connStr.add("jdbc:mysql://johndoe:secret@mysql:1234/sakila?propertiesTransform=" + propsTransClassName);
    connStr.add("jdbc:mysql://johndoe:secret@mysql:1234/sakila?stars=*&propertiesTransform=" + propsTransClassName);
    connStr.add("jdbc:mysql://johndoe:secret@address=(host=mysql)(port=1234)(propertiesTransform=" + propsTransClassName + ")/sakila");
    connStr.add("jdbc:mysql://johndoe:secret@address=(host=mysql)(port=1234)(propertiesTransform=" + propsTransClassName + ")(stars=*)/sakila");
    connStr.add("jdbc:mysql://johndoe:secret@address=(host=mysql)(port=1234)(propertiesTransform=" + propsTransClassName + ")/sakila?stars=*");
    for (String cs : connStr) {
        Properties props = new Properties();
        if (cs.indexOf(PropertyKey.propertiesTransform.getKeyName()) == -1) {
            // Send "propertiesTransform" parameter through external properties.
            props.setProperty(PropertyKey.propertiesTransform.getKeyName(), propsTransClassName);
        }
        if (cs.indexOf("stars") == -1) {
            // Send "stars" parameter through external properties.
            props.setProperty("stars", "*");
        }
        ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(cs, props);
        // "propertiesTransform" doesn't apply when set through host internal properties.
        boolean transforms = cs.indexOf("(propertiesTransform") == -1;
        assertEquals(transforms ? "**" : "*", connUrl.getMainHost().getProperty("stars"), cs + "#hostProps");
        if (cs.indexOf("(stars") == -1) {
            assertEquals("*", connUrl.getOriginalProperties().get("stars"), cs + "#originalProps");
            assertEquals(transforms ? "**" : "*", connUrl.getConnectionArgumentsAsProperties().getProperty("stars"), cs + "#connProps");
        } else {
            assertNull(connUrl.getOriginalProperties().get("stars"), cs + "#originalProps");
            assertNull(connUrl.getConnectionArgumentsAsProperties().getProperty("stars"), cs + "#connProps");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ReplicationConnectionUrl(com.mysql.cj.conf.url.ReplicationConnectionUrl) XDevApiConnectionUrl(com.mysql.cj.conf.url.XDevApiConnectionUrl) SingleConnectionUrl(com.mysql.cj.conf.url.SingleConnectionUrl) LoadBalanceConnectionUrl(com.mysql.cj.conf.url.LoadBalanceConnectionUrl) XDevApiDnsSrvConnectionUrl(com.mysql.cj.conf.url.XDevApiDnsSrvConnectionUrl) LoadBalanceDnsSrvConnectionUrl(com.mysql.cj.conf.url.LoadBalanceDnsSrvConnectionUrl) FailoverDnsSrvConnectionUrl(com.mysql.cj.conf.url.FailoverDnsSrvConnectionUrl) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) ReplicationDnsSrvConnectionUrl(com.mysql.cj.conf.url.ReplicationDnsSrvConnectionUrl) FailoverConnectionUrl(com.mysql.cj.conf.url.FailoverConnectionUrl) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 15 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class NonRegisteringDriverTest method testSingleConnectionProtocolReturnsConnectionImpl.

@Test
public void testSingleConnectionProtocolReturnsConnectionImpl() throws SQLException {
    String url = "jdbc:mysql://somehost:1234/test";
    JdbcConnection mockConnectionImplConn = Mockito.mock(JdbcConnection.class);
    ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(url, new Properties());
    NonRegisteringDriver driver = new NonRegisteringDriver();
    try (MockedStatic<ConnectionImpl> mockStaticConnectionImpl = mockStatic(ConnectionImpl.class)) {
        mockStaticConnectionImpl.when(() -> ConnectionImpl.getInstance(eq(connUrl.getMainHost()))).thenReturn(mockConnectionImplConn);
        Connection conn = driver.connect(url, new Properties());
        assertEquals(mockConnectionImplConn, conn);
    }
}
Also used : Connection(java.sql.Connection) LoadBalancedConnection(com.mysql.cj.jdbc.ha.LoadBalancedConnection) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionUrl (com.mysql.cj.conf.ConnectionUrl)49 Test (org.junit.jupiter.api.Test)41 Properties (java.util.Properties)39 HostInfo (com.mysql.cj.conf.HostInfo)21 SQLException (java.sql.SQLException)10 ReplicationConnectionUrl (com.mysql.cj.conf.url.ReplicationConnectionUrl)9 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)9 ResultSet (java.sql.ResultSet)8 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)8 FailoverConnectionUrl (com.mysql.cj.conf.url.FailoverConnectionUrl)7 FailoverDnsSrvConnectionUrl (com.mysql.cj.conf.url.FailoverDnsSrvConnectionUrl)7 LoadBalanceConnectionUrl (com.mysql.cj.conf.url.LoadBalanceConnectionUrl)7 LoadBalanceDnsSrvConnectionUrl (com.mysql.cj.conf.url.LoadBalanceDnsSrvConnectionUrl)7 ReplicationDnsSrvConnectionUrl (com.mysql.cj.conf.url.ReplicationDnsSrvConnectionUrl)7 SingleConnectionUrl (com.mysql.cj.conf.url.SingleConnectionUrl)7 XDevApiConnectionUrl (com.mysql.cj.conf.url.XDevApiConnectionUrl)7 XDevApiDnsSrvConnectionUrl (com.mysql.cj.conf.url.XDevApiDnsSrvConnectionUrl)7 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)6 Connection (java.sql.Connection)6