public void testCommitFails() throws Exception {
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyNonDurableTransactionParticipant() {
       public void commit(Transaction txn) {
         throw new RuntimeException("Commit failed");
       }
     },
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyTransactionParticipant() {}
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   handle.commit();
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(
           (participant == participants[2] ? State.PREPARED : State.COMMITTED),
           participant.getState());
     }
   }
   assertCommitted();
 }
 public void testCommitPrepareAbortsAndFailsLast() throws Exception {
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyTransactionParticipant() {
       public void prepareAndCommit(Transaction txn) throws Exception {
         try {
           txn.abort(abortXcp);
         } catch (RuntimeException e) {
           fail("Unexpected exception: " + e);
         }
         throw new IOException("Prepare failed");
       }
     }
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   try {
     handle.commit();
     fail("Expected IOException");
   } catch (IOException e) {
     System.err.println(e);
   }
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(State.ABORTED, participant.getState());
     }
   }
   assertAborted(abortXcp);
 }
  public void testAbortAborting() {
    final Exception abortCause = new Exception("Why we aborted");
    DummyTransactionParticipant[] participants = {
      new DummyNonDurableTransactionParticipant() {
        public void abort(Transaction txn) {
          assertAborted(abortCause);
          super.abort(txn);
        }
      },
      new DummyNonDurableTransactionParticipant() {
        protected boolean prepareResult() {
          return true;
        }

        public void abort(Transaction txn) {
          assertAborted(abortCause);
          super.abort(txn);
        }
      },
      new DummyNonDurableTransactionParticipant() {
        public void abort(Transaction txn) {
          assertAborted(abortCause);
          try {
            txn.abort(abortXcp);
          } catch (RuntimeException e) {
            fail("Unexpected exception: " + e);
          }
          assertAborted(abortCause);
        }
      },
      new DummyNonDurableTransactionParticipant() {
        protected boolean prepareResult() {
          return true;
        }

        public void abort(Transaction txn) {
          assertAborted(abortCause);
          super.abort(txn);
        }
      }
    };
    for (TransactionParticipant participant : participants) {
      txn.join(participant);
    }
    txn.abort(abortCause);
    for (DummyTransactionParticipant participant : participants) {
      if (!participant.prepareReturnedTrue()) {
        assertEquals(
            participant == participants[2] ? State.ACTIVE : State.ABORTED, participant.getState());
      }
    }
    assertAborted(abortCause);
  }
 public void testJoinPreparing() throws Exception {
   final Exception[] abortCause = {null};
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyNonDurableTransactionParticipant() {
       public boolean prepare(Transaction txn) {
         try {
           txn.join(this);
           fail("Expected IllegalStateException");
         } catch (IllegalStateException e) {
           System.err.println(e);
           abortCause[0] = e;
           throw e;
         } catch (RuntimeException e) {
           fail("Unexpected exception: " + e);
         }
         return false;
       }
     },
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     }
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   try {
     handle.commit();
     fail("Expected IllegalStateException");
   } catch (IllegalStateException e) {
     System.err.println(e);
   }
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(State.ABORTED, participant.getState());
     }
   }
   assertAborted(abortCause[0]);
 }
 public void testAbortCommitting() throws Exception {
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyNonDurableTransactionParticipant() {
       public void commit(Transaction txn) {
         assertNotAborted();
         try {
           txn.abort(abortXcp);
           fail("Expected IllegalStateException");
         } catch (IllegalStateException e) {
           System.err.println(e);
           throw e;
         } catch (RuntimeException e) {
           fail("Unexpected exception: " + e);
         }
       }
     },
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     }
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   handle.commit();
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(
           participant == participants[2] ? State.PREPARED : State.COMMITTED,
           participant.getState());
     }
   }
   assertCommitted();
 }
 public void testCommitActive() throws Exception {
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyTransactionParticipant()
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   handle.commit();
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(State.COMMITTED, participant.getState());
     }
   }
   assertCommitted();
 }
 public void testAbortActive() throws Exception {
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyTransactionParticipant()
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   assertNotAborted();
   txn.abort(abortXcp);
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(State.ABORTED, participant.getState());
     }
   }
   assertAborted(abortXcp);
 }
 public void testJoinAborting() {
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyNonDurableTransactionParticipant() {
       public void abort(Transaction txn) {
         try {
           txn.join(this);
           fail("Expected IllegalStateException");
         } catch (IllegalStateException e) {
           System.err.println(e);
           throw e;
         } catch (RuntimeException e) {
           fail("Unexpected exception: " + e);
         }
       }
     },
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     }
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   txn.abort(abortXcp);
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(
           participant == participants[2] ? State.ACTIVE : State.ABORTED, participant.getState());
     }
   }
   assertAborted(abortXcp);
 }
 public void testCommitPrepareFailsMiddle() throws Exception {
   final Exception abortCause = new IOException("Prepare failed");
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyNonDurableTransactionParticipant() {
       public boolean prepare(Transaction txn) throws Exception {
         throw abortCause;
       }
     },
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyTransactionParticipant()
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   try {
     handle.commit();
     fail("Expected IOException");
   } catch (IOException e) {
     System.err.println(e);
   }
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(State.ABORTED, participant.getState());
     }
   }
   assertAborted(abortCause);
 }
 public void testAbortPrepareAndCommitting() throws Exception {
   final Exception abortCause = new IllegalArgumentException();
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       protected boolean prepareResult() {
         return true;
       }
     },
     new DummyTransactionParticipant() {
       public void prepareAndCommit(Transaction txn) {
         assertNotAborted();
         try {
           txn.abort(abortCause);
         } catch (RuntimeException e) {
           fail("Unexpected exception: " + e);
         }
       }
     }
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   try {
     handle.commit();
     fail("Expected TransactionAbortedException");
   } catch (TransactionAbortedException e) {
     System.err.println(e);
     assertEquals(abortCause, e.getCause());
   }
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(State.ABORTED, participant.getState());
     }
   }
   assertAborted(abortCause);
 }
 public void testAbortFails() throws Exception {
   DummyTransactionParticipant[] participants = {
     new DummyNonDurableTransactionParticipant(),
     new DummyNonDurableTransactionParticipant() {
       public void abort(Transaction txn) {
         throw new RuntimeException("Abort failed");
       }
     },
     new DummyTransactionParticipant() {}
   };
   for (TransactionParticipant participant : participants) {
     txn.join(participant);
   }
   Exception abortCause = new IllegalArgumentException();
   txn.abort(abortCause);
   for (DummyTransactionParticipant participant : participants) {
     if (!participant.prepareReturnedTrue()) {
       assertEquals(
           (participant == participants[1] ? State.ACTIVE : State.ABORTED),
           participant.getState());
     }
   }
   assertAborted(abortCause);
 }