DataSourceFactory { public static GenericObjectPoolConfig setupPoolConfig() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TOTAL, GenericObjectPoolConfig.DEFAULT_MAX_TOTAL)); if (config.getMaxTotal() == 0) { config.setMaxTotal(-1); } config.setMaxIdle(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_IDLE, GenericObjectPoolConfig.DEFAULT_MAX_IDLE)); if (config.getMaxIdle() == 0) { config.setMaxIdle(-1); } config.setMinIdle(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MIN_IDLE, GenericObjectPoolConfig.DEFAULT_MIN_IDLE)); config.setMaxWaitMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_WAIT, GenericObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS)); config.setMinEvictableIdleTimeMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS)); config.setTimeBetweenEvictionRunsMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS)); if (LOG.isDebugEnabled()) { LOG.debug("Config settings for idle object eviction: " + "time interval between eviction thread runs (" + config.getTimeBetweenEvictionRunsMillis() + " millis): minimum timeout for idle objects (" + config.getMinEvictableIdleTimeMillis() + " millis)"); } config.setTestWhileIdle(true); config.setTestOnBorrow(true); config.setTestOnReturn(true); return config; }  static PoolableDataSource create(String url, Properties mysqlConnectionProperties); static GenericObjectPoolConfig setupPoolConfig();  }
@Test public void testPoolConfigDefaultValues() { GenericObjectPoolConfig config = DataSourceFactory.setupPoolConfig(); assertNotNull(config); assertEquals(config.getMaxTotal(), GenericObjectPoolConfig.DEFAULT_MAX_TOTAL); assertEquals(config.getMaxIdle(), GenericObjectPoolConfig.DEFAULT_MAX_IDLE); assertEquals(config.getMinIdle(), GenericObjectPoolConfig.DEFAULT_MIN_IDLE); assertEquals(config.getMaxWaitMillis(), GenericObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS); assertEquals(config.getMinEvictableIdleTimeMillis(), BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); assertEquals(config.getTimeBetweenEvictionRunsMillis(), BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); assertTrue(config.getTestWhileIdle()); assertTrue(config.getTestOnBorrow()); }
@Test public void testPoolConfigSpecifiedValues() { System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TOTAL, "10"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_IDLE, "20"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MIN_IDLE, "30"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_WAIT, "40"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT, "50"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL, "60"); GenericObjectPoolConfig config = DataSourceFactory.setupPoolConfig(); assertNotNull(config); assertEquals(config.getMaxTotal(), 10); assertEquals(config.getMaxIdle(), 20); assertEquals(config.getMinIdle(), 30); assertEquals(config.getMaxWaitMillis(), 40); assertEquals(config.getMinEvictableIdleTimeMillis(), 50); assertEquals(config.getTimeBetweenEvictionRunsMillis(), 60); assertTrue(config.getTestWhileIdle()); assertTrue(config.getTestOnBorrow()); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TOTAL); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_IDLE); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MIN_IDLE); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_WAIT); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL); }
@Test public void testPoolConfigInvalidValues() { System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TOTAL, "a"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_IDLE, "b"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MIN_IDLE, "c"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_WAIT, "d"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT, "e"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL, "f"); GenericObjectPoolConfig config = DataSourceFactory.setupPoolConfig(); assertNotNull(config); assertEquals(config.getMaxTotal(), GenericObjectPoolConfig.DEFAULT_MAX_TOTAL); assertEquals(config.getMaxIdle(), GenericObjectPoolConfig.DEFAULT_MAX_IDLE); assertEquals(config.getMinIdle(), GenericObjectPoolConfig.DEFAULT_MIN_IDLE); assertEquals(config.getMaxWaitMillis(), GenericObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS); assertEquals(config.getMinEvictableIdleTimeMillis(), BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); assertEquals(config.getTimeBetweenEvictionRunsMillis(), BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); assertTrue(config.getTestWhileIdle()); assertTrue(config.getTestOnBorrow()); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TOTAL); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_IDLE); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MIN_IDLE); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_WAIT); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL); }
@Test public void testPoolConfigZeroValues() { System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TOTAL, "0"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_IDLE, "0"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MIN_IDLE, "0"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_WAIT, "0"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT, "0"); System.setProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL, "0"); GenericObjectPoolConfig config = DataSourceFactory.setupPoolConfig(); assertNotNull(config); assertEquals(config.getMaxTotal(), -1); assertEquals(config.getMaxIdle(), -1); assertEquals(config.getMinIdle(), 0); assertEquals(config.getMaxWaitMillis(), 0); assertEquals(config.getMinEvictableIdleTimeMillis(), 0); assertEquals(config.getTimeBetweenEvictionRunsMillis(), 0); assertTrue(config.getTestWhileIdle()); assertTrue(config.getTestOnBorrow()); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_TOTAL); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_IDLE); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MIN_IDLE); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_MAX_WAIT); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT); System.clearProperty(DataSourceFactory.ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL); }