ScopeObserver implements ScopePublisher { public void exitScope(final Scope scope) { if (scope == null) { LOGGER.warn("ignoring exited null scope"); return; } LOGGER.info("exiting scope '" + scope.getName() + "'..."); final int position = scopes.search(scope); if (position < 0) { LOGGER.warn("mismatched scope order. cannot exit '" + scope.getName() + "'"); LOGGER.warn("current scope stack: " + scopes); return; } for (int i = 0; i < position; i++) { scopes.pop(); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("current scope stack: " + scopes); } final Scope previous = currentScope(); synchronized (scopeSubscriber) { for (ScopeSubscriber listener : scopeSubscriber) { listener.exitScope(scope, previous); } } } ScopeObserver(); void enterScope(final Scope scope); void exitScope(final Scope scope); Scope currentScope(); void addScopeSubscriber(final ScopeSubscriber subscriber); void removeScopeSubscriber(final ScopeSubscriber subscriber);  }
@Test public void testExitScope() { Assert.assertNull(observer.currentScope()); observer.enterScope(Scope.SESSION); observer.enterScope(Scope.APPLICATION); Assert.assertEquals(Scope.APPLICATION, observer.currentScope()); observer.exitScope(Scope.APPLICATION); Assert.assertEquals(Scope.SESSION, observer.currentScope()); observer.exitScope(Scope.SESSION); Assert.assertNull(observer.currentScope()); observer.enterScope(Scope.SESSION); observer.enterScope(Scope.APPLICATION); Assert.assertEquals(Scope.APPLICATION, observer.currentScope()); observer.exitScope(Scope.DIALOG); Assert.assertEquals(Scope.APPLICATION, observer.currentScope()); observer.exitScope(null); Assert.assertEquals(Scope.APPLICATION, observer.currentScope()); }