// 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());
  }
 public static Collection<TestEvent<String>> writeRemove(String... values) {
   List<TestEvent<String>> all = new ArrayList<>();
   for (String s : values) all.addAll(TestEvent.create(WRITE, s));
   for (String s : values) all.addAll(TestEvent.create(REMOVE, null, s));
   for (String s : values) all.addAll(TestEvent.create(WRITE, null));
   return all;
 }
 public static Collection<TestEvent<String>> createRemove(String value) {
   List<TestEvent<String>> all = new ArrayList<>();
   all.addAll(TestEvent.create(CREATE, value));
   all.addAll(TestEvent.create(WRITE, value));
   all.addAll(TestEvent.create(REMOVE, null, value));
   all.addAll(TestEvent.create(WRITE, null));
   return all;
 }
 public static Collection<TestEvent<String>> createModify(
     String createdValue, String modifiedValue) {
   List<TestEvent<String>> all = new ArrayList<>();
   all.addAll(TestEvent.create(CREATE, createdValue));
   all.addAll(TestEvent.create(WRITE, createdValue));
   all.addAll(TestEvent.create(MODIFY, modifiedValue, createdValue));
   all.addAll(TestEvent.create(WRITE, modifiedValue));
   return all;
 }
  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());
  }
 @Override
 public boolean equals(Event event, Metadata data, Parameter param) {
   if (event instanceof TestEvent) {
     TestEvent e = (TestEvent) event;
     if (e != null) {
       return e.getNumber() == i;
     }
   }
   return false;
 }
 public static Collection<TestEvent<String>> createModifyRemove(
     List<String> created, List<String> modified) {
   List<TestEvent<String>> all = new ArrayList<>();
   created.forEach(s -> all.addAll(TestEvent.create(CREATE, s)));
   created.forEach(s -> all.addAll(TestEvent.create(WRITE, s)));
   modified.forEach(s -> all.addAll(TestEvent.create(WRITE, s)));
   modified.forEach(s -> all.addAll(TestEvent.create(REMOVE, null, s)));
   modified.forEach(s -> all.addAll(TestEvent.create(WRITE, null)));
   return all;
 }
 public static Collection<TestEvent<String>> writeModify(
     List<String> written, List<String> modified) {
   List<TestEvent<String>> all = new ArrayList<>();
   for (String value : written) all.addAll(TestEvent.create(WRITE, value));
   IntStream.range(0, modified.size())
       .forEach(
           i -> {
             all.addAll(TestEvent.create(MODIFY, modified.get(i), written.get(i)));
             all.addAll(TestEvent.create(WRITE, modified.get(i)));
           });
   return all;
 }
 public static Collection<TestEvent<String>> createAllRemoveAll(String... values) {
   List<TestEvent<String>> all = new ArrayList<>();
   for (String s : values) {
     all.addAll(TestEvent.create(CREATE, s));
     all.addAll(TestEvent.create(WRITE, s));
   }
   for (String s : values) {
     all.addAll(TestEvent.create(REMOVE, null, s));
     all.addAll(TestEvent.create(WRITE, null));
   }
   return all;
 }
  private FunctionalListenerAssertions(FunctionalListeners<K, V> listeners, Runnable runnable) {
    this.listeners = listeners;
    this.runnable = runnable;
    Listeners.ReadWriteListeners<K, V> rw = this.listeners.readWriteListeners();
    closeables.add(
        rw.onCreate(c -> recorded.add(TestEvent.create(InternalTestType.LAMBDA_CREATE, c.get()))));
    closeables.add(
        rw.onModify(
            (b, a) ->
                recorded.add(TestEvent.create(InternalTestType.LAMBDA_MODIFY, a.get(), b.get()))));
    closeables.add(
        rw.onRemove(
            r -> recorded.add(TestEvent.create(InternalTestType.LAMBDA_REMOVE, null, r.get()))));
    closeables.add(
        rw.add(
            new Listeners.ReadWriteListeners.ReadWriteListener<K, V>() {
              @Override
              public void onCreate(ReadEntryView<K, V> created) {
                recorded.add(TestEvent.create(InternalTestType.LISTENER_CREATE, created.get()));
              }

              @Override
              public void onModify(ReadEntryView<K, V> before, ReadEntryView<K, V> after) {
                recorded.add(
                    TestEvent.create(InternalTestType.LISTENER_MODIFY, after.get(), before.get()));
              }

              @Override
              public void onRemove(ReadEntryView<K, V> removed) {
                recorded.add(
                    TestEvent.create(InternalTestType.LISTENER_REMOVE, null, removed.get()));
              }
            }));
    Listeners.WriteListeners<K, V> wo = this.listeners.writeOnlyListeners();
    closeables.add(
        wo.onWrite(
            w ->
                recorded.add(
                    TestEvent.create(InternalTestType.LAMBDA_WRITE, w.find().orElse(null)))));
    closeables.add(
        wo.add(
            w ->
                recorded.add(
                    TestEvent.create(InternalTestType.LISTENER_WRITE, w.find().orElse(null)))));
  }
  // 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
  public void transform_ShouldReturnAuditTrailEntryWithDataOfEvent() {
    testEvent.setStringField("foo");
    testEvent.setIntField(76);
    testEvent.setObjectField(null);

    AuditTrailEntry ae = transformer.transform(testEvent);
    assertThat(ae.getWorkflowModelElement(), is(NAME));

    List<Attribute> atts = ae.getData().getAttribute();
    assertThat(atts.size(), is(5));

    Matcher<Data.Attribute> stringName = hasProperty("name", is("stringField"));
    Matcher<Attribute> stringValue = hasProperty("value", is("foo"));
    @SuppressWarnings("unchecked")
    Matcher<Attribute> stringM = allOf(stringName, stringValue);

    Matcher<Attribute> intName = hasProperty("name", is("intField"));
    Matcher<Attribute> intValue = hasProperty("value", is("76"));
    @SuppressWarnings("unchecked")
    Matcher<Attribute> intM = allOf(intName, intValue);

    Matcher<Attribute> objName = hasProperty("name", is("objectField"));
    Matcher<Attribute> objValue = hasProperty("value", is("null"));
    @SuppressWarnings("unchecked")
    Matcher<Attribute> objM = allOf(objName, objValue);

    Matcher<Attribute> className = hasProperty("name", is("class"));
    Matcher<Attribute> classValue =
        hasProperty("value", is("class " + testEvent.getClass().getName()));
    @SuppressWarnings("unchecked")
    Matcher<Attribute> classM = allOf(className, classValue);

    @SuppressWarnings("unchecked")
    Matcher<Iterable<Attribute>> hasItems = Matchers.hasItems(stringM, intM, objM, classM);
    assertThat(atts, hasItems);
  }
 public static Collection<TestEvent<String>> createModifyRemove(String created, String modified) {
   List<TestEvent<String>> all = new ArrayList<>();
   all.addAll(TestEvent.create(CREATE, created));
   all.addAll(TestEvent.create(WRITE, created));
   all.addAll(TestEvent.create(MODIFY, modified, created));
   all.addAll(TestEvent.create(WRITE, modified));
   all.addAll(TestEvent.create(REMOVE, null, modified));
   all.addAll(TestEvent.create(WRITE, null));
   return all;
 }
 @Override
 public void onNext(TestEvent e) {
   set.add(e);
   System.out.println(name + " " + e + " " + e.getMsg());
   if (set.size() == expected) monitor.mnotify();
 }
 @Test
 public void getProcessId_ShouldReturnNull() {
   testEvent.setProcessId(null);
   assertThat(transformer.getProcessId(testEvent), nullValue());
 }
 public static Collection<TestEvent<String>> write(String... values) {
   List<TestEvent<String>> all = new ArrayList<>();
   for (String value : values) all.addAll(TestEvent.create(WRITE, value));
   return all;
 }
  // 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);
  }
 public static <V> Collection<TestEvent<V>> create(TestType type, V value, V prev) {
   return Arrays.asList(
       TestEvent.create(type.lambdaType(), value, prev),
       TestEvent.create(type.listenerType(), value, prev));
 }
Beispiel #19
0
 @Subscribe
 public void listen(TestEvent event) {
   lastMessage = event.getMessage();
   System.out.println("监控到message变化成:" + lastMessage);
 }