Search in sources :

Example 11 with CallableChain

use of org.eclipse.scout.rt.platform.chain.callable.CallableChain in project scout.rt by eclipse.

the class TransactionProcessorTest method testTransactionMemberSuccess.

@Test
public void testTransactionMemberSuccess() throws Exception {
    final ITransactionMember txMember = mock(ITransactionMember.class);
    when(txMember.getMemberId()).thenReturn("abc");
    when(txMember.needsCommit()).thenReturn(true);
    when(txMember.commitPhase1()).thenReturn(true);
    CallableChain<Object> chain = new CallableChain<>();
    chain.add(new TransactionProcessor<Object>().withCallerTransaction(null).withTransactionScope(TransactionScope.REQUIRES_NEW).withTransactionMembers(Arrays.asList(txMember)));
    chain.call(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertSame(txMember, ITransaction.CURRENT.get().getMember("abc"));
            return null;
        }
    });
    InOrder inOrder = Mockito.inOrder(txMember);
    inOrder.verify(txMember, times(1)).commitPhase1();
    inOrder.verify(txMember, times(1)).commitPhase2();
    inOrder.verify(txMember, never()).rollback();
    inOrder.verify(txMember, times(1)).release();
}
Also used : InOrder(org.mockito.InOrder) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 12 with CallableChain

use of org.eclipse.scout.rt.platform.chain.callable.CallableChain in project scout.rt by eclipse.

the class TransactionProcessorTest method testRequiredWithoutExistingTransactionAndSuccess.

@Test
public void testRequiredWithoutExistingTransactionAndSuccess() throws Exception {
    final Holder<ITransaction> actualTransaction = new Holder<>();
    CallableChain<Object> chain = new CallableChain<>();
    chain.add(new TransactionProcessor<Object>().withCallerTransaction(null).withTransactionScope(TransactionScope.REQUIRED));
    Object result = chain.call(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            actualTransaction.setValue(ITransaction.CURRENT.get());
            return "result";
        }
    });
    // verify
    assertEquals("result", result);
    assertSame(m_transaction, actualTransaction.getValue());
    verify(m_transaction, times(1)).release();
    InOrder inOrder = Mockito.inOrder(m_transaction);
    inOrder.verify(m_transaction, times(1)).commitPhase1();
    inOrder.verify(m_transaction, times(1)).commitPhase2();
    inOrder.verify(m_transaction, never()).rollback();
    inOrder.verify(m_transaction, times(1)).release();
}
Also used : InOrder(org.mockito.InOrder) Holder(org.eclipse.scout.rt.platform.holders.Holder) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 13 with CallableChain

use of org.eclipse.scout.rt.platform.chain.callable.CallableChain in project scout.rt by eclipse.

the class ThreadLocalProcessorTest method test.

@Test
public void test() throws Exception {
    final StringHolder actualValue = new StringHolder();
    THREAD_LOCAL.set("ORIG");
    CallableChain<Void> callableChain = new CallableChain<>();
    callableChain.add(new ThreadLocalProcessor<>(THREAD_LOCAL, "ABC"));
    callableChain.call(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            actualValue.setValue(THREAD_LOCAL.get());
            return null;
        }
    });
    assertEquals("ABC", actualValue.getValue());
    assertEquals("ORIG", THREAD_LOCAL.get());
}
Also used : StringHolder(org.eclipse.scout.rt.platform.holders.StringHolder) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Example 14 with CallableChain

use of org.eclipse.scout.rt.platform.chain.callable.CallableChain in project scout.rt by eclipse.

the class ServerRunContextChainTest method testCallableChain.

/**
 * Tests the correct order of interceptors in {@link ServerRunContext}.
 */
@Test
public void testCallableChain() throws Exception {
    CallableChain<Object> chain = new ServerRunContext() {

        @Override
        protected <RESULT> CallableChain<RESULT> createCallableChain() {
            // overwrite to be accessible in test
            return super.createCallableChain();
        }
    }.createCallableChain();
    Iterator<IChainable> chainIterator = chain.values().iterator();
    // 1. ThreadLocalProcessor for RunContext.CURRENT
    IChainable c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(RunContext.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 2. ThreadLocalProcessor for CorrelationId.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(CorrelationId.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 3. ThreadLocalProcessor for RunMonitor.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(RunMonitor.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 4. SubjectProcessor
    c = (IChainable) chainIterator.next();
    assertEquals(SubjectProcessor.class, c.getClass());
    // 5. DiagnosticContextValueProcessor
    c = chainIterator.next();
    assertEquals(DiagnosticContextValueProcessor.class, c.getClass());
    assertEquals("subject.principal.name", ((DiagnosticContextValueProcessor) c).getMdcKey());
    // 6. DiagnosticContextValueProcessor
    c = chainIterator.next();
    assertEquals(DiagnosticContextValueProcessor.class, c.getClass());
    assertEquals("scout.correlation.id", ((DiagnosticContextValueProcessor) c).getMdcKey());
    // 7. ThreadLocalProcessor for NlsLocale.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(NlsLocale.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 8. ThreadLocalProcessor for PropertyMap.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(PropertyMap.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 9. ThreadLocalProcessor for ISession.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(ISession.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 10. DiagnosticContextValueProcessor
    c = chainIterator.next();
    assertEquals(DiagnosticContextValueProcessor.class, c.getClass());
    assertEquals("scout.user.name", ((DiagnosticContextValueProcessor) c).getMdcKey());
    // 11. DiagnosticContextValueProcessor
    c = chainIterator.next();
    assertEquals(DiagnosticContextValueProcessor.class, c.getClass());
    assertEquals("scout.session.id", ((DiagnosticContextValueProcessor) c).getMdcKey());
    // 12. ThreadLocalProcessor for ISession.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(UserAgent.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 13. ThreadLocalProcessor for ClientNodeId.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(IClientNodeId.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 14. ThreadLocalProcessor for TransactionalClientNotificationCollector.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(ClientNotificationCollector.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 15. TransactionProcessor
    c = chainIterator.next();
    assertEquals(TransactionProcessor.class, c.getClass());
    assertFalse(chainIterator.hasNext());
}
Also used : CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) IChainable(org.eclipse.scout.rt.platform.chain.IChainable) Test(org.junit.Test)

Example 15 with CallableChain

use of org.eclipse.scout.rt.platform.chain.callable.CallableChain in project scout.rt by eclipse.

the class ExceptionProcessorTest method testWithSwallow.

@Test
public void testWithSwallow() throws Exception {
    final RuntimeException exception = new RuntimeException("expected JUnit test exception");
    JobInput jobInput = Jobs.newInput().withExceptionHandling(BEANS.get(ExceptionHandler.class), true);
    CallableChain<String> chain = new CallableChain<>();
    chain.add(new ExceptionProcessor<String>(jobInput));
    chain.call(new Callable<String>() {

        @Override
        public String call() throws Exception {
            throw exception;
        }
    });
    verify(m_exceptionHandler, times(1)).handle(eq(exception));
    verifyNoMoreInteractions(m_exceptionHandler);
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) ExceptionHandler(org.eclipse.scout.rt.platform.exception.ExceptionHandler) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Aggregations

CallableChain (org.eclipse.scout.rt.platform.chain.callable.CallableChain)17 Test (org.junit.Test)16 JobInput (org.eclipse.scout.rt.platform.job.JobInput)7 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)6 InOrder (org.mockito.InOrder)6 IChainable (org.eclipse.scout.rt.platform.chain.IChainable)3 Holder (org.eclipse.scout.rt.platform.holders.Holder)3 ExceptionHandler (org.eclipse.scout.rt.platform.exception.ExceptionHandler)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)1 StringHolder (org.eclipse.scout.rt.platform.holders.StringHolder)1 DiagnosticContextValueProcessor (org.eclipse.scout.rt.platform.logger.DiagnosticContextValueProcessor)1 SubjectProcessor (org.eclipse.scout.rt.platform.security.SubjectProcessor)1 TransactionProcessor (org.eclipse.scout.rt.platform.transaction.TransactionProcessor)1 ThreadLocalProcessor (org.eclipse.scout.rt.platform.util.ThreadLocalProcessor)1