geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject cvs commit: incubator-geronimo/modules/transaction/src/test/org/apache/geronimo/transaction TransactionManagerProxyTest.java
Date Wed, 25 Feb 2004 18:05:51 GMT
djencks     2004/02/25 10:05:51

  Modified:    modules/transaction/src/java/org/apache/geronimo/transaction
                        TransactionManagerProxy.java
               modules/transaction/src/test/org/apache/geronimo/transaction/manager
                        MockResource.java
  Added:       modules/transaction/src/test/org/apache/geronimo/transaction/manager
                        XidImporterTest.java
               modules/transaction/src/test/org/apache/geronimo/transaction
                        TransactionManagerProxyTest.java
  Log:
  a few simple tm tests
  
  Revision  Changes    Path
  1.5       +2 -2      incubator-geronimo/modules/transaction/src/java/org/apache/geronimo/transaction/TransactionManagerProxy.java
  
  Index: TransactionManagerProxy.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/transaction/src/java/org/apache/geronimo/transaction/TransactionManagerProxy.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TransactionManagerProxy.java	25 Feb 2004 09:58:19 -0000	1.4
  +++ TransactionManagerProxy.java	25 Feb 2004 18:05:51 -0000	1.5
  @@ -75,7 +75,7 @@
        */
       public TransactionManagerProxy() {
           this.delegate = new TransactionManagerImpl();
  -        this.importer = null;
  +        this.importer = (XidImporter)delegate;
       }
   
       public void setTransactionTimeout(int timeout) throws SystemException {
  
  
  
  1.3       +19 -1     incubator-geronimo/modules/transaction/src/test/org/apache/geronimo/transaction/manager/MockResource.java
  
  Index: MockResource.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/transaction/src/test/org/apache/geronimo/transaction/manager/MockResource.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MockResource.java	25 Feb 2004 09:58:19 -0000	1.2
  +++ MockResource.java	25 Feb 2004 18:05:51 -0000	1.3
  @@ -30,6 +30,9 @@
       private Xid xid;
       private MockResourceManager manager;
       private int timeout = 0;
  +    private boolean prepared;
  +    private boolean committed;
  +    private boolean rolledback;
   
       public MockResource(MockResourceManager manager) {
           this.manager = manager;
  @@ -67,13 +70,16 @@
       }
   
       public int prepare(Xid xid) throws XAException {
  +        prepared = true;
           return 0;
       }
   
       public void commit(Xid xid, boolean onePhase) throws XAException {
  +        committed = true;
       }
   
       public void rollback(Xid xid) throws XAException {
  +        rolledback = true;
           manager.forget(xid, this);
       }
   
  @@ -90,6 +96,18 @@
   
       public Xid[] recover(int flag) throws XAException {
           throw new UnsupportedOperationException();
  +    }
  +
  +    public boolean isPrepared() {
  +        return prepared;
  +    }
  +
  +    public boolean isCommitted() {
  +        return committed;
  +    }
  +
  +    public boolean isRolledback() {
  +        return rolledback;
       }
   
   }
  
  
  
  1.1                  incubator-geronimo/modules/transaction/src/test/org/apache/geronimo/transaction/manager/XidImporterTest.java
  
  Index: XidImporterTest.java
  ===================================================================
  /**
   *
   * Copyright 2004 The Apache Software Foundation
   *
   *  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.
   */
  
  package org.apache.geronimo.transaction.manager;
  
  import javax.transaction.xa.Xid;
  import javax.transaction.xa.XAResource;
  import javax.transaction.xa.XAException;
  import javax.transaction.Transaction;
  import javax.transaction.Status;
  
  import junit.framework.TestCase;
  
  /**
   *
   *
   * @version $Revision: 1.1 $ $Date: 2004/02/25 18:05:51 $
   *
   * */
  public class XidImporterTest extends TestCase{
  
      MockResourceManager rm1 = new MockResourceManager(true);
      MockResource r1_1 = new MockResource(rm1);
      MockResource r1_2 = new MockResource(rm1);
      MockResourceManager rm2 = new MockResourceManager(true);
      MockResource r2_1 = new MockResource(rm2);
      MockResource r2_2 = new MockResource(rm2);
  
      XidImporter tm = new TransactionManagerImpl();
      XidFactory xidFactory = new XidFactory();
  
      public void testImportXid() throws Exception {
          Xid externalXid = xidFactory.createXid();
          Transaction tx = tm.importXid(externalXid);
          assertNotNull(tx);
          assertEquals(Status.STATUS_ACTIVE, tx.getStatus());
      }
  
      public void testNoResourcesCommitOnePhase() throws Exception {
          Xid externalXid = xidFactory.createXid();
          Transaction tx = tm.importXid(externalXid);
          tm.commit(tx, true);
          assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
      }
  
      public void testNoResourcesCommitTwoPhase() throws Exception {
          Xid externalXid = xidFactory.createXid();
          Transaction tx = tm.importXid(externalXid);
          assertEquals(XAResource.XA_RDONLY, tm.prepare(tx));
          assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
      }
  
      public void testNoResourcesMarkRollback() throws Exception {
          Xid externalXid = xidFactory.createXid();
          Transaction tx = tm.importXid(externalXid);
          tx.setRollbackOnly();
          try {
              tm.prepare(tx);
              fail("should throw rollback exception in an XAException");
          } catch (XAException e) {
          }
          assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
      }
  
      public void testNoResourcesRollback() throws Exception {
          Xid externalXid = xidFactory.createXid();
          Transaction tx = tm.importXid(externalXid);
          tm.rollback(tx);
          assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
      }
  
      public void testOneResourceOnePhaseCommit() throws Exception {
          Xid externalXid = xidFactory.createXid();
          Transaction tx = tm.importXid(externalXid);
          tx.enlistResource(r1_1);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          tm.commit(tx, true);
          assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
      }
  
      public void testOneResourceTwoPhaseCommit() throws Exception {
          Xid externalXid = xidFactory.createXid();
          Transaction tx = tm.importXid(externalXid);
          tx.enlistResource(r1_1);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          assertEquals(XAResource.XA_OK, tm.prepare(tx));
          assertTrue(!r1_1.isCommitted());
          assertTrue(r1_1.isPrepared());
          assertTrue(!r1_1.isRolledback());
          tm.commit(tx, false);
          assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
          assertTrue(r1_1.isCommitted());
          assertTrue(r1_1.isPrepared());
          assertTrue(!r1_1.isRolledback());
      }
  
      public void testFourResourceTwoPhaseCommit() throws Exception {
          Xid externalXid = xidFactory.createXid();
          Transaction tx = tm.importXid(externalXid);
          tx.enlistResource(r1_1);
          tx.enlistResource(r1_2);
          tx.enlistResource(r2_1);
          tx.enlistResource(r2_2);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          tx.delistResource(r1_2, XAResource.TMSUCCESS);
          tx.delistResource(r2_1, XAResource.TMSUCCESS);
          tx.delistResource(r2_2, XAResource.TMSUCCESS);
          assertEquals(XAResource.XA_OK, tm.prepare(tx));
          assertTrue(!r1_1.isCommitted() & !r1_2.isCommitted());
          assertTrue(r1_1.isPrepared() ^ r1_2.isPrepared());
          assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback());
          assertTrue(!r2_1.isCommitted() & !r2_2.isCommitted());
          assertTrue(r2_1.isPrepared() ^ r2_2.isPrepared());
          assertTrue(!r2_1.isRolledback() & !r2_2.isRolledback());
          tm.commit(tx, false);
          assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
          assertTrue((r1_1.isCommitted() & r1_1.isPrepared()) ^ (r1_2.isCommitted() &
r1_2.isPrepared()));
          assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback());
          assertTrue((r2_1.isCommitted() & r2_1.isPrepared()) ^ (r2_2.isCommitted() &
r2_2.isPrepared()));
          assertTrue(!r2_1.isRolledback() & !r2_2.isRolledback());
      }
  
  }
  
  
  
  1.1                  incubator-geronimo/modules/transaction/src/test/org/apache/geronimo/transaction/TransactionManagerProxyTest.java
  
  Index: TransactionManagerProxyTest.java
  ===================================================================
  /**
   *
   * Copyright 2004 The Apache Software Foundation
   *
   *  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.
   */
  
  package org.apache.geronimo.transaction;
  
  import javax.transaction.RollbackException;
  import javax.transaction.Status;
  import javax.transaction.Transaction;
  import javax.transaction.xa.XAResource;
  
  import junit.framework.TestCase;
  import org.apache.geronimo.transaction.manager.MockResource;
  import org.apache.geronimo.transaction.manager.MockResourceManager;
  
  /**
   *
   *
   * @version $Revision: 1.1 $ $Date: 2004/02/25 18:05:51 $
   *
   * */
  public class TransactionManagerProxyTest extends TestCase {
  
      MockResourceManager rm1 = new MockResourceManager(true);
      MockResource r1_1 = new MockResource(rm1);
      MockResource r1_2 = new MockResource(rm1);
      MockResourceManager rm2 = new MockResourceManager(true);
      MockResource r2_1 = new MockResource(rm2);
      MockResource r2_2 = new MockResource(rm2);
  
      TransactionManagerProxy tm = new TransactionManagerProxy();
  
      public void testNoResourcesCommit() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
          tm.commit();
          assertNull(tm.getTransaction());
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
          Transaction tx = tm.getTransaction();
          assertNotNull(tx);
          tx.commit();
          assertNotNull(tm.getTransaction());
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
      }
  
      public void testNoResourcesMarkRollbackOnly() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
          tm.getTransaction().setRollbackOnly();
          assertEquals(Status.STATUS_MARKED_ROLLBACK, tm.getStatus());
          try {
              tm.commit();
              fail("tx should roll back");
          } catch (RollbackException e) {
              //expected
          }
          assertNull(tm.getTransaction());
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
          Transaction tx = tm.getTransaction();
          assertNotNull(tx);
          tx.setRollbackOnly();
          assertEquals(Status.STATUS_MARKED_ROLLBACK, tx.getStatus());
          try {
              tx.commit();
              fail("tx should roll back");
          } catch (RollbackException e) {
              //expected
          }
          assertNotNull(tm.getTransaction());
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
      }
  
      public void testNoResoucesRollback() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
          tm.rollback();
          assertNull(tm.getTransaction());
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
          Transaction tx = tm.getTransaction();
          assertNotNull(tx);
          tx.rollback();
          assertNotNull(tm.getTransaction());
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
  
          //check rollback when marked rollback only
          tm.begin();
          assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
          tm.getTransaction().setRollbackOnly();
          assertEquals(Status.STATUS_MARKED_ROLLBACK, tm.getStatus());
          tm.rollback();
          assertNull(tm.getTransaction());
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
      }
  
      public void testOneResourceCommit() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          Transaction tx = tm.getTransaction();
          tx.enlistResource(r1_1);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          tx.commit();
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          assertTrue(r1_1.isCommitted());
          assertTrue(!r1_1.isPrepared());
          assertTrue(!r1_1.isRolledback());
      }
  
      public void testOneResourceMarkedRollback() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          Transaction tx = tm.getTransaction();
          tx.enlistResource(r1_1);
          tx.setRollbackOnly();
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          try {
              tx.commit();
              fail("tx should roll back");
          } catch (RollbackException e) {
              //expected
          }
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          assertTrue(!r1_1.isCommitted());
          assertTrue(!r1_1.isPrepared());
          assertTrue(r1_1.isRolledback());
      }
  
      public void testOneResourceRollback() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          Transaction tx = tm.getTransaction();
          tx.enlistResource(r1_1);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          tx.rollback();
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          assertTrue(!r1_1.isCommitted());
          assertTrue(!r1_1.isPrepared());
          assertTrue(r1_1.isRolledback());
      }
  
      public void testTwoResourceOneRMCommit() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          Transaction tx = tm.getTransaction();
          tx.enlistResource(r1_1);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          tx.enlistResource(r1_2);
          tx.delistResource(r1_2, XAResource.TMSUCCESS);
          tx.commit();
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          assertTrue(r1_1.isCommitted() ^ r1_2.isCommitted());
          assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared());
          assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback());
      }
  
      public void testTwoResourceOneRMMarkRollback() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          Transaction tx = tm.getTransaction();
          tx.enlistResource(r1_1);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          tx.enlistResource(r1_2);
          tx.delistResource(r1_2, XAResource.TMSUCCESS);
          tx.setRollbackOnly();
          try {
              tx.commit();
              fail("tx should roll back");
          } catch (RollbackException e) {
              //expected
          }
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          assertTrue(!r1_1.isCommitted() & !r1_2.isCommitted());
          assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared());
          assertTrue(r1_1.isRolledback() ^ r1_2.isRolledback());
      }
  
      public void testTwoResourcesOneRMRollback() throws Exception {
          tm.begin();
          Transaction tx = tm.getTransaction();
          tx.enlistResource(r1_1);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          tx.enlistResource(r1_2);
          tx.delistResource(r1_2, XAResource.TMSUCCESS);
          tx.setRollbackOnly();
          tx.rollback();
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          assertTrue(!r1_1.isCommitted() & !r1_2.isCommitted());
          assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared());
          assertTrue(r1_1.isRolledback() ^ r1_2.isRolledback());
      }
  
      public void testFourResourceTwoRMCommit() throws Exception {
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          tm.begin();
          Transaction tx = tm.getTransaction();
          tx.enlistResource(r1_1);
          tx.enlistResource(r1_2);
          tx.enlistResource(r2_1);
          tx.enlistResource(r2_2);
          tx.delistResource(r1_1, XAResource.TMSUCCESS);
          tx.delistResource(r1_2, XAResource.TMSUCCESS);
          tx.delistResource(r2_1, XAResource.TMSUCCESS);
          tx.delistResource(r2_2, XAResource.TMSUCCESS);
          tx.commit();
          assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
          assertTrue((r1_1.isCommitted() & r1_1.isPrepared()) ^ (r1_2.isCommitted() &
r1_2.isPrepared()));
          assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback());
          assertTrue((r2_1.isCommitted() & r2_1.isPrepared()) ^ (r2_2.isCommitted() &
r2_2.isPrepared()));
          assertTrue(!r2_1.isRolledback() & !r2_2.isRolledback());
      }
  
  
  }
  
  
  

Mime
View raw message