use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.
the class ComponentEventImplTest method event_type_match_is_case_insensitive.
@Test
public void event_type_match_is_case_insensitive() {
ComponentEventCallback handler = mockComponentEventHandler();
EventContext context = mockEventContext();
Logger logger = mockLogger();
ComponentModel model = mockComponentModel();
train_isDebugEnabled(logger, false);
train_getCount(context, 0);
replay();
ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, null, false, model, logger);
assertTrue(event.matches("EVENTTYPE", "someid", 0));
verify();
}
use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.
the class ComponentEventImplTest method coerce_when_not_enough_context.
@Test
public void coerce_when_not_enough_context() {
ComponentEventCallback handler = mockComponentEventHandler();
EventContext context = mockEventContext();
Logger logger = mockLogger();
ComponentModel model = mockComponentModel();
train_isDebugEnabled(logger, true);
logger.debug(eq(TapestryMarkers.EVENT_HANDLER_METHOD), isA(String.class));
train_getCount(context, 0);
replay();
ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, null, false, model, logger);
event.setMethodDescription("foo.Bar.baz()");
try {
event.coerceContext(1, "java.lang.Integer");
} catch (IllegalArgumentException ex) {
assertEquals(ex.getMessage(), "Method foo.Bar.baz() has more parameters than there are context values for this component event.");
}
verify();
}
use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.
the class ComponentEventImplTest method component_id_matches_are_case_insensitive.
@Test
public void component_id_matches_are_case_insensitive() {
ComponentEventCallback handler = mockComponentEventHandler();
EventContext context = mockEventContext();
Logger logger = mockLogger();
ComponentModel model = mockComponentModel();
train_isDebugEnabled(logger, false);
train_getCount(context, 0);
replay();
ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, null, false, model, logger);
assertTrue(event.matches("eventtype", "SOMEID", 0));
verify();
}
use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.
the class ComponentEventImplTest method store_result_when_aborted_is_failure.
@SuppressWarnings("unchecked")
@Test
public void store_result_when_aborted_is_failure() {
Object result = new Object();
ComponentEventCallback handler = mockComponentEventHandler();
Logger logger = mockLogger();
ComponentPageElementResources resources = mockResources();
ComponentModel model = mockComponentModel();
train_isDebugEnabled(logger, true);
EasyMock.expectLastCall().atLeastOnce();
logger.debug(eq(TapestryMarkers.EVENT_HANDLER_METHOD), isA(String.class));
EasyMock.expectLastCall().atLeastOnce();
expect(handler.handleResult(result)).andReturn(true);
replay();
ComponentEvent event = new ComponentEventImpl("eventType", "someId", null, handler, resources, false, model, logger);
event.setMethodDescription("foo.Bar.baz()");
event.storeResult(result);
try {
event.setMethodDescription("foo.Bar.biff()");
event.storeResult(null);
unreachable();
} catch (IllegalStateException ex) {
assertEquals(ex.getMessage(), String.format("Can not store result from invoking method %s, because an event result value has already been obtained from some other event handler method.", "foo.Bar.biff()"));
}
verify();
}
use of org.apache.tapestry5.ComponentEventCallback in project tapestry-5 by apache.
the class Autocomplete method onAutocomplete.
Object onAutocomplete(final EventContext context, @RequestParameter("t:input") final String input) {
final Holder<List> matchesHolder = Holder.create();
// Default it to an empty list.
matchesHolder.put(Collections.emptyList());
ComponentEventCallback callback = new ComponentEventCallback() {
public boolean handleResult(Object result) {
List matches = coercer.coerce(result, List.class);
matchesHolder.put(matches);
return true;
}
};
EventContext newContext = new AbstractEventContext() {
@Override
public int getCount() {
return context.getCount() + 1;
}
@Override
public <T> T get(Class<T> desiredType, int index) {
if (index == 0) {
return coercer.coerce(input, desiredType);
}
return context.get(desiredType, index - 1);
}
};
resources.triggerContextEvent(EventConstants.PROVIDE_COMPLETIONS, newContext, callback);
JSONObject reply = new JSONObject();
reply.put("matches", JSONArray.from(matchesHolder.get()));
// A JSONObject response is always preferred, as that triggers the whole partial page render pipeline.
return reply;
}
Aggregations