@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(); }
@SuppressWarnings("unchecked") public Object coerceContext(int index, String desiredTypeName) { if (index >= context.getCount()) throw new IllegalArgumentException(String.format("Method %s has more parameters than there are context values for this component event.", getMethodDescription())); try { Class desiredType = elementResources.toClass(desiredTypeName); return context.get(desiredType, index); } catch (Exception ex) { throw new IllegalArgumentException(String.format("Exception in method %s, parameter #%d: %s", getMethodDescription(), index + 1, ex), ex); } }
ComponentEventImpl extends EventImpl implements ComponentEvent { @SuppressWarnings("unchecked") public Object coerceContext(int index, String desiredTypeName) { if (index >= context.getCount()) throw new IllegalArgumentException(String.format("Method %s has more parameters than there are context values for this component event.", getMethodDescription())); try { Class desiredType = elementResources.toClass(desiredTypeName); return context.get(desiredType, index); } catch (Exception ex) { throw new IllegalArgumentException(String.format("Exception in method %s, parameter #%d: %s", getMethodDescription(), index + 1, ex), ex); } } }
ComponentEventImpl extends EventImpl implements ComponentEvent { @SuppressWarnings("unchecked") public Object coerceContext(int index, String desiredTypeName) { if (index >= context.getCount()) throw new IllegalArgumentException(String.format("Method %s has more parameters than there are context values for this component event.", getMethodDescription())); try { Class desiredType = elementResources.toClass(desiredTypeName); return context.get(desiredType, index); } catch (Exception ex) { throw new IllegalArgumentException(String.format("Exception in method %s, parameter #%d: %s", getMethodDescription(), index + 1, ex), ex); } } ComponentEventImpl(String eventType, String originatingComponentId, EventContext context,
            ComponentEventCallback handler, ComponentPageElementResources elementResources,
            boolean exactParameterCountMatch, ComponentModel model, Logger logger); }
ComponentEventImpl extends EventImpl implements ComponentEvent { @SuppressWarnings("unchecked") public Object coerceContext(int index, String desiredTypeName) { if (index >= context.getCount()) throw new IllegalArgumentException(String.format("Method %s has more parameters than there are context values for this component event.", getMethodDescription())); try { Class desiredType = elementResources.toClass(desiredTypeName); return context.get(desiredType, index); } catch (Exception ex) { throw new IllegalArgumentException(String.format("Exception in method %s, parameter #%d: %s", getMethodDescription(), index + 1, ex), ex); } } ComponentEventImpl(String eventType, String originatingComponentId, EventContext context,
            ComponentEventCallback handler, ComponentPageElementResources elementResources,
            boolean exactParameterCountMatch, ComponentModel model, Logger logger); @Override String toString(); boolean matches(String eventType, String componentId, int parameterCount); @SuppressWarnings("unchecked") Object coerceContext(int index, String desiredTypeName); Object[] getContext(); EventContext getEventContext(); }
ComponentEventImpl extends EventImpl implements ComponentEvent { @SuppressWarnings("unchecked") public Object coerceContext(int index, String desiredTypeName) { if (index >= context.getCount()) throw new IllegalArgumentException(String.format("Method %s has more parameters than there are context values for this component event.", getMethodDescription())); try { Class desiredType = elementResources.toClass(desiredTypeName); return context.get(desiredType, index); } catch (Exception ex) { throw new IllegalArgumentException(String.format("Exception in method %s, parameter #%d: %s", getMethodDescription(), index + 1, ex), ex); } } ComponentEventImpl(String eventType, String originatingComponentId, EventContext context,
            ComponentEventCallback handler, ComponentPageElementResources elementResources,
            boolean exactParameterCountMatch, ComponentModel model, Logger logger); @Override String toString(); boolean matches(String eventType, String componentId, int parameterCount); @SuppressWarnings("unchecked") Object coerceContext(int index, String desiredTypeName); Object[] getContext(); EventContext getEventContext();  }