URLParser { public static ConnectionInfo parser(String url) { ConnectionURLParser parser = null; String lowerCaseUrl = url.toLowerCase(); if (lowerCaseUrl.startsWith(MYSQL_JDBC_URL_PREFIX)) { parser = new MysqlURLParser(url); } else if (lowerCaseUrl.startsWith(ORACLE_JDBC_URL_PREFIX)) { parser = new OracleURLParser(url); } else if (lowerCaseUrl.startsWith(H2_JDBC_URL_PREFIX)) { parser = new H2URLParser(url); } else if (lowerCaseUrl.startsWith(POSTGRESQL_JDBC_URL_PREFIX)) { parser = new PostgreSQLURLParser(url); } else if (lowerCaseUrl.startsWith(MARIADB_JDBC_URL_PREFIX)) { parser = new MariadbURLParser(url); } return parser.parse(); }  static ConnectionInfo parser(String url);  }
@Test public void testParseMysqlJDBCURLWithHost() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql assertThat(connectionInfo.getDBType(), is("Mysql")); assertThat(connectionInfo.getDatabaseName(), is("test")); assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3306")); }
@Test public void testParseMysqlJDBCURLWithoutDB() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql assertThat(connectionInfo.getDBType(), is("Mysql")); assertThat(connectionInfo.getDatabaseName(), is("")); assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3306")); }
@Test public void testParseMysqlJDBCURLWithHostAndPort() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql assertThat(connectionInfo.getDBType(), is("Mysql")); assertThat(connectionInfo.getDatabaseName(), is("test")); assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3307")); }
@Test public void testParseMysqlJDBCURLWithMultiHost() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql assertThat(connectionInfo.getDBType(), is("Mysql")); assertThat(connectionInfo.getDatabaseName(), is("test")); assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3307,secondaryhost1:3306,secondaryhost2:3306")); }
@Test public void testParseMysqlJDBCURLWithConnectorJs() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mysql:replication: assertThat(connectionInfo.getDBType(), is("Mysql")); assertThat(connectionInfo.getDatabaseName(), is("test")); assertThat(connectionInfo.getDatabasePeer(), is("master:3306,slave1:3306,slave2:3306,slave3:3306")); }
@Test public void testParseOracleJDBCURLWithHost() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:@localhost:orcl"); assertThat(connectionInfo.getDBType(), is("Oracle")); assertThat(connectionInfo.getDatabaseName(), is("orcl")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:1521")); }
@Test public void testParseOracleJDBCURLWithHostAndPort() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:@localhost:1522:orcl"); assertThat(connectionInfo.getDBType(), is("Oracle")); assertThat(connectionInfo.getDatabaseName(), is("orcl")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:1522")); }
@Test public void testParseOracleSID() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:@localhost:1522/orcl"); assertThat(connectionInfo.getDBType(), is("Oracle")); assertThat(connectionInfo.getDatabaseName(), is("orcl")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:1522")); }
@Test public void testParseOracleServiceName() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:@ assertThat(connectionInfo.getDBType(), is("Oracle")); assertThat(connectionInfo.getDatabaseName(), is("orcl")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:1521")); }
@Test public void testParseOracleTNSName() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST= localhost )(PORT= 1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))"); assertThat(connectionInfo.getDBType(), is("Oracle")); assertThat(connectionInfo.getDatabaseName(), is("orcl")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:1521")); }
@Test public void testParseOracleTNSNameWithMultiAddress() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL= TCP)(HOST=hostA)(PORT= 1523 ))(ADDRESS=(PROTOCOL=TCP)(HOST=hostB)(PORT= 1521 )))(SOURCE_ROUTE=yes)(CONNECT_DATA=(SERVICE_NAME=orcl)))"); assertThat(connectionInfo.getDBType(), is("Oracle")); assertThat(connectionInfo.getDatabaseName(), is("orcl")); assertThat(connectionInfo.getDatabasePeer(), is("hostA:1523,hostB:1521")); }
@Test public void testParseOracleJDBCURLWithUserNameAndPassword() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:oracle:thin:scott/tiger@myhost:1521:orcl"); assertThat(connectionInfo.getDBType(), is("Oracle")); assertThat(connectionInfo.getDatabaseName(), is("orcl")); assertThat(connectionInfo.getDatabasePeer(), is("myhost:1521")); }
@Test public void testParseH2JDBCURLWithEmbedded() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:file:/data/sample"); assertThat(connectionInfo.getDBType(), is("H2")); assertThat(connectionInfo.getDatabaseName(), is("/data/sample")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1")); }
@Test public void testParseH2JDBCURLWithEmbeddedRunningInWindows() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:file:C:/data/sample"); assertThat(connectionInfo.getDBType(), is("H2")); assertThat(connectionInfo.getDatabaseName(), is("C:/data/sample")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1")); }
@Test public void testParseH2JDBCURLWithMemoryMode() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:mem:test_mem"); assertThat(connectionInfo.getDBType(), is("H2")); assertThat(connectionInfo.getDatabaseName(), is("test_mem")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1")); }
@Test public void testParseH2JDBCURL() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:tcp: assertThat(connectionInfo.getDBType(), is("H2")); assertThat(connectionInfo.getDatabaseName(), is("sample")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:8084")); }
@Test public void testParseMariadbJDBCURLWithHost() { ConnectionInfo connectionInfo = new URLParser().parser("jdbc:mariadb assertThat(connectionInfo.getDBType(), is("Mariadb")); assertThat(connectionInfo.getDatabaseName(), is("test")); assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3306")); }