Search in sources :

Example 11 with ComponentEvent

use of org.apache.tapestry5.runtime.ComponentEvent in project tapestry-5 by apache.

the class ComponentEventImplTest method unable_to_coerce.

@Test
public void unable_to_coerce() {
    ComponentEventCallback handler = mockComponentEventHandler();
    EventContext context = mockEventContext();
    ComponentPageElementResources resources = mockComponentPageElementResources();
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, true);
    logger.debug(eq(TapestryMarkers.EVENT_HANDLER_METHOD), isA(String.class));
    train_toClass(resources, Integer.class.getName(), Integer.class);
    train_getCount(context, 1);
    expect(context.get(Integer.class, 0)).andThrow(new NumberFormatException("Not so easy, is it?"));
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, resources, false, model, logger);
    event.setMethodDescription("foo.Bar.baz()");
    try {
        event.coerceContext(0, "java.lang.Integer");
        unreachable();
    } catch (IllegalArgumentException ex) {
        // Different JVMs will report the conversion error slightly differently,
        // so we don't try to check that part of the error message.
        assertTrue(ex.getMessage().startsWith("Exception in method foo.Bar.baz(), parameter #1:"));
    }
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 12 with ComponentEvent

use of org.apache.tapestry5.runtime.ComponentEvent in project tapestry-5 by apache.

the class ComponentEventImplTest method store_result_and_abort.

@Test
public void store_result_and_abort() {
    Object result = new Object();
    String methodDescription = "foo.Bar.baz()";
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, true);
    EasyMock.expectLastCall().atLeastOnce();
    ComponentPageElementResources resources = mockResources();
    logger.debug(eq(TapestryMarkers.EVENT_HANDLER_METHOD), isA(String.class));
    ComponentEventCallback handler = mockComponentEventHandler();
    train_handleResult(handler, result, true);
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", null, handler, resources, false, model, logger);
    event.setMethodDescription(methodDescription);
    assertFalse(event.isAborted());
    assertTrue(event.storeResult(result));
    assertTrue(event.isAborted());
    verify();
}
Also used : ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 13 with ComponentEvent

use of org.apache.tapestry5.runtime.ComponentEvent 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();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 14 with ComponentEvent

use of org.apache.tapestry5.runtime.ComponentEvent 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();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Example 15 with ComponentEvent

use of org.apache.tapestry5.runtime.ComponentEvent 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();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Aggregations

ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)18 Logger (org.slf4j.Logger)13 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)12 ComponentModel (org.apache.tapestry5.model.ComponentModel)12 Test (org.testng.annotations.Test)12 EventContext (org.apache.tapestry5.EventContext)8 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)6 Component (org.apache.tapestry5.runtime.Component)5 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)5 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)3 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)2 FieldHandle (org.apache.tapestry5.plastic.FieldHandle)2 ValueEncoder (org.apache.tapestry5.ValueEncoder)1 Location (org.apache.tapestry5.commons.Location)1 Link (org.apache.tapestry5.http.Link)1 AbstractEventContext (org.apache.tapestry5.internal.AbstractEventContext)1 ComponentEventImpl (org.apache.tapestry5.internal.services.ComponentEventImpl)1 NotificationEventCallback (org.apache.tapestry5.internal.util.NotificationEventCallback)1 IdAllocator (org.apache.tapestry5.ioc.util.IdAllocator)1 JSONObject (org.apache.tapestry5.json.JSONObject)1