ScopeGuard implements IScopeGuard { public void close() throws ScopeGuardException { try { if (previousScope != null) { ScopeProvider.setCurrentScope(previousScope); } else { throw new Exception(); } } catch (Exception e) { throw new ScopeGuardException("ScopeGuard could not restore original state.", e); } } ScopeGuard(final Object key); void close();  }
@Test public void checkGuard() throws Exception { Object key1 = ScopeProvider.createScope(null); Object key2 = ScopeProvider.createScope(null); IScope scope1 = ScopeProvider.getScope(key1); IScope scope2 = ScopeProvider.getScope(key2); assertNotSame(scope1, scope2); ScopeProvider.setCurrentScope(scope1); ScopeGuard guard = new ScopeGuard(key2); assertEquals(scope2, ScopeProvider.getCurrentScope()); guard.close(); assertEquals(scope1, ScopeProvider.getCurrentScope()); }