CloseState { public void makeSureOpen() { if(state.get() != INIT){ throw new CloseStateException("not open:" + state.get()); } } CloseState(); boolean isOpen(); boolean isClosing(); boolean isClosed(); void setClosing(); void setClosed(); void makeSureNotClosed(); void makeSureOpen(); @Override String toString();  }
@Test public void testMakeSureOpen() { CloseState closeState = new CloseState(); closeState.makeSureOpen(); closeState.setClosing(); try { closeState.makeSureOpen(); Assert.fail(); } catch (Exception e) { } closeState.setClosed(); try { closeState.makeSureOpen(); Assert.fail(); } catch (Exception e) { } }