Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion faces
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ public UIViewRoot restoreView(FacesContext context, String viewId) {
String clientId = viewRoot.getClientId(context);
Object stateObj = state.get(clientId);
if (stateObj != null) {
context.getAttributes().put(ViewScopeManager.RESTORE_VIEW_SCOPE_ONLY, true);
viewRoot.restoreState(context, stateObj);
context.getAttributes().remove(ViewScopeManager.RESTORE_VIEW_SCOPE_ONLY);
viewRoot.restoreViewScopeState(context, stateObj);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ public class ViewScopeManager implements HttpSessionListener, ViewMapListener {
* Stores the view map id.
*/
public static final String VIEW_MAP_ID = "org.glassfish.mojarra.application.view.viewMapId";
/**
* Stores the constant indicating that only the view scope must be restored.
*/
public static final String RESTORE_VIEW_SCOPE_ONLY = "org.glassfish.mojarra.application.view.restoreViewScopeOnly";
/**
* Stores the constant to keep track of the ViewScopeManager.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.when;

import java.lang.reflect.Method;
Expand All @@ -38,6 +39,9 @@

public class UIViewRootTest {

private static final String SAVED_VIEW_MAP_ID = "savedViewMapId";
private static final String REPLACEMENT_VIEW_MAP_ID = "replacementViewMapId";

@Test
public void testViewMapPostConstructViewMapEvent() {
FacesContext facesContext = Mockito.mock(FacesContext.class);
Expand Down Expand Up @@ -131,6 +135,89 @@ public void testViewMapSaveAndRestoreState() {
setFacesContext(null);
}

@Test
public void testViewMapRestoreViewScopeState() {
FacesContext facesContext = Mockito.mock(FacesContext.class);
Application application = Mockito.mock(Application.class);
ExternalContext externalContext = Mockito.mock(ExternalContext.class);
HashMap<Object, Object> attributes = new HashMap<>();
HashMap<String, Object> sessionMap = new HashMap<>();

setFacesContext(facesContext);

when(facesContext.getAttributes()).thenReturn(attributes);
when(facesContext.getApplication()).thenReturn(application);
when(application.getProjectStage()).thenReturn(ProjectStage.UnitTest);
when(facesContext.getExternalContext()).thenReturn(externalContext);
when(externalContext.getApplicationMap()).thenReturn(null);
when(externalContext.getSessionMap()).thenReturn(sessionMap);

UIViewRoot viewRoot1 = new UIViewRoot();
viewRoot1.setRenderKitId("HTML_BASIC_TEST");
viewRoot1.getTransientStateHelper().putTransient(ViewScopeManager.VIEW_MAP_ID, SAVED_VIEW_MAP_ID);
viewRoot1.getViewMap().put("one", "one");
Object saved = viewRoot1.saveState(facesContext);

Map<String, Object> viewMaps = new HashMap<>();
viewMaps.put(SAVED_VIEW_MAP_ID, viewRoot1.getViewMap());
sessionMap.put(ViewScopeManager.ACTIVE_VIEW_MAPS, viewMaps);

UIViewRoot viewRoot2 = new UIViewRoot();
viewRoot2.restoreViewScopeState(facesContext, saved);

assertEquals("one", viewRoot2.getViewMap().get("one"));
assertNull(viewRoot2.getRenderKitId());

setFacesContext(null);
}

@Test
public void testViewMapRestoreStateAfterRestoreViewScopeState() {
FacesContext facesContext = Mockito.mock(FacesContext.class);
Application application = Mockito.mock(Application.class);
ExternalContext externalContext = Mockito.mock(ExternalContext.class);
HashMap<Object, Object> attributes = new HashMap<>();
HashMap<String, Object> sessionMap = new HashMap<>();

setFacesContext(facesContext);

when(facesContext.getAttributes()).thenReturn(attributes);
when(facesContext.getApplication()).thenReturn(application);
when(application.getProjectStage()).thenReturn(ProjectStage.UnitTest);
when(facesContext.getExternalContext()).thenReturn(externalContext);
when(externalContext.getApplicationMap()).thenReturn(null);
when(externalContext.getSessionMap()).thenReturn(sessionMap);

UIViewRoot viewRoot1 = new UIViewRoot();
viewRoot1.getTransientStateHelper().putTransient(ViewScopeManager.VIEW_MAP_ID, SAVED_VIEW_MAP_ID);
viewRoot1.getViewMap().put("one", "one");
Object saved = viewRoot1.saveState(facesContext);

/*
* The saved view map is no longer among the active view maps, e.g. LRU-evicted or gone with an expired session.
*/
Map<String, Object> viewMaps = new HashMap<>();
sessionMap.put(ViewScopeManager.ACTIVE_VIEW_MAPS, viewMaps);

UIViewRoot viewRoot2 = new UIViewRoot();
viewRoot2.restoreViewScopeState(facesContext, saved);

/*
* Simulate our ViewScopeManager minting a replacement view map while the view is being built.
*/
Map<String, Object> replacementViewMap = viewRoot2.getViewMap();
replacementViewMap.put("two", "two");
viewMaps.put(REPLACEMENT_VIEW_MAP_ID, replacementViewMap);
viewRoot2.getTransientStateHelper().putTransient(ViewScopeManager.VIEW_MAP_ID, REPLACEMENT_VIEW_MAP_ID);

viewRoot2.restoreState(facesContext, saved);

assertEquals(REPLACEMENT_VIEW_MAP_ID, viewRoot2.getTransientStateHelper().getTransient(ViewScopeManager.VIEW_MAP_ID));
assertEquals("two", viewRoot2.getViewMap().get("two"));

setFacesContext(null);
}

private void setFacesContext(FacesContext facesContext) {
try {
Method method = FacesContext.class.getDeclaredMethod("setCurrentInstance", FacesContext.class);
Expand Down