Search in sources :

Example 6 with RunContext

use of org.eclipse.scout.rt.platform.context.RunContext in project scout.rt by eclipse.

the class PlatformTestRunner method classBlock.

@Override
protected Statement classBlock(final RunNotifier notifier) {
    final Statement s4 = super.classBlock(notifier);
    final Statement s3 = new RunContextStatement(s4, new IRunContextProvider() {

        @Override
        public RunContext create() {
            return createJUnitRunContext();
        }
    });
    final Statement s2 = new AssertNoRunningJobsStatement(s3, "Test class");
    final Statement s1 = new PlatformStatement(s2, ReflectionUtility.getAnnotation(RunWithNewPlatform.class, getTestClass().getJavaClass()));
    return s1;
}
Also used : IRunContextProvider(org.eclipse.scout.rt.testing.platform.runner.statement.RunContextStatement.IRunContextProvider) Statement(org.junit.runners.model.Statement) SubjectStatement(org.eclipse.scout.rt.testing.platform.runner.statement.SubjectStatement) BeanAnnotationsCleanupStatement(org.eclipse.scout.rt.testing.platform.runner.statement.BeanAnnotationsCleanupStatement) AssertNoRunningJobsStatement(org.eclipse.scout.rt.testing.platform.runner.statement.AssertNoRunningJobsStatement) TimeoutRunContextStatement(org.eclipse.scout.rt.testing.platform.runner.statement.TimeoutRunContextStatement) BeanAnnotationsInitStatement(org.eclipse.scout.rt.testing.platform.runner.statement.BeanAnnotationsInitStatement) RegisterBeanStatement(org.eclipse.scout.rt.testing.platform.runner.statement.RegisterBeanStatement) ThrowHandledExceptionStatement(org.eclipse.scout.rt.testing.platform.runner.statement.ThrowHandledExceptionStatement) ClearThreadInterruptionStatusStatement(org.eclipse.scout.rt.testing.platform.runner.statement.ClearThreadInterruptionStatusStatement) RunContextStatement(org.eclipse.scout.rt.testing.platform.runner.statement.RunContextStatement) PlatformStatement(org.eclipse.scout.rt.testing.platform.runner.statement.PlatformStatement) TransactionAddFailureOnAnyExceptionStatement(org.eclipse.scout.rt.testing.platform.runner.statement.TransactionAddFailureOnAnyExceptionStatement) TimesStatement(org.eclipse.scout.rt.testing.platform.runner.statement.TimesStatement) TimeoutRunContextStatement(org.eclipse.scout.rt.testing.platform.runner.statement.TimeoutRunContextStatement) RunContextStatement(org.eclipse.scout.rt.testing.platform.runner.statement.RunContextStatement) RunContext(org.eclipse.scout.rt.platform.context.RunContext) AssertNoRunningJobsStatement(org.eclipse.scout.rt.testing.platform.runner.statement.AssertNoRunningJobsStatement) PlatformStatement(org.eclipse.scout.rt.testing.platform.runner.statement.PlatformStatement)

Example 7 with RunContext

use of org.eclipse.scout.rt.platform.context.RunContext 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)

Example 8 with RunContext

use of org.eclipse.scout.rt.platform.context.RunContext in project scout.rt by eclipse.

the class BlockingTestUtility method runBlockingAction.

/**
 * Helper method to test code which will enter a blocking condition.
 * <p>
 * If <code>runnableOnceBlocked</code> throws an exception, it is given to {@link JUnitExceptionHandler} to make the
 * JUnit test fail.
 *
 * @param runnableGettingBlocked
 *          {@code IRunnable} that will enter a blocking condition.
 * @param runnableOnceBlocked
 *          {@code IRunnable} to be executed once the 'runnableGettingBlocked' enters a blocking condition.
 * @param awaitBackgroundJobs
 *          true waits for background jobs running in the same session to complete before runnableOnceBlocked is
 *          called
 */
public static void runBlockingAction(final IRunnable runnableGettingBlocked, final IRunnable runnableOnceBlocked, final boolean awaitBackgroundJobs) {
    final ClientRunContext runContext = ClientRunContexts.copyCurrent();
    final IBlockingCondition onceBlockedDoneCondition = Jobs.newBlockingCondition(true);
    // remember the list of client jobs before blocking
    final Set<IFuture<?>> jobsBefore = new HashSet<>();
    jobsBefore.addAll(BEANS.get(IJobManager.class).getFutures(new IFilter<IFuture<?>>() {

        @Override
        public boolean accept(IFuture<?> cand) {
            final RunContext candContext = cand.getJobInput().getRunContext();
            return candContext instanceof ClientRunContext && ((ClientRunContext) candContext).getSession() == runContext.getSession();
        }
    }));
    final IRegistrationHandle listenerRegistration = IFuture.CURRENT.get().addListener(Jobs.newEventFilterBuilder().andMatchEventType(JobEventType.JOB_STATE_CHANGED).andMatchState(JobState.WAITING_FOR_BLOCKING_CONDITION).andMatchExecutionHint(ModelJobs.EXECUTION_HINT_UI_INTERACTION_REQUIRED).toFilter(), new IJobListener() {

        @Override
        public void changed(final JobEvent event) {
            // waitFor was entered
            final IRunnable callRunnableOnceBlocked = new IRunnable() {

                @Override
                public void run() throws Exception {
                    try {
                        runnableOnceBlocked.run();
                    } finally {
                        event.getData().getBlockingCondition().setBlocking(false);
                        onceBlockedDoneCondition.setBlocking(false);
                    }
                }
            };
            final JobInput jobInputForRunnableOnceBlocked = ModelJobs.newInput(runContext).withExceptionHandling(BEANS.get(JUnitExceptionHandler.class), true).withName("JUnit: Handling blocked thread because waiting for a blocking condition");
            if (awaitBackgroundJobs) {
                // wait until all background jobs finished
                Jobs.schedule(new IRunnable() {

                    @Override
                    public void run() throws Exception {
                        jobsBefore.add(IFuture.CURRENT.get());
                        BEANS.get(IJobManager.class).awaitFinished(new IFilter<IFuture<?>>() {

                            @Override
                            public boolean accept(IFuture<?> f) {
                                RunContext candContext = f.getJobInput().getRunContext();
                                return candContext instanceof ClientRunContext && ((ClientRunContext) candContext).getSession() == runContext.getSession() && !jobsBefore.contains(f);
                            }
                        }, 5, TimeUnit.MINUTES);
                        // call runnableOnceBlocked
                        ModelJobs.schedule(callRunnableOnceBlocked, jobInputForRunnableOnceBlocked);
                    }
                }, Jobs.newInput().withName("wait until background jobs finished"));
            } else {
                // call runnableOnceBlocked directly
                ModelJobs.schedule(callRunnableOnceBlocked, jobInputForRunnableOnceBlocked);
            }
        }
    });
    try {
        // this action will enter a blocking condition which causes the 'runnableOnceBlocked' to be executed.
        runnableGettingBlocked.run();
    } catch (final Exception e) {
        throw BEANS.get(DefaultRuntimeExceptionTranslator.class).translate(e);
    } finally {
        listenerRegistration.dispose();
    }
    // we need to wait until the runnableOnceBlocked is completed.
    // runnableOnceBlocked may, during its execution,  set the original blocking condition to non-blocking but still execute
    // important code afterwards. Therefore, the original blocking condition that starts runnableOnceBlocked is only used
    // to indicate the start of the runnableOnceBlocked, but this method returns only AFTER runnableOnceBlocked completes execution.
    onceBlockedDoneCondition.waitForUninterruptibly(120, TimeUnit.SECONDS);
}
Also used : IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) JUnitExceptionHandler(org.eclipse.scout.rt.testing.platform.runner.JUnitExceptionHandler) IJobManager(org.eclipse.scout.rt.platform.job.IJobManager) IFuture(org.eclipse.scout.rt.platform.job.IFuture) IJobListener(org.eclipse.scout.rt.platform.job.listener.IJobListener) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) JobInput(org.eclipse.scout.rt.platform.job.JobInput) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) JobEvent(org.eclipse.scout.rt.platform.job.listener.JobEvent) IFilter(org.eclipse.scout.rt.platform.filter.IFilter) RunContext(org.eclipse.scout.rt.platform.context.RunContext) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) IRegistrationHandle(org.eclipse.scout.rt.platform.util.IRegistrationHandle) IBlockingCondition(org.eclipse.scout.rt.platform.job.IBlockingCondition) HashSet(java.util.HashSet)

Example 9 with RunContext

use of org.eclipse.scout.rt.platform.context.RunContext in project scout.rt by eclipse.

the class ClientRunContextTest method testRunContextsCopyCurrent.

@Test
public void testRunContextsCopyCurrent() {
    final IClientSession session = mock(IClientSession.class);
    final UserAgent sessionUserAgent = UserAgents.create().build();
    final Locale sessionLocale = Locale.CANADA_FRENCH;
    final Subject sessionSubject = new Subject();
    final IDesktop sessionDesktop = mock(IDesktop.class);
    when(session.getUserAgent()).thenReturn(sessionUserAgent);
    when(session.getLocale()).thenReturn(sessionLocale);
    when(session.getSubject()).thenReturn(sessionSubject);
    when(session.getDesktopElseVirtualDesktop()).thenReturn(sessionDesktop);
    ClientRunContexts.empty().withSession(session, true).run(new IRunnable() {

        @Override
        public void run() throws Exception {
            RunContext runContext = RunContexts.copyCurrent();
            assertThat(runContext, CoreMatchers.instanceOf(ClientRunContext.class));
            ClientRunContext clientCtx = (ClientRunContext) runContext;
            assertSame(session, clientCtx.getSession());
            assertSame(sessionLocale, clientCtx.getLocale());
            assertSame(sessionUserAgent, clientCtx.getUserAgent());
            assertSame(sessionDesktop, clientCtx.getDesktop());
        }
    });
}
Also used : NlsLocale(org.eclipse.scout.rt.platform.nls.NlsLocale) Locale(java.util.Locale) UserAgent(org.eclipse.scout.rt.shared.ui.UserAgent) IClientSession(org.eclipse.scout.rt.client.IClientSession) RunContext(org.eclipse.scout.rt.platform.context.RunContext) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) Subject(javax.security.auth.Subject) IDesktop(org.eclipse.scout.rt.client.ui.desktop.IDesktop) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 10 with RunContext

use of org.eclipse.scout.rt.platform.context.RunContext in project scout.rt by eclipse.

the class ClientRunContextTest method testRunContextsEmpty.

@Test
public void testRunContextsEmpty() {
    RunContext runContext = RunContexts.empty();
    assertThat(runContext, CoreMatchers.instanceOf(ClientRunContext.class));
    ClientRunContext clientCtx = (ClientRunContext) runContext;
    assertNull(clientCtx.getSubject());
    assertNull(clientCtx.getSession());
    assertNull(clientCtx.getUserAgent());
    assertNull(clientCtx.getLocale());
    assertEquals(TransactionScope.REQUIRED, clientCtx.getTransactionScope());
}
Also used : RunContext(org.eclipse.scout.rt.platform.context.RunContext) Test(org.junit.Test)

Aggregations

RunContext (org.eclipse.scout.rt.platform.context.RunContext)14 Test (org.junit.Test)8 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)6 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)5 Subject (javax.security.auth.Subject)3 Locale (java.util.Locale)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)2 UserAgent (org.eclipse.scout.rt.shared.ui.UserAgent)2 HashSet (java.util.HashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 MessageContext (javax.xml.ws.handler.MessageContext)1 IClientSession (org.eclipse.scout.rt.client.IClientSession)1 ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)1 IDesktop (org.eclipse.scout.rt.client.ui.desktop.IDesktop)1 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)1 IFilter (org.eclipse.scout.rt.platform.filter.IFilter)1 IBlockingCondition (org.eclipse.scout.rt.platform.job.IBlockingCondition)1 IFuture (org.eclipse.scout.rt.platform.job.IFuture)1