Search in sources :

Example 1 with AssertionException

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

the class JobCancelTest method testShutdownJobManagerAndSchedule.

@Test
public void testShutdownJobManagerAndSchedule() throws InterruptedException {
    // Use dedicated job manager because job manager is shutdown in tests.
    IBean<IJobManager> jobManagerBean = JobTestUtil.replaceCurrentJobManager(new JobManager() {
    });
    try {
        // synchronized because modified/read by different threads.
        final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
        final BlockingCountDownLatch latch = new BlockingCountDownLatch(2);
        Jobs.getJobManager().schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-1");
                try {
                    latch.countDownAndBlock();
                } catch (InterruptedException e) {
                    protocol.add("interrupted-1");
                } finally {
                    protocol.add("done-1");
                }
            }
        }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
        IFuture<Void> future2 = Jobs.getJobManager().schedule(new IRunnable() {

            @Override
            public void run() throws Exception {
                protocol.add("running-2");
                try {
                    latch.countDownAndBlock();
                } catch (InterruptedException e) {
                    protocol.add("interrupted-2");
                } finally {
                    protocol.add("done-2");
                }
            }
        }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
        assertTrue(latch.await());
        // SHUTDOWN
        Jobs.getJobManager().shutdown();
        try {
            Jobs.schedule(mock(IRunnable.class), Jobs.newInput());
            fail("AssertionError expected");
        } catch (AssertionException e) {
        // NOOP
        }
        // VERIFY
        assertEquals(CollectionUtility.hashSet("running-1", "running-2", "interrupted-1", "interrupted-2", "done-1", "done-2"), protocol);
        future2.awaitDone(1, TimeUnit.SECONDS);
        assertTrue(future2.isCancelled());
    } finally {
        JobTestUtil.unregisterAndShutdownJobManager(jobManagerBean);
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) JobManager(org.eclipse.scout.rt.platform.job.internal.JobManager) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 2 with AssertionException

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

the class JobManagerTest method testShutdown.

@Test
public void testShutdown() throws Exception {
    // synchronized because modified/read by different threads.
    final Set<String> protocol = Collections.synchronizedSet(new HashSet<String>());
    final BlockingCountDownLatch setupLatch = new BlockingCountDownLatch(3);
    final BlockingCountDownLatch verifyLatch = new BlockingCountDownLatch(3);
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("interrupted-1");
            } finally {
                verifyLatch.countDown();
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("interrupted-2");
            } finally {
                verifyLatch.countDown();
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    Jobs.getJobManager().schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            try {
                setupLatch.countDownAndBlock();
            } catch (InterruptedException e) {
                protocol.add("interrupted-3");
            } finally {
                verifyLatch.countDown();
            }
        }
    }, Jobs.newInput().withRunContext(RunContexts.copyCurrent()).withExceptionHandling(null, false));
    assertTrue(setupLatch.await());
    // RUN THE TEST
    Jobs.getJobManager().shutdown();
    // VERIFY
    assertTrue(verifyLatch.await());
    assertEquals(CollectionUtility.hashSet("interrupted-1", "interrupted-2", "interrupted-3"), protocol);
    try {
        Jobs.schedule(mock(IRunnable.class), Jobs.newInput());
        fail("AssertionError expected");
    } catch (AssertionException e) {
    // NOOP
    }
}
Also used : BlockingCountDownLatch(org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 3 with AssertionException

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

the class MutualExclusionTest method testMutexDeadlock.

/**
 * Tests that a model-job cannot wait for a scheduled job.
 */
@Test
public void testMutexDeadlock() {
    // synchronized because modified/read by different threads.
    final List<Integer> protocol = Collections.synchronizedList(new ArrayList<Integer>());
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(1);
            IFuture<Void> future = ModelJobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    protocol.add(3);
                }
            }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
            try {
                future.awaitDoneAndGet(1, TimeUnit.SECONDS, DefaultExceptionTranslator.class);
            } catch (AssertionException e) {
                protocol.add(2);
            } catch (Exception e) {
                protocol.add(4);
            }
        }
    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
    awaitDoneElseFail(JOB_IDENTIFIER);
    assertEquals(CollectionUtility.arrayList(1, 2, 3), protocol);
}
Also used : DefaultExceptionTranslator(org.eclipse.scout.rt.platform.exception.DefaultExceptionTranslator) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) IFuture(org.eclipse.scout.rt.platform.job.IFuture) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 4 with AssertionException

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

the class MutualExclusionTest method testNestedModelJobs.

/**
 * Tests serial execution of nested model jobs.
 */
@Test
public void testNestedModelJobs() {
    // synchronized because modified/read by different threads.
    final List<Integer> protocol = Collections.synchronizedList(new ArrayList<Integer>());
    ModelJobs.schedule(new IRunnable() {

        @Override
        public void run() throws Exception {
            protocol.add(1);
            // SCHEDULE
            ModelJobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    protocol.add(4);
                    // SCHEDULE
                    IFuture<Void> future = ModelJobs.schedule(new IRunnable() {

                        @Override
                        public void run() throws Exception {
                            protocol.add(9);
                        }
                    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
                    try {
                        future.awaitDoneAndGet(1, TimeUnit.SECONDS);
                    } catch (AssertionException e) {
                        protocol.add(5);
                    }
                    protocol.add(6);
                }
            }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
            protocol.add(2);
            // SCHEDULE
            ModelJobs.schedule(new IRunnable() {

                @Override
                public void run() throws Exception {
                    protocol.add(7);
                    ModelJobs.schedule(new IRunnable() {

                        @Override
                        public void run() throws Exception {
                            protocol.add(10);
                        }
                    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
                    protocol.add(8);
                }
            }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
            protocol.add(3);
        }
    }, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExecutionHint(JOB_IDENTIFIER));
    awaitDoneElseFail(JOB_IDENTIFIER);
    assertEquals(CollectionUtility.arrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), protocol);
}
Also used : AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) Test(org.junit.Test)

Example 5 with AssertionException

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

the class SunSecurityProvider method createHash.

@Override
public byte[] createHash(InputStream data, byte[] salt, int iterations) {
    if (data == null) {
        throw new AssertionException("no data provided");
    }
    try {
        MessageDigest digest = MessageDigest.getInstance(getDigestAlgorithm(), getDigestAlgorithmProvider());
        digest.reset();
        if (salt != null && salt.length > 0) {
            digest.update(salt);
        }
        int n;
        byte[] buf = new byte[BUF_SIZE];
        while ((n = data.read(buf)) >= 0) {
            digest.update(buf, 0, n);
        }
        byte[] key = digest.digest();
        for (int i = 1; i < iterations; i++) {
            key = digest.digest(key);
            digest.reset();
        }
        return key;
    } catch (NoSuchAlgorithmException | NoSuchProviderException | IOException e) {
        throw new ProcessingException("Unable to hash.", e);
    }
}
Also used : AssertionException(org.eclipse.scout.rt.platform.util.Assertions.AssertionException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) NoSuchProviderException(java.security.NoSuchProviderException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Aggregations

AssertionException (org.eclipse.scout.rt.platform.util.Assertions.AssertionException)13 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)8 Test (org.junit.Test)7 IOException (java.io.IOException)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NoSuchProviderException (java.security.NoSuchProviderException)5 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)5 InvalidKeyException (java.security.InvalidKeyException)4 IExecutionSemaphore (org.eclipse.scout.rt.platform.job.IExecutionSemaphore)3 KeyFactory (java.security.KeyFactory)2 Signature (java.security.Signature)2 SignatureException (java.security.SignatureException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 SecretKeyFactory (javax.crypto.SecretKeyFactory)2 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)2 OutputStream (java.io.OutputStream)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 MessageDigest (java.security.MessageDigest)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1