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);
}
}
}
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);
}
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"));
}
}
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");
}
}
}
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);
}
}
Aggregations