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();
}
Aggregations