DefaultExecution implements Execution { @Override public void setContext(ExecutionContext context) { Stack<ExecutionContext> stack = this.context.get(); if (stack == null) { stack = new Stack<>(); this.context.set(stack); stack.push(context); } else if (stack.isEmpty()) { stack.push(context); } else { if (context != null) { context.inheritFrom(stack.peek()); } stack.set(stack.size() - 1, context); } }  @Override void pushContext(ExecutionContext context); @Override void pushContext(ExecutionContext context, boolean inherit); @Override void popContext(); @Override ExecutionContext getContext(); @Override void setContext(ExecutionContext context); @Override void removeContext();  }
@Test void setContext() { Execution execution = new DefaultExecution(); assertNull(execution.getContext()); ExecutionContext context1 = new ExecutionContext(); execution.setContext(context1); assertSame(context1, execution.getContext()); ExecutionContext context2 = new ExecutionContext(); execution.pushContext(context2); assertSame(context2, execution.getContext()); ExecutionContext context3 = new ExecutionContext(); execution.setContext(context3); assertSame(context3, execution.getContext()); execution.popContext(); assertSame(context1, execution.getContext()); execution.popContext(); assertNull(execution.getContext()); }