DataSourceFactory { public static PoolableDataSource create(String url, Properties mysqlConnectionProperties) { String driver = null; try { if (url.indexOf(":mysql:") > 0) { driver = System.getProperty(DRIVER_CLASS_NAME, "com.mysql.cj.jdbc.Driver"); Class.forName(driver); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, mysqlConnectionProperties); return create(connectionFactory); } else { throw new RuntimeException("Cannot figure out how to instantiate this data source: " + url); } } catch (ClassNotFoundException e) { throw new RuntimeException("Cannot load driver class: " + driver); } catch (Exception exc) { throw new RuntimeException("Failed to create database source(" + url + ") with driver(" + driver + ")", exc); } }  static PoolableDataSource create(String url, Properties mysqlConnectionProperties); static GenericObjectPoolConfig setupPoolConfig();  }
@Test public void testCrateDataSourceWithMysqlUrl() { Properties props = new Properties(); props.setProperty("user", "user"); props.setProperty("password", "password"); PoolableDataSource src = DataSourceFactory.create("jdbc:mysql:localhost:3306/athenz", props); assertNotNull(src); }
@Test public void testCreateDataSourceWithUnknownUrl() { Properties props = new Properties(); props.setProperty("user", "user"); props.setProperty("password", "password"); try { DataSourceFactory.create("jdbc:tdb:localhost:11080", props); fail(); } catch (RuntimeException ex) { assertTrue(true); } }
@Test public void testCreateDataSourceWithFactory() { System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TTL, "10000"); MockConnectionFactory connectionFactory = new MockConnectionFactory(); PoolableDataSource src = DataSourceFactory.create(connectionFactory); assertNotNull(src); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TTL); }
@Test public void testCreateDataSourceWithFactoryInvalidTTL() { System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TTL, "abc"); MockConnectionFactory connectionFactory = new MockConnectionFactory(); PoolableDataSource src = DataSourceFactory.create(connectionFactory); assertNotNull(src); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TTL); }
@Test public void testWrongDbClass() { System.setProperty(DataSourceFactory.DRIVER_CLASS_NAME, "testDbDriverClass"); Properties props = new Properties(); props.setProperty("user", "user"); props.setProperty("password", "password"); assertThrows(RuntimeException.class, () -> { DataSourceFactory.create("jdbc:mysql:localhost:3306/athenz", props); }); System.clearProperty(DataSourceFactory.DRIVER_CLASS_NAME); }