// Test event queuing and dequeuing during broadcasting public void testEventBroadcasting() { // Register a listener that will conditionally queue a new event UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null); facesContext.setViewRoot(root); root.addFacesListener(new TestListener("t", "2", "4")); TestListener.trace(null); // Queue some events, including the trigger one TestEvent event = new TestEvent(root, "1"); event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); root.queueEvent(event); event = new TestEvent(root, "2"); event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); root.queueEvent(event); event = new TestEvent(root, "3"); event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); root.queueEvent(event); // Simulate the Apply Request Values phase root.processDecodes(facesContext); // Validate the results (expect 4th event to also be queued) String expected = "/t/1/t/2/t/3/t/4"; assertEquals(expected, TestListener.trace()); }
private void checkEventQueueing(PhaseId phaseId) { // NOTE: Current semantics for ANY_PHASE listeners is that // the event should be delivered exactly once, so the existence // of such a listener does not cause the event to remain queued. // Therefore, the expected string is the same as for any // phase-specific listener, and it should get matched after // Apply Request Values processing since that is first phase // for which events are fired // Register an event listener for the specified phase id UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null); facesContext.setViewRoot(root); TestEvent event = null; TestListener listener = new TestListener("t"); root.addFacesListener(listener); // Queue some events to be processed event = new TestEvent(root, "1"); event.setPhaseId(phaseId); root.queueEvent(event); event = new TestEvent(root, "2"); event.setPhaseId(phaseId); root.queueEvent(event); String expected = "/t/1/t/2"; // Fire off the relevant lifecycle methods and check expected results TestListener.trace(null); assertEquals("", TestListener.trace()); root.processDecodes(facesContext); if (PhaseId.APPLY_REQUEST_VALUES.equals(phaseId) || PhaseId.ANY_PHASE.equals(phaseId)) { assertEquals(expected, TestListener.trace()); } else { assertEquals("", TestListener.trace()); } root.processValidators(facesContext); if (PhaseId.PROCESS_VALIDATIONS.equals(phaseId) || PhaseId.APPLY_REQUEST_VALUES.equals(phaseId) || PhaseId.APPLY_REQUEST_VALUES.equals(phaseId) || PhaseId.ANY_PHASE.equals(phaseId)) { assertEquals(expected, TestListener.trace()); } else { assertEquals("", TestListener.trace()); } root.processUpdates(facesContext); if (PhaseId.UPDATE_MODEL_VALUES.equals(phaseId) || PhaseId.PROCESS_VALIDATIONS.equals(phaseId) || PhaseId.APPLY_REQUEST_VALUES.equals(phaseId) || PhaseId.ANY_PHASE.equals(phaseId)) { assertEquals(expected, TestListener.trace()); } else { assertEquals("", TestListener.trace()); } root.processApplication(facesContext); assertEquals(expected, TestListener.trace()); }
// Test AbortProcessingException support public void testAbortProcessingException() { // Register three listeners, with the second one set to abort UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null); facesContext.setViewRoot(root); root.addFacesListener(new TestListener("a", false)); root.addFacesListener(new TestListener("b", true)); root.addFacesListener(new TestListener("c", false)); // Queue two event and check the results TestListener.trace(null); TestEvent event1 = new TestEvent(root, "1"); event1.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); root.queueEvent(event1); TestEvent event2 = new TestEvent(root, "2"); event2.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); root.queueEvent(event2); root.processDecodes(facesContext); assertEquals("/a/1/b/1/a/2/b/2", TestListener.trace()); }
// Test Events List Clearing public void testEventsListClear() { UIViewRoot root = facesContext.getApplication().getViewHandler().createView(facesContext, null); facesContext.setViewRoot(root); TestEvent event1, event2, event3, event4 = null; event1 = new TestEvent(root, "1"); event1.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); root.queueEvent(event1); event2 = new TestEvent(root, "2"); event2.setPhaseId(PhaseId.PROCESS_VALIDATIONS); root.queueEvent(event2); event3 = new TestEvent(root, "3"); event3.setPhaseId(PhaseId.UPDATE_MODEL_VALUES); root.queueEvent(event3); event4 = new TestEvent(root, "4"); event4.setPhaseId(PhaseId.INVOKE_APPLICATION); root.queueEvent(event4); final Field fields[] = UIViewRoot.class.getDeclaredFields(); Field field = null; List<List> events = null; for (int i = 0; i < fields.length; ++i) { if ("events".equals(fields[i].getName())) { field = fields[i]; field.setAccessible(true); try { events = TypedCollections.dynamicallyCastList((List) field.get(root), List.class); } catch (Exception e) { assertTrue(false); } break; } } // CASE: renderReponse not set; responseComplete not set; // check for existence of events before processDecodes checkEventQueuesSizes(events, 1, 1, 1, 1); root.processDecodes(facesContext); // there should be no events checkEventQueuesSizes(events, 0, 1, 1, 1); // requeue apply request event root.queueEvent(event1); // CASE: renderReponse set; // check for existence of events before processValidators checkEventQueuesSizes(events, 1, 1, 1, 1); facesContext.renderResponse(); root.processValidators(facesContext); // there should be no events checkEventQueuesSizes(events, 0, 0, 0, 0); // reset FacesContext facesContext.setRenderResponse(false); facesContext.setResponseComplete(false); // requeue all events root.queueEvent(event1); root.queueEvent(event2); root.queueEvent(event3); root.queueEvent(event4); try { events = TypedCollections.dynamicallyCastList((List) field.get(root), List.class); } catch (Exception e) { assertTrue(false); } // CASE: response set; // check for existence of events before processValidators checkEventQueuesSizes(events, 1, 1, 1, 1); facesContext.renderResponse(); root.processValidators(facesContext); // there should be no events checkEventQueuesSizes(events, 0, 0, 0, 0); // reset FacesContext facesContext.setRenderResponse(false); facesContext.setResponseComplete(false); // requeue all events root.queueEvent(event1); root.queueEvent(event2); root.queueEvent(event3); root.queueEvent(event4); try { events = TypedCollections.dynamicallyCastList((List) field.get(root), List.class); } catch (Exception e) { assertTrue(false); } // CASE: response complete; // check for existence of events before processUpdates checkEventQueuesSizes(events, 1, 1, 1, 1); facesContext.responseComplete(); root.processUpdates(facesContext); // there should be no events checkEventQueuesSizes(events, 0, 0, 0, 0); // reset FacesContext facesContext.setRenderResponse(false); facesContext.setResponseComplete(false); // requeue all events root.queueEvent(event1); root.queueEvent(event2); root.queueEvent(event3); root.queueEvent(event4); try { events = TypedCollections.dynamicallyCastList((List) field.get(root), List.class); } catch (Exception e) { assertTrue(false); } // CASE: response complete; // check for existence of events before processApplication checkEventQueuesSizes(events, 1, 1, 1, 1); facesContext.responseComplete(); root.processApplication(facesContext); // there should be no events checkEventQueuesSizes(events, 0, 0, 0, 0); // finally, get the internal events list one more time // to make sure it is null try { events = TypedCollections.dynamicallyCastList((List) field.get(root), List.class); } catch (Exception e) { assertTrue(false); } assertNull("events", events); }