com.gemstone.gemfire.internal.jta.GlobalTransactionTest 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.
*/
package com.gemstone.gemfire.internal.jta;
import junit.framework.TestCase;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.Context;
import javax.transaction.RollbackException;
import javax.transaction.Synchronization;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import javax.transaction.Status;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.distributed.DistributedSystem;
import com.gemstone.gemfire.internal.datasource.GemFireBasicDataSource;
import com.gemstone.gemfire.internal.datasource.GemFireTransactionDataSource;
public class GlobalTransactionTest extends TestCase {
private static Properties props = null;
private static DistributedSystem ds1 = null;
private static Cache cache = null;
private static UserTransaction utx = null;
private static TransactionManager tm = null;
static {
try {
props = new Properties();
// props.setProperty("mcast-port", "33405");
String path = System.getProperty("JTAXMLFILE");
props.setProperty("cache-xml-file", path);
ds1 = DistributedSystem.connect(props);
cache = CacheFactory.create(ds1);
utx = new UserTransactionImpl();
tm = TransactionManagerImpl.getTransactionManager();
}
catch (Exception e) {
fail("Exception occured in creation of ds and cache due to " + e);
e.printStackTrace();
}
}
public GlobalTransactionTest(String name) {
super(name);
}
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 occured in testGetSimpleDataSource due to " + e);
e.printStackTrace();
}
}
public void testSetRollbackOnly() {
try {
utx.begin();
utx.setRollbackOnly();
Transaction txn = tm.getTransaction();
if (txn.getStatus() != Status.STATUS_MARKED_ROLLBACK) {
utx.rollback();
fail("testSetRollbackonly failed");
}
utx.rollback();
}
catch (Exception e) {
fail("exception in testSetRollbackonly due to " + e);
e.printStackTrace();
}
}
public void testEnlistResource() {
try {
boolean exceptionoccured = false;
utx.begin();
try {
Context ctx = cache.getJNDIContext();
GemFireTransactionDataSource ds = (GemFireTransactionDataSource) ctx
.lookup("java:/XAPooledDataSource");
ds.getConnection();
}
catch (SQLException e) {
exceptionoccured = true;
}
if (exceptionoccured)
fail("SQLException occured while trying to enlist resource");
utx.rollback();
}
catch (Exception e) {
try {
utx.rollback();
}
catch (Exception e1) {
e1.printStackTrace();
}
fail("exception in testEnlistResource due to " + e);
e.printStackTrace();
}
}
public void testRegisterSynchronization() {
try {
boolean exceptionoccured = false;
utx.begin();
try {
Transaction txn = tm.getTransaction();
Synchronization sync = new SyncImpl();
txn.registerSynchronization(sync);
}
catch (RollbackException e) {
exceptionoccured = true;
}
if (exceptionoccured)
fail("exception occured while trying to register synchronization ");
utx.rollback();
}
catch (Exception e) {
try {
utx.rollback();
}
catch (Exception e1) {
e1.printStackTrace();
}
fail("exception in testRegisterSynchronization due to " + e);
e.printStackTrace();
}
}
public void testEnlistResourceAfterRollBack() {
try {
boolean exceptionoccured = false;
utx.begin();
utx.setRollbackOnly();
Context ctx = cache.getJNDIContext();
try {
GemFireTransactionDataSource ds = (GemFireTransactionDataSource) ctx
.lookup("java:/XAPooledDataSource");
ds.getConnection();
}
catch (SQLException e) {
exceptionoccured = true;
}
if (!exceptionoccured)
fail("SQLException not occured although the transaction was marked for rollback");
utx.rollback();
}
catch (Exception e) {
try {
utx.rollback();
}
catch (Exception e1) {
e1.printStackTrace();
}
fail("exception in testSetRollbackonly due to " + e);
e.printStackTrace();
}
}
public void testRegisterSynchronizationAfterRollBack() {
try {
boolean exceptionoccured = false;
utx.begin();
utx.setRollbackOnly();
Context ctx = cache.getJNDIContext();
try {
Transaction txn = tm.getTransaction();
Synchronization sync = new SyncImpl();
txn.registerSynchronization(sync);
}
catch (RollbackException e) {
exceptionoccured = true;
}
if (!exceptionoccured)
fail("RollbackException not occured although the transaction was marked for rollback");
utx.rollback();
}
catch (Exception e) {
try {
utx.rollback();
}
catch (Exception e1) {
e1.printStackTrace();
}
fail("exception in testSetRollbackonly due to " + e);
e.printStackTrace();
}
}
public void testSuspend() {
try {
utx.begin();
// Transaction txn = tm.getTransaction();
tm.suspend();
Transaction txn1 = tm.getTransaction();
if (txn1 != null)
fail("suspend failed to suspend the transaction");
}
catch (Exception e) {
fail("exception in testSuspend due to " + e);
e.printStackTrace();
}
}
public void testResume() {
try {
utx.begin();
Transaction txn = tm.getTransaction();
Transaction txn1 = tm.suspend();
tm.resume(txn1);
if (txn != tm.getTransaction())
fail("resume failed ");
utx.commit();
}
catch (Exception e) {
fail("exception in testSuspend due to " + e);
e.printStackTrace();
}
}
}