Search in sources :

Example 1 with SwingWorkerWithContext

use of org.knime.core.util.SwingWorkerWithContext in project knime-core by knime.

the class NodeContextTest method testSwingWorkerWithContext.

/**
 * Tests if {@link SwingWorkerWithContext} works as expected.
 *
 * @throws Exception if something goes wrong
 */
@Test
public void testSwingWorkerWithContext() throws Exception {
    List<NodeContainer> containers = new ArrayList<NodeContainer>(wfm.getNodeContainers());
    // Table Creator
    NodeContext.pushContext(containers.get(0));
    final AtomicReference<NodeContext> refInBackground = new AtomicReference<NodeContext>();
    final AtomicReference<NodeContext> refInDone = new AtomicReference<NodeContext>();
    final AtomicReference<NodeContext> refInProcess = new AtomicReference<NodeContext>();
    final ReentrantLock lock = new ReentrantLock();
    final Condition condition = lock.newCondition();
    SwingWorker<NodeContext, NodeContext> worker = new SwingWorkerWithContext<NodeContext, NodeContext>() {

        @Override
        protected NodeContext doInBackgroundWithContext() throws Exception {
            refInBackground.set(NodeContext.getContext());
            publish(NodeContext.getContext());
            return NodeContext.getContext();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected void doneWithContext() {
            lock.lock();
            try {
                refInDone.set(NodeContext.getContext());
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected void processWithContext(final List<NodeContext> chunks) {
            refInProcess.set(NodeContext.getContext());
        }
    };
    lock.lock();
    try {
        worker.execute();
        condition.await();
    } finally {
        lock.unlock();
    }
    assertThat("Wrong node context in doInBackgroundWithContext", refInBackground.get(), is(sameInstance(NodeContext.getContext())));
    assertThat("Wrong node context in doneWithContext", refInDone.get(), is(sameInstance(NodeContext.getContext())));
    assertThat("Wrong node context in processWithContext", refInProcess.get(), is(sameInstance(NodeContext.getContext())));
    NodeContext.removeLastContext();
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) List(java.util.List) SwingWorkerWithContext(org.knime.core.util.SwingWorkerWithContext) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Condition (java.util.concurrent.locks.Condition)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Test (org.junit.Test)1 SwingWorkerWithContext (org.knime.core.util.SwingWorkerWithContext)1