Search in sources :

Example 1 with FinalValue

use of org.eclipse.scout.rt.platform.util.FinalValue in project scout.rt by eclipse.

the class PlatformTestRunnerTransactionTest method runServerTestRunner.

protected static Result runServerTestRunner(String testMethod, boolean expectingTestFails, boolean expectingCommit) throws Exception {
    final Class<?> testClass = PlatformTestRunnerTransactionTestFixture.class;
    final FinalValue<PlatformTestRunnerTransactionTestFixture> testInstance = new FinalValue<PlatformTestRunnerTransactionTestFixture>();
    final PlatformTestRunner serverTestRunner = new PlatformTestRunner(PlatformTestRunnerTransactionTestFixture.class) {

        @Override
        protected Object createTest() throws Exception {
            Object test = super.createTest();
            testInstance.set((PlatformTestRunnerTransactionTestFixture) test);
            return test;
        }
    };
    JUnitCore jUnitCore = new JUnitCore();
    Request req = Request.runner(serverTestRunner).filterWith(Filter.matchMethodDescription(Description.createTestDescription(testClass, testMethod)));
    Result result = jUnitCore.run(req);
    assertEquals(0, result.getIgnoreCount());
    assertEquals(1, result.getRunCount());
    int expectedFailureCount = expectingTestFails ? 1 : 0;
    if (result.getFailureCount() != expectedFailureCount) {
        StringBuilder sb = new StringBuilder();
        sb.append("expected ");
        sb.append(expectedFailureCount);
        sb.append(" but caught ");
        sb.append(result.getFailureCount());
        sb.append(":");
        for (Failure f : result.getFailures()) {
            sb.append("\n  ");
            sb.append("Description: '" + f.getDescription() + "'");
            sb.append("\n  ");
            sb.append(f.getException());
            sb.append(Arrays.asList(f.getException().getStackTrace()));
            if (f.getException() != null && f.getException().getCause() != null) {
                sb.append("Cause:");
                sb.append(f.getException().getCause());
                sb.append(Arrays.asList(f.getException().getCause().getStackTrace()));
            }
        }
        Assert.fail(sb.toString());
    }
    final PlatformTestRunnerTransactionTestFixture fixture = testInstance.get();
    assertNotNull(fixture);
    fixture.verifyTransaction(expectingCommit);
    return result;
}
Also used : FinalValue(org.eclipse.scout.rt.platform.util.FinalValue) JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) Failure(org.junit.runner.notification.Failure) Result(org.junit.runner.Result)

Example 2 with FinalValue

use of org.eclipse.scout.rt.platform.util.FinalValue in project scout.rt by eclipse.

the class JmsMomImplementorTest method testRequestReplyCancellationInternal.

private void testRequestReplyCancellationInternal(final IBiDestination<String, String> destination) throws InterruptedException {
    final CountDownLatch neverLatch = new CountDownLatch(1);
    final CountDownLatch setupLatch = new CountDownLatch(1);
    final CountDownLatch verifyLatch = new CountDownLatch(2);
    final AtomicBoolean requestorInterrupted = new AtomicBoolean();
    final AtomicBoolean replierInterrupted = new AtomicBoolean();
    final AtomicBoolean replierCancelled = new AtomicBoolean();
    // Subscribe for the destination
    m_disposables.add(MOM.reply(JmsTestMom.class, destination, new IRequestListener<String, String>() {

        @Override
        public String onRequest(IMessage<String> request) {
            setupLatch.countDown();
            try {
                neverLatch.await();
            } catch (InterruptedException e) {
                replierInterrupted.set(true);
            } finally {
                replierCancelled.set(RunMonitor.CURRENT.get().isCancelled());
                verifyLatch.countDown();
            }
            return request.getTransferObject().toUpperCase();
        }
    }));
    // Initiate 'request-reply' communication
    final FinalValue<String> testee = new FinalValue<>();
    IFuture<Void> requestFuture = Jobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                String result = MOM.request(JmsTestMom.class, destination, "hello world");
                testee.set(result);
            } catch (ThreadInterruptedError e) {
                requestorInterrupted.set(true);
            } finally {
                verifyLatch.countDown();
            }
        }
    }, Jobs.newInput().withName("initiator").withExecutionHint(m_testJobExecutionHint));
    // Wait until reply message processing started
    setupLatch.await();
    // Cancel the publishing thread
    requestFuture.cancel(true);
    // wait for request / reply interrupted
    verifyLatch.await();
    // Verify
    assertTrue(requestorInterrupted.get());
    assertTrue(replierInterrupted.get());
    assertTrue(replierCancelled.get());
    assertFalse(testee.isSet());
}
Also used : FinalValue(org.eclipse.scout.rt.platform.util.FinalValue) IRequestListener(org.eclipse.scout.rt.mom.api.IRequestListener) IMessage(org.eclipse.scout.rt.mom.api.IMessage) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) CountDownLatch(java.util.concurrent.CountDownLatch) BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) VetoException(org.eclipse.scout.rt.platform.exception.VetoException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

FinalValue (org.eclipse.scout.rt.platform.util.FinalValue)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JMSException (javax.jms.JMSException)1 NamingException (javax.naming.NamingException)1 IMessage (org.eclipse.scout.rt.mom.api.IMessage)1 IRequestListener (org.eclipse.scout.rt.mom.api.IRequestListener)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)1 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)1 AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)1 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)1 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)1 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)1 JUnitCore (org.junit.runner.JUnitCore)1 Request (org.junit.runner.Request)1 Result (org.junit.runner.Result)1 Failure (org.junit.runner.notification.Failure)1