Search in sources :

Example 1 with IServerSession

use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.

the class InvocationContextTest method testWithException.

@Test
public void testWithException() {
    // Unregister JUnit exception handler
    BEANS.getBeanManager().unregisterBean(BEANS.getBeanManager().getBean(JUnitExceptionHandler.class));
    final Holder<ITransaction> currentTransaction = new Holder<>();
    final Holder<ITransaction> invocationTransaction = new Holder<>();
    final Holder<IServerSession> invocationServerSession = new Holder<>();
    final Holder<Exception> callableException = new Holder<>();
    // simulate that 'webMethod' throws an exception.
    final RuntimeException exception = new RuntimeException();
    doThrow(exception).when(m_port).webMethod();
    try {
        ServerRunContexts.copyCurrent().withCorrelationId(TESTING_CORRELATION_ID).withTransactionScope(// set transaction boundary
        TransactionScope.REQUIRES_NEW).run(new IRunnable() {

            @Override
            public void run() throws Exception {
                currentTransaction.setValue(ITransaction.CURRENT.get());
                InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
                invocationContext.withEndpointUrl("http://localhost");
                invocationContext.whenCommit(m_commitListener);
                invocationContext.whenRollback(m_rollbackListener);
                invocationContext.whenInvoke(new InvocationHandler() {

                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        invocationTransaction.setValue(ITransaction.CURRENT.get());
                        invocationServerSession.setValue(ServerSessionProvider.currentSession());
                        return method.invoke(proxy, args);
                    }
                });
                // run the test
                try {
                    invocationContext.getPort().webMethod();
                } catch (Exception e) {
                    callableException.setValue(e);
                    throw e;
                }
            }
        });
        fail("RuntimeException expected");
    } catch (RuntimeException e) {
    // NOOP
    }
    assertSame(currentTransaction.getValue(), invocationTransaction.getValue());
    assertSame(ISession.CURRENT.get(), invocationServerSession.getValue());
    assertEquals(TESTING_CORRELATION_ID, m_port.getRequestContext().get(MessageContexts.PROP_CORRELATION_ID));
    verify(m_port).webMethod();
    verify(m_commitListener, never()).onCommitPhase1();
    verify(m_commitListener, never()).onCommitPhase2();
    verify(m_rollbackListener).onRollback();
    assertSame(callableException.getValue(), exception);
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) BooleanHolder(org.eclipse.scout.rt.platform.holders.BooleanHolder) Holder(org.eclipse.scout.rt.platform.holders.Holder) JUnitExceptionHandler(org.eclipse.scout.rt.testing.platform.runner.JUnitExceptionHandler) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) InvocationHandler(java.lang.reflect.InvocationHandler) IServerSession(org.eclipse.scout.rt.server.IServerSession) Test(org.junit.Test)

Example 2 with IServerSession

use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.

the class InvocationContextTest method testWithSuccess.

@Test
public void testWithSuccess() {
    final Holder<ITransaction> currentTransaction = new Holder<>();
    final Holder<ITransaction> invocationTransaction = new Holder<>();
    final Holder<IServerSession> invocationServerSession = new Holder<>();
    ServerRunContexts.copyCurrent().withCorrelationId(TESTING_CORRELATION_ID).withTransactionScope(// set transaction boundary
    TransactionScope.REQUIRES_NEW).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            currentTransaction.setValue(ITransaction.CURRENT.get());
            InvocationContext<TestPort> invocationContext = new InvocationContext<>(m_port, "name");
            invocationContext.withEndpointUrl("http://localhost");
            invocationContext.whenCommit(m_commitListener);
            invocationContext.whenRollback(m_rollbackListener);
            invocationContext.whenInvoke(new InvocationHandler() {

                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    invocationTransaction.setValue(ITransaction.CURRENT.get());
                    invocationServerSession.setValue(ServerSessionProvider.currentSession());
                    return method.invoke(proxy, args);
                }
            });
            // run the test
            invocationContext.getPort().webMethod();
        }
    });
    assertSame(currentTransaction.getValue(), invocationTransaction.getValue());
    assertSame(ISession.CURRENT.get(), invocationServerSession.getValue());
    assertEquals(TESTING_CORRELATION_ID, m_port.getRequestContext().get(MessageContexts.PROP_CORRELATION_ID));
    verify(m_port).webMethod();
    verify(m_commitListener).onCommitPhase1();
    verify(m_commitListener).onCommitPhase2();
    verify(m_rollbackListener, never()).onRollback();
}
Also used : ITransaction(org.eclipse.scout.rt.platform.transaction.ITransaction) BooleanHolder(org.eclipse.scout.rt.platform.holders.BooleanHolder) Holder(org.eclipse.scout.rt.platform.holders.Holder) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Method(java.lang.reflect.Method) WebMethod(javax.jws.WebMethod) InvocationHandler(java.lang.reflect.InvocationHandler) IServerSession(org.eclipse.scout.rt.server.IServerSession) Test(org.junit.Test)

Example 3 with IServerSession

use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.

the class ServerSessionLifecycleHandler method create.

@Override
public IServerSession create() {
    LOG.debug("Creating scout server session id={}", m_sessionId);
    IServerSession session = BEANS.get(ServerSessionProvider.class).provide(m_sessionId, m_serverRunContext.copy());
    if (m_clientNodeId != null) {
        BEANS.get(IClientNotificationService.class).registerSession(m_clientNodeId, m_sessionId, session.getUserId());
    }
    return session;
}
Also used : IClientNotificationService(org.eclipse.scout.rt.shared.clientnotification.IClientNotificationService) IServerSession(org.eclipse.scout.rt.server.IServerSession)

Example 4 with IServerSession

use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.

the class ServerSessionCacheTest method testParallelRequestsWithdifferentIds.

@Test
public void testParallelRequestsWithdifferentIds() {
    final TestHttpSession httpSession1 = new TestHttpSession();
    final TestHttpSession httpSession2 = new TestHttpSession();
    final ServerSessionCache cache = BEANS.get(ServerSessionCache.class);
    final IBlockingCondition bc = Jobs.newBlockingCondition(true);
    final IServerSessionLifecycleHandler handler1 = new TestServerSessionLifecycleHandler("id1") {

        @Override
        public IServerSession create() {
            bc.waitFor(1, TimeUnit.SECONDS);
            return super.create();
        }
    };
    final IServerSessionLifecycleHandler handler2 = new TestServerSessionLifecycleHandler("id2") {

        @Override
        public IServerSession create() {
            bc.setBlocking(false);
            return super.create();
        }
    };
    IFuture<IServerSession> f1 = Jobs.schedule(new Callable<IServerSession>() {

        @Override
        public IServerSession call() throws Exception {
            return cache.getOrCreate(handler1, httpSession1);
        }
    }, Jobs.newInput());
    IFuture<IServerSession> f2 = Jobs.schedule(new Callable<IServerSession>() {

        @Override
        public IServerSession call() throws Exception {
            return cache.getOrCreate(handler2, httpSession2);
        }
    }, Jobs.newInput());
    IServerSession session2 = f2.awaitDoneAndGet();
    IServerSession session1 = f1.awaitDoneAndGet();
    assertNotNull(session2);
    assertNotNull(session1);
    assertNotSame(session1, session2);
}
Also used : IServerSession(org.eclipse.scout.rt.server.IServerSession) TestHttpSession(org.eclipse.scout.rt.testing.server.TestHttpSession) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) Test(org.junit.Test)

Example 5 with IServerSession

use of org.eclipse.scout.rt.server.IServerSession in project scout.rt by eclipse.

the class ServerRunContextTest method testRunContextsCopyCurrent.

@Test
public void testRunContextsCopyCurrent() {
    final IServerSession session = mock(IServerSession.class);
    final UserAgent userAgent = UserAgents.create().build();
    final Locale locale = Locale.CANADA_FRENCH;
    final Subject subject = new Subject();
    ServerRunContexts.empty().withSession(session).withUserAgent(userAgent).withLocale(locale).withSubject(subject).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            RunContext runContext = RunContexts.copyCurrent();
            assertThat(runContext, CoreMatchers.instanceOf(ServerRunContext.class));
            ServerRunContext serverCtx = (ServerRunContext) runContext;
            assertSame(session, serverCtx.getSession());
            assertSame(userAgent, serverCtx.getUserAgent());
            assertSame(locale, serverCtx.getLocale());
            assertSame(subject, serverCtx.getSubject());
        }
    });
}
Also used : Locale(java.util.Locale) UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IServerSession(org.eclipse.scout.rt.server.IServerSession) RunContext(org.eclipse.scout.rt.platform.context.RunContext) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Subject(javax.security.auth.Subject) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Aggregations

IServerSession (org.eclipse.scout.rt.server.IServerSession)18 Test (org.junit.Test)12 TestHttpSession (org.eclipse.scout.rt.testing.server.TestHttpSession)6 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 Subject (javax.security.auth.Subject)3 HttpSession (javax.servlet.http.HttpSession)3 ISession (org.eclipse.scout.rt.shared.ISession)3 InvocationHandler (java.lang.reflect.InvocationHandler)2 Method (java.lang.reflect.Method)2 WebMethod (javax.jws.WebMethod)2 BooleanHolder (org.eclipse.scout.rt.platform.holders.BooleanHolder)2 Holder (org.eclipse.scout.rt.platform.holders.Holder)2 ITransaction (org.eclipse.scout.rt.platform.transaction.ITransaction)2 IClientNotificationService (org.eclipse.scout.rt.shared.clientnotification.IClientNotificationService)2 UserAgent (org.eclipse.scout.rt.shared.ui.UserAgent)2 Principal (java.security.Principal)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Locale (java.util.Locale)1 ClientNotificationDispatcher (org.eclipse.scout.rt.client.clientnotification.ClientNotificationDispatcher)1