Example usage for java.lang IllegalStateException printStackTrace

List of usage examples for java.lang IllegalStateException printStackTrace

Introduction

In this page you can find the example usage for java.lang IllegalStateException printStackTrace.

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:eu.eubrazilcc.lvl.core.ConcurrencyTest.java

@Test
public void test() {
    System.out.println("ConcurrencyTest.test()");
    try {//from ww  w .  j  a va2 s  . com
        final String success = "OK";

        // test uninitialized task runner         
        try {
            TASK_RUNNER.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    return success;
                }
            });
            fail("Should have thrown an IllegalStateException because task runner is uninitialized");
        } catch (IllegalStateException e) {
            assertThat(e.getMessage(), containsString("Task runner uninitialized"));
        }

        // test run task
        TASK_RUNNER.preload();
        final ListenableFuture<String> future = TASK_RUNNER.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return success;
            }
        });
        assertThat("task future is not null", future, notNullValue());
        final String result = future.get(20, SECONDS);
        assertThat("task result is not null", result, notNullValue());
        assertThat("task result is not empty", isNotBlank(result));
        assertThat("task result coincides with expected", result, equalTo(success));

        // test uninitialized task scheduler
        try {
            TASK_SCHEDULER.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    System.out.println(" >> Scheluded job runs");
                }
            }, 0, 20, SECONDS);
            fail("Should have thrown an IllegalStateException because task runner is uninitialized");
        } catch (IllegalStateException e) {
            assertThat(e.getMessage(), containsString("Task scheduler uninitialized"));
        }

        // test schedule task
        TASK_SCHEDULER.preload();
        final ListenableScheduledFuture<?> future2 = TASK_SCHEDULER.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(" >> Scheluded job runs");
            }
        }, 0, 20, SECONDS);
        assertThat("scheduled task future is not null", future2, notNullValue());
        future2.addListener(new Runnable() {
            @Override
            public void run() {
                isCompleted = true;
            }
        }, directExecutor());
        Thread.sleep(2000);
        assertThat("scheduled task result coincides with expected", isCompleted, equalTo(isCompleted));
    } catch (Exception e) {
        e.printStackTrace(System.err);
        fail("ConcurrencyTest.test() failed: " + e.getMessage());
    } finally {
        System.out.println("ConcurrencyTest.test() has finished");
    }
}