Search in sources :

Example 1 with CallableChain

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

the class RunContext method createCallableChain.

/**
 * Creates the {@link CallableChain} to initialize this context, and provides basic functionality for this
 * {@link RunContext}.
 * <p>
 * This method is not intended to be overwritten. To contribute behavior, overwrite
 * {@link #interceptCallableChain(CallableChain)}. Contributions are added before setting the transaction boundary.
 */
protected <RESULT> CallableChain<RESULT> createCallableChain() {
    final CallableChain<RESULT> contributions = new CallableChain<RESULT>();
    interceptCallableChain(contributions);
    @SuppressWarnings("unchecked") final TransactionProcessor<RESULT> transactionProcessor = BEANS.get(TransactionProcessor.class).withCallerTransaction(m_transaction).withTransactionScope(m_transactionScope).withTransactionMembers(m_transactionMembers);
    return new CallableChain<RESULT>().add(new ThreadLocalProcessor<>(RunContext.CURRENT, this)).add(new ThreadLocalProcessor<>(CorrelationId.CURRENT, m_correlationId)).add(new ThreadLocalProcessor<>(RunMonitor.CURRENT, Assertions.assertNotNull(m_runMonitor))).add(new SubjectProcessor<RESULT>(m_subject)).add(new DiagnosticContextValueProcessor(BEANS.get(PrinicpalContextValueProvider.class))).add(new DiagnosticContextValueProcessor(BEANS.get(CorrelationIdContextValueProvider.class))).add(new ThreadLocalProcessor<>(NlsLocale.CURRENT, m_locale)).add(new ThreadLocalProcessor<>(PropertyMap.CURRENT, m_propertyMap)).addAll(m_threadLocalProcessors.values()).addAll(contributions.asList()).addAll(m_diagnosticProcessors.values()).add(transactionProcessor).addAll(m_interceptors);
}
Also used : DiagnosticContextValueProcessor(org.eclipse.scout.rt.platform.logger.DiagnosticContextValueProcessor) TransactionProcessor(org.eclipse.scout.rt.platform.transaction.TransactionProcessor) ThreadLocalProcessor(org.eclipse.scout.rt.platform.util.ThreadLocalProcessor) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) SubjectProcessor(org.eclipse.scout.rt.platform.security.SubjectProcessor)

Example 2 with CallableChain

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

the class ClientRunContextChainTest method testCallableChain.

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

        @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 UserAgent.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(UserAgent.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 13. ThreadLocalProcessor for IDesktop.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(IDesktop.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 14. ThreadLocalProcessor for IOutline.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(IOutline.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 15. ThreadLocalProcessor for IForm.CURRENT
    c = chainIterator.next();
    assertEquals(ThreadLocalProcessor.class, c.getClass());
    assertSame(IForm.CURRENT, ((ThreadLocalProcessor) c).getThreadLocal());
    // 16. 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 3 with CallableChain

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

the class ExceptionProcessorTest method testSuccess.

@Test
public void testSuccess() throws Exception {
    JobInput jobInput = Jobs.newInput();
    CallableChain<String> chain = new CallableChain<>();
    chain.add(new ExceptionProcessor<String>(jobInput));
    String result = chain.call(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "result";
        }
    });
    assertEquals("result", result);
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Example 4 with CallableChain

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

the class ExceptionProcessorTest method testWithNullExceptionHandlerAndPropagate.

@Test
public void testWithNullExceptionHandlerAndPropagate() throws Exception {
    final RuntimeException exception = new RuntimeException("expected JUnit test exception");
    JobInput jobInput = Jobs.newInput().withExceptionHandling(null, false);
    CallableChain<String> chain = new CallableChain<>();
    chain.add(new ExceptionProcessor<String>(jobInput));
    try {
        chain.call(new Callable<String>() {

            @Override
            public String call() throws Exception {
                throw exception;
            }
        });
        fail();
    } catch (RuntimeException e) {
        assertSame(exception, e);
        verify(m_exceptionHandler, never()).handle(eq(exception));
    }
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Example 5 with CallableChain

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

the class ExceptionProcessorTest method testDefaultSettingsWithException.

@Test
public void testDefaultSettingsWithException() throws Exception {
    final RuntimeException exception = new RuntimeException("expected JUnit test exception");
    JobInput jobInput = Jobs.newInput();
    CallableChain<String> chain = new CallableChain<>();
    chain.add(new CallableChainExceptionHandler<String>());
    chain.add(new ExceptionProcessor<String>(jobInput));
    try {
        chain.call(new Callable<String>() {

            @Override
            public String call() throws Exception {
                throw exception;
            }
        });
        fail();
    } catch (RuntimeException e) {
        assertSame(exception, e);
        verify(m_exceptionHandler, times(1)).handle(eq(exception));
        verifyNoMoreInteractions(m_exceptionHandler);
    }
}
Also used : JobInput(org.eclipse.scout.rt.platform.job.JobInput) 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