### Question:
WrapperHandler { public T getWrapped() { FacesContext facesContext = FacesContext.getCurrentInstance(); if (facesContext == null) { this.wrapped = null; return this.wrappedAccessor.getWrapped(WrappedAccessType.WRAP); } ExternalContext externalContext = facesContext.getExternalContext(); if ((this.wrapped == null) || (SpringFacesIntegration.isInstalled(externalContext) && (!SpringFacesIntegration .getLastRefreshedDate(externalContext).equals(this.lastRefreshedDate)))) { WrappedAccessType accessType = (this.wrapped == null ? WrappedAccessType.WRAP : WrappedAccessType.REWRAP); if (this.logger.isDebugEnabled()) { this.logger.debug((accessType == WrappedAccessType.WRAP ? "Wrapping " : "Rewrapping ") + this.wrappedAccessor.getDescription()); } this.wrapped = wrap(externalContext, this.wrappedAccessor.getWrapped(accessType)); if (SpringFacesIntegration.isInstalled(externalContext)) { this.lastRefreshedDate = SpringFacesIntegration.getLastRefreshedDate(externalContext); } } return this.wrapped; } WrapperHandler(Class<T> typeClass, T wrapped);  WrapperHandler(Class<T> typeClass, WrappedAccessor<T> delegate); void setWarnOnMissingSpringFaces(boolean warnOnMissingSpringFaces); T getWrapped(); static WrapperHandler<T> get(Class<T> typeClass, T delegate);  }### Answer:
@Test public void shouldReturnDelegateWithoutApplicationContext() throws Exception { FacesContextSetter.setCurrentInstance(this.facesContext); Object actual = this.wrapperHandler.getWrapped(); assertThat(actual, is(sameInstance(this.delegate))); }
@Test public void shouldReturnDelegateWithoutFacesContext() throws Exception { cleanupFacesContext(); Object actual = this.wrapperHandler.getWrapped(); assertThat(actual, is(sameInstance(this.delegate))); }
@Test public void shouldWrap() throws Exception { Object wrapped = setupWrapperFactory(); Object actual = this.wrapperHandler.getWrapped(); assertThat(actual, sameInstance(wrapped)); }
@Test public void shouldCacheWrapped() throws Exception { setupWrapperFactory(); this.wrapperHandler.getWrapped(); this.wrapperHandler.getWrapped(); verify(this.factory).newWrapper(Object.class, this.delegate); verifyNoMoreInteractions(this.factory); }
@Test @SuppressWarnings({ "unchecked", "rawtypes" }) public void shouldWrapInOrder() throws Exception { FacesContextSetter.setCurrentInstance(this.facesContext); setupApplicationContext(this.applicationContext); FacesWrapperFactory f1 = mock(FacesWrapperFactory.class, withSettings().extraInterfaces(Ordered.class)); FacesWrapperFactory f2 = mock(FacesWrapperFactory.class, withSettings().extraInterfaces(Ordered.class)); FacesWrapperFactory f3 = mock(FacesWrapperFactory.class, withSettings().extraInterfaces(Ordered.class)); given(((Ordered) f1).getOrder()).willReturn(1); given(((Ordered) f2).getOrder()).willReturn(2); given(((Ordered) f3).getOrder()).willReturn(3); this.facesWrapperFactoryBeans.put("f3", f3); this.facesWrapperFactoryBeans.put("f2", f2); this.facesWrapperFactoryBeans.put("f1", f1); this.wrapperHandler.getWrapped(); InOrder inOrder = Mockito.inOrder(f1, f2, f3); inOrder.verify(f1).newWrapper(any(Class.class), any()); inOrder.verify(f2).newWrapper(any(Class.class), any()); inOrder.verify(f3).newWrapper(any(Class.class), any()); }
@Test public void shouldRewrapIfAppicationContextRefreshed() throws Exception { setupWrapperFactory(); this.wrapperHandler.getWrapped(); Calendar c = Calendar.getInstance(); c.add(Calendar.SECOND, 1); this.applicationMap.put(LAST_REFRESHED_DATE_ATTRIBUTE, c.getTime()); this.wrapperHandler.getWrapped(); verify(this.factory, times(2)).newWrapper(Object.class, this.delegate); }