com.gemstone.gemfire.internal.datasource.AbstractPoolCacheTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-junit Show documentation
Show all versions of gemfire-junit Show documentation
SnappyData store based off Pivotal GemFireXD
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
/*
* AbstractPoolCacheTest.java
* JUnit based test
*
* Created on March 3, 2005, 5:24 PM
*/
package com.gemstone.gemfire.internal.datasource;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import javax.naming.Context;
import javax.sql.PooledConnection;
import javax.sql.XAConnection;
import javax.transaction.xa.XAResource;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.distributed.DistributedSystem;
/**
* @author tnegi
*/
public class AbstractPoolCacheTest extends TestCase {
private static Properties props = null;
private static DistributedSystem ds1 = null;
private static Cache cache = null;
static {
try {
props = new Properties();
props.setProperty("log-level", "info");
props.setProperty("mcast-port", "0");
props.setProperty("locators", "");
String path = System.getProperty("JTAXMLFILE");
props.setProperty("cache-xml-file", path);
ds1 = DistributedSystem.connect(props);
cache = CacheFactory.create(ds1);
}
catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public AbstractPoolCacheTest(java.lang.String testName) {
super(testName);
}
public static Test suite() {
TestSuite suite = new TestSuite(AbstractPoolCacheTest.class);
return suite;
}
public void setup() {
}
public void teardown() {
}
public void testGetSimpleDataSource() throws Exception {
try {
Context ctx = cache.getJNDIContext();
GemFireBasicDataSource ds = (GemFireBasicDataSource) ctx
.lookup("java:/SimpleDataSource");
Connection conn = ds.getConnection();
if (conn == null)
fail("DataSourceFactoryTest-testGetSimpleDataSource() Error in creating the GemFireBasicDataSource");
}
catch (Exception e) {
fail("Exception thrown in testGetSimpleDataSource due to " + e);
e.printStackTrace();
}
}
/**
* Test of closeActiveConnection method, of class
* com.gemstone.gemfire.internal.datasource.AbstractPoolCache.
*/
/*
* public void testCloseActiveConnection() { try { Context ctx =
* cache.getJNDIContext(); GemFireConnPooledDataSource ds =
* (GemFireConnPooledDataSource) ctx .lookup("java:/PooledDataSource");
* GemFireConnectionPoolManager provider = (GemFireConnectionPoolManager) ds
* .getConnectionProvider(); ConnectionPoolCacheImpl poolCache =
* (ConnectionPoolCacheImpl) provider .getConnectionPoolCache();
* PooledConnection conn = poolCache.getPooledConnectionFromPool();
* poolCache.closeActiveConnection(conn); if
* (poolCache.activeCache.containsKey(conn)) fail("close active connection
* failed"); } catch (Exception e) { e.printStackTrace(); } }
*/
/**
* Test of returnPooledConnectionToPool method, of class
* com.gemstone.gemfire.internal.datasource.AbstractPoolCache.
*/
public void testReturnPooledConnectionToPool() {
try {
Context ctx = cache.getJNDIContext();
GemFireConnPooledDataSource ds = (GemFireConnPooledDataSource) ctx
.lookup("java:/PooledDataSource");
GemFireConnectionPoolManager provider = (GemFireConnectionPoolManager) ds
.getConnectionProvider();
ConnectionPoolCacheImpl poolCache = (ConnectionPoolCacheImpl) provider
.getConnectionPoolCache();
PooledConnection conn = (PooledConnection) poolCache
.getPooledConnectionFromPool();
if (poolCache.availableCache.containsKey(conn))
fail("connection not removed from available cache list");
if (!poolCache.activeCache.containsKey(conn))
fail("connection not put in active connection list");
provider.returnConnection(conn);
if (!poolCache.availableCache.containsKey(conn))
fail("connection not returned to pool");
if (poolCache.activeCache.containsKey(conn))
fail("connection not returned to active list");
}
catch (Exception e) {
fail("Exception occured in testReturnPooledConnectionToPool due to " + e);
e.printStackTrace();
}
}
/**
* Test of validateConnection method, of class
* com.gemstone.gemfire.internal.datasource.AbstractPoolCache.
*/
public void testValidateConnection() {
try {
Context ctx = cache.getJNDIContext();
GemFireConnPooledDataSource ds = (GemFireConnPooledDataSource) ctx
.lookup("java:/PooledDataSource");
GemFireConnectionPoolManager provider = (GemFireConnectionPoolManager) ds
.getConnectionProvider();
ConnectionPoolCacheImpl poolCache = (ConnectionPoolCacheImpl) provider
.getConnectionPoolCache();
PooledConnection poolConn = (PooledConnection) poolCache
.getPooledConnectionFromPool();
Connection conn = poolConn.getConnection();
if (!ds.validateConnection(conn)) fail("validate connection failed");
conn.close();
if (ds.validateConnection(conn)) fail("validate connection failed");
}
catch (Exception e) {
fail("Exception occured in testValidateConnection due to " + e);
e.printStackTrace();
}
}
/**
* Test of getPooledConnectionFromPool method, of class
* com.gemstone.gemfire.internal.datasource.AbstractPoolCache.
*/
public void testGetPooledConnectionFromPool() {
try {
Context ctx = cache.getJNDIContext();
GemFireConnPooledDataSource ds = (GemFireConnPooledDataSource) ctx
.lookup("java:/PooledDataSource");
GemFireConnectionPoolManager provider = (GemFireConnectionPoolManager) ds
.getConnectionProvider();
ConnectionPoolCacheImpl poolCache = (ConnectionPoolCacheImpl) provider
.getConnectionPoolCache();
PooledConnection poolConn = (PooledConnection) poolCache
.getPooledConnectionFromPool();
if (poolConn == null)
fail("getPooledConnectionFromPool failed to get a connection from pool");
}
catch (Exception e) {
fail("Exception occured in testGetPooledConnectionFromPool due to " + e);
e.printStackTrace();
}
}
public void testCleanUp() {
cache.close();
ds1.disconnect();
}
/**
* Tests if an XAresource obtained from an XAConnection which is already
* closed , can return null or not.
* @author Asif
*/
public void testEffectOfBlockingTimeoutOnXAConnection()
{
try {
Map map = new HashMap();
map.put("init-pool-size", "2");
map.put("jndi-name", "TestXAPooledDataSource");
map.put("max-pool-size", "7");
map.put("idle-timeout-seconds", "20");
map.put("blocking-timeout-seconds", "2");
map.put("login-timeout-seconds", "5");
//map.put("xa-datasource-class","org.apache.derby.jdbc.EmbeddedXADataSource");
map.put("jdbc-driver-class", "org.apache.derby.jdbc.EmbeddedDriver");
map.put("user-name", "mitul");
map.put("password", "83f0069202c571faf1ae6c42b4ad46030e4e31c17409e19a");
map.put("connection-url", "jdbc:derby:newDB;create=true");
List props = new ArrayList();
props
.add(new ConfigProperty("databaseName", "newDB", "java.lang.String"));
GemFireBasicDataSource gbds = (GemFireBasicDataSource)DataSourceFactory
.getSimpleDataSource(map, props);
map.put("xa-datasource-class",
"org.apache.derby.jdbc.EmbeddedXADataSource");
map.put("connection-url", "jdbc:derby:newDB;create=true");
GemFireTransactionDataSource gtds = (GemFireTransactionDataSource)DataSourceFactory
.getTranxDataSource(map, props);
XAConnection xaconn = (XAConnection)gtds.provider.borrowConnection();
try { Thread.sleep(4); } catch (InterruptedException e) { fail("interrupted"); }
for (int i = 0; i < 1000; ++i) {
XAResource xar = xaconn.getXAResource();
System.out.println("XAResource=" + xar);
assertNotNull(xar);
}
}
catch (Exception ignore) {
// TODO Auto-generated catch block
}
}
}