public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy)
     throws Throwable {
   WorkContext context = WorkContextTunnel.getThreadWorkContext();
   authenticate(context);
   Message message = new MessageImpl(args, false, context);
   InvocationChain invocationChain = invocationChains.get(method.getName());
   if (invocationChain != null) {
     Interceptor headInterceptor = invocationChain.getHeadInterceptor();
     Message ret = headInterceptor.invoke(message);
     if (ret.isFault()) {
       throw (Throwable) ret.getBody();
     } else {
       return ret.getBody();
     }
   } else {
     return null;
   }
 }
  public void testAlreadyBegunNoCommit() throws Exception {
    Transaction trx = EasyMock.createMock(Transaction.class);
    EasyMock.expect(tm.getTransaction()).andReturn(trx);
    EasyMock.expect(next.invoke(EasyMock.isA(Message.class))).andReturn(message);
    EasyMock.replay(trx, tm, next);

    TxInterceptor interceptor = new TxInterceptor(tm, TxAction.BEGIN, monitor);
    interceptor.setNext(next);

    interceptor.invoke(message);

    EasyMock.verify(trx, tm, next);
  }
  public void testPropagate() throws Exception {
    Transaction trx = EasyMock.createMock(Transaction.class);
    EasyMock.expect(tm.getTransaction()).andReturn(trx);

    TxInterceptor interceptor = new TxInterceptor(tm, TxAction.PROPAGATE, monitor);
    interceptor.setNext(next);
    EasyMock.expect(next.invoke(EasyMock.isA(Message.class))).andReturn(message);
    EasyMock.replay(tm, next);

    interceptor.invoke(message);

    EasyMock.verify(tm, next);
  }
  public void testNoRollbackOnFaultWhenNotBegun() throws Exception {
    Message fault = new MessageImpl("", true, null);
    Transaction trx = EasyMock.createMock(Transaction.class);
    EasyMock.expect(tm.getTransaction()).andReturn(trx);
    EasyMock.expect(next.invoke(EasyMock.isA(Message.class))).andReturn(fault);
    EasyMock.replay(tm, next);

    TxInterceptor interceptor = new TxInterceptor(tm, TxAction.BEGIN, monitor);
    interceptor.setNext(next);

    interceptor.invoke(message);

    EasyMock.verify(tm, next);
  }
  public void testBeginCommit() throws Exception {
    EasyMock.expect(tm.getTransaction()).andReturn(null);
    EasyMock.expect(tm.getStatus()).andReturn(Status.STATUS_ACTIVE);
    EasyMock.expect(next.invoke(EasyMock.isA(Message.class))).andReturn(message);
    tm.begin();
    tm.commit();
    EasyMock.replay(tm, next);

    TxInterceptor interceptor = new TxInterceptor(tm, TxAction.BEGIN, monitor);
    interceptor.setNext(next);

    interceptor.invoke(message);

    EasyMock.verify(tm, next);
  }
  public void testRollbackOnFault() throws Exception {
    Message fault = new MessageImpl("", true, null);
    EasyMock.expect(tm.getTransaction()).andReturn(null);
    EasyMock.expect(next.invoke(EasyMock.isA(Message.class))).andReturn(fault);
    tm.begin();
    tm.rollback();
    EasyMock.replay(tm, next);

    TxInterceptor interceptor = new TxInterceptor(tm, TxAction.BEGIN, monitor);
    interceptor.setNext(next);

    interceptor.invoke(message);

    EasyMock.verify(tm, next);
  }
  public void testNoRollbackOnThrownExceptionWhenNotBegun() throws Exception {
    Transaction trx = EasyMock.createMock(Transaction.class);
    EasyMock.expect(tm.getTransaction()).andReturn(trx);
    EasyMock.expect(next.invoke(EasyMock.isA(Message.class))).andThrow(new MockException());
    EasyMock.replay(trx, tm, next);

    try {
      TxInterceptor interceptor = new TxInterceptor(tm, TxAction.BEGIN, monitor);
      interceptor.setNext(next);

      interceptor.invoke(message);
      fail();
    } catch (MockException e) {
      // expected
    }

    EasyMock.verify(trx, tm, next);
  }
  public void testRollbackOnThrownException() throws Exception {
    EasyMock.expect(tm.getTransaction()).andReturn(null);
    EasyMock.expect(next.invoke(EasyMock.isA(Message.class))).andThrow(new MockException());
    tm.begin();
    tm.rollback();
    EasyMock.replay(tm, next);

    try {
      TxInterceptor interceptor = new TxInterceptor(tm, TxAction.BEGIN, monitor);
      interceptor.setNext(next);

      interceptor.invoke(message);
      fail();
    } catch (MockException e) {
      // expected
    }

    EasyMock.verify(tm, next);
  }