@Test
  public void testChangeScopeEvents() throws Exception {
    final ThreadDelegatedContext plate = scope.getContext();
    Assert.assertNotNull(plate);
    Assert.assertEquals(0, plate.size());

    final EventRecordingObject fooEventTest = new EventRecordingObject();

    plate.putIfAbsent(fooStringKey, Providers.of(fooEventTest));
    Assert.assertEquals(1, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.ENTER, fooEventTest.getLastEvent());

    scope.changeScope(null);

    Assert.assertEquals(2, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.LEAVE, fooEventTest.getLastEvent());
  }
  @Test
  public void testScopeEnterLeaveEvents() throws Exception {
    final ThreadDelegatedContext plate = new ThreadDelegatedContext();
    final EventRecordingObject fooEventTest = new EventRecordingObject();

    plate.putIfAbsent(fooStringKey, Providers.of(fooEventTest));
    Assert.assertEquals(1, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.ENTER, fooEventTest.getLastEvent());

    scope.changeScope(plate);

    Assert.assertSame(plate, scope.getContext());

    Assert.assertEquals(2, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.ENTER, fooEventTest.getLastEvent());

    scope.changeScope(null);

    Assert.assertNotSame(plate, scope.getContext());
    Assert.assertEquals(3, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.LEAVE, fooEventTest.getLastEvent());

    // Put the plate back in.
    scope.changeScope(plate);

    Assert.assertSame(plate, scope.getContext());

    Assert.assertEquals(4, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.ENTER, fooEventTest.getLastEvent());

    // Replace with a new plate
    scope.changeScope(new ThreadDelegatedContext());

    Assert.assertNotSame(plate, scope.getContext());

    Assert.assertEquals(5, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.LEAVE, fooEventTest.getLastEvent());

    // Put it back in one more time...
    scope.changeScope(plate);

    Assert.assertSame(plate, scope.getContext());

    Assert.assertEquals(6, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.ENTER, fooEventTest.getLastEvent());

    // Pathologic case: Replace with itself.
    scope.changeScope(plate);

    Assert.assertSame(plate, scope.getContext());

    Assert.assertEquals(6, fooEventTest.getEventCount());
    Assert.assertEquals(ScopeEvent.ENTER, fooEventTest.getLastEvent());
  }